nagios plugin API | pythonで書いてみる。

http://nagiosplug.sourceforge.net/developer-guidelines.html

// http://nagios.sourceforge.net/docs/3_0/pluginapi.html

決め事はシンプルかな 基本3点

* 80字以内の1行で出力する

* return code
0 OK
1 Warning
2 Critical
3 Unknown

* option
There are a few reserved options that should not be used for other purposes:

-V version (–version)
-h help (–help)
-t timeout (–timeout)
-w warning threshold (–warning)
-c critical threshold (–critical)
-H hostname (–hostname)
-v verbose (–verbose) * verbose : 複数行メッセージを利用する場合は説明通りに

In addition to the reserved options above, some other standard options are:

-C SNMP community (–community)
-a authentication password (–authentication)
-l login name (–logname)
-p port or password (–port or –passwd/–password)monitors operational
-u url or username (–url or –username)

さっそく書いてみる。
memcachedをチェックしてみる

check_memcached.py : /usr/lib64/nagios/pluginsに+x実行権限を付けておく

#!/usr/bin/python
import sys
import socket
import re
from optparse import OptionParser

OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
RECVLEN = 1024

try:
 parser = OptionParser()
 parser.add_option(“-H”, “–hostname”, dest=”hostname”, type=”string”, help=”hostname”)
 parser.add_option(“-p”, “–port”, dest=”port”, type=”int”, help=”port number”)
 (options, args) = parser.parse_args()

 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect_ex(( options.hostname, options.port ))
 sent = s.send(“stats\r\n”)
 if sent == 0:
   raise RuntimeError(“socket connection broken sent”)

 msg = ”
 while True:
   chunk = s.recv(RECVLEN)
   if chunk == ”:
     raise RuntimeError(“socket connection broken recv”)
   msg = msg + chunk
   if len(chunk) < RECVLEN:
break

sent = s.send("quit\r\n")
s.close()
match = re.search(r'STAT pid [0-9]+', msg)
if match:
print "OK: memcache status: ", match.group(0)
else:
else:
raise RuntimeError("response status invalid")
except:
print "check memcached error:", sys.exc_info()[1]
sys.exit(CRITICAL)

sys.exit(OK)

/usr/lib64/nagios/plugins
-rwxr-xr-x 1 root root 1079 11月 25 01:24 2012 check_memcached.py

objects/command.cfg

# ‘check_memcached.py’ command definition
define command{
       command_name    check_memcached
       command_line    $USER1$/check_memcached.py -H $HOSTADDRESS$ $ARG1$
       }

conf.d/の対象 *.cfg

# _MEMCACHEDPORTを追加、複数ある場合はまた別にhostを追加

define host{
       use             template-internet-host
       host_name       groundroad_internal
       address         192.168.0.170
       _HTTPHOSTNAME   groundroad.com
       _HTTPPORT       80
       _HTTPURI        /company
       _HTTPSTRING     company profile
       _MEMCACHEDPORT  55001
       notes           groundroad local check
       notes_url       http://groundroad.com
       }
# hostgroupとserviceを追加
define hostgroup{
       hostgroup_name  LocalMemcachedHosts
       alias           Local Memcached Hosts
       members         groundroad_internal
       }

define service{
       use             generic-service
       hostgroup_name          LocalMemcachedHosts
       service_description     memcached internal check
       check_command   check_memcached!-p $_HOSTMEMCACHEDPORT$
}

# nagios -v nagios.cfg
# /etc/init.d/nagios restart

とりあえず動作自体はOKでした。
後は、必要に応じてタイムアウトやデータチェック、warning, critical 閾値チェックをいれると良いようです。

設定ファイルの配置はどんなのが良いのかな、ホスト、サービス毎でどんどん増えてしまうけど

This entry was posted on 日曜日, 11月 25th, 2012 at 1:46 AM and is filed under blog, nagios, Python. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Unityによるゲームを公開しております iOS/Android
https://itunes.apple.com/jp/app/lost-knight-3d-action/id900917032
https://play.google.com/store/apps/details?id=com.groundroad.runknight

各種開発支援・機能開発等小さいお仕事などもお請けしております。
unity開発支援, PHPシステム開発, javascript/html5 フロント開発, titanium mobileアプリ開発
お気軽にお問い合わせください
大崎・五反田近郊での対面でのお打ち合わせはいつでも可能です。