source: monitoring/plugins/mpi/check_clarin_saml.py @ 3305

Last change on this file since 3305 was 3305, checked in by sanmai, 11 years ago
  • Add -d command line parameter for check_clarin_saml.py .
  • Property svn:executable set to *
File size: 6.1 KB
Line 
1#!/usr/bin/python2
2
3# This Nagios monitoring plugin is not as lean and clean as the other ones. Do not use this
4# as an example.
5
6import sys, getopt, httplib, subprocess, tempfile, os, re, pdb
7import os.path
8#import xml.etree.ElementTree
9
10
11DESCRIPTION = "SAML"
12command_line_parameters = [("-h", "host",),
13                           ("-u", "URL"),
14                           ("-d", "infra.clarin.eu root directory path",)]
15
16nagios_codes = {
17                'OK'        : 0,
18                'WARNING'   : 1,
19                'CRITICAL'  : 2,
20                'UNKNOWN'   : 3,
21                'DEPENDENT' : 4
22                }
23
24def nagios_return(code, response) :
25    """ prints the response message
26        and exits the script with one
27        of the defined exit codes
28        DOES NOT RETURN
29    """
30    print code + ": " + response
31    sys.exit(nagios_codes[code])
32
33def check_response_data_validity(data) :
34
35    def interpret_for_well_formedness(stdout) :
36        pattern                             = '^INFO : XML document parsed and is well-formed.'
37        pattern_regex                       = re.compile(pattern, re.MULTILINE)
38        results                             = pattern_regex.search(stdout)
39   
40        if results is not None :
41            return(True)
42
43        return(False)
44
45    def interpret_for_validity(stdout) :
46        pattern                             = '^INFO : XML document is schema valid'
47        pattern_regex                       = re.compile(pattern, re.MULTILINE)
48        results                             = pattern_regex.search(stdout)
49   
50        if results is not None :
51            return(True)
52
53        return(False)
54
55    #pdb.set_trace()
56
57    check_saml_metadata_file_path       = os.path.join(CLARIN_server_root_directory_path, "aai/check-saml-metadata/check_saml_metadata.sh")
58
59    temporary_file                      = tempfile.NamedTemporaryFile(mode = 'wb')                                   
60    temporary_file.write(data)
61    temporary_file.flush()
62
63    XML_response_metadata_file_path     = temporary_file.name
64
65    command                             = [check_saml_metadata_file_path, XML_response_metadata_file_path]
66
67    environment_variables               = { "JAVA_HOME" : '/usr/lib/jvm/java-6-openjdk-amd64/jre' } # X- hard coding
68    process                             = subprocess.Popen(command, 
69                                                           stdout = subprocess.PIPE, 
70                                                           stderr = subprocess.PIPE,
71                                                                                       env    = environment_variables)
72
73    stdout, stderr                      = process.communicate()
74
75    #pdb.set_trace()
76   
77    temporary_file.close()
78
79    interpretation_tuple                = (interpret_for_well_formedness(stdout), interpret_for_validity(stdout),)
80
81    #os.unlink(XML_response_metadata_file_path)
82
83    return(interpretation_tuple)
84
85
86def check_condition(host, URL, CLARIN_server_root_directory_path) :
87
88    conn    = httplib.HTTPSConnection(host)
89   
90    # Use exception handling.
91    conn.request("GET", URL)
92   
93    r1      = conn.getresponse()
94
95    data    = r1.read()
96   
97    conn.close()
98
99    #pdb.set_trace()
100   
101    if r1.status == 200 :
102        interpretation_tuple = check_response_data_validity(data)
103
104        well_formed = interpretation_tuple[0]
105        valid       = interpretation_tuple[1]
106
107        if well_formed :
108            if valid :
109                return {
110                        "code"      : "OK", 
111                        "message"   : 'Host %s, service %s is up and returns well-formed and valid XML data at %s.' % (host, DESCRIPTION, URL)
112                       }
113            else :
114                return {
115                        "code"      : "CRITICAL", 
116                        "message"   : 'Host %s, service %s is up and returns well-formed, but invalid XML data at %s.' % (host, DESCRIPTION, URL)
117                       }
118        else :
119            return {
120                        "code"      : "CRITICAL", 
121                        "message"   : 'Host %s, service %s is up but returns non-well-formed XML data %s.' % (host, DESCRIPTION, URL)
122                    }
123    else :
124        return {
125                "code"      : "CRITICAL", 
126                "message"   : 'Host %s, service %s, location %s has a problem.' % (host, DESCRIPTION, URL)
127               }
128
129def test_case(host, URL, CLARIN_server_root_directory_path) :
130    metadata_file       = open(os.path.join(CLARIN_server_root_directory_path, "aai/clarin-sp-metadata.xml"),
131              mode      = "r+", 
132              buffering = 0)
133       
134    data = metadata_file.read()
135
136    metadata_file.close()       
137
138    interpretation_tuple = check_response_data_validity(data)
139
140    well_formed = interpretation_tuple[0]
141    valid       = interpretation_tuple[1]
142
143    if well_formed :
144        if valid :
145            return {
146                    "code"      : "OK", 
147                    "message"   : 'Host %s, service %s is up and returns well-formed and valid XML data at %s.' % (host, DESCRIPTION, URL)
148                   }
149        else :
150            return {
151                    "code"      : "CRITICAL", 
152                    "message"   : 'Host %s, service %s is up and returns well-formed, but invalid XML data at %s.' % (host, DESCRIPTION, URL)
153                   }
154    else :
155        return {
156                "code"      : "CRITICAL", 
157                "message"   : 'Host %s, service %s is up, but returns non-well-formed XML data at %s.' % (host, DESCRIPTION, URL)
158               }
159
160    Sys.exit(0)
161
162
163def special_main_subroutine(host, 
164                            URL, 
165                            CLARIN_server_root_directory_path) :
166
167    result = check_condition(host = host, 
168                             URL = URL, 
169                             CLARIN_server_root_directory_path = CLARIN_server_root_directory_path)
170   
171    nagios_return(result['code'], result['message'])
172
173if __name__ == "__main__" :
174    generic_tla_monitoring.main(special_main_subroutine, 
175                                command_line_parameters)
Note: See TracBrowser for help on using the repository browser.