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

Last change on this file since 3560 was 3560, checked in by sanmai, 11 years ago
  • Fix privilege code.
  • Property svn:executable set to *
File size: 13.4 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    current_process_ID = os.fork()
197
198    if current_process_ID == 0 :
199        ## Child process code
200       
201        try :
202            os.setuid(OpenDJ_uid)
203        except OSError, e :
204            raise e
205
206        process = subprocess.Popen(command, 
207                                   stdout = subprocess.PIPE, 
208                                   stderr = subprocess.PIPE)
209
210        stdout, stderr = process.communicate()
211
212        if process.returncode == 0 : 
213            result = { "code"      : "OK", 
214                       "message"   : 'Host %s is up and responds as expected to a query "%s" with base DN "%s".' % (host, query, base_DN)
215                     }
216        else :
217            result = { "code"      : "CRITICAL", 
218                       "message"   : 'Host %s is not up or does not respond as expected to a query "%s" with base DN "%s".' % (host, query, base_DN)
219                     }
220
221        os._exit(0)
222     
223    ## Parent process code. Wait for ldapsearch child process.
224    os.waitpid(current_process_ID, 0)
225   
226    try :
227        os.setuid(Nagios_uid)
228    except OSError, e :
229        raise e
230
231    #print stdout
232    #print stderr
233    #pdb.set_trace()
234
235    return result
236
237def check_condition(host, 
238                    http_path,
239                    HTTP_method,
240                    port_number, 
241                    authorize,
242                    validator,
243                    **validator_arguments) :
244
245    #pdb.set_trace()
246    # X- remove implicit argument values
247    #if port_number is None : port_number = 80
248    #if authorize is None : authorize = True
249
250    def handle_connection_failure(problem_description) :
251        err_log_file_path = os.path.normpath("/tmp/" + special_plugin_file_name + "_err_connection_failure__" + http_path.replace('/' , '%2F') + "__" + timestamp + ".log")
252
253        with open(name = err_log_file_path, 
254                  mode = "wt") as debugging_output_file :
255           debugging_output_file.write(problem_description)
256
257    special_plugin_file_name = validator_arguments['special_plugin_file_name'] # X-
258   
259    #http_path  = str(urllib.quote_plus(http_path))   
260   
261    timestamp = datetime.datetime.today().isoformat()
262   
263    try :
264        conn = httplib.HTTPConnection(host = host, port = port_number) # X- accomodate https?
265
266        request = conn.request(HTTP_method, http_path)
267    except :
268        traceback_string = traceback.format_exc()
269
270        handle_connection_failure(traceback_string + "\nThis problem originates from location 1 in '" + special_plugin_file_name + "'.\n")
271
272        return {
273                "code"      : "CRITICAL", 
274                "message"   : 'HTTP connection to host %s failed.' % (host)
275               }
276    else :
277        try :
278            response = conn.getresponse()
279
280            data = response.read()
281           
282            conn.close()
283        except :
284            traceback_string = traceback.format_exc()
285
286            handle_connection_failure(traceback_string + "\nThis problem originates from location 2 in '" + special_plugin_file_name + "'.\n")
287        else :
288
289            redirecting_responses = frozenset([
290                                            httplib.MOVED_PERMANENTLY,
291                                            httplib.FOUND,
292                                            httplib.SEE_OTHER,
293                                            httplib.TEMPORARY_REDIRECT,
294                                          ]) 
295
296            if response.status == httplib.OK :
297            # HTTP status codes 200 and 302
298            # X- Resolve redirect in case of HTTP status == 302
299                well_formed = validator(data                    = data, 
300                                        descriptive_string     = http_path, 
301                                        **validator_arguments) # ['validator_arguments'])
302
303                if well_formed :
304                    return {
305                            "code"      : "OK", 
306                            "message"   : 'Host %s is up and returns well-formed data at "%s".' % (host, http_path)
307                           }
308                else :
309                    return {
310                            "code"      : "CRITICAL", 
311                            "message"   : 'Host %s is up but returns non-well-formed data at "%s".' % (host, http_path)
312                            }
313
314            elif response.status in redirecting_responses :
315                new_location_URL = dict(response.getheaders())['location']
316
317                parsed_new_location_URL = urlparse.urlparse(new_location_URL)
318
319               
320
321                return check_condition(host        = parsed_new_location_URL.netloc, 
322                                       http_path   = parsed_new_location_URL.path, 
323                                       HTTP_method = HTTP_method, 
324                                       port_number = port_number, 
325                                       authorize   = authorize,
326                                       validator   = validator, 
327                                       **validator_arguments) # special_plugin_file_name = validator_arguments['special_plugin_file_name']
328
329            elif response.status == httplib.UNAUTHORIZED and authorize == False :
330                return {
331                        "code"      : "OK", 
332                        "message"   : 'Host %s is up and requests authorization at "%s".' % (host, http_path)
333                       }
334
335            else :
336                #pdb.set_trace()
337                handle_connection_failure("Unreachable URL! HTTP response code: " + str(response.status) + 
338                                            "\nThis problem originates from location 3 in '" + special_plugin_file_name + "'.\n")
339
340                return {
341                        "code"      : "CRITICAL", 
342                        "message"   : 'Host %s has a problem with the URL path component "%s".' % (host, http_path)
343                       }
344
345def main(special_main_subroutine, 
346         command_line_parameters) :
347    #X- command_line_parameters = [("-h", "host",)]
348    ##  Process plugin-specific command line parameters.
349    command_line_parameters_getopt_string = command_line_parameters_usage_string = "\\ \n"
350    for (parameter, description) in command_line_parameters :
351       command_line_parameters_usage_string = command_line_parameters_usage_string + parameter + "  " + description + "\\ \n"
352       command_line_parameters_getopt_string = command_line_parameters_getopt_string + parameter.lstrip("-")  + ":"
353
354    try :
355        opts = filter(None, getopt.getopt(sys.argv[1:], command_line_parameters_getopt_string))
356        if len(opts) > 0 : opts = opts[0]
357    except getopt.GetoptError, err :
358        usage(command_line_parameters_usage_string)
359    else : 
360        if len(command_line_parameters) == len(opts) :
361            ## main_subroutine_argument_values is based on the argument order of special_main_subroutine(). They are not mapped to the argument names!
362            ## Therefore, command_line_parameters must be in the same order as special_main_subroutine()'s arguments.
363            main_subroutine_argument_values = [parameter_value for parameter_name, parameter_value in opts]
364            # pdb.set_trace()
365            special_main_subroutine(*main_subroutine_argument_values)       
366        else :
367            usage(command_line_parameters_usage_string)
368   
369
370if __name__ == "__main__" :
371    main()
Note: See TracBrowser for help on using the repository browser.