def send_failure_email(self, suppress_email=False): """Send an email about the failed check *check* unless the email should be suppressed, in which case simply log that it was suppressed.""" if suppress_email: LOGGER.info('Suppressing email for failed check [{}]'.format( check['title'])) send_alert_email( EMAIL_CONTENT.format( process=self.name, ), EMAIL_SUBJECT.format(self.name), )
def passes_checks(self): """Return True if the process exists.""" process = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE) output, error = process.communicate() if error: return False for line in output.splitlines(): if self.pattern.upper() in line.upper(): LOGGER.info('[{}] passed check.'.format(self.name, self.description)) return True LOGGER.error('[{}] is NOT currently running.'.format(self.name, self.description)) return False
def web_applications_from_config(config_file): """Return a list of web applications configured from *config_file*.""" applications = [] with open(config_file, 'r') as input_file: parsed_file = json.load(input_file) for app in parsed_file['applications']: applications.append(MonitoredWebApplication( app['name'], app['checks'], )) LOGGER.info( 'Added application [{}] for monitoring'.format(app['name'])) return applications
def _passes_check(self, check, suppress_email): """Return True if successfully able to ping the application.""" if check['method'] == 'GET': try: response = requests.get(check['url']) except requests.exceptions.ConnectionError: LOGGER.exception('Exception raised.') self.send_failure_email( check, 'Exception raised when connecting to URL', suppress_email) elif check['method'] == 'POST': try: response = requests.post(check['url'], data=check['message']) except requests.exceptions.ConnectionError: LOGGER.exception('Exception raised.') self.send_failure_email( check, 'Exception raised when connecting to URL', suppress_email) else: LOGGER.fatal('Unsupported HTTP method [{}]'.format( check['method'])) sys.exit(1) passed = response.status_code == check['expected_status'] if passed: LOGGER.info('[{}] passed check [{}]'.format( self.name, check['title'])) else: self.send_failure_email(check, response.status_code, suppress_email) return passed
def web_applications_from_config(config_file): """Return a list of web applications configured from *config_file*.""" applications = [] with open(config_file, 'r') as input_file: parsed_file = json.load(input_file) for app in parsed_file['applications']: applications.append( MonitoredWebApplication( app['name'], app['checks'], )) LOGGER.info('Added application [{}] for monitoring'.format( app['name'])) return applications
def processes_from_config(config_file): """Return a list of processes configured from *config_file*.""" processes = [] with open(config_file, 'r') as input_file: parsed_file = json.load(input_file) for process in parsed_file['processes']: processes.append(MonitoredProcess( process['name'], process['description'], process['pattern'], )) LOGGER.info( 'Added process [{}] for monitoring'.format(process['name'])) return processes
def processes_from_config(config_file): """Return a list of processes configured from *config_file*.""" processes = [] with open(config_file, 'r') as input_file: parsed_file = json.load(input_file) for process in parsed_file['processes']: processes.append( MonitoredProcess( process['name'], process['description'], process['pattern'], )) LOGGER.info('Added process [{}] for monitoring'.format( process['name'])) return processes
def send_failure_email(self, check, reason, suppress_email=False): """Send an email about the failed check *check* unless the email should be suppressed, in which case simply log that it was suppressed.""" if suppress_email: LOGGER.info('Suppressing email for failed check [{}]'.format( check['title'])) send_alert_email( EMAIL_CONTENT.format( app_name=self.name, check=check['title'], address=check['url'], method=check['method'], expected=check['expected'], actual=reason, ), EMAIL_SUBJECT.format(self.name), )
def check_web_applications(web_applications): """Loop forever, checking the status of *web_applications* every 5 seconds.""" LOGGER.info('Beginning web application checks') while True: for application in web_applications: LOGGER.info('Starting check for process [{}]'.format( application.name)) if not application.passes_checks(): LOGGER.error('Process [{}] failed status check'.format( application.name)) else: LOGGER.info('Process [{}] passed status check'.format( application.name)) time.sleep(5)
def check_web_applications(web_applications): """Loop forever, checking the status of *web_applications* every 5 seconds.""" LOGGER.info('Beginning web application checks') while True: for application in web_applications: LOGGER.info( 'Starting check for process [{}]'.format(application.name)) if not application.passes_checks(): LOGGER.error( 'Process [{}] failed status check'.format( application.name)) else: LOGGER.info( 'Process [{}] passed status check'.format( application.name)) time.sleep(5)
def check_processes(processes): """Loop forever, checking the status of *processes* every 5 seconds.""" LOGGER.info('Beginning process checks') failed_processes = set() suppress_email = False while True: for process in processes: if process in failed_processes: suppress_email = True LOGGER.info('Starting check for process [{}]'.format(process.name)) if not process.passes_checks(suppress_email): LOGGER.error('Process [{}] failed status check'.format( process.name)) failed_processes.add(process) else: LOGGER.info('Process [{}] passed status check'.format( process.name)) if process in failed_processes: del failed_processes[process] time.sleep(5)
def _passes_check(self, check, suppress_email): """Return True if successfully able to ping the application.""" if check['method'] == 'GET': try: response = requests.get(check['url']) except requests.exceptions.ConnectionError: LOGGER.exception('Exception raised.') self.send_failure_email( check, 'Exception raised when connecting to URL', suppress_email) elif check['method'] == 'POST': try: response = requests.post( check['url'], data=check['message']) except requests.exceptions.ConnectionError: LOGGER.exception('Exception raised.') self.send_failure_email( check, 'Exception raised when connecting to URL', suppress_email) else: LOGGER.fatal('Unsupported HTTP method [{}]'.format( check['method'])) sys.exit(1) passed = response.status_code == check['expected_status'] if passed: LOGGER.info('[{}] passed check [{}]'.format( self.name, check['title'])) else: self.send_failure_email( check, response.status_code, suppress_email) return passed