#!/usr/bin/python2 # This Nagios monitoring plugin is not as lean and clean as the other ones. Do not use this # as an example. import sys, getopt, httplib, subprocess, tempfile, os, re, pdb import os.path import generic_tla_monitoring #import xml.etree.ElementTree DESCRIPTION = "SAML" command_line_parameters = [("-h", "host",), ("-u", "http_path"), ("-d", "infra.clarin.eu root directory path",)] def nagios_return(code, response) : """ prints the response message and exits the script with one of the defined exit codes DOES NOT RETURN """ print code + ": " + response sys.exit(generic_tla_monitoring.nagios_codes[code]) def check_response_data_validity(data, CLARIN_server_root_directory_path) : def interpret_for_well_formedness(stdout) : pattern = '^INFO : XML document parsed and is well-formed.' pattern_regex = re.compile(pattern, re.MULTILINE) results = pattern_regex.search(stdout) if results is not None : return(True) return(False) def interpret_for_validity(stdout) : pattern = '^INFO : XML document is schema valid' pattern_regex = re.compile(pattern, re.MULTILINE) results = pattern_regex.search(stdout) if results is not None : return(True) return(False) #pdb.set_trace() check_saml_metadata_file_path = os.path.join(CLARIN_server_root_directory_path, "aai/check-saml-metadata/check_saml_metadata.sh") temporary_file = tempfile.NamedTemporaryFile(mode = 'wb') temporary_file.write(data) temporary_file.flush() XML_response_metadata_file_path = temporary_file.name command = [check_saml_metadata_file_path, XML_response_metadata_file_path] environment_variables = { "JAVA_HOME" : '/usr/lib/jvm/java-6-openjdk-amd64/jre' } # X- hard coding process = subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.PIPE, env = environment_variables) stdout, stderr = process.communicate() #pdb.set_trace() temporary_file.close() interpretation_tuple = (interpret_for_well_formedness(stdout), interpret_for_validity(stdout),) #os.unlink(XML_response_metadata_file_path) return(interpretation_tuple) def check_condition(host, http_path, CLARIN_server_root_directory_path) : conn = httplib.HTTPSConnection(host) # Use exception handling. conn.request("GET", http_path) r1 = conn.getresponse() data = r1.read() conn.close() #pdb.set_trace() if r1.status == 200 : interpretation_tuple = check_response_data_validity(data = data, CLARIN_server_root_directory_path = CLARIN_server_root_directory_path) well_formed = interpretation_tuple[0] valid = interpretation_tuple[1] if well_formed : if valid : return { "code" : "OK", "message" : 'Host %s, service %s is up and returns well-formed and valid XML data at %s.' % (host, DESCRIPTION, http_path) } else : return { "code" : "CRITICAL", "message" : 'Host %s, service %s is up and returns well-formed, but invalid XML data at %s.' % (host, DESCRIPTION, http_path) } else : return { "code" : "CRITICAL", "message" : 'Host %s, service %s is up but returns non-well-formed XML data %s.' % (host, DESCRIPTION, http_path) } else : return { "code" : "CRITICAL", "message" : 'Host %s, service %s, location %s has a problem.' % (host, DESCRIPTION, http_path) } def test_case(host, http_path, CLARIN_server_root_directory_path) : metadata_file = open(os.path.join(CLARIN_server_root_directory_path, "aai/clarin-sp-metadata.xml"), mode = "r+", buffering = 0) data = metadata_file.read() metadata_file.close() interpretation_tuple = check_response_data_validity(data) well_formed = interpretation_tuple[0] valid = interpretation_tuple[1] if well_formed : if valid : return { "code" : "OK", "message" : 'Host %s, service %s is up and returns well-formed and valid XML data at %s.' % (host, DESCRIPTION, http_path) } else : return { "code" : "CRITICAL", "message" : 'Host %s, service %s is up and returns well-formed, but invalid XML data at %s.' % (host, DESCRIPTION, http_path) } else : return { "code" : "CRITICAL", "message" : 'Host %s, service %s is up, but returns non-well-formed XML data at %s.' % (host, DESCRIPTION, http_path) } Sys.exit(0) def special_main_subroutine(host, http_path, CLARIN_server_root_directory_path) : result = check_condition(host = host, http_path = http_path, CLARIN_server_root_directory_path = CLARIN_server_root_directory_path) nagios_return(result['code'], result['message']) if __name__ == "__main__" : generic_tla_monitoring.main(special_main_subroutine, command_line_parameters)