LPIC3 キャパシティプランニング(for debian squeeze)

☆概要
ボトルネック等、問題点を洗い出す為のツールを使えるようにする。
常時監視のMRTGやSNMPを使う前振りのようなもの。
トラブルシュートと今後の予測の裏づけとなる情報を自在に取得出来るようにすることが目的。

☆課題
・デフォルトでの最大は1ヶ月分。
・年月の情報が必要な場合は自分で追加する必要がある。

■topコマンドで使えるオプションを一覧します。

$ LANG=C;man top | grep "\-.\ \:"
-b : Batch mode operation
-c : Command line/Program name toggle
-d : Delay time interval as: -d ss.tt (seconds.tenths)
-h : Help
-H : Threads toggle
-i : Idle Processes toggle
-n : Number of iterations limit as: -n number
-u : Monitor by user as: -u somebody
-U : Monitor by user as: -U somebody
-p : Monitor PIDs as: -pN1 -pN2 ... or -pN1, N2 [,...]
-s : Secure mode operation
-S : Cumulative time mode toggle
-v : Version

■topコマンドの略称の意味を一覧します。

$ top -b -n 1 | head -7 | tail -1 | for list in `xargs`;do LANG=C;man top | \
grep "\: $list";done | grep -v " "
a: PID -- Process Id
e: USER -- User Name
h: PR -- Priority
i: NI -- Nice value
o: VIRT -- Virtual Image (kb)
q: RES -- Resident size (kb)
t: SHR -- Shared Mem size (kb)
-s : Secure mode operation
p: SWAP -- Swapped size (kb)
t: SHR -- Shared Mem size (kb)
w: S -- Process Status
k: %CPU -- CPU usage
n: %MEM -- Memory usage (RES)
m: TIME+ -- CPU Time, hundredths

■数字を色づけして、全体の状況を確認します。
 課題の洗い出しの最初のステップです。

$ top -b -n 1 | head -6 | grep '[0-9]' --color
top - 21:37:50 up 1:34, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 72 total, 1 running, 71 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.4%us, 0.2%sy, 0.0%ni, 99.0%id, 0.5%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 1997200k total, 212848k used, 1784352k free, 30480k buffers
Swap: 3229024k total, 0k used, 3229024k free, 106808k cached

■例えば、以下のように表示の形式を変えてみると、
 スリープ状態のプロセスが多く、idle値が高い事、swapを利用していない事などが更によく見えてきます。

$ top -b -n 1 | head -6 | sed s/","/"\n"/g
top - 21:41:28 up 1:37
2 users
load average: 0.00
0.00
0.00
Tasks: 72 total
2 running
70 sleeping
0 stopped
0 zombie
Cpu(s): 0.4%us
0.2%sy
0.0%ni
99.0%id
0.5%wa
0.0%hi
0.0%si
0.0%st
Mem: 1997200k total
212828k used
1784372k free
30496k buffers
Swap: 3229024k total
0k used
3229024k free
106808k cached

【読み取り】
 スワップも無く、ゾンビプロセスも無い為、システムは健康な状態です。
 起動したばかりでほとんどアイドル(何もしていない)状態です。
 空きメモリも1.7GBあり、余裕がある状態。
 仮想マシンのメモリ割り当てを512MB〜768MBで2台まで動かせるものと予想。
 応答速度を気にしなければ、スワップを利用可能。
 恐らく仮想マシン2台使用時にはスワップされるものと予想する。

 ⇒予想部分に対し、現在の状況と監視による根拠を示す方法を考える。
  例えば2台の仮想マシンによる検証ドメイン環境の構築を行うものとして、以下のシナリオを考える。

  ・WindowsServer2008R2のActiveDirectoryとの連携はスワップされる為、レスポンスが悪い。
  ・LinuxのLDAPとの連携であればスワップも少なく、可能な範囲。

  ※実稼動での(特にWindowsでの)ドメイン環境を仮想マシン内で構築する事は推奨されていません。
   ホストマシンとの時刻同期やスナップショット、状態保存、複数台起動時の遅延起動等、
   別途考えるべきことが増えます。

