Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Zabbix4.0 (3) Nail, email, Wechat alarm

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

One, zabbix mail alarm setting

Principle: after violating the alarm, you can configure the corresponding action, in the action can send e-mail, Wechat, nails, text messages and so on.

For the location of the alarm script, please see configuration / usr/local/zabbix/etc/zabbix_server.conf

LogFile=/usr/local/zabbix/zabbix_server.logDBHost=127.0.0.1DBName=zabbixDBUser=zabbixDBPassword=zabbixpwdDBPort=3306Timeout=30AlertScriptsPath=/usr/local/zabbix/alertscripts # # trigger script placement location ExternalScripts=/usr/local/zabbix/externalscriptsLogSlowQueries=3000

Python email sending script

/ usrLocalGap ZabixActur alertScriptsZabbixAccordsendmail.pymail.pymail.UsrLocalAccording to QQpassword='mypassword' #-*-coding: utf-8-*-from email.mime.text import MIMETextfrom email.header import Headerfrom smtplib import SMTP_SSLimport syssmtpaddr = 'smtp.qq.com'myemail='327627448@qq.com' # # fill in the authorization code for logging in to the client In the mailbox, you need to open the POP3,IMAP service # f = open ('/ usr/local/zabbix/.passwd','r') # password = f.readline (). Strip () recvmail=sys.argv [1] # # send mail to whom subject=sys.argv [2] # # send subject content=sys.argv [3] # # send content msg = MIMEText ("% s"% (content)," plain " "utf-8") # # content sent msg ['Subject'] = Header (subject,' utf-8'). Encode () msg ['From'] = myemailmsg [' To'] = recvmailtry: 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 execution permissions Test whether the script is running properly and can send email / usr/local/zabbix/alertscripts/zabbix_sendmail.py 1065236572@qq.com 'zabbix disk'' content: disk > 90%'

2. Set up mail alarm in the alarm media.

The name of the script, notice that it is placed in the / usr/local/zabbix/alertscripts directory, adding three accepted parameters, to whom, what the theme is, and what the content is. This place needs to add three parameters.

{ALERT.SENDTO}

{ALERT.SUBJECT}

{ALERT.MESSAGE}

Add user media settings that need to be sent

Send executed script, to whom, time, level, there are multiple separated by commas

Create a trigger action in the template or host

Set the action after the trigger is triggered (action)

The content user sent in the problem, the script executed

After the fault recovery, the action sends a recovered email.

Second, alarm content customization and alarm suppression

Add the custom content parameter host.name host ip1 to the alarm content

The operation time after the Default operation step duration is triggered is suppressed by the following steps, for example, 30s is set above.

Step 4-4, then send an alarm mail after 4 times step 4: 302 minutes.

Third, Wechat alarm

Register to create WeCom

Add self-built applications in the management background

Fill in the script to get WeCom's agentid and secrct after creation.

Add Wechat alarm script at the same location as email

Vim / usr coding coding: utf-8-*-import jsonimport sysimport urllib Urllib2agentid = 'xxx' # Wechat self-built App IDcorpid =' xxx' # Wechat Enterprise ID View corpsecret= 'xxxx' # Wechat self-built app secret#get tockengettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid +' & corpsecret=' + corpsecrettoken_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] # contentpost_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_tokenresponse = urllib2.urlopen (url) Json_content) print (response.read (). Decode ('utf-8')) # # Test / usr/local/zabbix/alertscripts/zabbix_wx.py' address book Wechat name''disk is not enough'

Add content that creates 2 variables to send to whom

Action after the alarm (pay attention to changing the name of the script sent)

When adding user, fill in the sending name and alarm level.

Four: nail alarm

The nail alarm is similar to the Wechat alarm.

Alarm script / usr/local/zabbix/alertscripts/

# alarm robot script # install pip, install the request module yum-y install epel-releaseyum-y install python-pippip install requestsrequests, install USR binbinv pythonimport jsonimport requestsimport sysdef 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.textif _ _ name__ = ='_ main__': msg = sys.argv [1] remiders = [] url = 'alarm robot webrooturl' print (send_msg (url, remiders, msg))

Relevant configuration parameters (send users to fill in admin)

Nailing work notification alarm script

#! / usr/bin/python#-*-coding: utf-8-*-# curl 'https://oapi.dingtalk.com/gettoken?corpid=xxx&corpsecret=xxx'import json,urllib2 Sysappkey = 'nail self-built app view' appsecret = 'nail self-built app view' agentid = nail self-built application view 'touser = sys.argv [1] content = sys.argv [2] tockenurl =' https://oapi.dingtalk.com/gettoken?corpid=' + appkey + "& corpsecret= + appsecrettockenresponse = urllib2.urlopen (tockenurl) tockenresult = json.loads (tockenresponse.read (). Decode ('utf-8')) tocken = tockenresult [' access_token'] sendurl = 'https: / / oapi.dingtalk.com/message/send?access_token=' + tockenheaders = {'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 nailing user ID 'disk > 90%'

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report