ในบทความก่อนหน้า เราได้เรียนรู้เกี่ยวกับการติดตั้งและอัปเกรดแพ็กเกจ sysstat และเข้าใจสั้นๆ เกี่ยวกับ Utilities ที่มาพร้อมกับแพ็กเกจ
- Sysstat – Performance and Usage Activity Monitoring Tool For Linux
วันนี้ เราจะทำงานกับตัวอย่างปฏิบัติที่น่าสนใจของ mpstat, pidstat, iostat และ sar Utilities ซึ่งช่วยให้เราระบุปัญหาได้ เรามีตัวเลือกต่างๆ ในการใช้ Utilities เหล่านี้ คุณสามารถรันคำสั่งด้วยตัวเองด้วยตัวเลือกต่างๆ สำหงานต่างๆ หรือสร้าง Customized Scripts ตามความต้องการ คุณรู้ว่า Sysadmins มัก Lazy 一点 และพยายามหาวิธีง่ายที่สุดในการทำสิ่งต่างๆ ด้วยความพยายามน้อยที่สุด
mpstat – Processor Statistics
1. ใช้คำสั่ง mpstat โดยไม่มีตัวเลือก จะแสดง Global Average Activities ของ All CPUs
tecmint@tecmint ~ $ mpstat Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 12:23:57 IST CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 12:23:57 IST all 37.35 0.01 4.72 2.96 0.00 0.07 0.00 0.00 0.00 54.88
2. ใช้ mpstat กับตัวเลือก '-P' (ระบุ Processor Number) และ 'ALL' จะแสดงสถิติของทุก CPUs ทีละตัว เริ่มจาก 0 (0 คือตัวแรก)
tecmint@tecmint ~ $ mpstat -P ALL Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 12:29:26 IST CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 12:29:26 IST all 37.33 0.01 4.57 2.58 0.00 0.07 0.00 0.00 0.00 55.44 12:29:26 IST 0 37.90 0.01 4.96 2.62 0.00 0.03 0.00 0.00 0.00 54.48 12:29:26 IST 1 36.75 0.01 4.19 2.54 0.00 0.11 0.00 0.00 0.00 56.40
3. เพื่อแสดงสถิติสำห N จำนวน Iterations หลังจาก n วินาที Interval พร้อม Average ของแต่ละ CPU ใช้คำสั่งต่อไปนี้
tecmint@tecmint ~ $ mpstat -P ALL 2 5 Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 12:36:21 IST CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 12:36:23 IST all 53.38 0.00 2.26 0.00 0.00 0.00 0.00 0.00 0.00 44.36 12:36:23 IST 0 46.23 0.00 1.51 0.00 0.00 0.00 0.00 0.00 0.00 52.26 12:36:23 IST 1 60.80 0.00 3.02 0.00 0.00 0.00 0.00 0.00 0.00 36.18 ... (output ถูกตัดทอนเพื่อความกระชับ)
4. ตัวเลือก 'I' จะพิมพ์จำนวน Interrupt Statistics ทั้งหมดเกี่ยวกับ Per Processor
tecmint@tecmint ~ $ mpstat -I Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 12:39:56 IST CPU intr/s 12:39:56 IST all 651.04 12:39:56 IST CPU 0/s 1/s 6/s 8/s 9/s 12/s 16/s 17/s 20/s 21/s 22/s 23/s 45/s 46/s 47/s NMI/s LOC/s SPU/s PMI/s IWI/s RTR/s RES/s CAL/s TLB/s TRM/s THR/s MCE/s MCP/s ERR/s MIS/s 12:39:56 IST 0 76.27 1.73 0.00 0.00 0.42 0.33 0.00 0.06 11.46 0.00 0.00 0.01 7.62 1.87 0.05 0.33 182.26 0.00 0.33 3.03 0.00 22.66 0.16 5.14 0.00 0.00 0.00 0.00 0.00 0.00 12:39:56 IST 1 70.88 1.44 0.00 0.00 0.41 0.33 0.00 27.91 10.33 0.00 0.00 0.01 7.27 1.79 0.05 0.32 184.11 0.00 0.32 5.17 0.00 22.09 0.13 4.73 0.00 0.00 0.00 0.00 0.00 0.00 ... (output ถูกตัดทอน)
5. ได้ข้อมูลทั้งหมดข้างต้นในคำสั่งเดียว คือ เทียบเท่า "-u -I ALL -p ALL"
tecmint@tecmint ~ $ mpstat -A ... (output แบบครบถ้วน)
pidstat – Process and Kernel Threads Statistics
ใช้สำห Process Monitoring และ Current Threads ที่ถูกจัดการโดย Kernel pidstat ยังตรวจสอบสถานะ Child Processes และ Threads ได้
Syntax
# pidstat <OPTIONS> [INTERVAL] [COUNT]
6. ใช้คำสั่ง pidstat โดยไม่มี Argument จะแสดง Active Tasks ทั้งหมด
tecmint@tecmint ~ $ pidstat Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 12:47:24 IST UID PID %usr %system %guest %CPU CPU Command 12:47:24 IST 0 1 0.01 0.12 0.00 0.13 1 init 12:47:24 IST 0 3 0.00 0.01 0.00 0.01 0 ksoftirqd/0 ... (รายการ Processes)
7. เพื่อพิมพ์ Active และ Non-active Tasks ทั้งหมด ใช้ตัวเลือก '-p' (processes)
tecmint@tecmint ~ $ pidstat -p ALL ... (รายการ Processes ทั้งหมดรวม Idle)
8. ใช้ pidstat กับ '-d 2' จะได้ I/O Statistics และ 2 คือ Interval ในวินาที เพื่อ Refresh Statistics ตัวเลือกนี้มีประโยชน์เมื่อ System กำลัง Heavy I/O และคุณต้องการ सुराग เกี่ยวกับ Processes ที่ใช้ Resource สูง
tecmint@tecmint ~ $ pidstat -d 2 Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 03:26:53 EDT PID kB_rd/s kB_wr/s kB_ccwr/s Command 03:26:55 EDT PID kB_rd/s kB_wr/s kB_ccwr/s Command 03:26:57 EDT 574 0.00 148.00 2.00 miniserv.pl ... (I/O Stats)
9. เพื่อทราบ CPU Statistics พร้อม Threads ทั้งหมดของ Process ID 4164 ที่ Interval 2 วินาที จำนวน 3 ครั้ง ใช้คำสั่งพร้อมตัวเลือก '-t' (แสดงสถิติของ Process ที่เลือก)
tecmint@tecmint ~ $ pidstat -t -p 4164 2 3 Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 01:09:06 IST UID TGID TID %usr %system %guest %CPU CPU Command 01:09:08 IST 1000 4164 - 22.00 1.00 0.00 23.00 1 firefox 01:09:08 IST 1000 - 4164 20.00 0.50 0.00 20.50 1 |__firefox ... (Thread Stats)
10. ใช้ตัวเลือก '-rh' เพื่อทราบ Memory Utilization ของ Processes ที่เปลี่ยนแปลงบ่อยใน Interval 2 วินาที
tecmint@tecmint ~ $ pidstat -rh 2 3 Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) # Time UID PID minflt/s majflt/s VSZ RSS %MEM Command 1409816695 1000 3958 3378.22 0.00 707420 215972 5.32 cinnamon 1409816695 1000 4164 406.93 0.00 1252024 461404 11.36 firefox ... (Memory Stats)
11. เพื่อพิมพ์ Processes ทั้งหมดที่มี String "VB" ใช้ '-t' เพื่อดู Threads ด้วย
tecmint@tecmint ~ $ pidstat -G VB ... (VB-related Processes)
tecmint@tecmint ~ $ pidstat -t -G VB ... (VB Processes with Threads)
12. เพื่อได้ Realtime Priority และ Scheduling Information ใช้ตัวเลือก '-R'
tecmint@tecmint ~ $ pidstat -R Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 01:09:06 IST UID PID prio policy Command 01:09:08 IST 1000 3 99 FIFO migration/0 01:09:08 IST 1000 5 99 FIFO migration/0 01:09:08 IST 1000 6 99 FIFO watchdog/0
ที่นี่ ผมจะไม่ครอบคลุม Iostat Utility เนื่องจากเราครอบคลุมแล้ว โปรดดู "Linux Performance Monitoring with Vmstat and Iostat" เพื่อรายละเอียด iostat
sar – System Activity Reporter
ใช้คำสั่ง "sar" เราสามารถได้ Reports เกี่ยวกับ Performance ของ Whole System ช่วย Locate System Bottleneck และช่วยหา Solutions สำห Performance Issues ที่รบกวน
Linux Kernel Maintains Counters ภายใน ซึ่งติดตาม Requests ทั้งหมด Completion Time และ I/O Block Counts เป็นต้น จากข้อมูลทั้งหมดนี้ sar คำนวณ Rates และ Ratios เพื่อหา Bottleneck Areas
สิ่งสำคัญของ sar คือ มัน Report Activities ทั้งหมดในช่วงเวลา ดังนั้น ต้องแน่ใจว่า sar Collect Data ในเวลาที่เหมาะสม (ไม่ใช่ Lunch Time หรือ Weekend :))
13. คำสั่งพื้นฐานเพื่อ Invoke sar จะสร้างไฟล์ชื่อ "sarfile" ใน Current Directory ตัวเลือก '-u' สำห CPU Details และจะ Collect 5 Reports ที่ Interval 2 วินาที
tecmint@tecmint ~ $ sar -u -o sarfile 2 5 Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 01:42:28 IST CPU %user %nice %system %iowait %steal %idle 01:42:30 IST all 36.52 0.00 3.02 0.00 0.00 60.45 01:42:32 IST all 43.32 0.00 5.04 0.00 0.00 51.64 01:42:34 IST all 56.46 0.00 4.05 0.00 0.00 39.49 01:42:36 IST all 44.44 0.00 3.79 0.00 0.00 51.77 01:42:38 IST all 50.75 0.00 3.75 0.00 0.00 45.50 Average: all 46.30 0.00 3.93 0.00 0.00 49.77
14. ในตัวอย่างข้างต้น เรา Invoke sar แบบ Interactive เรายังมีตัวเลือก Invoke แบบ Non-interactive ผ่าน Cron ใช้ Scripts /usr/local/lib/sa1 และ /usr/local/lib/sa2 (หากคุณใช้ /usr/local เป็น Prefix ตอนติดตั้ง)
- /usr/local/lib/sa1 เป็น Shell Script สำห Schedule Cron ซึ่งสร้าง Daily Binary Log File
- /usr/local/lib/sa2 เป็น Shell Script จะเปลี่ยน Binary Log File ให้เป็น Human-readable Form
ใช้ Cron Entries ต่อไปนี้สำห Non-interactive:
# Run sa1 shell script every 10 minutes for collecting data */2 * * * * /usr/local/lib/sa/sa1 2 10 # Generate a daily report in human readable format at 23:53 53 23 * * * /usr/local/lib/sa/sa2 -A
ที่ Back-end sa1 Script จะเรียก sadc (System Activity Data Collector) Utility สำห Fetch Data ที่ Interval เฉพาะ sa2 จะเรียก sar สำหเปลี่ยน Binary Log ให้ Human Readable
15. ตรวจสอบ Run Queue Length, Total Number of Processes และ Load Average ใช้ '-q'
tecmint@tecmint ~ $ sar -q 2 5 Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 02:00:44 IST runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked 02:00:46 IST 1 431 1.67 1.22 0.97 0 02:00:48 IST 4 431 1.70 1.23 0.97 0 ... (Load Average Stats)
16. ตรวจสอบสถิติ Mounted File Systems ใช้ '-F'
tecmint@tecmint ~ $ sar -F 2 4 ... (Filesystem Stats)
17. ดู Network Statistics ใช้ '-n DEV'
tecmint@tecmint ~ $ sar -n DEV 1 3 | egrep -v lo Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 02:11:59 IST IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s 02:12:00 IST wlan0 8.00 10.00 1.23 0.92 0.00 0.00 0.00 02:12:00 IST vmnet8 0.00 0.00 0.00 0.00 0.00 0.00 0.00 ... (Network Stats)
18. ดู Block Device Statistics เหมือน iostat ใช้ '-d'
tecmint@tecmint ~ $ sar -d 1 3 Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 02:13:17 IST DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util 02:13:18 IST dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 ... (Block Device Stats)
19. เพื่อพิมพ์ Memory Statistics ใช้ '-r'
tecmint@tecmint ~ $ sar -r 1 3 Linux 3.11.0-23-generic (tecmint.com) Thursday 04 September 2014 _i686_ (2 CPU) 02:14:29 IST kbmemfree kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty 02:14:30 IST 1465660 2594840 63.90 133052 1549644 3710800 45.35 1133148 1359792 392 02:14:31 IST 1472724 2587776 63.73 133060 1549792 3715504 45.40 1125816 1360000 836 ... (Memory Stats)
20. ใช้ 'safd -d' เราสามารถ Extract Data ใน Format ที่ Process ได้ด้วย Databases
tecmint@tecmint ~ $ safd -d /var/log/sa/sa20140903 -- -n DEV | grep -v lo # hostname;interval;timestamp;IFACE;rxpck/s;txpck/s;rxkB/s;txkB/s;rxcmp/s;txcmp/s;rxmcst/s;%ifutil tecmint;2;2014-09-03 07:53:29 UTC;eth0;1.50;0.00;0.13;0.00;0.00;0.00;0.00;0.00 tecmint;2;2014-09-03 07:53:31 UTC;eth0;2.00;0.00;0.18;0.00;0.00;0.00;0.00;0.00 ... (CSV Format Output)
คุณยังสามารถ Save เป็น CSV แล้ววาด Chart สำห Presentation ได้
ขอจบเพียงเท่านี้ คุณสามารถอ่าน Man Pages สำหข้อมูลเพิ่มเติมเกี่ยวกับแต่ละตัวเลือก และอย่าลืมบอกเราเกี่ยวกับบทความพร้อมความคิดเห็นที่มีค่า