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

Last change on this file since 4057 was 4057, checked in by sanmai, 11 years ago
  • Make protocol/scheme (http, https) a command-line parameter and an internal

parameter. Fixes redirects from http URLs to https URLs and vice versa.

  • Property svn:executable set to *
File size: 15.2 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
263
264def check_condition(host, 
265                    http_path, 
266                    protocol, 
267                    HTTP_method,
268                    port_number, 
269                    authorize,
270                    validator,
271                    **validator_arguments) :
272
273    #pdb.set_trace()
274    # X- remove implicit argument values
275    #if port_number is None : port_number = 80
276    #if authorize is None : authorize = True
277
278    try:
279        port_number = int(port_number)
280    except ValueError : 
281        return { "code"      : "CRITICAL", 
282         "message"   : 'The port number specified, "{0}", cannot be converted to an integer.'.format(port_number)
283       }
284
285    def handle_connection_failure(problem_description) :
286        err_log_file_path = os.path.normpath("/tmp/" + special_plugin_file_name + "_err_connection_failure__" + http_path.replace('/' , '%2F') + "__" + timestamp + ".log")
287
288        with open(name = err_log_file_path, 
289                  mode = "wt") as debugging_output_file :
290           debugging_output_file.write(problem_description)
291
292    special_plugin_file_name = validator_arguments['special_plugin_file_name'] # X-
293   
294    #http_path  = str(urllib.quote_plus(http_path))   
295   
296    timestamp = datetime.datetime.today().isoformat()
297   
298    try :
299        if protocol = 'http' :
300            conn = httplib.HTTPConnection(host = host, 
301                                          port = port_number, 
302                                          strict = True)
303        elif protocol = 'https' :
304            conn = httplib.HTTPSConnection(host = host, 
305                                           port = port_number, 
306                                           strict = True)
307        request = conn.request(HTTP_method, 
308                               http_path)
309    except :
310        traceback_string = traceback.format_exc()
311
312        handle_connection_failure(traceback_string + "\nThis problem originates from location 1 in '" + special_plugin_file_name + "'.\n")
313
314        return {
315                "code"      : "CRITICAL", 
316                "message"   : 'HTTP connection to host %s failed.' % (host)
317               }
318    else :
319        try :
320            response = conn.getresponse()
321
322            data = response.read()
323           
324            conn.close()
325        except :
326            traceback_string = traceback.format_exc()
327
328            handle_connection_failure(traceback_string + "\nThis problem originates from location 2 in '" + special_plugin_file_name + "'.\n")
329        else :
330
331            redirecting_responses = frozenset([
332                                            httplib.MOVED_PERMANENTLY,
333                                            httplib.FOUND,
334                                            httplib.SEE_OTHER,
335                                            httplib.TEMPORARY_REDIRECT,
336                                          ]) 
337
338            if response.status == httplib.OK :
339            # HTTP status codes 200 and 302
340            # X- Resolve redirect in case of HTTP status == 302
341                well_formed = validator(data                   = data, 
342                                        descriptive_string     = http_path, 
343                                        **validator_arguments) # ['validator_arguments'])
344
345                if well_formed :
346                    return {
347                            "code"      : "OK", 
348                            "message"   : 'Host %s is up and returns well-formed data at "%s".' % (host, http_path)
349                           }
350                else :
351                    return {
352                            "code"      : "CRITICAL", 
353                            "message"   : 'Host %s is up but returns non-well-formed data at "%s".' % (host, http_path)
354                            }
355
356            elif response.status in redirecting_responses :
357                new_location_URL = dict(response.getheaders())['location']
358
359                parsed_new_location_URL = urlparse.urlparse(new_location_URL)
360               
361                if parsed_new_location_URL.scheme == protocol and host == parsed_new_location_URL.netloc and http_path == parsed_new_location_URL.path :
362                    return {
363                              "code"      : "CRITICAL", 
364                              "message"   : 'Host %s is up but the response to GET "%s" implies an infinite redirection (to "%s").' % (host, http_path, http_path)
365                            }
366
367                else : 
368
369                    return check_condition(host        = parsed_new_location_URL.netloc, 
370                                           http_path   = parsed_new_location_URL.path, 
371                                           protocol    = parsed_new_location_URL.scheme, 
372                                           HTTP_method = HTTP_method, 
373                                           port_number = port_number, 
374                                           authorize   = authorize,
375                                           validator   = validator, 
376                                           **validator_arguments) # special_plugin_file_name = validator_arguments['special_plugin_file_name']
377
378            elif response.status == httplib.UNAUTHORIZED and authorize == False :
379                return {
380                        "code"      : "OK", 
381                        "message"   : 'Host %s is up and requests authorization at "%s".' % (host, http_path)
382                       }
383
384            else :
385                #pdb.set_trace()
386                handle_connection_failure("Unreachable URL! HTTP response code: " + str(response.status) + 
387                                            "\nThis problem originates from location 3 in '" + special_plugin_file_name + "'.\n")
388
389                return {
390                        "code"      : "CRITICAL", 
391                        "message"   : 'Host %s has a problem with the URL path component "%s".' % (host, http_path)
392                       }
393
394def main(special_main_subroutine, 
395         command_line_parameters) :
396   
397    ## Process plugin-specific command line parameters.
398    command_line_parameters_getopt_string = command_line_parameters_usage_string = "\\ \n"
399    for (parameter, description) in command_line_parameters :
400       command_line_parameters_usage_string = command_line_parameters_usage_string + parameter + "  " + description + "\\ \n"
401       command_line_parameters_getopt_string = command_line_parameters_getopt_string + parameter.lstrip("-")  + ":"
402
403    try :
404        opts = filter(None, getopt.getopt(sys.argv[1:], command_line_parameters_getopt_string))
405        if len(opts) > 0 : opts = opts[0]
406    except getopt.GetoptError, err :
407        usage(command_line_parameters_usage_string)
408    else : 
409        if len(command_line_parameters) == len(opts) :
410            ## main_subroutine_argument_values is based on the argument order of special_main_subroutine(). They are not mapped to the argument names!
411            ## Therefore, command_line_parameters must be in the same order as special_main_subroutine()'s arguments.
412            main_subroutine_argument_values = [parameter_value for parameter_name, parameter_value in opts]
413            # pdb.set_trace()
414            special_main_subroutine(*main_subroutine_argument_values)       
415        else :
416            usage(command_line_parameters_usage_string)
417   
418
419if __name__ == "__main__" :
420    main()
Note: See TracBrowser for help on using the repository browser.