import yaml import os import json import traceback import sys from library.logger import AlertLogging logger = AlertLogging('aom') logger.start() def render_config(config): """ Reads in the config dict and renders to file. config usually from web interface Args: config: The config to use to generate the yaml file Returns: boolean string of 0 if successful and the yaml as string, or 1 and the error """ try: # GET THE NAME OF THE FILE FROM THE CONFIG file_name = ''.join([config['alert_name'], '.yaml']) logger.debug("Filename: {}".format(file_name)) # THIS SHOULD BE A PARAMETER PASSED IN file_path = os.path.join('alert_configs', file_name) logger.debug("Full path: {}".format(file_path)) # SANITIZE THE CONFIG TO A NEW OBJECT yaml_config = {'alerts': {}, 'id': config['alert_name'], 'interval': 30 if int(config['interval']) < 30 else int(config['interval'])} # SET THE INTERVAL TO lowest value of 30 seconds # SPLIT THE ALERTS INTO A LIST if 'vo' in config: yaml_config['alerts']['vo'] = [x for x in config['vo_list'].split(',') if x] if 'email' in config: yaml_config['alerts']['email'] = [x for x in config['email_list'].split(',') if x] if 'slack' in config: yaml_config['alerts']['slack'] = [x for x in config['slack_list'].split(',') if x] # GET THRESHOLDS AS FLOATS if 'critical_threshold' in config: if config['critical_upper_threshold'] is not "": yaml_config['critical_upper_threshold'] = float(config['critical_threshold']) if 'critical_upper_threshold' in config: if config['critical_upper_threshold'] is not "": yaml_config['critical_upper_threshold'] = float(config['critical_upper_threshold']) if 'warning_threshold' in config: yaml_config['warning_upper_threshold'] = float(config['warning_threshold']) if 'warning_upper_threshold' in config: yaml_config['warning_upper_threshold'] = float(config['warning_upper_threshold']) if 'critical_lower_threshold' in config: if config['critical_lower_threshold'] is not "": yaml_config['critical_lower_threshold'] = float(config['critical_lower_threshold']) if 'warning_lower_threshold' in config: yaml_config['warning_lower_threshold'] = float(config['warning_lower_threshold']) if 'occurrences' in config: yaml_config['occurrences_threshold'] = int(config['occurrences_threshold']) # PARSE THE QUERY OUT INTO A DICT OBJECT if config['prometheus_query']: yaml_config['query_type'] = 'prometheus' yaml_config['prometheus_url'] = config['prometheus_url'] yaml_config['query'] = config['prometheus_query'] yaml_config['start_time'] = config['start_time'] yaml_config['end_time'] = config['end_time'] else: yaml_config['query_type'] = 'kairosdb' yaml_config['query'] = json.loads(config['kairosdb_query']) # GET THE TAGS, COMMA SEPARATED tags = config['tags'].split(',') yaml_config['tags'] = [x for x in tags if x] # GET THE URL yaml_config['url'] = config['url'] # WRITE TO FILE yaml_str = yaml.dump(yaml_config, default_flow_style=False, explicit_start=True) with open(file_path, 'w') as f: f.write(yaml_str) return 0, yaml_str except json.decoder.JSONDecodeError: return 1, "Query string is not valid json: {}".format(traceback.format_stack()) except Exception as e: logger.error("Unable to render yaml config file to disk") _, _, ex_traceback = sys.exc_info() return 1, render_traceback(e, ex_traceback) def render_yaml(alert_id): """ Reads in a yaml file into the config that the web expects. Args: alert_id: then name of the config Returns: Dictionary """ file_name = ''.join([alert_id, '.yaml']) file_path = os.path.join('alert_configs', file_name) config = yaml.load(open(file_path, 'r').read()) yaml_config = dict() yaml_config['alert_name'] = config['id'] yaml_config['interval'] = config['interval'] if 'critical_threshold' in config: yaml_config['critical_upper_threshold'] = config['critical_threshold'] if 'critical_upper_threshold' in config: yaml_config['critical_upper_threshold'] = config['critical_upper_threshold'] if 'critical_lower_threshold' in config: yaml_config['critical_lower_threshold'] = config['critical_lower_threshold'] if 'warning_threshold' in config: yaml_config['warning_upper_threshold'] = config['warning_threshold'] if 'warning_upper_threshold' in config: yaml_config['warning_upper_threshold'] = config['warning_upper_threshold'] if 'warning_lower_threshold' in config: yaml_config['warning_lower_threshold'] = config['warning_lower_threshold'] if 'occurrences_threshold' in config: yaml_config['occurrences_threshold'] = config['occurrences_threshold'] yaml_config['url'] = config['url'] if 'email' in config['alerts']: yaml_config['email'] = 'on' yaml_config['email_list'] = ','.join(config['alerts']['email']) if 'vo' in config['alerts']: yaml_config['vo'] = 'on' yaml_config['vo_list'] = ','.join(config['alerts']['vo']) if 'slack' in config['alerts']: yaml_config['slack'] = 'on' yaml_config['slack_list'] = ','.join(config['alerts']['slack']) if 'tags' in config: yaml_config['tags'] = ','.join(config['tags']) if config.get('query_type') == 'prometheus': yaml_config['prometheus_query'] = config['query'] yaml_config['prometheus_url'] = config['prometheus_url'] yaml_config['start_time'] = config['start_time'] yaml_config['end_time'] = config['end_time'] else: yaml_config['kairosdb_query'] = json.dumps(config['query'], sort_keys=True, indent=4, separators=(',', ': ')) return yaml_config def render_traceback(ex, ex_traceback): tb_lines = traceback.format_exception(ex.__class__, ex, ex_traceback) logger.exception("Exception") return '\n'.join(tb_lines)