def generate_report(): """ Created report of all fails since last email was sent """ # Start message creation here fail_list = db_helpers.server_stats.failures_in_x_minutes_ago( db_helpers.email_log.email_sent_x_minutes_ago()) monitor_list = db_helpers.monitor_list.get_server_list() unique_failed_monitors = list(set([x[2] for x in fail_list])) subj = "Server Status Report - # of failures: " + str(len(unique_failed_monitors)) # noinspection PyListCreation msg = [] # Email Message Body msg.append('Failure report- \n') msg.append('# of failures: ' + str(len(fail_list)) + '\n') msg.append('# of services monitored: ' + str(len(monitor_list)) + '\n') msg.append('# of services failed: ' + str(len(unique_failed_monitors)) + '\n\n') # Generates list of failed services msg.append('-' * 70 + '\n') msg.append("| {0} |".format(str('Failed Servers').ljust(66)) + '\n') msg.append('-' * 70 + '\n') for i in unique_failed_monitors: msg.append("| {0} |".format(str(i).ljust(66))) msg.append('\n') msg.append('-' * 70 + '\n\n') # Generates log of failures msg.append(email_actions.generate_fail_table(fail_list, monitor_list)) # Calls the fail table generator above # Generates table of monitored services msg.append("\nMonitored Servers / Services:" + '\n' + '-' * 98 + '\n' + "| {0} | {1} | {2} | {3} |".format("Hostname".ljust(35), "Port".ljust(10), "Service".ljust(15), "Note".ljust(17 + 8)) + '\n' + '-' * 98 + '\n') for i in monitor_list: msg.append("| {0} | {1} | {2} | {3} |".format(str(i[1]).ljust(35), str(i[2]).ljust(10), str(i[3]).ljust(15), str(i[4]).ljust(17 + 8)), ) msg.append('\n') msg.append('-' * 98) # FIXME Create nice looking table msg.append('\n\nReport Generated @ ' + str(datetime.now())) logging.debug('\n' + ''.join(msg)) logging.debug('BREAKPOINT') email_controller.send_gmail().send(subject=subj, text=''.join(msg))
def multi_server(self): print("Multi Server mode") print("Press Ctrl-C to quit") while True: self.server_list = db_helpers.monitor_list.get_server_list() # Gets server list on each refresh, in-case of updates logging.debug(self.server_list) # Send each row of monitor_list to logic gate for i in self.server_list: server_logger(i, sleep_delay=self.sleep_delay, alert_timeout=self.alert_timeout, host_timeout=self.host_timeout).check_server_status() last_email = db_helpers.email_log.email_sent_x_minutes_ago() last_fail = db_helpers.monitor_list.get_time_from_last_failure() logging.debug( 'Last e-mail sent: ' + str(last_email) + ' Timeout: ' + str(self.alert_timeout) + ' Last Failure: ' + str(last_fail)) if db_helpers.email_log.email_sent_x_minutes_ago() > self.alert_timeout \ > db_helpers.monitor_list.get_time_from_last_failure(): # Are we spamming alerts? # Check if any servers have gone down in the the last X minutes # If any have gone down, send report if email_controller.send_gmail().test_login(): logging.info('SENDING REPORT') email_alerts.email_actions.generate_report() else: logging.info('E-mail timeout still cooling off') self.sleep()
def send_alert(server_info_object): logging.debug(server_info_object) subj = server_info_object.sl_service_type + " @ " + server_info_object.sl_host + ' is DOWN' # noinspection PyListCreation msg = [] # Email Message Body msg.append('Report: ') msg.append('\nHost: ' + server_info_object.sl_host) msg.append('\nPort: ' + str(server_info_object.sl_port)) msg.append('\nService: ' + server_info_object.sl_service_type) msg.append('\nNote: ' + str(server_info_object.sl_note)) msg.append('\n\nReport Generated @ ' + str(datetime.now())) logging.debug('Subject:' + subj) logging.debug(''.join(msg)) email_controller.send_gmail().send(subject=subj, text=''.join(msg)) db_helpers.email_log.log_email_sent(''.join(msg))
def main(): """ Take arguments and direct program """ parser = argparse.ArgumentParser(description="A Network Stats Database Report Generator" " (http://github.com/Jelloeater/NetworkMonitor)", version=__version__, epilog="Please specify action") multi_server_group = parser.add_argument_group('Multi Server Mode') multi_server_group.add_argument("-m", "--monitor", help="Multi server watch mode", action="store_true") report_group = parser.add_argument_group('Actions') report_group.add_argument("-g", "--generate_report_flag", help="Generate Weekly Report", action="store_true") email_group = parser.add_argument_group('E-mail Config') email_group.add_argument("-config_email", help="Configure email alerts", action="store_true") email_group.add_argument("-rm_email_pass_store", help="Removes password stored in system keyring", action="store_true") db_group = parser.add_argument_group('Database Settings') db_group.add_argument("-config_db", help="Configure database settings", action="store_true") db_group.add_argument("-rm_db_pass_store", help="Removes password stored in system keyring", action="store_true") monitor_list_group = parser.add_argument_group('Monitor List') monitor_list_group.add_argument("-config_monitors", help="Configure servers to monitor", action="store_true") monitor_list_group.add_argument("-list", help="List servers to monitor", action="store_true") parser.add_argument("-d", "--delay", action="store", type=int, default=600, help="Wait x second between checks (10 min)") parser.add_argument("-a", "--alert_timeout", action="store", type=int, default=60, help="Wait x minutes between alerts (1 hr)") parser.add_argument("-t", "--host_timeout", action="store", type=int, default=10, help="Wait x seconds for failure (10)") parser.add_argument("--debug", action="store_true", help="Debug Mode Logging") args = parser.parse_args() if args.debug: logging.basicConfig(format="[%(asctime)s] [%(levelname)8s] --- %(message)s (%(filename)s:%(lineno)s)", level=logging.DEBUG) logging.debug(sys.path) logging.debug(args) logging.debug('Debug Mode Enabled') else: logging.basicConfig(filename=LOG_FILENAME, format="[%(asctime)s] [%(levelname)8s] --- %(message)s (%(filename)s:%(lineno)s)", level=logging.WARNING) mode = modes(sleep_delay=args.delay, alert_timeout=args.alert_timeout, host_timeout=args.host_timeout) # Create new mode object for flow, I'll buy that :) if len(sys.argv) == 1: # Displays help and lists servers (to help first time users) parser.print_help() sys.exit(1) # Arg Logic here if args.list: db_monitor_list.get_print_server_list() if args.config_monitors: db_monitor_list.config_monitor_list() if args.config_db: db_controller.db_helper().configure() if args.rm_db_pass_store: db_controller.db_helper().clear_password_store() if args.config_email: email_controller.send_gmail().configure() if args.rm_email_pass_store: email_controller.send_gmail().clear_password_store() # Magic starts here if args.generate_report_flag: db_controller.db_helper().test_db_setup() email_controller.send_gmail().test_login() generate_report() print('*NOTE: If report is empty, that just means nothing has failed since we sent an email, ' 'run -m to "fix" it*') if args.monitor: db_controller.db_helper().test_db_setup() email_controller.send_gmail().test_login() mode.multi_server()