def quit_script(message): """"Called when a fatal error is encountered in the script""" try: emailaction.send_email(config.configs['smtp.address.from'], config.configs['smtp.address.to'], config.configs['smtp.subject'], message) except Exception as e: log.logger.error(u'Unknown error-sending exception alert email: {}'.format(e.message)) sys.exit(1)
def quit_script(message): """"Called when a fatal error is encountered in the script""" try: email_instance = emailaction.Email(config.configs['smtp.address.from'], config.configs['smtp.address.to'], config.configs['smtp.subject'], message) emailaction.send_email(email_instance) except Exception as e: log.logger.error(u'Unknown error-sending exception alert email: {}'.format(e.message)) sys.exit(1)
def main(): try: # parse command-line arguments parser = argparse.ArgumentParser(description='Execute the VizAlerts process.') parser.add_argument('-c', '--configpath', help='Path to .yml configuration file') args = parser.parse_args() # validate and load configs from yaml file configfile = u'.\\config\\vizalerts.yaml' if args.configpath is not None: configfile = args.configpath config.validate_conf(configfile) # initialize logging log.logger = logging.getLogger() if not len(log.logger.handlers): log.logger = log.LoggerQuickSetup(config.configs['log.dir'] + 'vizalerts.log', log_level=config.configs['log.level']) except Exception as e: print(u'Could not initialize configuration file due to an unknown error: {}'.format(e.message)) # we have our logger, so start writing log.logger.info(u'VizAlerts v{} is starting'.format(__version__)) # cleanup old temp files try: cleanup_dir(config.configs['temp.dir'], config.configs['temp.dir.file_retention_seconds']) except OSError as e: # Send mail to the admin informing them of the problem, but don't quit errormessage = u'OSError: Unable to cleanup temp directory {}, error: {}'.format(config.configs['temp.dir'], e) log.logger.error(errormessage) email_instance = emailaction.Email(config.configs['smtp.address.from'], config.configs['smtp.address.to'], config.configs['smtp.subject'], errormessage) emailaction.send_email(email_instance) except Exception as e: errormessage = u'Unable to cleanup temp directory {}, error: {}'.format(config.configs['temp.dir'], e) log.logger.error(errormessage) email_instance = emailaction.Email(config.configs['smtp.address.from'], config.configs['smtp.address.to'], config.configs['smtp.subject'], errormessage) emailaction.send_email(email_instance) # cleanup old log files try: cleanup_dir(config.configs['log.dir'], config.configs['log.dir.file_retention_seconds']) except OSError as e: # Send mail to the admin informing them of the problem, but don't quit errormessage = u'OSError: Unable to cleanup log directory {}, error: {}'.format(config.configs['log.dir'], e) log.logger.error(errormessage) emailaction.send_email(config.configs['smtp.address.from'], config.configs['smtp.address.to'], config.configs['smtp.subject'], errormessage) except Exception as e: errormessage = u'Unable to cleanup log directory {}, error: {}'.format(config.configs['log.dir'], e) log.logger.error(errormessage) emailaction.send_email(config.configs['smtp.address.from'], config.configs['smtp.address.to'], config.configs['smtp.subject'], errormessage) # test ability to connect to Tableau Server and obtain a trusted ticket trusted_ticket_test() # if SMS Actions are enabled, attempt to obtain an sms client if config.configs['smsaction.enable']: try: smsaction.smsclient = smsaction.get_sms_client() log.logger.info(u'SMS Actions are enabled') except Exception as e: errormessage = u'Unable to get SMS client, error: {}'.format(e.message) log.logger.error(errormessage) quit_script(errormessage) # get the alerts to process try: alerts = get_alerts() log.logger.info(u'Processing a total of {} alerts'.format(len(alerts))) except Exception as e: errormessage = u'Unable to get alerts to process, error: {}'.format(e.message) log.logger.error(errormessage) quit_script(errormessage) if alerts: """Iterate through the list of applicable alerts, and process each""" alert_queue = Queue() for alert in sorted(alerts, key=attrgetter('priority')): log.logger.debug('Queueing subscription id {} for processing'.format(alert.subscription_id)) alert_queue.put(alert) # create all worker threads for index in range(config.configs['threads']): threadname = index + 1 # start thread names at 1 worker = VizAlertWorker(threadname, alert_queue) log.logger.debug('Starting thread with name: {}'.format(threadname)) worker.start() # loop until work is done while 1 == 1: if threading.active_count() == 1: log.logger.info('Worker threads have completed. Exiting') return time.sleep(10) log.logger.info('Waiting on {} worker threads. Currently active threads:: {}'.format( threading.active_count() - 1, threading.enumerate()))
def main(configfile=u'.\\config\\vizalerts.yaml', logfile=u'.\\logs\\vizalerts.log'): # initialize logging log.logger = logging.getLogger() if not len(log.logger.handlers): log.logger = log.LoggerQuickSetup(logfile, log_level=logging.DEBUG) log.logger.info(u'VizAlerts v{} is starting'.format(__version__)) # validate and load configs from yaml file config.validate_conf(configfile) # set the log level based on the config file log.logger.setLevel(config.configs['log.level']) # cleanup old temp files try: cleanup_dir(config.configs['temp.dir'], config.configs['temp.dir.file_retention_seconds']) except OSError as e: # Send mail to the admin informing them of the problem, but don't quit errormessage = u'OSError: Unable to cleanup temp directory {}, error: {}'.format(config.configs['temp.dir'], e) log.logger.error(errormessage) emailaction.send_email(config.configs['smtp.address.from'], config.configs['smtp.address.to'], config.configs['smtp.subject'], errormessage) except Exception as e: errormessage = u'Unable to cleanup temp directory {}, error: {}'.format(config.configs['temp.dir'], e) log.logger.error(errormessage) emailaction.send_email(config.configs['smtp.address.from'], config.configs['smtp.address.to'], config.configs['smtp.subject'], errormessage) # cleanup old log files try: cleanup_dir(config.configs['log.dir'], config.configs['log.dir.file_retention_seconds']) except OSError as e: # Send mail to the admin informing them of the problem, but don't quit errormessage = u'OSError: Unable to cleanup log directory {}, error: {}'.format(config.configs['log.dir'], e) log.logger.error(errormessage) emailaction.send_email(config.configs['smtp.address.from'], config.configs['smtp.address.to'], config.configs['smtp.subject'], errormessage) except Exception as e: errormessage = u'Unable to cleanup log directory {}, error: {}'.format(config.configs['log.dir'], e) log.logger.error(errormessage) emailaction.send_email(config.configs['smtp.address.from'], config.configs['smtp.address.to'], config.configs['smtp.subject'], errormessage) # test ability to connect to Tableau Server and obtain a trusted ticket trusted_ticket_test() # if SMS Actions are enabled, attempt to obtain an sms client if config.configs['smsaction.enable']: try: smsaction.smsclient = smsaction.get_sms_client() log.logger.info(u'SMS Actions are enabled') except Exception as e: errormessage = u'Unable to get SMS client, error: {}'.format(e.message) log.logger.error(errormessage) quit_script(errormessage) # get the alerts to process try: alerts = get_alerts() log.logger.info(u'Processing a total of {} alerts'.format(len(alerts))) except Exception as e: errormessage = u'Unable to get alerts to process, error: {}'.format(e.message) log.logger.error(errormessage) quit_script(errormessage) if alerts: """Iterate through the list of applicable alerts, and process each""" alert_queue = Queue() for alert in sorted(alerts, key=attrgetter('priority')): log.logger.debug('Queueing subscription id {} for processing'.format(alert.subscription_id)) alert_queue.put(alert) # create all worker threads for index in range(config.configs['threads']): threadname = index + 1 # start thread names at 1 worker = VizAlertWorker(threadname, alert_queue) log.logger.debug('Starting thread with name: {}'.format(threadname)) worker.start() # loop until work is done while 1 == 1: if threading.active_count() == 1: log.logger.info('Worker threads have completed. Exiting') return time.sleep(10) log.logger.info('Waiting on {} worker threads. Currently active threads:: {}'.format(threading.active_count() - 1,threading.enumerate()))