☆ポイント
 ・CPU使用率、メモリ空き容量、ネットワーク利用率(エラー率)、ディスク利用率を対象とする。
 ・512MB〜768MBx2台の仮想マシン割り当てで、スワップせずに正常に動作すること。
 ・1GBx2台の仮想マシン割り当てで、スワップし、応答速度の低下が見られること。

 ⇒上記要件でのチューニングの要、不要の切り分け。
  チューニングが要なら、要件の優先度、クリア出来る、出来ないを切り分ける。
  プロセスの特定や、優先度の設定等のトラブルシューティングを含む。

 ⇒必要最小限の監視要件を洗い出し、SNMP経由の監視に移行する。
  仮想マシン2台とホストマシンの監視を一元的に行い、性能監視による負荷を減らす。

■現在のユーザと各ユーザのプロセス状況。

$ w
21:44:27 up 1:40, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user pts/0 from-system 05:15 3:00 0.44s 0.44s -bash
user pts/1 from-system 05:18 0.00s 0.16s 0.00s w

 実行しているユーザのTTY宛てにメッセージを送ることが可能です。
 例えば、ゾンビプロセスの停止予告等が可能です。

$ echo "Hello , user" > /dev/pts/1

■以下は同じ結果を表示します。

$ top -b -n 1 | head -1 | sed s/"top \- "/" "/
$ w | head -1
$ uptime

■ヘッダを表示して数字を色づけして表示します。
 (※このブログでは色は付きません)

$ free | head -1;free | grep [0-9] --color
total used free shared buffers cached
Mem: 1997200 212748 1784452 0 30684 106848
-/+ buffers/cache: 75216 1921984
Swap: 3229024 0 3229024

■例えば、空きメモリだけを表示します。

$ free | cut -c 1-7,33-40
free
Mem: 1784004
-/+ buf 1922004
Swap: 3229024

vmstat のヘッダの意味を一覧します。

$ vmstat | head -2 | tail -1 | for list in `xargs`;do LANG=C;man vmstat | grep "$list\:";done
r: The number of processes waiting for run time.
cur: I/O in progress
b: The number of processes in uninterruptible sleep.
swpd: the amount of virtual memory used.
free: the amount of idle memory.
buff: the amount of memory used as buffers.
cache: the amount of memory used as cache.
cache: Cache name
si: Amount of memory swapped in from disk (/s).
so: Amount of memory swapped to disk (/s).
bi: Blocks received from a block device (blocks/s).
bo: Blocks sent to a block device (blocks/s).
in: The number of interrupts per second, including the clock.
cs: The number of context switches per second.
us: Time spent running non-kernel code. (user time, including nice time)
sy: Time spent running kernel code. (system time)
id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.

■「procinfo」はdebianでは標準では入っていないので入れます。

$ sudo apt-get install procinfo

■「procinfo」で表示可能な項目を列挙してみる。

$ procinfo -a | grep "\:" | awk -F\: '{print $1}' | column
Memory hw irq irq 8
Mem sw irq irq 9
Swap idle Kernel Command Line
Bootup uptime Modules
user irq 0 Character Devices
nice irq 1 File Systems
system irq 6
IOwait irq 7

■pstreeでプロセスIDを()内に、Unicode形式で表示、gdmの関係を表示する。
 gdmはXを呼び出す際、gdmgreeterで制御可能であることが読み取れる。

$ pstree -p -U | grep gdm
├─gdm(2209)───gdm(2210)─┬─Xorg(2238)
│ └─gdmgreeter(2255)

$ pstree -a -p -U | grep gdm | grep -v "grep"
├─gdm,2209
│ └─gdm,2210
│ ├─Xorg,2238 :0 -audit 0 -auth /var/lib/gdm/:0.Xauth -nolisten tcp vt7
│ └─gdmgreeter,2255

■gdmgreeterがどこから実行されているか確認する。
 「*bin」では無いコマンドは、標準ではパスが通っていない可能性が高い。
 これは、「whereis gdmgreeter」では見つからない可能性が高いことを意味する。

