import yaml import glob if __name__ == "__main__": alert_list = [] bad_alert_list = [] print("Collecting all yaml configs") # COLLECT CONFIG FILES for config_file in glob.glob("./alert_configs/*.yaml"): print("Found {} config".format(config_file)) alert_list.append(config_file) print("Collecting all yaml configs") # PARSE CONFIG FILES AND VALIDATE THEIR VALUES for alert in alert_list: print("Validating file {}".format(alert)) try: config = yaml.load(open(alert, 'rb').read()) assert len(config['alerts']) > 0, "No Alerts configured, this is a dead config" assert len(config['query']) > 0, "No Query, this is a dead config" assert config['interval'] >= 30, "Intervals less than 30 are invalid" assert len(config['id']) > 0, "Alert ID is empty, this is a dead config" if config.get('query_type') == 'prometheus': assert type(config['query']) is str, "Invalid Prometheus query" assert "$" not in config['query'], "Prometheus query should not contain variables" else: assert type(config['query']) is dict, "Kairosdb Query string cannot be validated as proper JSON" defined_tags = set(config['query']['metrics'][0]['tags'].keys()).union({'','dc','fqdn'}) # IF THERE IS AGGREGATION WE HAVE TO ADD THESE TAGS if 'group_by' in config['query']['metrics'][0]: defined_tags.update(set(config['query']['metrics'][0]['group_by'][0]['tags'])) # for undefined_tag in set(config['tags']).difference(defined_tags): # print("WARNING! {} tag is not defined on the query. Please make sure it does exist to "\ # "prevent empty results".format(undefined_tag)) # OUR MINIMUM THRESHOLD NEED assert 'critical_lower_threshold' in config or 'critical_upper_threshold' in config or \ 'warning_lower_threshold' in config or 'warning_upper_threshold' in config, \ "Config must have at least one threshold set." # JUST MAKE SURE YOU ARE NOT DOING SOMETHING STUPID WITH WARNING COMING AFTER CRITICAL if 'warning_lower_threshold' in config and 'critical_lower_threshold' in config: assert config['critical_lower_threshold'] < config['warning_lower_threshold'], \ "Lower Critical must be less than Lower Warning" if 'warning_upper_threshold' in config and 'critical_upper_threshold' in config: assert config['critical_upper_threshold'] > config['warning_upper_threshold'], \ "Upper Critical must be greater than Upper Warning" if 'occurrences_threshold' in config: assert config['occurrences_threshold'] >= 1, \ "Having an occurrences value less than 2 is assumed and pointless to specify" except Exception as e: print("Invalid config file: {}\n{}".format(alert, str(e))) bad_alert_list.append("{}\n{}".format(alert, str(e))) # WRITE OUT BAD CONFIGS TO THE RESULTS FILE # with open("./results/test_results.log", "w+") as f: # for alert in bad_alert_list: # f.write("Config is bad: {}".format(alert.replace('\n', ' '))) for alert in bad_alert_list: print("Config is bad: {}".format(alert.replace('\n', ' '))) if bad_alert_list: exit(1)