lost and found ( for me ? )

Showing posts with label sysstat. Show all posts
Showing posts with label sysstat. Show all posts

dstat, netstat, sar and pidstat

here are small tips how to monitor system resources.

many thanks!

[ monitor network info with netstat ]

IP,TCP,UDP
root@ubuntu:~# netstat -s
Ip:
   150665 total packets received
   0 forwarded
   0 incoming packets discarded
   149429 incoming packets delivered
   117790 requests sent out
<snip>



TCP
root@ubuntu:~# netstat -st
IcmpMsg:
   InType3: 28
   OutType3: 582
Tcp:
   3376 active connections openings
   1 passive connection openings
   3 failed connection attempts
   421 connection resets received
   52 connections established
   138317 segments received
   106504 segments send out
<snip>

UDP
root@ubuntu:~# netstat -su
IcmpMsg:
   InType3: 28
   OutType3: 584
Udp:
   9955 packets received
   584 packets to unknown port received.
   0 packet receive errors
   10143 packets sent
UdpLite:
IpExt:
   InMcastPkts: 154
   OutMcastPkts: 168
   InOctets: 153334177
<snip>

[ monitor network info with dstat ]

install dstat
root@ubuntu:~# apt-get install dstat



root@ubuntu:~# dstat --tcp
----tcp-sockets----
lis act syn tim clo
 9   7   0   1   0
 9   7   0   1   0
 9   7   0   1   0^C
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~# dstat --udp
--udp--
lis act
14   0
14   0
14   0
14   0^C
root@ubuntu:~#
root@ubuntu:~# dstat --net
-net/total-
recv  send
  0     0
  0     0
 60B   42B^C





[ monitor CPU, I/O resource with dstat  ]

CPU , Disk , Network etc
root@ubuntu:~# dstat -a -T
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system-- --epoch---
usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw |  epoch   
 5   1  93   0   0   0|  90k   87k|   0     0 |   0     0 |1271  2624 |1373561131
 1   1  99   0   0   0|   0     0 |   0     0 |   0     0 | 252   500 |1373561132
 1   0  99   0   0   0|   0     0 |  60B   54B|   0     0 | 229   440 |1373561133^C



root@ubuntu:~# dstat --top-cpu 1 3
-most-expensive-
 cpu process   
chromium-brow1.4
X            0.2
gnome-termina0.2
kworker/3:4  0.2




[ monitor I/O with dstat ]

block I/O , I/O
root@ubuntu:~# dstat --top-bio
----most-expensive----
 block i/o process   
init
       65k 1131B
                     
jbd2/dm-1-8   0  4096B
                     
chromium-br   0    16k^C
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~# dstat --top-io
----most-expensive----
    i/o process      
chromium-br  50k   78k
X          3600B 1720B
X          4856B  956B^C



[ sar ]

install sysstat
root@ubuntu:~# apt-get install sysstat atsar

error
root@ubuntu:~# sar
open /var/log/atsar/atsa12: No such file or directory

change “ENABLED” option to true from false
root@ubuntu:~# egrep -i ^enable /etc/default/sysstat
ENABLED="true"

restart sysstat
root@ubuntu:~# service sysstat restart



root@ubuntu:~# sar -u 1 3

Linux  ubuntu  3.8.0-26-generic  #38-Ubuntu SMP Mon Jun 17 21:43:33 UTC 2013  x86_64  07/12/2013

01:52:45  cpu %usr %nice   %sys %irq %softirq    %wait %idle             _cpu_
01:52:45  all    1     0      2    0        0        0    98
           0    0     0      0    0        0        0   100
           1    0     0      3    0        0        0    97
           2    0     0      3    0        0        0    97
           3    0     0      0    0        0        0   100
01:52:46  all    0     0      0    0        0        0   100
           0    1     0      0    0        0        0    99
           1    0     0      0    0        0        0   100
           2    1     0      0    0        0        0    99
           3    1     0      0    0        0        0    99
01:52:47  all    0     0      0    0        0        0   100
           0    0     0      1    0        0        0    99
           1    1     0      0    0        0        0    99
           2    0     0      0    0        0        0   100
           3    0     0      0    0        0        0   100

[ pidstat ]

monitor specific processes
root@ubuntu:~# unbound-control status | grep -i pid
unbound (pid 1393) is running...

root@ubuntu:~# pidstat -u -p 1393 1 3
Linux 3.8.0-26-generic (ubuntu) 07/12/2013 _x86_64_ (4 CPU)

01:54:42 AM       PID    %usr %system  %guest    %CPU   CPU  Command
01:54:43 AM      1393    0.00    0.00    0.00    0.00     2  unbound
01:54:44 AM      1393    0.00    0.00    0.00    0.00     2  unbound
01:54:45 AM      1393    0.00    0.00    0.00    0.00     2  unbound
Average:         1393    0.00    0.00    0.00    0.00     -  unbound
root@ubuntu:~#