$ ps ef -p 2255
PID TTY STAT TIME COMMAND
2255 ? Ss 0:05 /usr/lib/gdm/gdmgreeter

■更に詳細を表示する。
 ゾンビプロセスや、スリーププロセスなどが、「STAT」の項目で分かる。

$ ps el -p 2255
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND
4 107 2255 2210 20 0 273772 27644 ? Ss ? 0:05 /usr/lib/gdm/gdmgreeter

■STATの意味を列挙してみる。

$ man ps | grep -A 21 "PROCESS STATE CODE"
PROCESS STATE CODES
Here are the different values that the s, stat and state output
specifiers (header "STAT" or "S") will display to describe the state of
a process.
D Uninterruptible sleep (usually IO)
R Running or runnable (on run queue)
S Interruptible sleep (waiting for an event to complete)
T Stopped, either by a job control signal or because it is being
traced.
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z Defunct ("zombie") process, terminated but not reaped by its
parent.

For BSD formats and when the stat keyword is used, additional
characters may be displayed:
< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group

■「sysstat」ユーティリティを導入する

$ sudo apt-get install sysstat

$ apt-cache show sysstat | grep -A 7 "Description"
Description: sar, iostat and mpstat - system performance tools for Linux
The sysstat package contains the following system performance tools:
* sar - collects and reports system activity information;
* iostat - reports CPU utilization and I/O statistics for disks;
* mpstat - reports global and per-processor statistics;
* pidstat - reports statistics for Linux tasks (processes);
* sadf - displays data collected by sar in various formats.
.

■LPICの範囲と直接の関係はないが、GNUPLOTを使ったグラフ化も可能。

$ apt-cache show isag | grep -A 3 "Description"
Description: Interactive System Activity Grapher for sysstat
This package includes the isag command, which graphically displays the
system activity data stored in a binary data produced by a sar command
from a sysstat package.

■サンプルにもあるようにシステム監視は基本的にcronと連携して行う。
 SNMPによる監視項目として盛り込みたい項目を洗い出す。

$ ls /usr/share/doc/sysstat/examples/crontab | xargs cat
# Crontab sample for root or adm
#
# 8am-7pm activity reports every 20 minutes during weekdays.
# 0 8-18 * * 1-5 /usr/lib/sysstat/sa1 -d 1200 3 &
# activity reports every 10 minutes everyday.
0 * * * * /usr/lib/sysstat/sa1 -d 600 6 &
#
# Activity reports every an hour on Saturday and Sunday.
# 0 * * * 0,6 /usr/lib/sysstat/sa1 -d &
#
# 7pm-8am activity reports every an hour during weekdays.
# 0 19-7 * * 1-5 /usr/lib/sysstat/sa1 -d &
#
# Daily summary prepared at 19:05.
# 5 19 * * 1-5 /usr/lib/sysstat/sa2 -A &
5 19 * * * /usr/lib/sysstat/sa2 -A &

■インストールしただけではcronに登録されません。

$ sudo crontab -l
$ sudo -u root sh -c 'crontab -l'
no crontab for root

■コマンドラインでテストする為のオプションを確認します。
 ディスクと、すべてのシステムへの割り込みを含め、60秒間隔で5回取得します。

$ man sadc | grep -A 2 "\-I\|\-d"
/usr/lib/sysstat/sadc [ -C comment ] [ -d ] [ -F ] [ -I ] [ -L ] [ -V ]
[ interval [ count ] ] [ outfile ]

--
-d Tell sadc to report statistics for disks. By default sadc does
not report disks activity to prevent data files from growing too
large.
--
-I Tell sadc to report statistics for all system interrupts. By
default sadc only reports statistics for the total number of
interrupts.

$ sudo /usr/lib/sysstat/sadc -d -I 60 5 sample_sadc.log


■以下のようなバイナリ出力を溜めていきます。
 数値を見るには別途コマンドが必要です。

$ strings sample_sadc.log | xargs echo -n | cut -c -80;echo ""
Linux sample-system 2.6.26-2-amd64 x86_64 eth0 vmnet1 vmnet8 eth0 vmnet1 vmnet8

