2 ################################################
4 ################################################
6 from optparse
import OptionParser
8 import shinken
.pyro_wrapper
as pyro
9 from shinken
.pyro_wrapper
import Pyro
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
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
):
30 def count_alive(result
):
33 if result
[cur
]['alive'] == True:
37 def count_total_spare(result
):
40 if result
[cur
]['spare'] == True:
44 def count_alive_spare(result
):
47 if (result
[cur
]['spare'] == True and result
[cur
]['alive'] == True):
51 #this function prints the names of the daemons fallen
52 def find_dead(result
):
55 if (result
[cur
]['alive'] == False):
56 dead_ones
= dead_ones
+' '+cur
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?
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
)
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)
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
)
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
)
115 print 'OK - %s alive' % daemon_name
118 print 'CRITICAL - %s down' % daemon_name
119 raise SystemExit, CRITICAL
121 print 'UNKNOWN - %s status could not be retrieved' % daemon_name
122 raise SystemExit, UNKNOWN
124 #If no daemonname is specified, we want a general overview of the "target" daemons
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
132 check_deamons_numbers(result
, options
.target
)
134 print 'UNKNOWN - Arbiter could not retrieve status for %s' % options
.target
135 raise SystemExit, UNKNOWN