Linux-Zabbix mail alarm settings

Email script alarm

Official zabbix script documentation

https://www.zabbix.com/documentation/4.0/zh/manual/

Case background

Enterprises use centos6, zabbix2.*, zabbix does not have an email alarm media module

1. Close the mail service on zabbix-server, install mailx

[root@zabbix-server ~]# systemctl stop postfix.service 
[root@zabbix-server ~]# systemctl disable postfix.service 
Removed symlink /etc/systemd/system/multi-user.target.wants/postfix.service.
[root@zabbix-server ~]# yum -y install mailx 

2. Edit the configuration of mailx, specify the recipient mailbox and user password

[root@zabbix-server ~]# vim /etc/mail.rc 
......
set [email protected]
set smtp=smtps://smtp.qq.com:465
set [email protected]
set smtp-auth-password=ljzxbtttimmbdgdh
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/

3. Test mail

[root@zabbix-server ~]# echo 欢迎使用! | mail -s 请柬  [email protected]
[root@zabbix-server ~]# Error in certificate: Peer's certificate issuer has been marked as not trusted by the.


4. Log in to qq mailbox to view

Insert picture description here

5.zabbix-server writes email alarm scripts

[root@zabbix-server ~]# yum -y install  dos2unix   //安装工具
[root@zabbix-server ~]# cd /usr/lib/zabbix/alertscripts/
[root@zabbix-server alertscripts]# vim email_notice.sh
#!/bin/bash
#export UTF-8
FILE=/tmp/mail.txt
echo "$3" > $FILE
dos2unix -k $FILE
/bin/mail -s "$2" $1 < $FILE
[root@zabbix-server alertscripts]# chmod +x email_notice.sh  //赋予执行权限

6.zabbix web console add email alert script

(1) Management-Alarm Media Type-Close the existing email alarm-"Create Media Type" in the upper right corner-"Name, Type (Script)-Script Name (must be written correctly), add 3 parameters: {ALERT.SENDTO} , {ALERT.SUBJECT}, {ALERT.MESSAGE}"-update

(2) Upper right corner-villain (user)-alarm medium-add-"type (mail script, recipient: real email address)"-deactivate existing-update

7. Log in to qq mailbox to view

Email script alarm is set successfully

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-xIVfsvZs-1594728558628) (C:\Users\Administrator\Desktop\photo\zabbix\email alarm settings\2. png)]

WeChat Alarm (Enterprise WeChat)

1. Apply for corporate WeChat (steps omitted)

2. Create an application (alarm robot)

Application management-create application-upload logo-create application name (alarm robot)-application introduction (alarm reminder)-set courseware scope-create application

3. Copy application data

Click to copy the alarm robot

AgentId 1000002

Secret EKD_UTFTQSElUbTv36gPsArEkgAPQUAkgakQckPSz0E

4...Write the python WeChat alarm script, and modify the company id and user password

Change enterprise ID, alarm robot password, alarm robot ID

[root@zabbix-server alertscripts]# [root@zabbix-server ~]# cd /usr/lib/zabbix/alertscripts/
[root@zabbix-server alertscripts]# vim weixin.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import requests
import sys
import os
import json
import logging
 
 
logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S',
filename = os.path.join('/tmp','weixin.log'),
filemode = 'a')
  
corpid='ww367a17810d625bee'      //企业微信的ID号
appsecret='EKD_UTFTQSElUbTv36gPsArEkgAPQUAkgakQckPSz0E'	    // 报警机器人的密码(上面复制的数据)
agentid=1000002    //报警机器人的ID号(上面复制的数据)
#获取accesstoken
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']
 
#发送消息
msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
 
touser=sys.argv[1]
subject=sys.argv[2]
#toparty='3|4|5|6'
message=sys.argv[2] + "\n\n" +sys.argv[3]
 
params={
"touser": touser,
# "toparty": toparty,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": message
},
"safe":0
}
 
req=requests.post(msgsend_url, data=json.dumps(params))
logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message) 
[root@zabbix-server alertscripts]# chmod 777 weixin.py     //赋予执行权
[root@zabbix-server alertscripts]# dos2unix -k winxin.py    //格式转换
[root@zabbix-server alertscripts]# ./weixin.py  //执行完 不管出现什么提示或者报错都不用管 直接往下做
[root@zabbix-server alertscripts]# chown zabbix:zabbix /tmp/weixin.log   ///赋权weixin.log

5. Test sending WeChat

[root@zabbix-server alertscripts]# ./weixin.py WangFuPing 报错  HTTP...

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-SxtvHhBE-1594728558630) (C:\Users\Administrator\Desktop\photo\zabbix\email alarm settings\3. png)]

6.zabbix web add WeChat alarm

(1) Management-Alarm Media Type-"Create Media Type" in the upper right corner-"Name (Enterprise WeChat), Type (Script), Script Name (must be written correctly), add 3 parameters: {ALERT.SENDTO}, {ALERT .SUBJECT}, {ALERT.MESSAGE}"-update

(2) Upper right corner-villain (user)-alarm media-"type (enterprise WeChat, recipient: WeChat account)"-add-disable existing account-update

(WeChat alarm setting is successful)

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-CkmhGjlc-1594728558633) (C:\Users\Administrator\Desktop\photo\zabbix\email alarm settings\4. png)]

Expansion (modify email notification content)

1. Configuration-Action-Report problems to Zabbix administrators

Corresponding content explanation

Operation: modify the default title, modify the content of the message
Subject: Problem:

  • {EVENT.NAME} failure
  • {TRIGGER.STATUS} server
  • {HOSTNAME1} happened:
  • {TRIGGER.NAME} failure!

content:

  • Alarm host: {HOSTNAME1}
  • Alarm time: {EVENT.DATE} {EVENT.TIME}
  • Warning level: {TRIGGER.SEVERITY}
  • Warning information: {TRIGGER.NAME}
  • Alarm item: {TRIGGER.KEY1}
  • Question details: {ITEM.NAME}: {ITEM.VALUE}
  • Current status: {TRIGGER.STATUS}: {ITEM.VALUE1}
  • Event ID: {EVENT.ID}

Recovery operation: modify the default title, modify the content of the message
Recovery topic: recovery {TRIGGER.STATUS}, server: {HOSTNAME1}: {TRIGGER.NAME} has been recovered!
Recovery information:

  • Alarm host: {HOSTNAME1}
  • Alarm time: {EVENT.DATE} {EVENT.TIME}
  • Warning level: {TRIGGER.SEVERITY}
  • Warning information: {TRIGGER.NAME}
  • Alarm item: {TRIGGER.KEY1}
  • Question details: {ITEM.NAME}: {ITEM.VALUE}
  • Current status: {TRIGGER.STATUS}: {ITEM.VALUE1}
  • Event ID: {EVENT.ID}

ENT.DATE} {EVENT.TIME}

  • Warning level: {TRIGGER.SEVERITY}
  • Warning information: {TRIGGER.NAME}
  • Alarm item: {TRIGGER.KEY1}
  • Question details: {ITEM.NAME}: {ITEM.VALUE}
  • Current status: {TRIGGER.STATUS}: {ITEM.VALUE1}
  • Event ID: {EVENT.ID}

2. After modification, update

Guess you like

Origin blog.csdn.net/weixin_45191791/article/details/107346422