$ sar -f sample_sadc.log
Linux 2.6.26-2-amd64 (sample-system) 20110717日 _x86_64_

000216秒 CPU %user %nice %system %iowait %steal %idle
000316秒 all 0.28 0.00 0.10 0.05 0.00 99.57
000416秒 all 0.05 0.00 0.02 0.00 0.00 99.93
000516秒 all 0.03 0.00 0.00 0.00 0.00 99.97
000616秒 all 0.02 0.00 0.03 0.00 0.00 99.95
平均値: all 0.10 0.00 0.04 0.01 0.00 99.85

■strings で取得した情報のネットワーク関連がありません。

 仮想マシンのネットワーク統計も取得出来る事、現在は仮想マシンのネットワークが稼動していない事が分かります。

$ LANG=C;sar -n DEV -f sample_sadc.log | grep "IFACE\|Average"
00:02:16 IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
Average: lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: eth0 2.42 1.72 0.22 0.23 0.00 0.00 0.01
Average: vmnet1 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: vmnet8 0.00 0.00 0.00 0.00 0.00 0.00 0.00

■平均値では無く、データだけを取得したい場合。

$ LANG=C;sar -n DEV -f sample_sadc.log | sed -e '1,3d' | grep -v Average | grep eth0
00:03:16 eth0 2.98 2.25 0.29 0.35 0.00 0.00 0.02
00:04:16 eth0 3.57 2.37 0.32 0.27 0.00 0.00 0.02
00:05:16 eth0 2.35 1.77 0.22 0.20 0.00 0.00 0.02
00:06:16 eth0 0.77 0.50 0.07 0.08 0.00 0.00 0.00

■以下のような多くの情報をバイナリで取得しています。

$ LANG=C;sar -A -f sample_sadc.log | sed -e '1,3d' | grep -i '[a-z]' | grep -v "Average" | \
for list in `xargs`;do echo "$list";done | grep -v "[0-9]\.[0-9][0-9]\|[0-9]\:[0-9]" | sort | uniq | column
%idle campg/s i021/s pgscank/s sum
%iowait coll/s i022/s pgsteal/s svctm
%memused cswch/s i023/s plist-sz swrite/s
%nice dentunusd inode-nr pswpin/s tcp-tw
%steal dev8-0 intr/s pswpout/s tcp/s
%swpused dev8-1 ip-frag pty-nr tcpsck
%system dev8-2 kbbuffers rawsck totsck
%user dev8-5 kbcached rd_sec/s tps
%util eth0 kbmemfree read/s txcarr/s
%vmeff fault/s kbmemused retrans/s txcmp/s
CPU file-nr kbswpcad rtps txdrop/s
DEV frmpg/s kbswpfree runq-sz txerr/s
IFACE getatt/s kbswpused rxcmp/s txfifo/s
INTR hit/s ldavg-1 rxdrop/s txkB/s
access/s i000/s ldavg-15 rxerr/s txpck/s
all i001/s ldavg-5 rxfifo/s udp/s
avgqu-sz i004/s lo rxfram/s udpsck
avgrq-sz i006/s majflt/s rxkB/s vmnet1
await i007/s miss/s rxmcst/s vmnet8
badcall/s i008/s packet/s rxpck/s wr_sec/s
bread/s i009/s pgfree/s saccess/s write/s
bufpg/s i011/s pgpgin/s scall/s wtps
bwrtn/s i014/s pgpgout/s sgetatt/s
call/s i020/s pgscand/s sread/s

■1秒間隔、1回のサンプリングでのCPUの使用率

・Step1 そのままデータだけ。

$ LANG=C;sar -u 1 1 | sed -e '1,2d' | grep -v "Average"
00:38:30 CPU %user %nice %system %iowait %steal %idle
00:38:31 all 0.00 0.00 0.00 0.00 0.00 100.00

・Step2 CSV形式での時間と 「100-アイドル値」、「取得回数-1回分」のデータ取得。

$ LANG=C;sar -u 1 5 | grep -v "Average" | sed -e '1,3d' | awk '{print "\"" $1 "\",\"" 100-$8 "\""}'
"00:52:38","0"
"00:52:39","0"
"00:52:40","0"
"00:52:41","0"
"00:52:42","0"

