1. 程式人生 > >Zabbix定義報警機制

Zabbix定義報警機制

緩存 host 有效 style 出現 sta ttl 第一個 國外

1. 修改zabbix配置文件

#取消註釋或添加一行
cat -n /etc/zabbix/zabbix_server.conf |grep --color=auto "AlertScriptsPath"
   484    ### Option: AlertScriptsPath
   490    # AlertScriptsPath=${datadir}/zabbix/alertscripts
   492    AlertScriptsPath=/usr/lib/zabbix/alertscripts

2. 編寫郵件腳本

[root@db01 ~]# cd /usr/lib/zabbix/alertscripts/
[root@db01 alertscripts]# ll total 16 -rwxr-xr-x. 1 root root 1113 May 17 13:59 email_monitor.py -rwxr-xr-x. 1 zabbix zabbix 1586 May 13 23:41 send_mail.py -rwxr-xr-x. 1 root root 1307 May 14 04:54 send_mail.py.bak -rw-r--r--. 1 root root 186 May 14 07:02 test.py

由於國外郵件測試過程頗為坎坷,就用到萬能的outlook做為報警郵件

[root@db01 alertscripts]# cat email_monitor.py 
#!/bin/env python
# coding:utf-8

import sys
from smtplib import SMTP as SMTP
from email.mime.text import MIMEText

Email_sender = [email protected]  # 發送方郵箱
Sender_passwd = rxxx6  # 填入發送方郵箱的授權碼
Send_to = [email protected]  # 收件人郵箱               #這個可以通過zabbix傳參進來,不過考慮到以後如果有多人寫列表池比較方便就沒有用zabbix傳郵箱地址方式
SMTP_HOST = "smtp-mail.outlook.com" SMTP_PORT = 587 def Email_monitor(subject,content): msg_from = Email_sender passwd = Sender_passwd msg_to = Send_to #print(type(content),content,‘#######‘) msg = MIMEText(content) # 正文 msg[Subject] = subject # 主題 msg[From] = msg_from msg[To] = msg_to s = SMTP(host=SMTP_HOST, port=SMTP_PORT) # 郵件服務器及端口號 s.starttls() s.login(msg_from, passwd) s.sendmail(msg_from, msg_to, msg.as_string()) s.quit() if __name__ == __main__: try: mailto_list = sys.argv[1].split(;) # 第一個參數是zabbix傳的發送給誰(接受郵箱賬號),上面說了這個沒有用到,寫死了郵件接收賬號 sub = sys.argv[2] # 第二個參數是zabbix傳的主題信息 content = sys.argv[3] # 第三個參數是zabbix傳的報警內容(後面會貼上報警內容,及恢復內容) Email_monitor(sub, content) print("發送成功".decode(utf-8)) # python2版本打印會出現亂碼 except Exception as e: print("發送失敗".decode(utf-8)) [root@db01 alertscripts]#

3. 測試腳本

python email_monitor.py  bc  python郵件測試發送-003 郵件內容233333

4. 微信報警腳本

[root@db01 alertscripts]# cat send_mail.py
#!/bin/env python
# coding:utf-8

import requests
import json
import sys

def get_access_token():
    """
    獲取微信全局接口的憑證(默認有效期倆個小時)
    如果不每天請求次數過多, 通過設置緩存即可
    https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
    """
    result = requests.get(
        url="https://api.weixin.qq.com/cgi-bin/token",
        params={
            "grant_type": "client_credential",
            "appid": "wx2499da7621f818e8",   
            "secret": "6239e3dfc5af686777ea40b9f3df5f48",
        }
    ).json()

    if result.get("access_token"):
        access_token = result.get(access_token)
    else:
        access_token = None
    return access_token


def sendmsg(openid, msg):
    # openid: 關註者的微信號
    access_token = get_access_token()

    body = {
        "touser": openid,
        "msgtype": "text",
        "text": {
            "content": msg
        }
    }
    response = requests.post(
        url="https://api.weixin.qq.com/cgi-bin/message/custom/send",
        params={
            access_token: access_token
        },
        data=bytes(json.dumps(body, ensure_ascii=False))
    )
    # 這裏可根據回執code進行判定是否發送成功(也可以根據code根據錯誤信息)
    result = response.json()
    print(result)


if __name__ == __main__:

    try:
        mailto_list = sys.argv[1].split(;)
        sub = sys.argv[2]
        content = sys.argv[3]
    print(mailto_list,sub,content)
        sendmsg(oYm3A06plEcQCItTxAFgoh18E-7M, content)
    except:
    pass
[root@db01 alertscripts]# 

#晚上再登錄已經登錄不上去163了就不截圖了

5. Zabbix上配置

這裏筆者就不讀寫了,直接傳送門:http://blog.51cto.com/jinlong/2051106

6. 報警操作及恢復操作(記得再動作裏配置)

技術分享圖片
接收人:{TRIGGER.STATUS}: {TRIGGER.NAME}



告警主機:{HOST.NAME} 
主機地址:{HOST.IP} 
告警時間:{EVENT.DATE} {EVENT.TIME} 
告警等級:{TRIGGER.SEVERITY} 
告警信息:{TRIGGER.NAME} 
問題詳情:{ITEM.NAME}:{ITEM.VALUE} 
事件代碼:{EVENT.ID}
報警操作 技術分享圖片
接收人:{TRIGGER.STATUS}: {TRIGGER.NAME}



恢復主機:{HOST.NAME} 
主機地址:{HOST.IP} 
恢復時間:{EVENT.DATE} {EVENT.TIME} 
恢復等級:{TRIGGER.SEVERITY} 
恢復信息:{TRIGGER.NAME} 
問題詳情:{ITEM.NAME}:{ITEM.VALUE} 
事件代碼:{EVENT.ID}
恢復操作

Zabbix定義報警機制