|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import division |
| 3 | +import json |
| 4 | +import sys |
| 5 | +import config |
| 6 | +import re |
| 7 | +import requests |
| 8 | + |
| 9 | + |
| 10 | +valid_value_notify = ['slack'] |
| 11 | + |
| 12 | + |
| 13 | +def load_json_file(file_name): |
| 14 | + try: |
| 15 | + with open(file_name) as data_file: |
| 16 | + data = json.load(data_file) |
| 17 | + except ValueError: |
| 18 | + return False |
| 19 | + except IOError: |
| 20 | + return None |
| 21 | + else: |
| 22 | + return data |
| 23 | + |
| 24 | + |
| 25 | +#result = load_json_file('sample.json') |
| 26 | +#print result |
| 27 | + |
| 28 | + |
| 29 | +def json_fields_check(json_arr): |
| 30 | + check_result = False |
| 31 | + missing_fields = [] |
| 32 | + invalid_fields = [] |
| 33 | + |
| 34 | + if 'monitor_id' not in json_arr: |
| 35 | + missing_fields.append('monitor_id') |
| 36 | + |
| 37 | + if 'enable' not in json_arr: |
| 38 | + missing_fields.append('enable') |
| 39 | + else: |
| 40 | + if json_arr['enable'] is not False: |
| 41 | + if json_arr['enable'] is not True: |
| 42 | + invalid_fields.append('enable') |
| 43 | + |
| 44 | + if 'formula' not in json_arr: |
| 45 | + missing_fields.append('formula') |
| 46 | + |
| 47 | + if 'parameters' not in json_arr: |
| 48 | + missing_fields.append('parameters') |
| 49 | + |
| 50 | + if 'notify' not in json_arr: |
| 51 | + missing_fields.append('notify') |
| 52 | + else: |
| 53 | + for item in json_arr['notify']: |
| 54 | + if item not in valid_value_notify: |
| 55 | + invalid_fields.append({"notify": item}) |
| 56 | + |
| 57 | + if 'alarms' not in json_arr: |
| 58 | + missing_fields.append('alarms') |
| 59 | + else: |
| 60 | + if 'alert' not in json_arr['alarms']: |
| 61 | + if 'warning' not in json_arr['alarms']: |
| 62 | + missing_fields.append({"alarms": "alert"}) |
| 63 | + missing_fields.append({"alarms": "warning"}) |
| 64 | + |
| 65 | + if 'elk_env' not in json_arr: |
| 66 | + missing_fields.append('elk_env') |
| 67 | + else: |
| 68 | + if json_arr['elk_env'] != 'staging': |
| 69 | + if json_arr['elk_env'] != 'production': |
| 70 | + invalid_fields.append('elk_env') |
| 71 | + |
| 72 | + if len(missing_fields) == 0 and len(invalid_fields) == 0: |
| 73 | + check_result = True |
| 74 | + |
| 75 | + return_arr = { |
| 76 | + "check_result": check_result, |
| 77 | + "missing_fields": missing_fields, |
| 78 | + "invalid_fields": invalid_fields |
| 79 | + } |
| 80 | + |
| 81 | + return return_arr |
| 82 | + |
| 83 | + |
| 84 | +def get_log_dict_value(elk_conn, json_data, value_param): |
| 85 | + |
| 86 | + r = requests.post(elk_conn + "?size=0", data=json_data) |
| 87 | + json_res = json.loads(r.text) |
| 88 | + |
| 89 | + dict_list = value_param.split(".") |
| 90 | + |
| 91 | + str_key = "" |
| 92 | + for key in dict_list: |
| 93 | + str_key += '[\'' + key + '\']' |
| 94 | + |
| 95 | + return eval("json_res" + str_key) |
| 96 | + |
| 97 | + |
| 98 | +def get_log_hit(elk_conn, json_data): |
| 99 | + r = requests.post(elk_conn, data=json_data) |
| 100 | + json_res = json.loads(r.text) |
| 101 | + return json_res['hits']['total'] |
| 102 | + |
| 103 | + |
| 104 | +''' |
| 105 | +def formula_check(formula): |
| 106 | + try: |
| 107 | + cal_result = eval(formula_replaced + ".0") |
| 108 | + # print cal_result |
| 109 | + except ZeroDivisionError: |
| 110 | + # print "Division by zero" |
| 111 | + # raise Exception("Division by zero") |
| 112 | +''' |
| 113 | + |
| 114 | + |
| 115 | +def main(argv): |
| 116 | + if len(argv) == 2: |
| 117 | + file_name = argv[1] |
| 118 | + json_arr = load_json_file(file_name) |
| 119 | + if json_arr is None: |
| 120 | + return {"valid": False, "message": "[Error] File not found", "detail": ""} |
| 121 | + elif json_arr is False: |
| 122 | + return {"valid": False, "message": "[Error] Json parsing error", "detail": ""} |
| 123 | + else: |
| 124 | + json_check_arr = json_fields_check(json_arr) |
| 125 | + if json_check_arr['check_result']: |
| 126 | + # Get ELK endpoint from config |
| 127 | + elk_env = json_arr['elk_env'] |
| 128 | + if elk_env == 'staging': |
| 129 | + elk_conn = config.elk_stg |
| 130 | + elif elk_env == 'production': |
| 131 | + elk_conn = config.elk_prod |
| 132 | + |
| 133 | + formula = json_arr['formula'] |
| 134 | + |
| 135 | + # Replace ${} params |
| 136 | + pattern = re.compile(r'\$\{([A-Za-z0-9_.]+)\}') |
| 137 | + params = re.findall(pattern, formula) |
| 138 | + dict_log_count = {} |
| 139 | + for param in params: |
| 140 | + param_log_count = get_log_hit(elk_conn, json.dumps(json_arr['parameters'][param])) |
| 141 | + dict_log_count.update({"${" + param + "}": param_log_count}) |
| 142 | + |
| 143 | + # Replace %{} params |
| 144 | + pattern_2 = re.compile(r'%\{([A-Za-z0-9_.]+)\}') |
| 145 | + params_2 = re.findall(pattern_2, formula) |
| 146 | + dict_log_value = {} |
| 147 | + for param in params_2: |
| 148 | + param_log_value = get_log_dict_value(elk_conn, json.dumps(json_arr['parameters'][param]), param) |
| 149 | + dict_log_value.update({"%{" + param + "}": param_log_value}) |
| 150 | + |
| 151 | + # Replace formula for calculation |
| 152 | + formula_replaced = formula |
| 153 | + |
| 154 | + for key, value in dict_log_count.iteritems(): |
| 155 | + formula_replaced = formula_replaced.replace(key, str(value)) |
| 156 | + |
| 157 | + for key, value in dict_log_value.iteritems(): |
| 158 | + formula_replaced = formula_replaced.replace(key, str(value)) |
| 159 | + |
| 160 | + try: |
| 161 | + #cal_result = float(eval(formula_replaced)) |
| 162 | + cal_result = eval(formula_replaced) |
| 163 | + |
| 164 | + config_alarms = json_arr['alarms'] |
| 165 | + bool_warning = None |
| 166 | + bool_alert = None |
| 167 | + if 'warning' in config_alarms: |
| 168 | + bool_warning = eval(str(cal_result) + config_alarms['warning']) |
| 169 | + |
| 170 | + if 'alert' in config_alarms: |
| 171 | + bool_alert = eval(str(cal_result) + config_alarms['alert']) |
| 172 | + |
| 173 | + detail_arr = { |
| 174 | + "cal_result": cal_result, |
| 175 | + "dict_log_count": dict_log_count, |
| 176 | + "config_alarms": config_alarms, |
| 177 | + "bool_warning": bool_warning, |
| 178 | + "bool_alert": bool_alert |
| 179 | + } |
| 180 | + return {"valid": True, "message": "OK", "detail": detail_arr} |
| 181 | + except ZeroDivisionError: |
| 182 | + return {"valid": False, "message": "[ERROR] Division by zero", "detail": dict_log_count} |
| 183 | + |
| 184 | + else: |
| 185 | + return {"valid": False, "message": "[ERROR] json_fields_check failed", "detail": json_check_arr} |
| 186 | + else: |
| 187 | + return {"valid": False, "message": "[Error] Argument error", "detail": ""} |
| 188 | + |
| 189 | +if __name__ == "__main__": |
| 190 | + # For Testing |
| 191 | + # sys.argv.append('sample3.json') |
| 192 | + |
| 193 | + result_arr = main(sys.argv) |
| 194 | + print result_arr |
| 195 | + |
| 196 | + |
0 commit comments