・Step3 システムの利用値のCSV出力

$ LANG=C;sar -u 1 5 | grep -v "Average" | sed -e '1,3d' | awk '{print "\"" $1 "\",\"" $5 "\""}'
"00:53:09","0.00"
"00:53:10","0.00"
"00:53:11","0.00"
"00:53:12","0.00"
"00:53:13","0.00"1秒間隔、1回のサンプリングでのメモリの使用率

・Step1 そのままデータだけ。

$ LANG=C;sar -r 1 1 | sed -e '1,2d' | grep -v "Average"
00:39:24 kbmemfree kbmemused %memused kbbuffers kbcached kbswpfree kbswpused %swpused kbswpcad
00:39:25 1691836 305364 15.29 36160 183444 3229024 0 0.00 0

・Step2 MB単位での空きメモリ情報のCSV出力

$ LANG=C;sar -r 1 5 | sed -e '1,3d' | grep -v "Average" | awk '{print "\"" $1 "," $2/1024 "\""}'
"00:55:17,1651.68"
"00:55:18,1651.68"
"00:55:19,1651.68"
"00:55:20,1651.68"
"00:55:21,1651.68"

・Step3 MB単位でのスワップ利用率のCSV出力

$ LANG=C;sar -r 1 5 | sed -e '1,3d' | grep -v "Average" | awk '{print "\"" $1 "," $8/1024 "\""}'
"00:57:29,0"
"00:57:30,0"
"00:57:31,0"
"00:57:32,0"
"00:57:33,0"

■ネットワークエラー情報の統計

・Step1 そのままデータだけ。

$ LANG=C;sar -n EDEV 1 1 | sed -e '1,2d' | grep -v "Average" | grep "IFACE\|eth0"
00:41:43 IFACE rxerr/s txerr/s coll/s rxdrop/s txdrop/s txcarr/s rxfram/s rxfifo/s txfifo/s
00:41:44 eth0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

・Step2 受信時にエラーとなったパケットのCSV出力

$ LANG=C;sar -n EDEV 1 5 | sed -e '1,2d' | grep -v "Average" | grep "eth0" | awk '{print "\"" $1 "\",\"" $3 "\""}'
"01:02:05","0.00"
"01:02:06","0.00"
"01:02:07","0.00"
"01:02:08","0.00"
"01:02:09","0.00"

・Step3 「netstat -i」コマンドを使った受信時エラーのCSV

$ netstat -i | sed -e '1,2d' | awk '{print "\"" $1 "\",\"" $5 "\""}'
"eth0","0"
"lo","0"
"vmnet1","0"
"vmnet8","0"


■iostat によるディスク統計

・Step1 そのままデータだけ。

$ iostat -d 1 1 | sed -e '1,2d'
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
sda 0.74 16.83 21.70 311380 401424
sda1 0.73 16.78 21.70 310483 401424
sda2 0.00 0.00 0.00 14 0
sda5 0.00 0.03 0.00 627 0

・Step2 sda1の情報だけ。

$ iostat -d 1 5 | sed -e '1,3d' | grep -v "^\$\|Device" | grep sda1
sda1 0.74 16.85 21.77 310483 401184
sda1 0.00 0.00 0.00 0 0
sda1 0.00 0.00 0.00 0 0
sda1 0.00 0.00 0.00 0 0
sda1 0.00 0.00 0.00 0 0

■cronによる監視

毎日、00分に12分間隔、120回のデータを取得する

$ sudo -u root sh -c 'export EDITOR=vim;crontab -e;crontab -l'
0 0 * * * /usr/lib/sysstat/sa1 720 120 &

以下の形式で保存される。

$ ls /var/log/sysstat/sa`date +%d`

■cronによる監視を停止する。

コメントアウトするだけです。
また必要になったら、#を取り除きます。

$ sudo -u root sh -c 'export EDITOR=vim;crontab -e;crontab -l'
#0 0 * * * /usr/lib/sysstat/sa1 720 120 &