4

I would like to log CPU usage at a frequency of 1 second.

One possible way to do it is via vmstat 1 command.

The problem is that the time between each output is not always exactly one second, especially on a busy server. I would like to be able to output the timestamp along with the CPU usage every second. What would be a simple way to accomplish this, without installing special tools?

dabest1
  • 2,347
  • 6
  • 25
  • 25
  • Can you use the well-known UNIX tool `top` that is normally available on Linux systems: `top -b -d 1 > /tmp/top.log` The first line of each output listing from `top` contains a timestamp. – Simon C Oct 26 '11 at 21:17
  • You should post it as an answer. Is there an option to limit the output to the first few lines, so a list of processes is not shown, preferably not through a pipe? – dabest1 Oct 26 '11 at 21:28

5 Answers5

5

There are many ways to do that. Except top another way is to you the "sar" utility. So something like

sar -u 1 10

will give you the cpu utilization for 10 times every 1 second. At the end it will print averages for each one of the sys, user, iowait, idle

Another utility is the "mpstat", that gives you similar things with sar

Vasileios Lekakis
  • 5,492
  • 2
  • 15
  • 17
4

Use the well-known UNIX tool top that is normally available on Linux systems:

    top -b -d 1 > /tmp/top.log

The first line of each output block from top contains a timestamp.

I see no command line option to limit the number of rows that top displays.

Section 5a. SYSTEM Configuration File and 5b. PERSONAL Configuration File of the top man page describes pressing W when running top in interactive mode to create a $HOME/.toprc configuration file.

I did this, then edited my .toprc file and changed all maxtasks values so that they are maxtasks=4. Then top only displays 4 rows of output.

For completeness, the alternative way to do this using pipes is:

    top -b -d 1 | awk '/load average/ {n=10} {if (n-- > 0) {print}}' > /tmp/top.log
Simon C
  • 1,977
  • 11
  • 14
  • Thanks. `top -p 1 -b -d 1 | awk '/^top/ {n=2} !/Tasks/ {if (n-- > 0) {print}}'` seems to work well at limiting the output to just what I need. Now I have a follow up question: [Join and process two lines from top command output](http://stackoverflow.com/questions/7909695/join-and-process-two-lines-from-top-command-output). – dabest1 Oct 26 '11 at 22:41
1

You might want to try htop and atop. htop is beautifully interactive while atop gathers information and can report CPU usage even for terminated processes.

Onlyjob
  • 5,692
  • 2
  • 35
  • 35
0

I found a neat way to get the timestamp information to be displayed along with the output of vmstat.

Sample command:
vmstat -n 1 3 | while read line; do echo "$(date --iso-8601=seconds) $line"; done

Output:
2013-09-13T14:01:31-0700 procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
2013-09-13T14:01:31-0700 r b swpd free buff cache si so bi bo in cs us sy id wa
2013-09-13T14:01:31-0700 1 1 4197640 29952 124584 12477708 12 5 449 147 2 0 7 4 82 7
2013-09-13T14:01:32-0700 3 0 4197780 28232 124504 12480324 392 180 15984 180 1792 1301 31 15 38 16
2013-09-13T14:01:33-0700 0 1 4197656 30464 124504 12477492 344 0 2008 0 1892 1929 32 14 43 10

dabest1
  • 2,347
  • 6
  • 25
  • 25
0

To monitor the disk usage, cpu and load i created a small bash scripts that writes the values to a log file every 10 seconds.

This logfile is processed by logstash kibana and riemann.

# #!/usr/bin/env bash

# Define a timestamp function
LOGPATH="/var/log/systemstatus.log"



timestamp() {
  date +"%Y-%m-%dT%T.%N"
}

#server load
while ( sleep 10 ) ; do
echo -n "$(timestamp) linux::systemstatus::load " >> $LOGPATH
cat /proc/loadavg >> $LOGPATH

#cpu usage
echo -n "$(timestamp) linux::systemstatus::cpu " >> $LOGPATH
top -bn 1 | sed -n 3p >> $LOGPAT


#disk usage
echo -n "$(timestamp) linux::systemstatus::storage " >> $LOGPATH
df --total|grep total|sed "s/total//g"| sed 's/^ *//' >> $LOGPATH



done
Brian van Rooijen
  • 1,906
  • 1
  • 14
  • 10