source: monitoring/plugins/mpi/generic_tla_monitoring.py @ 4049

Last change on this file since 4049 was 4049, checked in by sanmai, 11 years ago
  • Fix infinite loop in case of HTTP redirect to itself.
  • Property svn:executable set to *
File size: 14.5 KB
Line 
1#!/usr/bin/python2
2
3import sys, getopt, httplib, xml.etree.ElementTree, subprocess, re, pdb, traceback, datetime, urlparse, urllib
4import simplejson as json
5import pwd, os
6
7nagios_codes    = {
8                    'OK'        : 0,
9                    'WARNING'   : 1,
10                    'CRITICAL'  : 2,
11                    'UNKNOWN'   : 3,
12                    'DEPENDENT' : 4
13                    }
14
15def 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
24def 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
38def 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
46def 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
74def 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
103def 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
127def 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
152def 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
171def 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 = [OpenDJ_directory_path + "/bin/ldapsearch", 
180              '--port', '10389',
181              '--baseDN', base_DN,
182              '--useStartTLS',
183              '--trustAll',
184              '--hostname', host,
185              '--bindDN', bind_DN,
186              "--bindPasswordFile", '/root/LDAP_passwdfile',
187              query,
188              'isMemberOf']
189
190    # Run OpenDJ's "ldapsearch" command line utility
191    #pdb.set_trace()
192
193    OpenDJ_uid = pwd.getpwnam('opendj')[2]
194    Nagios_uid = pwd.getpwnam('nagios')[2]
195
196    #import multiprocessing
197
198    def LDAP_query(OpenDJ_uid, command) :
199
200        #pdb.set_trace()
201
202        try :
203            os.setuid(0) # OpenDJ_uid
204        except OSError, e :
205            raise e
206        else :
207            process = subprocess.Popen(command, 
208                                       stdout = subprocess.PIPE, 
209                                       stderr = subprocess.PIPE)
210
211            #print " ".join(command)
212
213            stdout, stderr = process.communicate()
214           
215            if process.returncode == 0 :
216                return True
217                #LDAP_result_queue.put(True)
218            else :
219                return False
220                #LDAP_result_queue.put(False)
221
222
223    # LDAP_result_queue = multiprocessing.Queue()
224    # LDAP_query_process = multiprocessing.Process(target = LDAP_query, args = (OpenDJ_uid, command, LDAP_result_queue))
225    # LDAP_query_process.start()
226    # result = LDAP_result_queue.get()
227    # LDAP_query_process.join()
228
229    result = LDAP_query(OpenDJ_uid, command)
230
231    if result == True:
232        return { "code"      : "OK", 
233                 "message"   : 'Host %s is up and responds as expected to a query "%s" with base DN "%s".' % (host, query, base_DN)
234               }
235    else :
236        return { "code"      : "CRITICAL", 
237                 "message"   : 'Host %s is not up or does not respond as expected to a query "%s" with base DN "%s".' % (host, query, base_DN)
238               }
239
240    # current_process_ID = os.fork()
241
242    # if current_process_ID == 0 :
243    #     ## Child process code
244       
245     
246
247    #     os._exit(0)
248     
249    # ## Parent process code. Wait for ldapsearch child process.
250    # os.waitpid(current_process_ID, 0)
251   
252    # try :
253    #     os.setuid(Nagios_uid)
254    # except OSError, e :
255    #     raise e
256
257    #print stdout
258    #print stderr
259    #pdb.set_trace()
260
261    return result
262
263def check_condition(host, 
264                    http_path,
265                    HTTP_method,
266                    port_number, 
267                    authorize,
268                    validator,
269                    **validator_arguments) :
270
271    #pdb.set_trace()
272    # X- remove implicit argument values
273    #if port_number is None : port_number = 80
274    #if authorize is None : authorize = True
275
276    def handle_connection_failure(problem_description) :
277        err_log_file_path = os.path.normpath("/tmp/" + special_plugin_file_name + "_err_connection_failure__" + http_path.replace('/' , '%2F') + "__" + timestamp + ".log")
278
279        with open(name = err_log_file_path, 
280                  mode = "wt") as debugging_output_file :
281           debugging_output_file.write(problem_description)
282
283    special_plugin_file_name = validator_arguments['special_plugin_file_name'] # X-
284   
285    #http_path  = str(urllib.quote_plus(http_path))   
286   
287    timestamp = datetime.datetime.today().isoformat()
288   
289    try :
290        conn = httplib.HTTPConnection(host = host, 
291                                      port = port_number
292                                      strict = True) # X- accomodate https?
293
294        request = conn.request(HTTP_method, 
295                               http_path)
296    except :
297        traceback_string = traceback.format_exc()
298
299        handle_connection_failure(traceback_string + "\nThis problem originates from location 1 in '" + special_plugin_file_name + "'.\n")
300
301        return {
302                "code"      : "CRITICAL", 
303                "message"   : 'HTTP connection to host %s failed.' % (host)
304               }
305    else :
306        try :
307            response = conn.getresponse()
308
309            data = response.read()
310           
311            conn.close()
312        except :
313            traceback_string = traceback.format_exc()
314
315            handle_connection_failure(traceback_string + "\nThis problem originates from location 2 in '" + special_plugin_file_name + "'.\n")
316        else :
317
318            redirecting_responses = frozenset([
319                                            httplib.MOVED_PERMANENTLY,
320                                            httplib.FOUND,
321                                            httplib.SEE_OTHER,
322                                            httplib.TEMPORARY_REDIRECT,
323                                          ]) 
324
325            if response.status == httplib.OK :
326            # HTTP status codes 200 and 302
327            # X- Resolve redirect in case of HTTP status == 302
328                well_formed = validator(data                   = data, 
329                                        descriptive_string     = http_path, 
330                                        **validator_arguments) # ['validator_arguments'])
331
332                if well_formed :
333                    return {
334                            "code"      : "OK", 
335                            "message"   : 'Host %s is up and returns well-formed data at "%s".' % (host, http_path)
336                           }
337                else :
338                    return {
339                            "code"      : "CRITICAL", 
340                            "message"   : 'Host %s is up but returns non-well-formed data at "%s".' % (host, http_path)
341                            }
342
343            elif response.status in redirecting_responses :
344                new_location_URL = dict(response.getheaders())['location']
345
346                parsed_new_location_URL = urlparse.urlparse(new_location_URL)
347
348               
349                if host == parsed_new_location_URL and http_path == parsed_new_location_URL :
350                    return {
351                              "code"      : "CRITICAL", 
352                              "message"   : 'Host %s is up but the response to GET "%s" implies an infinite redirection (to "%s").' % (host, http_path)
353                            }
354
355                else : 
356
357                    return check_condition(host        = parsed_new_location_URL.netloc, 
358                                           http_path   = parsed_new_location_URL.path, 
359                                           HTTP_method = HTTP_method, 
360                                           port_number = port_number, 
361                                           authorize   = authorize,
362                                           validator   = validator, 
363                                           **validator_arguments) # special_plugin_file_name = validator_arguments['special_plugin_file_name']
364
365            elif response.status == httplib.UNAUTHORIZED and authorize == False :
366                return {
367                        "code"      : "OK", 
368                        "message"   : 'Host %s is up and requests authorization at "%s".' % (host, http_path)
369                       }
370
371            else :
372                #pdb.set_trace()
373                handle_connection_failure("Unreachable URL! HTTP response code: " + str(response.status) + 
374                                            "\nThis problem originates from location 3 in '" + special_plugin_file_name + "'.\n")
375
376                return {
377                        "code"      : "CRITICAL", 
378                        "message"   : 'Host %s has a problem with the URL path component "%s".' % (host, http_path)
379                       }
380
381def main(special_main_subroutine, 
382         command_line_parameters) :
383   
384    ## Process plugin-specific command line parameters.
385    command_line_parameters_getopt_string = command_line_parameters_usage_string = "\\ \n"
386    for (parameter, description) in command_line_parameters :
387       command_line_parameters_usage_string = command_line_parameters_usage_string + parameter + "  " + description + "\\ \n"
388       command_line_parameters_getopt_string = command_line_parameters_getopt_string + parameter.lstrip("-")  + ":"
389
390    try :
391        opts = filter(None, getopt.getopt(sys.argv[1:], command_line_parameters_getopt_string))
392        if len(opts) > 0 : opts = opts[0]
393    except getopt.GetoptError, err :
394        usage(command_line_parameters_usage_string)
395    else : 
396        if len(command_line_parameters) == len(opts) :
397            ## main_subroutine_argument_values is based on the argument order of special_main_subroutine(). They are not mapped to the argument names!
398            ## Therefore, command_line_parameters must be in the same order as special_main_subroutine()'s arguments.
399            main_subroutine_argument_values = [parameter_value for parameter_name, parameter_value in opts]
400            # pdb.set_trace()
401            special_main_subroutine(*main_subroutine_argument_values)       
402        else :
403            usage(command_line_parameters_usage_string)
404   
405
406if __name__ == "__main__" :
407    main()
Note: See TracBrowser for help on using the repository browser.