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

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