Muninにはプロセス数をカウントしてくれるpsプラグインが用意されている。
これを使ってhttpdプロセス数をカウントする場合、手っ取り早くは以下のようにシンボリックリンクを用意してやればよい(CentOS 5.5上でRPMForgeからMuninパッケージをインストールしている場合)。
[root@localhost ~]# ln -s /usr/share/munin/plugins/ps_ /etc/munin/plugins/ps_httpd
この結果発行されるプロセス数確認コマンドは以下のようになる。
pgrep -f -l "httpd" | grep "httpd" | wc -l
しかしこれだけでは、vi httpd.confなどのプロセス分もカウントされてしまうため都合がよくない。
厳密にフルパス(/usr/sbin/httpd)でカウントするためには以下のようにnameパラメータで指定する。
[root@localhost ~]# cat /etc/munin/plugin-conf.d/ps [ps_httpd] env.name /usr/sbin/httpd
nameはpgrepでリストする際の引数になるので、厳密なフルパスでのプロセス数がカウントされるはず。
[root@localhost ~]# pgrep -f -l "/usr/sbin/httpd" | grep "/usr/sbin/httpd" 6545 /usr/sbin/httpd 6547 /usr/sbin/httpd 6552 /usr/sbin/httpd 6555 /usr/sbin/httpd 6561 /usr/sbin/httpd 6563 /usr/sbin/httpd 19819 grep /usr/sbin/httpd
おや?これでは途中のgrepコマンド分までカウントされてしまい都合がよくない。さらにregexパラメータで対象を絞り込むことにする。
[root@localhost ~]# cat /etc/munin/plugin-conf.d/ps [ps_httpd] env.name /usr/sbin/httpd env.regex \w\W/usr/sbin/httpd
\w\W部分が先頭のプロセス番号とそのあとの空白にマッチするので、以下のような出力になる。
[root@localhost ~]# pgrep -f -l "/usr/sbin/httpd" | grep "\w\W/usr/sbin/httpd" 6545 /usr/sbin/httpd 6547 /usr/sbin/httpd 6552 /usr/sbin/httpd 6555 /usr/sbin/httpd 6561 /usr/sbin/httpd 6563 /usr/sbin/httpd
これで/usr/sbin/httpdのプロセス数を正確にカウントできるようになる(見落としがあったらゴメンなさい)。
■2010-07-18追記
RPMForgeで提供されているMunin 1.2系列では、以下のチケットで修正が入っている。
#73 (Bug i ps_ plugin on systems using pgrep) – Munin – Trac
そもそも最初のpgrepコマンドで後続のgrep分が出てくるのがおかしいのかとも思うが、パイプ先のファイルハンドルオープンを先におこなっている(=先に起動している)と考えればおかしいとも言い切れない。
ちなみにFedora 13ではpgrep出力に後続grepが含まれなくなっていた。
[root@localhost ~]# pgrep -f -l "/usr/sbin/httpd" | grep "\w\W/usr/sbin/httpd" 7861 grep /w/W/usr/sbin/httpd [root@localhost ~]# pgrep -f -l "/usr/sbin/httpd" 1540 /usr/sbin/httpd 1664 /usr/sbin/httpd 1665 /usr/sbin/httpd 1666 /usr/sbin/httpd 1667 /usr/sbin/httpd 1668 /usr/sbin/httpd 1669 /usr/sbin/httpd 1670 /usr/sbin/httpd 1671 /usr/sbin/httpd
Fedora 13の場合、Muninがプラグインの修正が入っていることと、pgrepの出力もそもそも違うということで、env.regex指定しなくても正しいプロセス数がカウントできる。