Zabbix4.0 Pins, Mail, WeChat Alarm

Keywords: Linux Zabbix JSON Python pip

1. zabbix Mail Alarm Settings
Principle: After breaking the alarm, you can configure corresponding actions, in which you can send mail, WeChat, pins, text messages, etc.
Place the alert script in Configuration/usr/local/zabbix/etc/zabbix_server.conf

LogFile=/usr/local/zabbix/zabbix_server.log
DBHost=127.0.0.1
DBName=zabbix
DBUser=zabbix
DBPassword=zabbixpwd
DBPort=3306
Timeout=30
AlertScriptsPath=/usr/local/zabbix/alertscripts  ##Trigger script placement
ExternalScripts=/usr/local/zabbix/externalscripts
LogSlowQueries=3000

python mailing script

/usr/local/zabbix/alertscripts/zabbix_sendmail.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
import sys

smtpaddr = 'smtp.qq.com'
myemail='327627448@qq.com'     ##QQ to Send Mail
password='mypassword'         ##Fill in the authorization number of the landing client, generate in the mailbox and need to open POP3,IMAP service
#f = open('/usr/local/zabbix/.passwd','r')
#password = f.readline().strip()

recvmail=sys.argv[1]   ##To whom to send mail
subject=sys.argv[2]    ## Subject Sent
content=sys.argv[3]   ##Sent Content

msg = MIMEText("""%s"""%(content), "plain", "utf-8")    ##Sent Content
msg['Subject'] = Header(subject, 'utf-8').encode()
msg['From'] = myemail
msg['To'] =  recvmail

try:
  smtp = SMTP_SSL( smtpaddr )
  smtp.login(myemail, password)
  smtp.sendmail(myemail, recvmail.split(','), msg.as_string()) 
  smtp.quit()
  print("success")
except Exception as e:
  print("fail: "+str(e))
    ##Increase execute privileges to test whether the script is working properly to send mail
    /usr/local/zabbix/alertscripts/zabbix_sendmail.py 1065236572@qq.com 'zabbix disk' 'content: disk > 90%'   

2, alarm media set up mail alarm

The name of the script placement, note that it is placed in the / usr/local/zabbix/alertscripts directory, add three accepted parameters, who to send, what the subject is, what the content is and where three parameters need to be added
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}

Add User Media Settings to Send

Send executed scripts, to whom, time, level, multiple separated by commas

Create trigger action in template or host

Set the action after the trigger is triggered
Content user sent in question, script executed

Action Sends a Recovered Mail After Recovery


2. Customization of warning content and warning suppression
Add custom content parameter host.name host ip1 to alert content
The operation time after the Default operation step duration trigger is suppressed by following steps, such as setting 30s above
Steps 4-4 Then send an alert mail after 4 steps 4*302 minutes


Third, WeChat Warning
Register to Create Enterprise WeChat
Add self-built applications in the management background

Get Enterprise WeChat's agentid and secrct after creation in script

Add the WeChat alarm script in the same location as the mail

vim /usr/local/zabbix/alertscripts/zabbix_wx.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import sys
import urllib,urllib2

agentid = 'xxx'     ###WeChat Self-Built Application ID
corpid = 'xxx'       ###WeChat Enterprise ID View in My Enterprise
corpsecret = 'xxx'    ### WeChat Self-Built Application secret

#get tocken
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
token_file = urllib2.urlopen(gettoken_url)
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
my_token = token_json['access_token']

#send wechart 
touser=sys.argv[1]  #many user: 'zhangsan|wangwu'
content=sys.argv[2] #content
post_content = {
        "touser":touser,
        "agentid":agentid,
        "msgtype": "text",
        "text":{
                "content":content,
        }
}
json_content = json.dumps(post_content)
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + my_token
response = urllib2.urlopen(url,json_content)
print(response.read().decode('utf-8'))
##test
/usr/local/zabbix/alertscripts/zabbix_wx.py 'WeChat Name in Address Book' 'disk is not enough'

Add content that creates two variables to whom to send

Action after alarm (note changing script name sent)

user adds attention to fill in the send name and alert level

Fourth, pin warning
Pin warning is similar to WeChat warning
Alert script/usr/local/zabbix/alertscripts/

###Warning Robot Script
###Install pip, install request module
yum -y install epel-release
yum -y install python-pip
pip install requests
#!/usr/bin/env python
import json
import requests
import sys

def send_msg(url, remiders, msg):
    headers = {'Content-Type': 'application/json; charset=utf-8'}
    data = {
        "msgtype": "text",
        "at": {
            "atMobiles": remiders,
            "isAtAll": False,
        },
        "text": {
            "content": msg,
        }
    }
    r = requests.post(url, data=json.dumps(data), headers=headers)
    return r.text

if __name__ == '__main__':
    msg = sys.argv[1]
    remiders = []
    url = 'Warning Robot webrooturl'
    print(send_msg(url, remiders, msg))

Related configuration parameters (send the user to fill in admin)

Nail Work Notification Warning Script

#!/usr/bin/python
# -*- coding: utf-8 -*-
#curl 'https://oapi.dingtalk.com/gettoken?corpid=xxx&corpsecret=xxx'
import json,urllib2,sys

appkey = 'Nail Build-in Application View'
appsecret = 'Nail Build-in Application View'
agentid = Nail Build-in Application View'
touser = sys.argv[1]
content = sys.argv[2]

tockenurl = 'https://oapi.dingtalk.com/gettoken?corpid=' + appkey + "&corpsecret=" + appsecret
tockenresponse = urllib2.urlopen(tockenurl)
tockenresult = json.loads(tockenresponse.read().decode('utf-8'))
tocken =  tockenresult['access_token']

sendurl = 'https://oapi.dingtalk.com/message/send?access_token=' + tocken
headers = {
        'Content-Type':'application/json'
}
main_content = {
        "touser": touser,
        "toparty": "",
        "agentid": agentid,
        "msgtype": "text",
        "text": {
                "content": content
        }
}
main_content = json.dumps(main_content)
req = urllib2.Request(sendurl,headers=headers)
response = urllib2.urlopen(req, main_content.encode('utf8'))
print(response.read().decode('utf-8'))
##test
/usr/local/zabbix/alertscripts/zabbix_dd.py Fill in staple users ID 'disk > 90%'

Posted by newbiez on Mon, 23 Dec 2019 13:09:36 -0800