linux command – sar

Loading

文字介面下 Linux 預設最古老的系統資源監控程式 top , 但只能看到 CPU 與 Memory 的使用狀況, iostat 也只能看硬碟 , 還有 mpstat 也只能看個別的 CPU 使用率與統計,有哪支文字介面的可以網路流量,找到 SAR ,出乎意料的是 SAR 可以看的東西頗多.

先來了解 網路流量 要怎麼觀察.

root@ubuntu:~# sar -n DEV 1 1000
The program 'sar' can be found in the following packages:
 * sysstat
 * atsar
Try: apt-get install <selected package>

Ubuntu 14.04 預設是沒有安裝 sar ,直接用 apt-get 就可以找到.

root@ubuntu:~# apt-get install sysstat
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  isag
The following NEW packages will be installed:
  sysstat
0 upgraded, 1 newly installed, 0 to remove and 6 not upgraded.
Need to get 283 kB of archives.
After this operation, 877 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/main sysstat amd64 10.2.0-1 [283 kB]
Fetched 283 kB in 4s (57.9 kB/s)  
Preconfiguring packages ...
Selecting previously unselected package sysstat.
(Reading database ... 195052 files and directories currently installed.)
Preparing to unpack .../sysstat_10.2.0-1_amd64.deb ...
Unpacking sysstat (10.2.0-1) ...
Processing triggers for ureadahead (0.100.0-16) ...
ureadahead will be reprofiled on next reboot
Processing triggers for doc-base (0.10.5) ...
Processing 1 added doc-base file...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up sysstat (10.2.0-1) ...

Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Processing triggers for ureadahead (0.100.0-16) ...

sar 使用很簡單 -n DEV 代表要看網路裝置,1 1000 分別表示,1 [秒數] 1000 [次數] -> 每隔1秒顯示一次,顯示 1000 次.

root@ubuntu:~# sar -n DEV 1 1000
08:28:45 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
08:28:46 PM      eth0      0.00  23392.86      0.00  17786.92      0.00      0.00      0.00     14.57
08:28:46 PM      eth1  23392.86      0.00  17786.92      0.00      0.00      0.00      0.00     14.57
08:28:46 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
 
Average:        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
Average:         eth0      1.38  15296.88      0.59  11546.07      0.00      0.00      0.00      9.46
Average:         eth1  15295.93     12.92  11545.90      0.76      0.00      0.00      0.00      9.46
Average:           lo      0.33      0.33      0.04      0.04 

除了網路 CPU , Memory ,I/O (Disk, Network) 都可以看到.使用方式可以參考 http://www.thegeekstuff.com/2011/03/sar-examples/ 10 Useful Sar (Sysstat) Examples

  1. Collective CPU usage
    root@ubuntu:~# sar -u 1 3
    
  2. Individual CPU statistics
    root@ubuntu:~# sar -P ALL 1 1
    
  3. Memory used and available
    root@ubuntu:~# sar -r 1 3
    
  4. Swap space used and available
    root@ubuntu:~# sar -S 1 3
    
  5. Overall I/O activities of the system
    root@ubuntu:~# sar -b 1 3
    
  6. Individual device I/O activities
    root@ubuntu:~# sar -d 1 1
    
  7. Context switch statistics
    root@ubuntu:~# sar -w 1 3
    
  8. Run queue and load average data
    root@ubuntu:~# sar -q 1 3
    
  9. Network statistics
    root@ubuntu:~# sar -n KEYWORD
    

    除了剛剛示範的 DEV 網路可以統計的還有下面幾種 KEYWORD 可以使用.

    • DEV – Displays network devices vital statistics for eth0, eth1, etc.,
    • EDEV – Display network device failure statistics
    • NFS – Displays NFS client activities
    • NFSD – Displays NFS server activities
    • SOCK – Displays sockets in use for IPv4
    • IP – Displays IPv4 network traffic
    • EIP – Displays IPv4 network errors
    • ICMP – Displays ICMPv4 network traffic
    • EICMP – Displays ICMPv4 network errors
    • TCP – Displays TCPv4 network traffic
    • ETCP – Displays TCPv4 network errors
    • UDP – Displays UDPv4 network traffic
    • SOCK6, IP6, EIP6, ICMP6, UDP6 are for IPv6
    • ALL – This displays all of the above information. The output will be very long.
  10. Report sar data from a specific time
    root@ubuntu:~# sar -q -f /var/log/sa/sa23 -s 10:00:01 
    

sar 只是 Sysstat 套件中的一個程式,sysstat 它還包含下列幾種可以用來統計系統資訊的程式.

  • sar collects and displays ALL system activities statistics.
  • sadc stands for “system activity data collector”. This is the sar backend tool that does the data collection.
  • sa1 stores system activities in binary data file. sa1 depends on sadc for this purpose. sa1 runs from cron.
  • sa2 creates daily summary of the collected statistics. sa2 runs from cron.
  • sadf can generate sar report in CSV, XML, and various other formats. Use this to integrate sar data with other tools.
  • iostat generates CPU, I/O statistics
  • mpstat displays CPU statistics.
  • pidstat reports statistics based on the process id (PID)
  • nfsiostat displays NFS I/O statistics.
  • cifsiostat generates CIFS statistics.
沒有解決問題,試試搜尋本站其他內容

3 thoughts on “linux command – sar

  1. 自動引用通知: nmon | Benjr.tw

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料