sysstat: monitoring CPU usage , memory consumption , network bandwidth , disk I/O


# cat /etc/redhat-release
CentOS release 5.4 (Final)

# yum install sysstat.i386 -y

sysstat をインストールすると、cron で下記のジョブも登録される。

# cat /etc/cron.d/sysstat
# run system activity accounting tool every 10 minutes
*/10 * * * * root /usr/lib/sa/sa1 1 1
# generate a daily summary of process accounting at 23:53
53 23 * * * root /usr/lib/sa/sa2 -A

- /usr/lib/sa/sa1 [ interval count ]  

Collect  and  store  binary data in the system activity daily data file

sa1 1 1 は interval 1秒で1回 システムの情報を取得

バイナリデータとして下記に保存される。( dd は 日にち )

# ls /var/log/sa/sa*
/var/log/sa/sa21  /var/log/sa/sa22

sar -f ファイル名で読める

# sar -f /var/log/sa/sa22


-  /usr/lib/sa/sa2

sa2 - Write a daily report in the /var/log/sa directory

ログファイル

# ls /var/log/sa/sar*
/var/log/sa/sar21

/usr/lib/sa/sa1 は sadc 実行

# egrep -i sadc /usr/lib/sa/sa1 | uniq
        exec ${ENDIR}/sadc -F -L 1 1 -
        exec ${ENDIR}/sadc -F -L $* -

/usr/lib/sa/sa2 は sar を実行

# egrep -i sar /usr/lib/sa/sa2 | uniq
RPT=/var/log/sa/sar${DATE}
${ENDIR}/sar $* -f ${DFILE} > ${RPT}
find /var/log/sa \( -name 'sar??' -o -name 'sa??' \) -mtime +"${HISTORY}" -exec rm -f {} \;

sar で CPU , Memory etc の情報をチェック。

[ CPU usage ]

# LANG=C sar
Linux 2.6.18-164.el5 (hat2.localdomain)         04/22/10

00:20:01          CPU     %user     %nice   %system   %iowait    %steal     %idle
00:30:01          all      0.07      0.00      0.10      0.00      0.00     99.83
Average:          all      0.07      0.00      0.10      0.00      0.00     99.83

%user + %system の合計がCPU使用率なので、

# LANG=C sar | grep -i average | awk '{print $3+$5"%"}'
0.17%

[ memory consumption ]

# LANG=C sar -r
Linux 2.6.18-164.el5 (hat2.localdomain)         04/22/10

00:20:01    kbmemfree kbmemused  %memused kbbuffers  kbcached kbswpfree kbswpused  %swpused  kbswpcad
00:30:01        69288    445544     86.54     19196    279824    451192     70912     13.58     17672
00:40:01        69164    445668     86.57     19368    279808    451192     70912     13.58     17672
Average:        69226    445606     86.55     19282    279816    451192     70912     13.58     17672

実質メモリ使用量は kbmemused - ( kbbuffers + kbcached ) なので、

# LANG=C sar -r | grep -i average | awk '{print $3-$5-$6"Kbytes"}'
146508Kbytes

[ network bandwidth ]

# LANG=C sar -n DEV | head -5
Linux 2.6.18-164.el5 (hat2.localdomain)         04/22/10

00:20:01        IFACE   rxpck/s   txpck/s   rxbyt/s   txbyt/s   rxcmp/s   txcmp/s  rxmcst/s
00:30:01           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
00:30:01         eth0      0.00      0.00      0.00      0.00      0.00      0.00      0.00

受信、送信データは、 rxbyt/s , txbyt/s

# LANG=C sar -n DEV | grep eth0 | grep -i average | awk '{print "Rx:"$5"byte/sec","Tx:"$6"byte/sec"}'
Rx:0.00byte/sec Tx:0.00byte/sec

[ disk I/O ]

# LANG=C sar -b
Linux 2.6.18-164.el5 (hat2.localdomain)         04/22/10

00:20:01          tps      rtps      wtps   bread/s   bwrtn/s
00:30:01         0.26      0.00      0.26      0.00      3.12
00:40:01         0.37      0.00      0.37      0.00      7.04
00:50:01         0.68      0.00      0.68      0.00     12.85
01:00:01         0.29      0.00      0.29      0.00      4.48
01:10:01         0.46      0.00      0.46      0.00      7.55
01:20:01         0.28      0.00      0.28      0.00      4.03
Average:         0.39      0.00      0.39      0.00      6.51

bread/s = 読み出したデータ量
bwrtn/s = 書き込んだデータ量

単位はブロック

# LANG=C sar -b | egrep -i average | awk '{print "Read:"$5"block/sec","Write:"$6"block/sec"}'
Read:0.00block/sec Write:6.51block/sec