Add : (Denis GERMAIN) check_shinken plugin and interface in the arbiter to get data.
[shinken.git] / libexec / check_shinken.py
blob3c28c2f70070c8f68b6059f01a53d8059bdf382d
1 #!/usr/bin/env python
2 ################################################
3 #check_shinken.py
4 ################################################
6 from optparse import OptionParser
7 try:
8 import shinken.pyro_wrapper as pyro
9 from shinken.pyro_wrapper import Pyro
10 except ImportError:
11 print 'CRITICAL : check_shinken requires the Python Pyro and the shinken.pyro_wrapper module. Please install it.'
12 raise SystemExit, CRITICAL
14 # Exit statuses recognized by Nagios and thus by Shinken
15 UNKNOWN = -1
16 OK = 0
17 WARNING = 1
18 CRITICAL = 2
19 #Name of the Pyro Object we are searching
20 PYRO_OBJECT = 'ForArbiter'
21 daemon_types = ['arbiter', 'broker', 'scheduler', 'poller', 'reactionner']
23 #The following functions just count the number of deamons with the associated parameters
24 def count_total(result):
25 num = 0
26 for cur in result:
27 num += 1
28 return num
30 def count_alive(result):
31 num = 0
32 for cur in result:
33 if result[cur]['alive'] == True:
34 num += 1
35 return num
37 def count_total_spare(result):
38 num = 0
39 for cur in result:
40 if result[cur]['spare'] == True:
41 num += 1
42 return num
44 def count_alive_spare(result):
45 num = 0
46 for cur in result:
47 if (result[cur]['spare'] == True and result[cur]['alive'] == True):
48 num += 1
49 return num
51 #this function prints the names of the daemons fallen
52 def find_dead(result):
53 dead_ones = ''
54 for cur in result:
55 if (result[cur]['alive'] == False):
56 dead_ones = dead_ones+' '+cur
57 return dead_ones
59 def check_deamons_numbers(result, target):
60 total_number = count_total(result)
61 alive_number = count_alive(result)
62 total_spare_number = count_total_spare(result)
63 alive_spare_number = count_alive_spare(result)
64 dead_number = total_number - alive_number
65 dead_list = find_dead(result)
66 #TODO : perfdata to graph deamons would be nice (in big HA architectures)
67 #if alive_number <= critical, then we have a big problem
68 if alive_number <= options.critical:
69 print "CRITICAL - %d/%d %s(s) UP" % (alive_number, total_number, target)
70 raise SystemExit, CRITICAL
71 #We are not in a case where there is no more daemons, but are there daemons down?
72 elif dead_number >= options.warning:
73 print "WARNING - %d/%d %s(s) DOWN :%s" % (dead_number, total_number, target, dead_list)
74 raise SystemExit, WARNING
75 #Everything seems fine. But that's no surprise, is it?
76 else :
77 print "OK - %d/%d %s(s) UP, with %d/%d spare(s) UP" % (alive_number, total_number, target, alive_spare_number, total_spare_number)
78 raise SystemExit, OK
80 # Adding options. None are required, check_shinken will use shinken defaults
81 #TODO : Add more control in args problem and usage than the default OptionParser one
82 parser = OptionParser()
83 parser.add_option('-a', '--hostname', dest='hostname', default='127.0.0.1')
84 parser.add_option('-p', '--portnumber', dest='portnum', default=7770)
85 parser.add_option('-s', '--ssl', dest='ssl', default=False)
86 #TODO : Add a list of correct values for target and don't authorize anything else
87 parser.add_option('-t', '--target', dest='target')
88 parser.add_option('-d', '--daemonname', dest='daemon', default='')
89 #In HA architectures, a warning should be displayed if there's one daemon down
90 parser.add_option('-w','--warning', dest='warning', default = 1)
91 #If no deamon is left, display a critical (but shinken will be probably dead already)
92 parser.add_option('-c', '--critical', dest='critical', default = 0)
94 #Retrieving options
95 options, args = parser.parse_args()
96 #TODO : for now, helpme doesn't work as desired
97 options.helpme = False
99 # Check for required option target
100 if not getattr(options, 'target'):
101 print 'CRITICAL - target is not specified; You must specify which daemons you want to check!'
102 raise SystemExit, CRITICAL
103 elif options.target not in daemon_types:
104 print 'CRITICAL - target %s is not a Shinken daemon!' % options.target
105 raise SystemExit, CRITICAL
107 uri = pyro.create_uri(options.hostname, options.portnum, PYRO_OBJECT , options.ssl)
108 if options.daemon:
109 #We just want a check for a single satellite daemon
110 #Only OK or CRITICAL here
111 daemon_name = options.daemon
112 result = Pyro.core.getProxyForURI(uri).get_satellite_status(options.target, daemon_name)
113 if result:
114 if result['alive']:
115 print 'OK - %s alive' % daemon_name
116 raise SystemExit, OK
117 else:
118 print 'CRITICAL - %s down' % daemon_name
119 raise SystemExit, CRITICAL
120 else:
121 print 'UNKNOWN - %s status could not be retrieved' % daemon_name
122 raise SystemExit, UNKNOWN
123 else:
124 #If no daemonname is specified, we want a general overview of the "target" daemons
125 result = {}
126 daemon_list = Pyro.core.getProxyForURI(uri).get_satellite_list(options.target)
127 for daemon_name in daemon_list:
128 #Getting individual daemon and putting status info in the result dictionnary
129 result[daemon_name] = Pyro.core.getProxyForURI(uri).get_satellite_status(options.target, daemon_name)
130 #Now we have all data
131 if result:
132 check_deamons_numbers(result, options.target)
133 else :
134 print 'UNKNOWN - Arbiter could not retrieve status for %s' % options.target
135 raise SystemExit, UNKNOWN