#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time
try:
    import simplejson as json
except ImportError:
    import json
from conf import pn_problem_collect_conf
from src.lib import db_mysql
from src.lib import zabbix_api
from conf import cannon_conf
#mysql_conn = db_mysql.MyPymysqlPool()
zabbix = zabbix_api.ZabbixApi()
hostip_list = pn_problem_collect_conf.hostip_list
pn_node_list = pn_problem_collect_conf.pn_node_list
trigger_delay = pn_problem_collect_conf.trigger_delay
trigger_block = pn_problem_collect_conf.trigger_block
trigger_telnet = pn_problem_collect_conf.trigger_telnet
template_name = pn_problem_collect_conf.template_name

pn_event_table = 'pn_event'
pn_event_source_data_table = 'pn_event_source_data'
pn_event_history_table = 'pn_event_history'

task_log_file = cannon_conf.task_manage_log
eventid_list = []


def get_itemid(triggerid):
    itemid = []
Exemple #2
0
def zabbix_alert_handler(task_id: int = 0) -> bool:
    zabbix = zabbix_api.ZabbixApi()
    result = zabbix.get_trigger()
    success = False  #默认未采集到数据

    if not result:
        return success
    try:
        alert_list = result['result']
        print('---------alert_list-------', alert_list)
    except:
        return success

    mysql_conn = db_mysql.MyPymysqlPool()

    # 提前获取数据库中的所有在报警状态的zabbix告警记录
    sql = "select * from %s where alert_from = 'zabbix' and current_state = 'ALARM' order by start_time desc;" % (
        alert_conf.table_name)
    db_zabbix_result = mysql_conn.select(sql)
    print('---------db_zabbix_result-------', db_zabbix_result)
    zabbix_result = []
    success = True
    alert_list_filter = []
    for single_alert in alert_list:
        try:
            priority = single_alert['priority']
            if priority == '2':
                level = 3
                continue
            elif priority == '3' or priority == '4':
                level = 2
            elif priority == '5':
                level = 1
            description = single_alert['description']
            resource = single_alert['hosts'][0]['host']
            expression = single_alert['expression']
            start_time = time.strftime(
                "%Y-%m-%d %H:%M:%S",
                time.localtime(int(single_alert['lastchange'])))
            alert_detail = "%s" % (description)
            current_state = "ALARM"
            alert_from = "zabbix"
            production = "ecs"
        except:
            print("error!")
            continue
        else:
            alert_list_filter.append(single_alert)

        tmp = [production, resource, alert_detail, expression]
        zabbix_result.append(tmp)
        '''检查production、resource、alert_detail是否有重复
        如果不重复,直接写入db
        如果有重复,例如同一个触发器连续报警,需要检查之前最后一条的报警状态:
                if 之前状态是恢复,本次是告警,则直接写入db
                if 之前状态是告警,本次是告警,则跳过。'''

        insert_new_record_sql = "insert into %s(alert_from,production,resource,current_state,alert_detail,expression,start_time,priority) " \
                                "values('%s','%s','%s','%s','%s','%s','%s','%s')" % \
                                (alert_conf.table_name,alert_from,production,resource,current_state,alert_detail,expression,start_time,level)

        # 判断产品+资源+告警详情是否重复
        sql = "select * from %s where production = '%s' and resource = '%s' and alert_detail = '%s' limit 1;" % (
            alert_conf.table_name, production, resource, alert_detail)
        result = mysql_conn.select(sql)
        if not result:  # 不重复,则写入这条告警
            mysql_conn.insert(insert_new_record_sql)
        else:
            # 再增加一个时间的判断
            sql = "select * from %s where production='%s' and resource='%s' and alert_detail='%s' and start_time='%s' limit 1;" % (
                alert_conf.table_name, production, resource, alert_detail,
                start_time)
            result = mysql_conn.select(sql)
            if result:  # 记录已存在
                continue

            sql = "select * from %s where production = '%s' and resource = '%s' and alert_detail = '%s' order by " \
                  "start_time desc limit 1" % (alert_conf.table_name, production, resource, alert_detail)
            result = mysql_conn.select(sql)
            last_state = result[0][5]

            if last_state == "ALARM" and current_state == "ALARM":
                continue
            elif last_state == "OK" and current_state == "ALARM":
                mysql_conn.insert(insert_new_record_sql)
    '''
    获取db中所有报警状态的zabbix报警项,和当前报警项做对比
    如果存在,则跳过
    如果不存在,则update该项状态,置为恢复,并获取当前时间,置为该条的结束时间
    '''
    if db_zabbix_result:
        for record in db_zabbix_result:
            record_id_in_db = record[0]
            production_in_db = record[2]
            resource_in_db = record[4]
            alert_detail_in_db = record[6]
            expression_in_db = record[7]
            result = False
            for i in zabbix_result:
                current_production = i[0]
                current_resource = i[1]
                current_alert_detail = i[2]
                current_expression = i[3]
                if production_in_db == current_production and resource_in_db == current_resource and \
                        alert_detail_in_db == current_alert_detail and expression_in_db == current_expression:
                    result = True
                    break
            if not result:
                end_time = time.strftime("%Y-%m-%d %H:%M:%S",
                                         time.localtime(time.time()))
                sql = "UPDATE %s SET current_state='OK', end_time='%s' WHERE id=%s" % (
                    alert_conf.table_name, end_time, record_id_in_db)
                mysql_conn.update(sql)
    mysql_conn.dispose()
    return success
