CPU使用率を調べるプラグインは世の中にいろいろ出回ってるっぽいけど、何が重いかを調べられるのが見つからなかったので、自分で作った。
$SERVICEOUTPUT$がプラグインの出力に置き換えられるっぽいけど、なぜか1行目しかメールで来ない。
Webからはちゃんと全部見れたけど、メールで一覧が見れなきゃ意味ないじゃないか。
こんな感じでメールが来る。
***** Nagios ***** Notification Type: RECOVERY Service: Current CPU Host: localhost Address: 127.0.0.1 State: OK Date/Time: Fri Oct 1 06:07:24 JST 2010 Additional Info: OK - CPU: 15.0%
/usr/lib/nagios/plugins/check_cpu
実行権限も与える。
#!/usr/local/bin/python from optparse import OptionParser import sys import subprocess, re class Ps: ITEMS = { 'USER' : 0, 'PID' : 1, 'CPU' : 2, 'MEM' : 3, 'VSZ' : 4, 'RSS' : 5, 'TTY' : 6, 'STAT' : 7, 'START' : 8, 'TIME' : 9, 'COMMAND' : 10, } def __init__(self): self.ps = None def load(self): cmd = "ps -e aux" proc = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE) stdout, stderr = proc.communicate() proc.wait() self.ps = stdout def getTotal(self, item): # item index index = self.ITEMS.get(item.upper()) if index is None: raise Exception("item is not found: %s" % item) # ps load if self.ps is None: self.load() # split lines = self.ps.strip().split("\n") lines.pop(0) # total p = re.compile(r'\W+') total = 0.0 for line in lines: value = p.split(line)[index] total += float(value) return total def dump(self): # ps load if self.ps is None: self.load() return self.ps # Return status RETURN_OK = 0 RETURN_WARNING = 1 RETURN_CRITICAL = 2 RETURN_UNKNOWN = 3 RETURN_VALUES = [ 'OK', 'Warning', 'Critical', 'Unknown', ] # main def main(): # parse arguments usage = "%prog -w warning_value -c critical_value [-v]" parser = OptionParser(usage=usage) parser.add_option("-w", "--warning", type="float", dest="warning") parser.add_option("-c", "--critical", type="float", dest="critical") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False) (options, args) = parser.parse_args() if options.warning is None or options.critical is None: print parser.get_usage(), sys.exit(RETURN_UNKNOWN) # check cpu ps = Ps() cpu = ps.getTotal("CPU") status = RETURN_OK if options.warning <= cpu: status = RETURN_WARNING if options.critical <= cpu: status = RETURN_CRITICAL print "%s - CPU: %.1f%%" % (RETURN_VALUES[status], cpu) if options.verbose: print "\n" + ps.dump() sys.exit(status) if __name__ == "__main__": main()
/etc/nagios/objects/commands.cfg
以下追加。
# 'check_cpu' command definition define command{ command_name check_cpu command_line $USER1$/check_cpu -w $ARG1$ -c $ARG2$ -v }
/etc/nagios/objects/localhost.cfg
以下追加。
# Define a service to CPU on the local machine. define service{ use local-service host_name localhost service_description Current CPU check_command check_cpu!50!80 }
追記 2010-10-12 13:25:48
2行目以降は、$LONGSERVICEOUTPUT$で置き換えられた。
Nagiosの標準マクロ
http://oss.aspect-sys.co.jp/nagios_jp/macrolist.html