def check_result(cls, output, alert_rules_id): msg = "" logging.info(output.strip()) try: for x in output.replace('*', '').strip().split("\n"): p1 = re.compile(r'failed=(\d)') r1 = p1.search(x) p2 = re.compile(r'unreachable=(\d)') r2 = p2.search(x) if r1 is None or r2 is None: continue else: flag = True failed_count = int(r1.group(1)) unreachable_count = int(r2.group(1)) if failed_count != 0 or unreachable_count != 0: msg += "There are {0} ansible-playbook sub task run into error".format( failed_count + unreachable_count) if flag is False: msg += "ansible-playbook command execute failed." except Exception as e: # return {'status': -1, 'result': str(e)} msg += str(e) try: if msg != "": logging.error(msg) #AlertRulesData.update_status(alert_rules_id, 2) AlertRulesData.delete_alert_rule(alert_rules_id) else: AlertRulesData.update_status(alert_rules_id, 1) m.reload() except Exception as e: logging.error(e)
def put(self, alert_rules_id): json_data = request.get_json(force=True) action = json_data['action'] if action['method'] == 'modify': statistical_period = action['param']['statistical_period'] # statistical_approach = action['param']['statistical_approach'] compute_mode = action['param']['compute_mode'] threshold_value = action['param']['threshold_value'] contact_groups = action['param']['contact_groups'] notify_type = action['param']['notify_type'] try: data_res = AlertManager.get_alert_rules(alert_rules_id) host_id = data_res['host_id'] port = data_res['port'] service = data_res['service'] monitor_items = data_res['monitor_items'] Ansible.check(MONITOR_SERVER) inv_file = Ansible.init_target_yaml(MONITOR_SERVER) instance = host_id + ":" + str(port) yml_file = Ansible.init_metrics_yaml(service, monitor_items, host_id, instance, threshold_value, statistical_period, compute_mode) Ansible.execute(yml_file, inv_file, alert_rules_id) data_res = AlertManager.update_alert_rules( alert_rules_id, statistical_period, compute_mode, threshold_value, contact_groups, notify_type) except Exception as e: raise Error(str(e)) elif action['method'] == 'disable': try: silence_time = action['param']['silence_time'] silence_time_hour = str(silence_time) + 'h' data_res = AlertManager.get_alert_rules(alert_rules_id) alert_name = data_res['service'] + '_' + data_res[ 'monitor_items'] + '_' + data_res['host_id'] + ':' + str( data_res['port']) AlertManager.disable_alert(alert_name, silence_time_hour) AlertRulesData.update_silence_time(alert_rules_id, int(silence_time)) except Exception as e: raise Error(str(e)) elif action['method'] == 'enable': data_res = AlertManager.get_alert_rules(alert_rules_id) alert_name = data_res['service'] + '_' + data_res[ 'monitor_items'] + '_' + data_res['host_id'] + ':' + str( data_res['port']) AlertManager.enable_alert(alert_name) AlertRulesData.update_silence_time(alert_rules_id, 0) return {'status': 'ok'}, 201
def put(self): json_data = request.get_json(force=True) host_id = json_data['id'] # need get from db port = 9100 service = json_data['service'] temp_name = json_data['name'] rules = json_data['rules'] contact_groups = json_data['contact_groups'] notify_type = json_data['notify_type'] for rule in rules: monitor_items = rule['monitor_items'] alert_rules = AlertManager.get_alert_rules_by_name(host_id, port, monitor_items) alert_rules_id = alert_rules[1] #default statistical_period = '5s' # default statistical_approach = 'max' compute_mode = rule['compute_mode'] threshold_value = rule['threshold_value'] silence_tage = rule['silence'] try: if silence_tage == 0: # running # update mysql data_res = AlertManager.update_alert_rules(alert_rules_id, statistical_period, compute_mode, threshold_value, ','.join(contact_groups), ','.join(notify_type), 0) Ansible.check(MONITOR_SERVER) inv_file = Ansible.init_target_yaml(MONITOR_SERVER) instance = host_id + ":" + str(port) yml_file = Ansible.init_metrics_yaml(service, monitor_items, host_id, instance, threshold_value, statistical_period, compute_mode) Ansible.execute(yml_file, inv_file, alert_rules_id) elif silence_tage == 1: silence_time = 2400 silence_time_hour = str(silence_time) + 'h' data_res = AlertManager.get_alert_rules(alert_rules_id) alert_name = data_res['service'] + '_' + data_res['monitor_items'] + '_' + data_res[ 'host_id'] + ':' + str(data_res['port']) AlertManager.disable_alert(alert_name, silence_time_hour) AlertRulesData.update_silence_time(alert_rules_id, int(silence_time)) elif silence_tage == 2: data_res = AlertManager.get_alert_rules(alert_rules_id) alert_name = data_res['service'] + '_' + data_res['monitor_items'] + '_' + data_res[ 'host_id'] + ':' + str(data_res['port']) AlertManager.enable_alert(alert_name) AlertRulesData.update_silence_time(alert_rules_id, 0) except Exception as e: raise Error(e) return {'status': 'ok'}, 201
def delete(self, alert_rules_id): try: data_res = AlertManager.get_alert_rules(alert_rules_id) file_name = data_res['host_id'] + '_' + str(data_res['port']) + '_' + data_res['service'] + '_' + data_res[ 'monitor_items'] + '.yml' AlertManager.delete_alert_rules(file_name) AlertRulesData.delete_alert_rule(alert_rules_id) except Exception as e: raise Error(str(e)) return {'status': 'ok'}, 201
def create_alert_rules(cls, *add): alert_rules_id, host_id, service, monitor_items, statistical_period, statistical_approach, compute_mode, threshold_value, status = add rs = AlertRulesData.add(alert_rules_id, host_id, service, monitor_items, statistical_period, statistical_approach, compute_mode, threshold_value, status) return rs
def get_alert_rules_by_name(cls, host_id, port, monitor_items): rs = AlertRulesData.get_alert_rules_by_name(host_id, port, monitor_items) if rs: return rs else: return
def create_alert_rules(cls, *add): alert_rules_id, host_id, port, service, temp_name, monitor_items, statistical_period, statistical_approach, compute_mode, threshold_value, contact_groups, notify_type, status = add rs = AlertRulesData.add(alert_rules_id, host_id, port, service, temp_name, monitor_items, statistical_period, statistical_approach, compute_mode, threshold_value, contact_groups, notify_type, status) return rs
def get_alert_rules(cls, alert_rules_id): rs = AlertRulesData.get_alert_rules(alert_rules_id) if rs: return [line.dump() for line in rs] else: return
def update_alert_rules(cls, *update): alert_rules_id, statistical_period, statistical_approach, compute_mode, threshold_value = update rs = AlertRulesData.update(alert_rules_id, statistical_period, statistical_approach, compute_mode, threshold_value) return rs
def get_alert_rules_list(cls): rs = AlertRulesData.get_alert_rules_list() if rs: return [line.dump() for line in rs] else: return
def get_alert_rules(cls, alert_rules_id): rs = AlertRulesData.get_alert_rules(alert_rules_id) if rs: return rs else: return
def update_alert_rules(cls, *update): alert_rules_id, statistical_period, compute_mode, threshold_value, contact_groups, notify_type = update rs = AlertRulesData.update(alert_rules_id, statistical_period, compute_mode, threshold_value, contact_groups, notify_type) return rs
def get_alert_rules_dict(cls, service_name): rs = AlertRulesData.show_alert_rules(service_name) if rs: return [line.dump() for line in rs] else: return