Question

for monitoring purpose of system, i need to redirect the output of top command in a file so i will use/parse it. i am trying to do same thing but CPU performance stats are not getting saved in a file see screen shots. expected output:

[root@v100 /usr/local/bin]# top
last pid:  6959;  load averages:  0.01,  0.03,  0.03                                                                                   up 0+02:47:34  17:51:16
114 processes: 1 running, 108 sleeping, 5 zombie
CPU:  0.0% user,  0.0% nice,  1.6% system,  0.0% interrupt, 98.4% idle
Mem: 734M Active, 515M Inact, 226M Wired, 212M Buf, 491M Free
Swap: 4095M Total, 4095M Free

 PID USERNAME  THR PRI NICE   SIZE    RES STATE    TIME   WCPU COMMAND
 1953 root      150  20    0  3084M   635M uwait    2:44  0.00% java
 1663 mysql      46  20    0   400M   139M sbwait   1:29  0.00% mysqld
 1354 root       31  20    0 94020K 50796K uwait    0:24  0.00% beam
 4233 root        1  20    0   122M 23940K select   0:06  0.00% python
 1700 zabbix      1  20    0 20096K  2436K nanslp   0:03  0.00% zabbix_agentd
 1799 zabbix      1  20    0   103M  7240K nanslp   0:02  0.00% zabbix_server
 4222 root        1  30    0   122M 23300K select   0:02  0.00% python
 1696 zabbix      1  20    0 19968K  2424K nanslp   0:02  0.00% zabbix_agentd
 2853 root        1  20    0   126M 29780K select   0:02  0.00% python
 1793 zabbix      1  20    0   103M  7152K nanslp   0:01  0.00% zabbix_server
 1797 zabbix      1  20    0   103M  8348K nanslp   0:01  0.00% zabbix_server
 1752 root        1  20    0   122M 22344K select   0:01  0.00% python
 1796 zabbix      1  20    0   103M  8136K nanslp   0:01  0.00% zabbix_server
 1795 zabbix      1  20    0   103M  8208K nanslp   0:01  0.00% zabbix_server
 1801 zabbix      1  20    0   103M  7100K nanslp   0:01  0.00% zabbix_server
 3392 root        1  20    0   122M 23392K select   0:01  0.00% python
 1798 zabbix      1  20    0   103M  7860K nanslp   0:01  0.00% zabbix_server
 2812 root        1  20    0   134M 25184K select   0:01  0.00% python
 1791 zabbix      1  20    0   103M  7188K nanslp   0:01  0.00% zabbix_server
 1827 root        1 -52   r0 14368K  1400K nanslp   0:01  0.00% watchdogd
 1790 zabbix      1  20    0   103M  7164K nanslp   0:01  0.00% zabbix_server
 1778 zabbix      1  20    0   103M  8608K nanslp   0:01  0.00% zabbix_server
 1780 zabbix      1  20    0   103M  8608K nanslp   0:01  0.00% zabbix_server
 2928 root        1  20    0   122M 23272K select   0:01  0.00% python
 2960 root        1  20    0   116M 22288K select   0:01  0.00% python
 1776 zabbix      1  20    0   103M  7248K nanslp   0:01  0.00% zabbix_server
 2892 root        1  20    0   122M 22648K select   0:01  0.00% python
 1789 zabbix      1  20    0   103M  7128K nanslp   0:01  0.00% zabbix_server
 1814 root        1  20    0   216M 15796K select   0:01  0.00% httpd
 1779 zabbix      1  20    0   103M  8608K nanslp   0:01  0.00% zabbix_server
 1783 zabbix      1  20    0   103M  8608K nanslp   0:01  0.00% zabbix_server
 1800 zabbix      1  20    0   103M  7124K nanslp   0:01  0.00% zabbix_server
 1782 zabbix      1  20    0   103M  8608K nanslp   0:01  0.00% zabbix_server
 1781 zabbix      1  20    0   103M  8608K nanslp   0:00  0.00% zabbix_server
 1792 zabbix      1  20    0   103M  7172K nanslp   0:00  0.00% zabbix_server
 2259 root        2  20    0 48088K  4112K uwait    0:00  0.00% cb_heuristics

If i do:

[root@v100 /usr/local/bin]# top > /tmp/top.output

then it shows:

[root@v100 /usr/local/bin]# cat /tmp/top.output

 last pid:  7080;  load averages:  0.09,  0.06,  0.03  up 0+02:52:24    17:56:06
 114 processes: 1 running, 108 sleeping, 5 zombie

 Mem: 731M Active, 515M Inact, 219M Wired, 212M Buf, 501M Free
 Swap: 4095M Total, 4095M Free

  PID USERNAME  THR PRI NICE   SIZE    RES STATE    TIME   WCPU COMMAND
  1953 root      150  20    0  3084M   633M uwait    2:17  0.00% java
  1663 mysql      46  20    0   400M   136M sbwait   1:08  0.00% mysqld
  1354 root       31  20    0 94020K 49924K uwait    0:18  0.00% beam
  4233 root        1  20    0   122M 23776K select   0:04  0.00% python
  1700 zabbix      1  20    0 20096K  2436K nanslp   0:02  0.00% zabbix_agentd
  1799 zabbix      1  20    0   103M  7240K nanslp   0:01  0.00% zabbix_server
  2853 root        1  20    0   126M 29780K select   0:01  0.00% python
  1696 zabbix      1  20    0 19968K  2424K nanslp   0:01  0.00% zabbix_agentd
  4222 root        1  28    0   122M 23264K select   0:01  0.00% python
  1793 zabbix      1  20    0   103M  7152K nanslp   0:01  0.00% zabbix_server
  1752 root        1  20    0   122M 22344K select   0:01  0.00% python
  1797 zabbix      1  20    0   103M  8088K nanslp   0:01  0.00% zabbix_server
  1796 zabbix      1  20    0   103M  7944K nanslp   0:01  0.00% zabbix_server
  1795 zabbix      1  20    0   103M  8044K nanslp   0:01  0.00% zabbix_server
  1801 zabbix      1  20    0   103M  7100K nanslp   0:01  0.00% zabbix_server
  3392 root        1  20    0   122M 23312K select   0:01  0.00% python
  2812 root        1  20    0   134M 25184K select   0:01  0.00% python
  1798 zabbix      1  20    0   103M  7628K nanslp   0:01  0.00% zabbix_server

so here, I am able to monitor Memory but not CPU reason is during redirect output of top CPU stats did not update

How can i capture CPU stats also?

if you have any suggestion pls tell me.

Was it helpful?

Solution

top -b -n 1 seems to work on my Linux box here (-b: batch mode operation, -n: number of iterations).

Edit:

I just tried it on FreeBSD 9.2 which uses the 3.5beta12 version of top. It seems it needs at least one additional iteration to get CPU stats. So you might want to use:

top -b -d2 -s1 | sed -e '1,/USERNAME/d' | sed -e '1,/^$/d'

-b: batch mode, -d2: 2 displays (the first one does not contain CPU stats, second one does), -s1: wait one seconds between displays

The sed pipeline removes the first display which does not contain CPU stats (by skipping header and process list).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top