Exemple #3
0
def zabbix_monitor_switch(request):
    action = int(request.GET['action'])
    hostip = request.GET['privateIp']
    zabbix_ak = request.GET['ak']
    success = False
    code = 500
    message = 'fail'
    result_dict = {'code': code, 'success': success, 'message': message}
    mysql_conn = db_mysql.MyPymysqlPool()

    if zabbix_ak != '1d42ee7b99a7d92bdbdaccc3edc30a9f' or action not in [0, 1]:
        result_dict['message'] = 'Args error!'
        result_json = json.dumps(result_dict, ensure_ascii=False)
        return HttpResponse(result_json,
                            content_type="application/json,charset=utf-8")

    retry = 0

    zabbix = zabbix_api.ZabbixApi()
    while retry < 3:
        if action == 1:
            action_name = 'enable'
            result = zabbix.host_enable(hostip)
        else:  # action == 0
            action_name = 'disable'
            result = zabbix.host_disable(hostip)

        if not result:
            result_dict['message'] = '%s %s 失败!' % (action_name, hostip)
            result_json = json.dumps(result_dict, ensure_ascii=False)
            return HttpResponse(result_json,
                                content_type="application/json,charset=utf-8")

        result_dict['code'] = 200
        result_dict['success'] = True
        result_dict['message'] = '%s %s 成功!' % (action_name, hostip)

        # 监控启停的操作记录入库
        action_time = time.strftime("%Y-%m-%d %H:%M:%S",
                                    time.localtime(time.time()))
        host_name = zabbix.get_hostname_with_hostip(hostip)

        if not host_name:
            result_dict['message'] = '通过zabbix_api获取hostname失败!'
            result_json = json.dumps(result_dict, ensure_ascii=False)
            return HttpResponse(result_json,
                                content_type="application/json,charset=utf-8")

        sql = "insert into %s(host_ip, host_name, action, time) values('%s', '%s', '%s', '%s')" %\
              (alert_conf.zabbix_switch_table, hostip, host_name, action_name, action_time)
        try:
            result = mysql_conn.insert(sql)
        except Exception as e:
            result_dict['message'] = '写入操作记录失败!'
        else:
            result_dict['message'] = '%s %s, 所有动作完成, 最终成功!' % (action_name,
                                                               hostip)
            break
        retry += 1
        time.sleep(2)
    mysql_conn.dispose()
    result_json = json.dumps(result_dict, ensure_ascii=False)
    return HttpResponse(result_json,
                        content_type="application/json,charset=utf-8")