enables keyword substitution on the trunks
[infrastructure.git] / nagios / plugin / check_planetlab.py
1 #!/usr/bin/env python
2
3 #
4 # This script is a nagios plugin that allows to check for a host
5 #
6
7 import sys
8 import getopt
9
10 import nagios
11 import comon_query
12 import comon_sensor
13
14 command=sys.argv[0]
15 revision="$Revision$"
16
17 #nagios_plugins_dir="/usr/lib/nagios/plugins"
18
19
20 options = 'vhnk:t:'
21 long_opts = [ 'version' , 'help', 'no-comon' , 'key=', 'time-out=' ]
22
23
24 usage_string="""Usage : %s [-n] [-k private_key] nodename
25 Revision %s
26 This nagios plugin checks for a given (planetlab) host
27 The regular approach is to
28 * First try to reach the comon query interface.
29   default host is %s
30   this can be overridden with the --host option (NIY)
31 TODO : prevent this stage if a 'none' host is provided
32 * If we cannot conclude from this, we then try and reach
33   the comon sensor on the node itself on port %d, 
34 TODO : skip on -n option
35 * Then, if an ssh private key is provided with the -k option,
36   we try to enter the node as root and check that
37   the pl_conf slice is up and running on the node
38 TODO : do this only of the -k option is provided
39 * if none of this is conclusive we just check for the ssh server on the node
40   with the standard ssh plugin
41 TODO : probably this should be left to the nagios config
42 """%(command,revision,comon_query.SERVER,comon_sensor.PORT)
43
44 def usage ():
45     print usage_string
46     sys.exit(1)
47
48 ####################
49 def main ():
50
51     try:
52         opts,args = getopt.getopt(sys.argv[1:], options, long_opts)
53     except getopt.GetoptError:
54         print "Unknown option"
55         usage()
56
57     opt_comon = True
58     opt_key = None
59     opt_timeout = 10
60
61     for o,a in opts:
62         if o in ['-v','--version']:
63             print command,'--',revision
64             sys.exit(3)
65         elif o in ['-h','--help']:
66             usage()
67         elif o in ['-n','--no-comon']:
68             opt_comon = False            
69         elif o in ['-k','--key']:
70             opt_key=a
71         elif o in ['-t','--time-out']:
72             opt_timeout=int(a)
73         else:
74             print "Unknown option",o
75             usage()
76
77     if not len(args) == 1:
78         usage()
79     nodename = args[0]
80
81     status = nagios.UNKNOWN
82
83     status = comon_query.check(nodename)
84     if status != nagios.UNKNOWN:
85         return status
86     
87     if opt_comon:
88         status = comon_sensor.check(nodename,opt_timeout)
89
90 #    print "status",status
91     return status
92
93
94 if __name__=='__main__':
95     sys.exit(main())
96         
97