add external commands as stubs for the nagios plugins
[monitor.git] / commands / checkmode.py
1 #!/usr/bin/python
2
3 import time
4 import sys
5 import os
6
7 from monitor.wrapper import plc
8
9 def argv_to_dict(argv):
10         """
11                 NOTE: very bare-bones, no error checking, will fail easily.
12         """
13         d = {}
14         prev=None
15         for a in argv:
16                 if "--" == a[0:2]:
17                         prev = a[2:]
18                 elif "-" == a[0:1]:
19                         prev = a[1:]
20                 else:
21                         d[prev] = a
22         return d
23
24 def main():
25         d = argv_to_dict(sys.argv[1:])
26
27         api = plc.api
28         if 'hostname' in d or 'H' in d:
29                 try:
30                         hostname = d['host']
31                 except:
32                         hostname = d['H']
33         else:
34                 print "UNKNOWN: argument error"
35                 sys.exit(3)
36
37         try:
38                 n = api.GetNodes(hostname)[0]
39         except:
40                 print "UNKNOWN: API failure"
41                 sys.exit(3)
42
43         if n['last_contact']:
44                 t1 = n['last_contact']
45         else:
46                 t1 = 0
47         t2 = time.time()
48         #print n['boot_state'], n['run_level'], t1, t2, t2-t1
49
50         if t2-t1 < 60*60*30:
51                 if n['boot_state'] == n['run_level']:
52                         print "OK: bootstate matches runlevel and lastcontact is up to date"
53                         sys.exit(0)
54                 else:
55                         print "WARNING: bootstate does not match runlevel"
56                         sys.exit(1)
57         else:
58                 print "CRITICAL: node last_contact is stale, assumed offline"
59                 sys.exit(2)
60
61
62 if __name__ == '__main__':
63         f = open("/tmp/checkmode", 'a')
64         f.write("checkmode %s %s\n" % (time.time(), " ".join(sys.argv[1:])))
65         f.close()
66         main()