Zabbix Monitoring Client and Mail, WeChat Alarm

The zabbix server-side deployment is available for reference: https://blog.51cto.com/14227204/2474670
Note: This article is based on the above link

1. Install Zabbix agent side
Here I started a server with IP 192.168.171.134 to act as an agent side

[root@agent /]# mkdir /zabbix          # Personal habits, negligible
[root@agent /]# cd /zabbix/
[root@agent /]# rz
[root@agent zabbix]# tar zxf zabbix-3.2.1.tar.gz 
[root@agent zabbix]# cd zabbix-3.2.1/
[root@agent zabbix-3.2.1]# useradd -M -s /sbin/nologin  zabbix
[root@agent zabbix-3.2.1]# ./configure --prefix=/usr/local/zabbix --enable-agent && make && make install
[root@agent zabbix-3.2.1]# cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
[root@agent /]# sed -i 's/BASEDIR=\/usr\/local/BASEDIR=\/usr\/local\/zabbix/g' /etc/init.d/zabbix_agentd 
[root@agent /]# sed -i 's/PIDFILE=\/tmp\/$BINARY_NAME.pid/PIDFILE=$BASEDIR\/logs\/$BINARY_NAME.pid/g' /etc/init.d/zabbix_agentd 
[root@agent /]# chmod +x /etc/init.d/zabbix_agentd
[root@agent /]# sed -i 's/# PidFile=\/tmp\/zabbix_agentd.pid/PidFile=\/usr\/local\/zabbix\/logs\/zabbix_agentd.pid/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/LogFile=\/tmp\/zabbix_agentd.log/LogFile=\/usr\/local\/zabbix\/logs\/zabbix_agentd.log/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/Server=127.0.0.1/Server=192.168.171.133/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# ListenPort=10050/ListenPort=10050/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/ServerActive=127.0.0.1/ServerActive=192.168.171.133/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/Hostname=Zabbix server/Hostname=192.168.171.134/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# Timeout=3/Timeout=15/g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# sed -i 's/# Include=\/usr\/local\/etc\/zabbix_agentd.conf.d\//Include=\/usr\/local\/zabbix\/etc\/zabbix_agentd.conf.d\//g' /usr/local/zabbix/etc/zabbix_agentd.conf
[root@agent /]# egrep -v '^$|^#' /usr/local/zabbix/etc/zabbix_agentd.conf 
PidFile=/usr/local/zabbix/logs/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
Server=192.168.171.133
ListenPort=10050
ServerActive=192.168.171.133
Hostname=192.168.171.134
Timeout=15
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
[root@agent /]# mkdir -p /usr/local/zabbix/logs       # Create log storage path
[root@agent /]# chown -R zabbix:zabbix /usr/local/zabbix/     # Grant privileges
[root@agent /]# /etc/init.d/zabbix_agentd start           # Start agent
Reloading systemd:                                         [  OK  ]
Starting zabbix_agentd (via systemctl):                    [  OK  ]

2. Log on to the web interface to add an agent host
Log on to the Zabbix server side to monitor the agent side you just configured






3. Zabbix Monitoring MySQL Database
The following can be done on the newly configured agent side

#Copy MySQL monitoring script provided by Zabbix
[root@agent /]# cd /zabbix/zabbix-3.2.1/conf/zabbix_agentd/
[root@agent zabbix_agentd]# cp userparameter_mysql.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/
[root@agent zabbix_agentd]# cd /usr/local/zabbix/etc/
[root@agent etc]# cat > .my.cnf << EOF
> [client]
> host=192.168.171.134
> user=test
> password=123.com
> socket=/usr/local/mysql/mysql.sock
> EOF
[root@agent etc]# cd /usr/local/zabbix/etc/zabbix_agentd.conf.d/
[root@agent zabbix_agentd.conf.d]# sed -i 's/mysql -N/\/usr\/local\/bin\/mysql -N/g' userparameter_mysql.conf 
[root@agent zabbix_agentd.conf.d]# sed -i 's/mysql -V/\/usr\/local\/bin\/mysql -V/g' userparameter_mysql.conf 
[root@agent zabbix_agentd.conf.d]# sed -i 's/mysqladmin ping/\/usr\/local\/bin\/mysqladmin ping/g' userparameter_mysql.conf
[root@agent zabbix_agentd.conf.d]# sed -i 's/HOME=\/var\/lib\/zabbix/HOME=\/usr\/local\/zabbix\/etc\//g' userparameter_mysql.conf 
[root@agent zabbix_agentd.conf.d]# /etc/init.d/zabbix_agentd restart
Restarting zabbix_agentd (via systemctl):                  [  OK  ]
[root@agent /]# mysql -u root -p123
mysql> grant all on *.* to test@'192.168.171.%' identified by '123.com';

The Zabbix-servre side tests whether MySQL status can be collected on the agent side:

[root@zabbix /]# zabbix_get -s 192.168.171.134 -p 10050 -k "mysql.status[Uptime]"
643
#The number you see when you execute the above command on the server side is the database-related state

Go back to Zabbix's web page and add the MySQL database monitoring template






4. Configure Mail Alarm
All of the following operations need to be configured on the Zabbix server side

