[Zabbix series] Alarm System Setting and elimination, zabbix series Alarm System
There are many solutions for email alert. Here we choose the msmtp + mutt solution with better stability.
A good solution is that you do not need to build an independent mail server on your own. You can use third-party mail. This method not only effectively solves the problem that unknown email addresses are processed as spam emails, at the same time, the third-party stable services provide accurate and timely alarms.
In fact, the most important thing is that msmtp can use gmail.
We need two components: msmtp and mutt. msmtp is the key to sending emails, and mutt is used to optimize msmtp.
[Msmtp]
Sudo apt-get install-y msmtp
Configure msmtp
Touch/var/log/msmtp. log
Vim ~ /. Msmtprc
Account default
Host smtp.126.com
Port 25
From cl0554@126.com
Auth login
Tls off
User cl0554@126.com
Password xxxxxxx
Logfile/var/log/msmtp. log
Chmod 600. msmtprc
Chown zabbix. zabbix/var/log/zabbix-R
Chown zabbix. zabbix/var/log/msmtp. log
Chmod 777/var/log/msmtp. log
[Mutt]
Install
Sudo apt-get install-y mutt
Sudo mv/etc/Muttrc. back
Sudo vim/etc/Muttrc
Set sendmail = "/usr/bin/msmtp" # path of your msmtp command
Set use_from = yes
Set realname = "cl0554@126.com"
Set editor = "vim"
Test email
Echo "hello zabbix" | mutt-s "zabbix test" chenlong0554@qq.com
[Zabbix configuration]
Modify the path of zabbix script
Sudo vim/etc/zabbix/zabbix_server.conf
AlertScriptsPath =/usr/local/zabbix/scripts/
Restart zabbix
Sudo service zabbix-server restart
Sudo mkdir-p/usr/local/zabbix/scripts/
Sudo chown zabbix. zabbix/usr/local/zabbix-R
Sudo vim/usr/local/zabbix/scripts/mail. sh
#! /Bin/sh
Echo "$3" | mutt-s "$2" $1
Sudo chown zabbix. zabbix mail. sh
Sudo chmod 777 mail. sh
Test script
./Mail. sh chenlong0554@qq.com zabbix test Hello zabbix
The zabbix web interface sets the email address to be sent through the media in the profile in the upper right corner.
Use the email in administrator-Media types on the toolbar to configure the mail sending method.
Type select script
The name is mail. sh.
PS: The web configuration of the alarm system is complicated. It is discussed in detail in the next section about mysql and basic system alarms. This section only implements the basic alarm function.
[The error message returned when zabbix is installed in a package cannot be reported]
If zabbix shows in the log and database that the email has been sent but you have not received the email.
Eliminate the issue of Blocking spam, that is, zabbix itself. There is no similar solution on the Internet, but the problem can be identified through analysis.
Analyze the problem:
1. The zabbix permission does not exist. The script has granted the highest permission and the owner group and user are both zabbix.
2. The script itself can correctly send and receive emails if it is directly executed by ubuntu users. Eliminate the script itself.
3. Since the zabbix log and DB both show that the mail has been sent, it means that at least zabbix is doing the sendmail operation, but it is not actually sent successfully. The root cause of the problem lies in the zabbix mail process.
4. We know that mutt calls msmtp to send an email, so zabbix should be like this in the mail sending process.
Trigger alert-> zabbix user calls bash-> execute mail. sh script
If the execution is successful, the message is sent successfully. If the execution fails, the email cannot be received.
Test and solve problems
1. manually log on to the zabbix user to simulate zabbix to send an email. We find that the first step will cause an error, because the zabbix user automatically created during apt-get installation of zabbix does not have the home directory or Login shell by default, at least zabbix should be given the bash call permission. Otherwise, zabbix users cannot successfully execute mail. sh script.
The solution is as follows:
Sudo vim/etc/passwd
Change/bin/false at the end of zabbix to/bin/bash.
After saving and exiting, use sudo su-zabbix to switch to zabbix. Although there is no home directory, there is a shell login.
2. Run the mail. sh test.
./Mail. sh chenlong0554@qq.com zabbix test Hello zabbix
Prompt
Msmtp: account default not found: no configuration file available
Error sending message, child exited 78 ().
Cocould not send the message.
Through msmtp, we understand that in the msmtp installed by apt-get, its configuration file is called by default in the home directory of the current user. msmtprc, although we have created it under the ubuntu user. this file is not found in the zabbix directory of msmtprc, so zabbix still cannot be used for msmtp.
The solution is as follows:
Sudo mkdir/home/zabbix
Sudo chown zabbix. zabbix-R/home/zabbix
Sudo vim/etc/passwd
Change/var/lib/zabbix of zabbix to/home/zabbix.
Sudo cp/home/ubuntu/. msmtprc/home/zabbix/. msmtprc
Chown zabbix. zabbix/home/zabbix/. msmtprc
Chmod 600/home/zabbix/. msmtprc
Run the test script again through the zabbix user!