This email alert script is everywhere, a lot of use of sendmail, Postfix, feel a bit overqualified, and some use Perl net::smtp and AUTHEN::SASL module, but I use Perl is not good, The old send out some baffling mails (for example if (a>1) {print (a);}, the last message shows the 0.99 ... Finally, using SendEmail this molded Perl program to send the alarm, and real-time monitoring part of the return shell, finally completed.
Copy Code code as follows:
wget <a href= "http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz" >http:// Caspian.dotconf.net/menu/software/sendemail/sendemail-v1.56.tar.gz</a>
Tar zxvf sendemail-v1.56.tar.gz
CP sendemail-v1.56/sendemail/shell/check/
Cat >> check.sh <<eof
File--check.sh:
Copy Code code as follows:
#!/bin/bash
Checkmail () {
/usr/bin/perl./sendemail-f userid@mail.com-t oneuserid@mail.com–cc twouserid@mail.com threeuserid@mail.com
-U "$subject"-M "$data"-s Smtp.mail.com-xu userid-xp password
Sleep 300
}
While True;do
Loadavg= ' awk ' {print $} '/proc/loadavg '
diskuse= ' DF |awk '/cache/{print $} '
servrun= ' netstat-pln|awk-f/'/:80/{print $NF} '
ip= ' Ifconfig|awk '/cast/{print $} ' |awk-f: ' {if (nr==1) {a=$2}else if (nr==2) {b=$2}}end{print B "-" A} "
Data= ' echo-e IP: $ip \nloadavg/5min: $loadavg \tcacheuse%: $diskuse \tservice: $servrun "'
Diskper= ' echo $diskuse |sed ' s/%//'
Num= ' PS aux|grep check.sh|grep-v grep|wc-l '
if [$num > 2]];then
Break 2
Fi
if [[$loadavg > 1.00]] && [[$diskper >]];then
subject= "warning-$ip-loadavg-disk"
Checkmail
else if [$loadavg > 1.00]];then
subject= "warning-$ip-loadavg"
Checkmail
else if [$diskper >]];then
subject= "warning-$ip-disk"
Checkmail
Fi
Sleep 60
Done
Eof
Finish, perform sh check.sh &>/dev/null.
Alert message:
Title: Warning-192.168.0.100-10.10.10.10-disk ip:192.168.0.100-10.10.10.10 loadavg/5min:0.38 cacheuse%:94% Service: Nginx