Implement server monitor and alarm small script with Shell+sendmail _linux shell

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.