d276ab45aa90ea8bdb307d9280711caf8c40bad0
[monitor.git] / nagios / plugins / checkpcu.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.RebootNodeWithPCU(hostname, True)
39     except Exception, e:
40         if "No PCUs associated with Node" in str(e):
41             print "CRITICAL: Failure: %s" % str(e)
42             sys.exit(2)
43         else:
44             print "UNKNOWN: Failure: %s" % str(e)
45             sys.exit(3)
46
47     t1 = 0
48     t2 = time.time()
49
50     if n == 0:
51         print "OK: PCU test successful"
52         sys.exit(0)
53     elif n != 0:
54         print "WARNING: PCU configuration incomplete: %s" % n
55         sys.exit(1)
56     else:
57         print "FAKE-CRITICAL: PCU test failed"
58         sys.exit(2)
59
60
61 if __name__ == '__main__':
62     f = open("/tmp/checkpcu", 'a')
63     f.write("checkpcu %s %s\n" % (time.time(), " ".join(sys.argv[1:])))
64     f.close()
65     main()