Use public Internet mailbox to send-Zabbix-alarm email

Brief introduction to the
mail system : The email system includes two components: MUA (Mail User Agent) and MTA (Mail Transport Agent, postfix) MUA is an interface provided by the mail system for users to read and write mail The MTA is a program that runs at the bottom and can handle the sending and receiving of mail.

Mail and mailx are the MUAs responsible for viewing, writing and sending mail to the MTA. mailx is an enhanced version of mail.

  1. Register 163 mailbox
  2. Login webpage mailbox to set client authorization password

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-1KSUgCHr-1585731397653) (https://upload-images.jianshu.io/upload_images/21294643-ab2d3aa031c3d2aa.png ? imageMogr2 / auto-orient / strip% 7CimageView2 / 2 / w / 1240)]

# 1. Server server side:

#安装MUA软件:mailx
[root@zabbix-server ~]#  yum install mailx -y
[root@zabbix-server ~]# mailx -V 
12.5 7/5/10
注:使用新的方式--利用公网邮件服务器发送报警,需要关闭postfix服务
[root@zabbix-server ~]# systemctl stop postfix

#配置公网邮箱信息
[root@zabbix-server ~]# vim /etc/mail.rc  ---在最后添加如下:
set [email protected](邮箱地址) 
set smtp=smtp.163.com(smtp服务器) 
set [email protected](用户名) 
set smtp-auth-password=qf123456(这里是邮箱的授权密码) 
set smtp-auth=login

#手动发送邮件测试
[root@zabbix-server ~]# mailx -v -s 'hello' '[email protected]'     #手写邮件内容 (回车,然后ctrl+d正常结束)
nihao
EOT
Resolving host smtp.163.com . . . done.
Connecting to 123.126.97.2:smtp . . . connected.
220 163.com Anti-spam GT for Coremail System (163com[20141201])
>>> EHLO zabbix-server
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN 
250-AUTH=LOGIN PLAIN
250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFeF38eUCa0xDrUUUUj
250-STARTTLS
250 8BITMIME
>>> AUTH LOGIN
334 dXNlcm5hbWU6
>>> bHd4MTgzNjYwMTkzNTZAMTYzLmNvbQ==
334 UGFzc3dvcmQ6
>>> bHd4MTgzNjYwMTkzNTY=
235 Authentication successful
>>> MAIL FROM:<[email protected]>
250 Mail OK
>>> RCPT TO:<[email protected]>
250 Mail OK
>>> DATA
354 End data with <CR><LF>.<CR><LF>
>>> .
250 Mail OK queued as smtp2,GtxpCgDXkqTEFERdskSAAA--.825S2 1564742867
>>> QUIT
221 Bye

Manually use mailx to send mail test results:
[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-hUioYKU4-1585731397653) (https://upload-images.jianshu.io /upload_images/21294643-ff858611b6b142de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

# 2, zabbix add mail alarm function:

The configuration of zabbix's email alarm function requires the participation of the following three roles:
1. Warning media (Media): Refers to the method used by zabbix to alarm. Currently, the warning media supported by Zabbix include email, Jabber, and SMS. jabber is an instant messaging tool under Linux.
2. Triggers: when the monitored object reaches a certain condition or set of conditions, it triggers Zabbix to generate events.
3. Action: It means that Zabbix sends an alarm through the warning media after generating the corresponding event.

### 1. Configuration of warning media:
First, you need to configure the mail function of Zabbix.
Click Manage-> Alarm Media Type-> Create Media Type

##### 1.1 Fill in your alarm media type information on the page, for example as shown in the figure below:
Note: The script name is arbitrary, and it is stored in / usr / lib / zabbix / alertscripts (production test service is in: / usr / local / zabbix / share / zabbix / alertscripts)

名称:sendmail                   //名称任意
类型:脚本
脚本名称:sendmail.sh      
脚本参数:                          //一定要写,否则可能发送不成功
    {ALERT.SENDTO}              //照填,收件人变量
    {ALERT.SUBJECT}             //照填,邮件主题变量,变量值来源于‘动作’中的‘默认接收人’
    {ALERT.MESSAGE}           //照填,邮件正文变量,变量值来源于‘动作’中的‘默认信息’

配置完成后,不要忘记点击存档,保存你的配置。   

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-NUJhXrG2-1585731397654) (https://upload-images.jianshu.io/upload_images/21294643-429554a073606587.png ? imageMogr2 / auto-orient / strip% 7CimageView2 / 2 / w / 1240)]

##### 1.2 Modify zabbix server configuration file & write script: specify the storage path of the script:

[root@zabbix-server ~]# vim /etc/zabbix/zabbix_server.conf
AlertScriptsPath=/usr/lib/zabbix/alertscripts

#编写邮件脚本
[root@zabbix-server ~]# cd /usr/lib/zabbix/alertscripts/
[root@zabbix-server alertscripts]# vim sendmail.sh   
#!/bin/sh 
#export.UTF-8
echo "$3" | sed s/'\r'//g | mailx -s "$2" $1

$1:接受者的邮箱地址:sendto,$2:邮件的主题:subject$,3:邮件内容:message

#修改权限
[root@zabbix-server alertscripts]# chmod u+x sendmail.sh && chown zabbix.zabbix sendmail.sh

##### 1.3 Modify the alarm medium of admin user

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-j9QZJz9h-1585731397654) (https://upload-images.jianshu.io/upload_images/21294643-71e765b28290c3c3.png ? imageMogr2 / auto-orient / strip% 7CimageView2 / 2 / w / 1240)] The
user does not set the alarm medium by default, after setting it, you can receive the alarm message.
Next, set up to accept e-mails from alarm users
Click: Admin-> Alarm Media Type-> Admin-> Alarm Media-> Add
[External link image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image directly Upload (img-7wL5zynF-1585731397654) (https://upload-images.jianshu.io/upload_images/21294643-3166f80b46641fb5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

### 2. Trigger configuration
Next, click on Configuration-> Host-> none1-> Trigger-> Create Trigger
We add a trigger to none1 this host. Click "Trigger" in the row of none1, and then click Create Trigger.
As follows:
Name: Fill in the name of the trigger
Expression: Used to configure the triggering condition of the trigger. Click the Add button to have conditional options. ---- Key value
[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-5B6MjpR7-1585731397654) (https://upload-images.jianshu.io/upload_images /21294643-7a22c966e02a78be.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

Then click the selection on the right side of the project, select the monitoring item "redis status" that we have configured before, and set the trigger threshold
Zabbix will automatically generate an expression. Next, select the severity of the incident according to the situation. After the configuration is complete, click Archive to save.

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-7tQbgwxg-1585731397654) (https://upload-images.jianshu.io/upload_images/21294643-d509f3508b1b5592.png ? imageMogr2 / auto-orient / strip% 7CimageView2 / 2 / w / 1240)]

### 3. Action configuration
Click: Configure-> Action-> Event Source drop-down menu, select Trigger-> Create Action

Name: redis-port

默认信息:邮件的主题
主机: {HOST.NAME1}
时间: {EVENT.DATE} {EVENT.TIME}
级别: {TRIGGER.SEVERITY}
触发: {TRIGGER.NAME}
详情: {ITEM.NAME1}:{ITEM.KEY1}:{ITEM.VALUE1}
状态: {TRIGGER.STATUS}
项目:{TRIGGER.KEY1} 
事件ID:{EVENT.ID}

Recover mail:

恢复主题:
服务器:[{HOSTNAME1}]故障已恢复。故障原因:{TRIGGER.NAME} 
恢复信息:恢复邮件的正文。当故障恢复正常后也发邮件通知一下

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-Pz1AUFCH-1585731397655) (https://upload-images.jianshu.io/upload_images/21294643-614e25013808cae1.png ? imageMogr2 / auto-orient / strip% 7CimageView2 / 2 / w / 1240)]

Click: Operation-> Edit:
[External image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-qeieEx1s-1585731397655) (https://upload-images.jianshu.io/ upload_images / 21294643-68fd8dfe0f659c26.png? imageMogr2 / auto-orient / strip% 7CimageView2 / 2 / w / 1240)]

Sending interval: 60 seconds, step: send 10 times to: admin user, only use: mail alarm to send
[external link image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the picture and upload it directly (img- HDM2cTBD-1585731397655) (https://upload-images.jianshu.io/upload_images/21294643-cbbed4126cb057e6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-lKvlBxHX-1585731397656) (https://upload-images.jianshu.io/upload_images/21294643-c951dd46b6b0c4fd.png ? imageMogr2 / auto-orient / strip% 7CimageView2 / 2 / w / 1240)]

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-mKX8eTq9-1585731397656) (https://upload-images.jianshu.io/upload_images/21294643-7b13106294dbe0f0.png ? imageMogr2 / auto-orient / strip% 7CimageView2 / 2 / w / 1240)]

What needs to be explained is the configuration of the "Steps" section. The so-called steps mean that the alarm can have multiple steps to make different alarms.

  • For example, from 1 to 3, there are three alarm steps. The step duration is that after a certain period of time, if the monitoring personnel has not responded to the alarm, the next alarm step is entered.
  • For example, send an e-mail to alert you, if you do not respond after 60 seconds, then send a jabber message to remind you. If there is no response after 60 seconds, then send a text message to you. If there is no response, there will be no more. You can visually understand it as Zabbix's crying, two troubles and three hangings.
  • At this point, a mail alarm function is configured. If you want to see the result immediately, you can modify the condition of the trigger and stop the redis service. You will receive an alarm email from Zabbix immediately

### 4. Test
Operate on the monitored terminal:

# systemctl stop redis

[External chain image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-FTPJgHuY-1585731397656) (https://upload-images.jianshu.io/upload_images/21294643-c886b559ae38963a.png ? imageMogr2 / auto-orient / strip% 7CimageView2 / 2 / w / 1240)]

Published 92 original articles · praised 0 · visits 1418

Guess you like

Origin blog.csdn.net/Forgetfanhua/article/details/105250011