1. 程式人生 > >zabbix配置

zabbix配置

tde try bject read sendmai 問題 vim .com ==

一、配置郵件報警

1、腳本

# cd /usr/lib/zabbix/alertscripts # vim Sendmail.sh
#!/bin/bash
file=/tmp/zabbix_mail.txt
echo "$3" > $file
dos2unix -k $file
/bin/mail -s "$2" $1 < $file
# echo "$3" | mail -s "$2" $1

2、安裝郵件發送軟件mailx

# yum install mailx

測試能否發送郵件:# echo "zabbix test mail " |mail -s "zabbix-title" [email protected]

3、安裝dos2unix(用於解決郵件內容變成附件的問題) # yum install dos2unix 4、創建報警媒介
  • {ALERT.SENDTO}
  • {ALERT.SUBJECT}
  • {ALERT.MESSAGE}
技術分享 5、配置用戶報警媒介 註:多個收件人用空格分隔 技術分享 6、配置動作 【故障】服務器:{HOST.IP}發生: {TRIGGER.NAME}故障! 告警級別:{TRIGGER.SEVERITY} 告警主機:{HOST.HOST} 告警IP:{HOST.IP} 告警時間:{EVENT.DATE} {EVENT.TIME} 告警內容:{TRIGGER.NAME} 當前值:{ITEM.VALUE} 告警狀態:{TRIGGER.STATUS} 告警ID:{EVENT.ID} 【恢復】服務器:{HOST.IP}: {TRIGGER.NAME}已恢復! 告警級別:{TRIGGER.SEVERITY} 告警主機:{HOST.HOST} 告警IP:{HOST.IP} 恢復時間:{EVENT.RECOVERY.TIME} 告警內容:{TRIGGER.NAME} 當前值:{ITEM.VALUE} 告警狀態:{TRIGGER.STATUS} 告警ID:{EVENT.ID} 技術分享

二、微信告警

配置微信報警 1、註冊微信企業號 https://qy.weixin.qq.com/ 2、登錄企業號 3、創建部門 4、腳本
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib,urllib2,json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

class WeChat(object):
        __token_id = ‘‘
        # init attribute
        def __init__(self,url):
                self.
__url = url.rstrip(/) self.__corpid = XXXXXXXX self.__secret = XXXXXXXXXXXXXXXXX # Get TokenID def authID(self): params = {corpid:self.__corpid, corpsecret:self.__secret} data = urllib.urlencode(params) content = self.getToken(data) try: self.__token_id = content[access_token] # print content[‘access_token‘] except KeyError: raise KeyError # Establish a connection def getToken(self,data,url_prefix=/): url = self.__url + url_prefix + gettoken? try: response = urllib2.Request(url + data) except KeyError: raise KeyError result = urllib2.urlopen(response) content = json.loads(result.read()) return content # Get sendmessage url def postData(self,data,url_prefix=/): url = self.__url + url_prefix + message/send?access_token=%s % self.__token_id request = urllib2.Request(url,data) try: result = urllib2.urlopen(request) except urllib2.HTTPError as e: if hasattr(e,reason): print reason,e.reason elif hasattr(e,code): print code,e.code return 0 else: content = json.loads(result.read()) result.close() return content # send message def sendMessage(self,touser,message): self.authID() data = json.dumps({ touser:touser, toparty:"1",  #部門ID msgtype:"text",   agentid:"1",  #應用ID text:{ content:message }, safe:"0" },ensure_ascii=False) response = self.postData(data) print response if __name__ == __main__: a = WeChat(https://qyapi.weixin.qq.com/cgi-bin) a.sendMessage(sys.argv[1],sys.argv[3])

5、創建報警媒介類型

  • corpid
  • secret
  • {ALERT.MESSAGE}

技術分享

6、創建報警媒介

技術分享

7、配置動作

【故障】服務器:{HOST.IP}發生: {TRIGGER.NAME}故障! 告警級別:{TRIGGER.SEVERITY} 告警主機:{HOST.HOST} 告警IP:{HOST.IP} 告警時間:{EVENT.DATE} {EVENT.TIME} 告警內容:{TRIGGER.NAME} 當前值:{ITEM.VALUE} 告警狀態:{TRIGGER.STATUS} 告警ID:{EVENT.ID} 【恢復】服務器:{HOST.IP}: {TRIGGER.NAME}已恢復! 告警級別:{TRIGGER.SEVERITY} 告警主機:{HOST.HOST} 告警IP:{HOST.IP} 恢復時間:{EVENT.RECOVERY.TIME} 告警內容:{TRIGGER.NAME} 當前值:{ITEM.VALUE} 告警狀態:{TRIGGER.STATUS} 告警ID:{EVENT.ID} 技術分享

zabbix配置