zabbix generated automatically and periodically send reports to monitor messages

Realization of ideas:

zabbix generated automatically and periodically send reports to monitor messages

  1. zabbix provides an acquisition event api, api can get zabbix raw alarm data in accordance with this
  2. The acquired raw data deduplication statistics, statistics trigger occurrences, and delete the duplicate trigger will need to use the data into a unified list
  3. The second step of the list is traversed and passed to HTML, or may be used directly to data modeling pandas, and then automatically generate HTML tables
  4. The generated HTML content sent as e-mail

Defined acquisition interval

x=(datetime.datetime.now()-datetime.timedelta(minutes=30)).strftime("%Y-%m-%d %H:%M:%S")
y=(datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
def timestamp(x,y):
    p=time.strptime(x,"%Y-%m-%d %H:%M:%S")
    starttime = str(int(time.mktime(p)))
    q=time.strptime(y,"%Y-%m-%d %H:%M:%S")
    endtime= str(int(time.mktime(q)))
    return starttime,endtime

This is obtained for 30 minutes alarm data

Get event data

def getevent(auth,timestamp):
    data={
        "jsonrpc": "2.0",
        "method": "event.get",
        "params": {
            "output": [
                "name",
                "severity"
            ],
            "value":1,
            "time_from":timestamp[0],
            "time_till":timestamp[1],
            "selectHosts":[
                #"hostid",
                "name"
            ]
        },
        "auth": auth,
        "id": 1
    }
    getevent=requests.post(url=ApiUrl,headers=header,json=data)
    triname=json.loads(getevent.content)['result']  

By zabbix api get events need to use the content, which includes alarm host name, host id, flip-flop, flip-flop severity of

The acquired data is processed

triggers=[]
a={}
for i in triname:
     triggers.append(i['name'])
for i in triggers:
     a[i]=triggers.count(i)
list2=[]
print(triname)
#print(a)
for key in a:
     b={}
     b['name']=key
     b['host']=[i['hosts'][0]['name'] for i in triname if i['name']==key][0]
     b['severity']=[i['severity'] for i in triname if i['name']==key][0]
     b['count']=a[key]
     list2.append(b)
return list2

Here is the trigger will be recurring and to re-count the number of occurrences, the acquired number of triggered and previous information into the same table, and passed to HTML

Transmitting data to HTML

def datatohtml(list2):
    tables = ''
    for i in range(len(list2)):
        name,host,severity,count = list2[i]['name'], list2[i]['host'], list2[i]['severity'], list2[i]['count']
        td = "<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td>"%(name, host, severity, count)
        tables = tables + "<tr>%s</tr>"%td
    base_html="""
    <!DOCTYPE html>
    <html>
    <head> 
    <meta charset="utf-8"> 
    <title>zabbix监控告警</title> 
    </head>
    <body>
    <table width="900" border="0">
    <tr>
    <td colspan="2" style="background-color:#FFA500;">
    <h4>告警级别: 1 表示:信息 2 表示:告警 3 表示:一般严重 4 表示:严重 5 表示:灾难</h4>
    </td>
    </tr>
    <tr>
    <td style="background-color:#FFD700;width:100px;">
    <TABLE BORDER=1><TR><TH>主机</TH><TH>触发器</TH><TH>告警级别</TH><TH>告警次数</TH></TR>%s</TABLE>
    </td>
    </tr>
    <tr>
    <td colspan="2" style="background-color:#FFA500;text-align:center;">
    zabbix告警统计</td>
    </tr>
    </table>
    </body>
    </html>
    """ %tables
    return base_html

Traverse the list of incoming and incoming HTML table

Send e-mail reports

The resulting HTML sent via e-mail

def sendmail(base_html):
    from_addr = '[email protected]'
    password = '没有故事的陈师傅'
    to_addr = '[email protected]'
    smtp_server = 'smtp.qq.com'

    msg = MIMEText(base_html, 'html', 'utf-8')
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Subject'] = Header('Zabbix本周监控报表', 'utf-8').encode()

    try:
        server=SMTP(smtp_server,"25")   #创建一个smtp对象
        #server.starttls()    #启用安全传输模式
        server.login(from_addr,password)  #邮箱账号登录
        server.sendmail(from_addr,to_addr,msg.as_string())  #发送邮件  
        server.quit()   #断开smtp连接
    except smtplib.SMTPException as a:
        print (a)

Complete script by GitHub: https://github.com/sunsharing-note/zabbix/blob/master/zhoubao.py acquisition, or public concern No. "There is no story of Chan master," replied obtain monitoring reports


No personal welcome attention to the public "is not the story of Master Chen"
zabbix generated automatically and periodically send reports to monitor messages

Guess you like

Origin blog.51cto.com/12970189/2428022