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

Zabbix mail alarm and nail alarm configuration

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Smtp email Alert

Zabbix comes with email alerts by default, but it's not easy to use.

Generally, we use smtp alarm email, configure the alarm email script, send it to the smtp mailbox server for forwarding, and then send it to the alarm user's mailbox to complete the email alarm, such as Ali mailbox, Tencent mailbox and so on.

The premise is that zabbix can access the public network, and the stmp mailbox is fully configured.

Use the python script as the message. The zabbix alarm script directory is under the / usr/lib/zabbix/alertscripts/ path by default.

#! / usr/bin/python#coding:utf-8import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerfrom email.utils import parseaddr, formataddrimport sysdef formatAddr (s): name, addr = parseaddr (s) return formataddr ((Header (name, 'utf-8'). Encode (), addr)) def send_mail (to_list,subject Content): mail_host = 'smtp.mxhichina.com' # smtp mail server address mail_user =' alert.xxxx@chucloud.com.cn' # smtp alarm mailbox account mail_pass = 'xxxxxxxxx' # smtp alarm email password # modify msg = MIMEText (content,'plain') according to your actual situation 'utf-8') msg [' Subject'] = Header (subject, 'utf-8'). Encode () msg [' From'] = formatAddr ('zabbix Monitoring'% mail_user). Encode () msg ['to'] = to_list try: s = smtplib.SMTP () s.connect (mail_host) s.login (mail_user,mail_pass) s.sendmail (mail_user,to_list) Msg.as_string () s.close () return True except Exception,e: print str (e) return Falseif _ _ name__ = "_ _ main__": send_mail (sys.argv [1], sys.argv [2], sys.argv [3]) # examples:. / sendmail_zabbix.py yangchao@chucloud.com.cn test

Script ideas:

1. Use the smtp module and refer to the official module manual.

2. Construct the send_mail function, which uses three parameters to specify the sender, title and content of the email.

3. The send_mail function defines the account password information of smtp mailbox.

3. Use the os method of sys module to pass parameters for testing.

Save the script name to sendmail_zabbix.py

In zabbix "Administration-alarm Media Type", create an alert mail script named sendmial_zabbix.py

The configuration is as follows

Then configure the action

The action information is configured as follows

The specific information format is as follows, which can be customized.

Alarm Host: {HOSTNAME1} alarm time: {EVENT.DATE} {EVENT.TIME} alarm level: {TRIGGER.SEVERITY} alarm Information: {TRIGGER.NAME} alarm Project: {TRIGGER.KEY1} question details: {ITEM.NAME}: {ITEM.VALUE} current status: {TRIGGER.STATUS}: {ITEM.VALUE1} event ID: {EVENT.ID}

Then assign alarm media to zabbix users who need to receive email alerts

Then you can receive an email alarm in the middle of the night every day.

Still sleeping? Hey, get up and work!

Configure nail alarm

The biggest problem of email alarm is that there are too many emails, so it is difficult to judge in time.

The best way is to nail the alarm.

First of all, you need nails.

Secondly, you have to configure a robot.

You can ask Baidu or see how to configure the robot for detailed nails.

Https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq

Here is the robot I added, the key point is webhook, each robot is unique

Here we still use the python script and save it under the usr/lib/zabbix/alertscripts/ path.

#! / usr/bin/python#-*-coding: utf-8-*-# use the nail alarm robot on zabbix and call the api script import requestsimport jsonimport sysimport osheaders = {'Content-Type':' application/json Charset=utf-8'} # define json http header information api_url = "https://oapi.dingtalk.com/robot/send?access_token=b5924a81e21921256eb22154b7614512a2ca6e4815bda7de2fed0b59bf03cf89"# where url is the api urldef msg (text) of the nailing robot: # define the function msg, and the parameter is txt json_text= {# define the json_text dictionary content" msgtype ":" text " "text": {"content": text},} print requests.post (api_url,json.dumps (json_text), headers=headers). Content # serializes dictionary information into json format to send if _ _ name__ ='_ main__': # main function text = sys.argv [1] # pass in zabbix to define action parameters Msg (text) # has been configured in the zabbix alarm action to call the msg (text) function

Save the name as dingding.py. The script is complete.

First, among the zabbix users, create a user that is used as a nailing robot message to send

Then create the script for the alarm media type

Configure as follows

The parameter format is

{ALERT.MESSAGE}

Then configure the action to define the alarm method to be sent.

The detailed configuration is as follows

Define alarm level

Define operation

The information content is recommended to be configured as follows

Alarm Host: {HOST.NAME} alarm address: {HOST.IP} Monitoring Project: {ITEM.NAME} Monitoring value: {ITEM.LASTVALUE} alarm level: {TRIGGER.SEVERITY} current status: {TRIGGER.STATUS} alarm Information: {TRIGGER.NAME} alarm time: {EVENT.DATE} {EVENT.TIME} event ID: {EVENT.ID}

All right, now you can see the nail alarm.

Nothing nails you to work every day.

Really TM is the boss who likes me, but the boss doesn't like me to be laid off.

Sometimes, I have nothing to do with my boss.

Isn't this AI?

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