1 | #!/usr/bin/python2 |
---|
2 | |
---|
3 | import sys, getopt, httplib, xml.etree.ElementTree, subprocess, re, pdb, traceback, datetime, urlparse, urllib |
---|
4 | import simplejson as json |
---|
5 | import pwd, os |
---|
6 | |
---|
7 | nagios_codes = { |
---|
8 | 'OK' : 0, |
---|
9 | 'WARNING' : 1, |
---|
10 | 'CRITICAL' : 2, |
---|
11 | 'UNKNOWN' : 3, |
---|
12 | 'DEPENDENT' : 4 |
---|
13 | } |
---|
14 | |
---|
15 | def nagios_return(code, response) : |
---|
16 | """ prints the response message |
---|
17 | and exits the script with one |
---|
18 | of the defined exit codes |
---|
19 | DOES NOT RETURN |
---|
20 | """ |
---|
21 | print code + ": " + response |
---|
22 | sys.exit(nagios_codes[code]) |
---|
23 | |
---|
24 | def nagios_return_complex(results, reporter) : |
---|
25 | def deal_with_result(result, reporter) : |
---|
26 | print "[" + reporter + "] " + result['code'] + ": " + result['message'] |
---|
27 | return result['code'] |
---|
28 | |
---|
29 | #pdb.set_trace() |
---|
30 | |
---|
31 | # Scan all condition/status check results and create a list of appropriate exit codes. |
---|
32 | exit_code_keys = map(lambda result : deal_with_result(result, reporter), results) |
---|
33 | suggested_exit_codes = list(map(lambda key : nagios_codes[key], exit_code_keys)) |
---|
34 | |
---|
35 | # Exit with the highest suggested exit code, because the higher the exit code the more problematic the status is and problems have priority over harmony. |
---|
36 | sys.exit(max(suggested_exit_codes)) |
---|
37 | |
---|
38 | def usage(command_line_parameters_usage_string) : |
---|
39 | """ returns Nagios status UNKNOWN with |
---|
40 | a one line usage description |
---|
41 | usage() calls nagios_return() |
---|
42 | """ |
---|
43 | nagios_return('UNKNOWN', |
---|
44 | "command line usage: \n" + sys.argv[0] + command_line_parameters_usage_string) |
---|
45 | |
---|
46 | def generic_validator(data, |
---|
47 | descriptive_string, |
---|
48 | kwargs, |
---|
49 | special_validator) : |
---|
50 | |
---|
51 | special_plugin_file_name = kwargs['special_plugin_file_name'] # X- |
---|
52 | |
---|
53 | timestamp = datetime.datetime.today().isoformat() |
---|
54 | valid = False |
---|
55 | |
---|
56 | try : |
---|
57 | valid = special_validator(data, |
---|
58 | descriptive_string, |
---|
59 | kwargs) |
---|
60 | except : |
---|
61 | traceback_string = traceback.format_exc() |
---|
62 | #pdb.set_trace() |
---|
63 | |
---|
64 | err_log_file_path = os.path.normpath("/tmp/" + special_plugin_file_name + "_err_generic_validator__" + descriptive_string.replace('/' , '%2F') + "__" + timestamp + ".log") |
---|
65 | |
---|
66 | with open(name = err_log_file_path, |
---|
67 | mode = "wt") as debugging_output_file : |
---|
68 | debugging_output_file.write(traceback_string) |
---|
69 | #sys.exit(999) # X- exit with some special error status |
---|
70 | |
---|
71 | return valid |
---|
72 | |
---|
73 | |
---|
74 | def check_HTML_wellformedness(data, |
---|
75 | descriptive_string, |
---|
76 | **kwargs) : # X- |
---|
77 | |
---|
78 | def special_validator(data, |
---|
79 | descriptive_string, |
---|
80 | kwargs) : |
---|
81 | pattern = '.*<html.*>.+</html>.*' |
---|
82 | pattern_regex = re.compile(pattern, re.MULTILINE | re.IGNORECASE | re.DOTALL) |
---|
83 | results = pattern_regex.search(data) |
---|
84 | |
---|
85 | # special_plugin_file_name |
---|
86 | |
---|
87 | if results is not None : |
---|
88 | return True |
---|
89 | else : |
---|
90 | return False |
---|
91 | |
---|
92 | #pdb.set_trace() |
---|
93 | |
---|
94 | # X- Do PROPER wellformedness check - once the tools are availble. |
---|
95 | wellformedness = generic_validator(data, |
---|
96 | descriptive_string, |
---|
97 | kwargs, |
---|
98 | special_validator) |
---|
99 | |
---|
100 | return wellformedness |
---|
101 | |
---|
102 | |
---|
103 | def check_JSON_wellformedness(data, |
---|
104 | descriptive_string, |
---|
105 | **kwargs) : |
---|
106 | def special_validator(data, |
---|
107 | descriptive_string, |
---|
108 | kwargs) : |
---|
109 | |
---|
110 | try : |
---|
111 | json.loads(data) |
---|
112 | except JSONDecodeError : |
---|
113 | valid = False |
---|
114 | else : |
---|
115 | valid = True |
---|
116 | |
---|
117 | return valid |
---|
118 | |
---|
119 | wellformedness = generic_validator(data, |
---|
120 | descriptive_string, |
---|
121 | kwargs, |
---|
122 | special_validator) |
---|
123 | |
---|
124 | return wellformedness |
---|
125 | |
---|
126 | |
---|
127 | def check_XML_validity(data, |
---|
128 | descriptive_string, |
---|
129 | **kwargs) : |
---|
130 | |
---|
131 | def special_validator(data, |
---|
132 | descriptive_string, |
---|
133 | kwargs) : |
---|
134 | |
---|
135 | try : |
---|
136 | data_tree_root_element = xml.etree.ElementTree.XML(text = data) |
---|
137 | |
---|
138 | if data_tree_root_element.tag == kwargs['valid_root_element_tag'] : |
---|
139 | return True |
---|
140 | else : |
---|
141 | return False |
---|
142 | except : |
---|
143 | return False |
---|
144 | |
---|
145 | wellformedness = generic_validator(data, |
---|
146 | descriptive_string, |
---|
147 | kwargs, |
---|
148 | special_validator) |
---|
149 | return wellformedness |
---|
150 | |
---|
151 | |
---|
152 | def check_LDAP_validity(data, |
---|
153 | descriptive_string, |
---|
154 | **kwargs) : |
---|
155 | |
---|
156 | def special_validator(data, |
---|
157 | descriptive_string, |
---|
158 | kwargs) : |
---|
159 | try : |
---|
160 | return True # X- |
---|
161 | except : |
---|
162 | return False |
---|
163 | |
---|
164 | wellformedness = generic_validator(data, |
---|
165 | descriptive_string, |
---|
166 | kwargs, |
---|
167 | special_validator) |
---|
168 | return wellformedness |
---|
169 | |
---|
170 | |
---|
171 | def check_ldap(host, |
---|
172 | bind_DN) : |
---|
173 | |
---|
174 | OpenDJ_directory_path = '/srv/LDAP/OpenDJ-2.5.0-Xpress1/' |
---|
175 | |
---|
176 | base_DN = 'dc=clarin,dc=eu' |
---|
177 | query = '(objectClass=CLARINPerson)' |
---|
178 | |
---|
179 | command = ['sudo', |
---|
180 | OpenDJ_directory_path + "/bin/ldapsearch", |
---|
181 | '--port', '10389', |
---|
182 | '--baseDN', base_DN, |
---|
183 | '--useStartTLS', |
---|
184 | '--trustAll', |
---|
185 | '--hostname', host, |
---|
186 | '--bindDN', bind_DN, |
---|
187 | "--bindPasswordFile", '/root/LDAP_passwdfile', |
---|
188 | query, |
---|
189 | 'isMemberOf'] |
---|
190 | |
---|
191 | # Run OpenDJ's "ldapsearch" command line utility |
---|
192 | #pdb.set_trace() |
---|
193 | |
---|
194 | OpenDJ_uid = pwd.getpwnam('opendj')[2] |
---|
195 | Nagios_uid = pwd.getpwnam('nagios')[2] |
---|
196 | |
---|
197 | try : |
---|
198 | os.setuid(OpenDJ_uid) |
---|
199 | except OSError, e : |
---|
200 | raise e |
---|
201 | |
---|
202 | process = subprocess.Popen(command, |
---|
203 | stdout = subprocess.PIPE, |
---|
204 | stderr = subprocess.PIPE) |
---|
205 | |
---|
206 | stdout, stderr = process.communicate() |
---|
207 | |
---|
208 | try : |
---|
209 | os.setuid(Nagios_uid) |
---|
210 | except OSError, e : |
---|
211 | raise e |
---|
212 | |
---|
213 | #print stdout |
---|
214 | #print stderr |
---|
215 | #pdb.set_trace() |
---|
216 | |
---|
217 | if process.returncode == 0 : |
---|
218 | return { |
---|
219 | "code" : "OK", |
---|
220 | "message" : 'Host %s is up and responds as expected to a query "%s" with base DN "%s".' % (host, query, base_DN) |
---|
221 | } |
---|
222 | else : |
---|
223 | return { |
---|
224 | "code" : "CRITICAL", |
---|
225 | "message" : 'Host %s is not up or does not respond as expected to a query "%s" with base DN "%s".' % (host, query, base_DN) |
---|
226 | } |
---|
227 | |
---|
228 | def check_condition(host, |
---|
229 | http_path, |
---|
230 | HTTP_method, |
---|
231 | port_number, |
---|
232 | authorize, |
---|
233 | validator, |
---|
234 | **validator_arguments) : |
---|
235 | |
---|
236 | #pdb.set_trace() |
---|
237 | # X- remove implicit argument values |
---|
238 | #if port_number is None : port_number = 80 |
---|
239 | #if authorize is None : authorize = True |
---|
240 | |
---|
241 | def handle_connection_failure(problem_description) : |
---|
242 | err_log_file_path = os.path.normpath("/tmp/" + special_plugin_file_name + "_err_connection_failure__" + http_path.replace('/' , '%2F') + "__" + timestamp + ".log") |
---|
243 | |
---|
244 | with open(name = err_log_file_path, |
---|
245 | mode = "wt") as debugging_output_file : |
---|
246 | debugging_output_file.write(problem_description) |
---|
247 | |
---|
248 | special_plugin_file_name = validator_arguments['special_plugin_file_name'] # X- |
---|
249 | |
---|
250 | #http_path = str(urllib.quote_plus(http_path)) |
---|
251 | |
---|
252 | timestamp = datetime.datetime.today().isoformat() |
---|
253 | |
---|
254 | try : |
---|
255 | conn = httplib.HTTPConnection(host = host, port = port_number) # X- accomodate https? |
---|
256 | |
---|
257 | request = conn.request(HTTP_method, http_path) |
---|
258 | except : |
---|
259 | traceback_string = traceback.format_exc() |
---|
260 | |
---|
261 | handle_connection_failure(traceback_string + "\nThis problem originates from location 1 in '" + special_plugin_file_name + "'.\n") |
---|
262 | |
---|
263 | return { |
---|
264 | "code" : "CRITICAL", |
---|
265 | "message" : 'HTTP connection to host %s failed.' % (host) |
---|
266 | } |
---|
267 | else : |
---|
268 | try : |
---|
269 | response = conn.getresponse() |
---|
270 | |
---|
271 | data = response.read() |
---|
272 | |
---|
273 | conn.close() |
---|
274 | except : |
---|
275 | traceback_string = traceback.format_exc() |
---|
276 | |
---|
277 | handle_connection_failure(traceback_string + "\nThis problem originates from location 2 in '" + special_plugin_file_name + "'.\n") |
---|
278 | else : |
---|
279 | |
---|
280 | redirecting_responses = frozenset([ |
---|
281 | httplib.MOVED_PERMANENTLY, |
---|
282 | httplib.FOUND, |
---|
283 | httplib.SEE_OTHER, |
---|
284 | httplib.TEMPORARY_REDIRECT, |
---|
285 | ]) |
---|
286 | |
---|
287 | if response.status == httplib.OK : |
---|
288 | # HTTP status codes 200 and 302 |
---|
289 | # X- Resolve redirect in case of HTTP status == 302 |
---|
290 | well_formed = validator(data = data, |
---|
291 | descriptive_string = http_path, |
---|
292 | **validator_arguments) # ['validator_arguments']) |
---|
293 | |
---|
294 | if well_formed : |
---|
295 | return { |
---|
296 | "code" : "OK", |
---|
297 | "message" : 'Host %s is up and returns well-formed data at "%s".' % (host, http_path) |
---|
298 | } |
---|
299 | else : |
---|
300 | return { |
---|
301 | "code" : "CRITICAL", |
---|
302 | "message" : 'Host %s is up but returns non-well-formed data at "%s".' % (host, http_path) |
---|
303 | } |
---|
304 | |
---|
305 | elif response.status in redirecting_responses : |
---|
306 | new_location_URL = dict(response.getheaders())['location'] |
---|
307 | |
---|
308 | parsed_new_location_URL = urlparse.urlparse(new_location_URL) |
---|
309 | |
---|
310 | |
---|
311 | |
---|
312 | return check_condition(host = parsed_new_location_URL.netloc, |
---|
313 | http_path = parsed_new_location_URL.path, |
---|
314 | HTTP_method = HTTP_method, |
---|
315 | port_number = port_number, |
---|
316 | authorize = authorize, |
---|
317 | validator = validator, |
---|
318 | **validator_arguments) # special_plugin_file_name = validator_arguments['special_plugin_file_name'] |
---|
319 | |
---|
320 | elif response.status == httplib.UNAUTHORIZED and authorize == False : |
---|
321 | return { |
---|
322 | "code" : "OK", |
---|
323 | "message" : 'Host %s is up and requests authorization at "%s".' % (host, http_path) |
---|
324 | } |
---|
325 | |
---|
326 | else : |
---|
327 | #pdb.set_trace() |
---|
328 | handle_connection_failure("Unreachable URL! HTTP response code: " + str(response.status) + |
---|
329 | "\nThis problem originates from location 3 in '" + special_plugin_file_name + "'.\n") |
---|
330 | |
---|
331 | return { |
---|
332 | "code" : "CRITICAL", |
---|
333 | "message" : 'Host %s has a problem with the URL path component "%s".' % (host, http_path) |
---|
334 | } |
---|
335 | |
---|
336 | def main(special_main_subroutine, |
---|
337 | command_line_parameters) : |
---|
338 | #X- command_line_parameters = [("-h", "host",)] |
---|
339 | ## Process plugin-specific command line parameters. |
---|
340 | command_line_parameters_getopt_string = command_line_parameters_usage_string = "\\ \n" |
---|
341 | for (parameter, description) in command_line_parameters : |
---|
342 | command_line_parameters_usage_string = command_line_parameters_usage_string + parameter + " " + description + "\\ \n" |
---|
343 | command_line_parameters_getopt_string = command_line_parameters_getopt_string + parameter.lstrip("-") + ":" |
---|
344 | |
---|
345 | try : |
---|
346 | opts = filter(None, getopt.getopt(sys.argv[1:], command_line_parameters_getopt_string)) |
---|
347 | if len(opts) > 0 : opts = opts[0] |
---|
348 | except getopt.GetoptError, err : |
---|
349 | usage(command_line_parameters_usage_string) |
---|
350 | else : |
---|
351 | if len(command_line_parameters) == len(opts) : |
---|
352 | ## main_subroutine_argument_values is based on the argument order of special_main_subroutine(). They are not mapped to the argument names! |
---|
353 | ## Therefore, command_line_parameters must be in the same order as special_main_subroutine()'s arguments. |
---|
354 | main_subroutine_argument_values = [parameter_value for parameter_name, parameter_value in opts] |
---|
355 | # pdb.set_trace() |
---|
356 | special_main_subroutine(*main_subroutine_argument_values) |
---|
357 | else : |
---|
358 | usage(command_line_parameters_usage_string) |
---|
359 | |
---|
360 | |
---|
361 | if __name__ == "__main__" : |
---|
362 | main() |
---|