[root@zabbix /]# yum -y install perl-Net-SSLeay perl-IO-Socket-SSL
[root@zabbix ~]# tar zxf /zabbix/sendEmail-v1.56.tar.gz -C /usr/src/
[root@zabbix ~]# cp /usr/src/sendEmail-v1.56/sendEmail /usr/local/bin/
[root@zabbix ~]# chown zabbix:zabbix /usr/local/bin/sendEmail 
[root@zabbix ~]# vim /usr/local/bin/sendEmail 
if (! IO::Socket::SSL->start_SSL($SERVER)) {
#As of line 1906, change to the following

Next comes the configuration on your mailbox:



When an authorization number is generated, a secure comrade will ask you to send a text message to confirm your identity

#Configuring on the zabbix-server side 
[root@zabbix /]# cd /usr/local/zabbix/share/zabbix/alertscripts/
[root@zabbix alertscripts]# vim SendEmail.sh
#!/bin/bash
to=$1
subject=$2
body=$3
/usr/local/bin/sendEmail -o tls=auto -f 848369866@qq.com -t "$to" -s smtp.qq.com -u "$subject" -o message-content-type=html -o message-charset=utf8 -xu 848369866@qq.com -xp wrsxyidhdokobdah -m "$body"
#The explanations are as follows:
# /usr/local/bin/sendEmail: Command main program;
# -f from@126.com: sender's mailbox
# -t to@126.com: recipient mailbox
# -s smtp.126.com: SMTP server for sender's mailbox
# -u "I am the subject of the message": the title of the message 
#-o message-content-type=html: The format of the message's content, which means it is in HTML format
# -o message-charset=utf8: mail content encoding
# -xu from@126.com: username of sender's mailbox
# -xp WEE78@12l$ #Sender's mailbox authorization number (the one I just generated)
# -m "I am the content of the message": the specific content of the message
#Test if mail can be sent successfully
[root@zabbix alertscripts]# sh SendEmail.sh 848369866@qq.com test 123456
Mar 06 18:04:11 zabbix sendEmail[4604]: Email was sent successfully!


Back to the web interface, add an alarm medium



Associate alarm media with users




Create a trigger action






Configure triggers



Alarm test:
Shutting down the zabbix_agent process on the agent-side server and letting the mailbox receive mail indicates correct configuration.

#Do the following on the agent server
[root@agent ~]# /etc/init.d/zabbix_agentd stop    #Close agent process

Here we can see that the mailbox has received an alarm message:

5. Configure enterprise WeChat alarm
1. Create an application:


When the application is successfully created, the application information is displayed as follows:

The Agent and Secret for this application are highlighted above for use in subsequent configurations.

View information such as enterprise ID, Department ID, user account, etc.

Enterprise ID:

Department ID:

User Account:

All of the above information will be used later

[root@zabbix /]# cd /usr/local/zabbix/share/zabbix/alertscripts/
[root@zabbix alertscripts]# wget https://www.ixdba.net/zabbix/weixin_linux_amd64
[root@zabbix alertscripts]# mv weixin_linux_amd64 weixin
[root@zabbix alertscripts]# chmod 755 weixin         #Give Execution Permission 
[root@zabbix alertscripts]# echo "AlertScriptsPath=/usr/local/zabbix/share/zabbix/alertscripts" >> /usr/local/zabbix/etc/zabbix_server.conf                
#Append the above to the main Zabbix configuration file
#To test whether the script is available, execute the following command (you need to modify the values):
./weixin --corpid=ww4b98220b16519a8d --corpsecret=oXQVxXF3TzBK1DyJuKHRJb90MIkiCcpVvF7WfaaCx0g --msg="tiny.Alert test"--user=ZhangYaZhuang --agentid=1000003
{"errcode":0,"errmsg":"ok","invaliduser":""}
#What needs to be modified is as follows:
# corpid=the enterprise ID in the enterprise number;
# corpsecret: Here's the ID of the Secret in our own app;
# Agentid: We build AgentIds inside our own applications;
# msg: the content of the message to be sent;
# User: The user account where we receive the message, note that it is the account on the member details page inside the WeChat enterprise number;

After execution, if there is no error, then we can receive the warning information of this command on the enterprise micro-signal, as follows:

Log on to the web interface to create a WeChat alarm medium


Associate with Zabbix administrative users:


Note the recipient settings above. This recipient is the account you see in the details of the members below the address book in the background of WeChat Enterprise Number. Make sure you don't write it wrong

Configure Actions


Server: {HOST.NAME} Occurred: {TRIGGER.NAME} Failure

Alert Host: {HOST.NAME}
Alert address: {HOST.IP}
Monitoring project: {ITEM.NAME}
Monitoring value: {ITEM.LASTVALUE}
Alert level: {TRIGGER.SEVERITY}
Current status: {TRIGGER.STATUS}
Alert information: {TRIGGER.NAME}
Warning time: {EVENT.DATE} {EVENT.TIME}
Event ID:{EVENT.ID}


Add it as follows:

Configure recovery operations:


Test the warning function of WeChat:

#Do the following on the agent to simulate the failure
[root@agent ~]# /etc/init.d/zabbix_agentd stop

When the agent end is closed, the following information will be received later on by Enterprise WeChat (mailbox will also receive):

Next, start the agent side and test the recovery operation:

Tags: Zabbix MySQL socket mysqladmin

Posted on Fri, 06 Mar 2020 10:38:41 -0800 by PDP11