Scene:
The Redis keys value needs to be monitored, and monitoring scripts are executed using the Crond cycle. If there is an exception, notify by email.
First, install the MSMTP
Download software
wget http://sourceforge.net/projects/msmtp/files/msmtp/1.4.32/msmtp-1.4.32.tar.bz2
TAR-JXVF msmtp-1.4.32.tar.bz2
CD msmtp-1.4.32
./configure--PREFIX=/USR/LOCAL/MSMTP
CD msmtp-1.4.32
Make && make install
Modify Configuration
cd/usr/local/msmtp/
mkdir etc #配置文件目录和配置文件都要自己建
CD etc
Vim MSMTPRC #手动创建配置文件
Account Default
Host smtp.163.com #你的发送邮件服务器
Port 25
From [email protected] #要从哪个邮箱发出
Auth Login
TLS off
User [email protected] #邮箱用户名
Password 123456 #邮箱密码, but this is clear, if you feel insecure, you can change the file to a 600 property
(chmod 600/ETC/MSMTPRC)
Account Default:admin
Logfile/data/log/msmtp.log
Second, installation Mutt
Direct Yum Install Mutt
Vim/etc/muttrc
Source/etc/muttrc.local
Set sendmail= "/usr/local/msmtp/bin/msmtp"
Set Use_from=yes
Set Envelope_from=yes
Set realname= "Redis keys Monitor"
Set [email protected]
Test:
echo "Test" |mutt-s "my_first_mail" [email protected]
Attach Attachments Send mail
echo "Test" |mutt-s "my_first_mail" [email protected] </tmp/redis.txt
###############
Script interpretation
The Redis keys value is queried through the Crond cycle, and a warning message is sent when the Redis keys are not consumed.
*/5 * * * */bin/sh/tmp/redismonitor.sh
Script instance:
#!/bin/bash
suffix=$ (Date +%y-%m-%d-d "Today")
Redis-cli-h 192.168.36.1-p 6379 llen bgm_info >/data/tmp/keys_$suffix.txt
result= Cat/data/tmp/keys_$suffix.txt
if [[0-eq $result]] #当keys值等于0的时候不做任何操作, not equal to zero sends an alarm message.
Then
echo "Nothing" >/dev/null 2>&1
Else
subject= ' redis surveillance alert! '
to= "[Email protected]"
echo "Redis Keys Monitor" | Mutt $to-S $subject-e ' Set content_type= "Text/txt" </tmp/keys_$suffix.txt
Fi
Exit 0
If Judgment statement Small note:
-eq: Equals
-ne: Not equal to
-le: Less than or equal to
-ge: greater than or equal to
-LT: Less than
-GT: Greater Than
Note:1, e-mail alerts in the production environment is still relatively common, you can use SQL to split the table to send messages.
2. Using Python XLWT module to generate Excel, and Python script to send email alerts
This article is from the "7835882" blog, please be sure to keep this source http://crfsz.blog.51cto.com/7835882/1892394
Using Msmtp+mutt+shell to implement email alerts