由于lvs 下的web不稳定,写了一个script来监控LVS的状态,使用perl的mail模板发信
bash脚本:
#!/bin/bash
lvs_list=`egrep -v "^ *#| bond" /etc/sysconfig/ha/lvs.cf | awk '/address/{ print $3}'`
lvs_list_num=`egrep -v "^ *#| bond" /etc/sysconfig/ha/lvs.cf | awk '/address/{ print $3}' | wc -l`
path=/home/infra/Monitor
cd $path
######## Check the lvs ####################################################
cmd=`/sbin/ipvsadm -ln | awk '/Route/{ print $2 }' | awk -F: '{ print $1 }'`
N_url=`/sbin/ipvsadm -ln | awk '/Route/{ print $2 }' | wc -l`
if [ $lvs_list_num -eq $N_url ];then ############# 判断当前web数量与路由表比对
:
else
case $N_url in
1)
for i in $lvs_list;do
if [ $i != "$cmd" ];then
perl mail.pl $i--2
fi
done
;;
2)
echo $lvs_list > $path/tmp.monitor1
echo $cmd > $path/tmp.monitor2
tr -s " " "\n" < $path/tmp.monitor1 > $path/tmp.monitor3
tr -s " " "\n" < $path/tmp.monitor2 > $path/tmp.monitor4
i=`cat tmp.monitor3 tmp.monitor4 | sort | uniq -u`
perl mail.pl $i--1
;;
0)
echo $lvs_list > $path/tmp.monitor
i=`sed -r 's/ /#/g' $path/tmp.monitor`
perl mail.pl ALL-----$i
rm -rf $path/tmp.monitor
;;
esac
fi
exit
perl 邮件模板
- #! /usr/bin/perl -w
-
-
-
- use Net::SMTP;
-
- my ($a)=@ARGV;
-
- my $mailhost = "10.82.67.200";
-
- my $mailfrom = ;
-
- # the recipient list
-
- my @mailto = ();
-
- my $subject = " WEB ERROR ";
-
- my $text = "the webs status is : The $a shutting down !!";
-
-
-
- $smtp = Net::SMTP->new($mailhost, Hello => 'localhost', Timeout => 120, Debug => 1);
-
- # auth login, type your user name and password here
-
- # $smtp->auth('user','pass');
-
-
-
- foreach my $mailto (@mailto) {
-
- # Send the From and Recipient for the mail servers that require it
-
- $smtp->mail($mailfrom);
-
- $smtp->to($mailto);
-
-
-
- # Start the mail
-
- $smtp->data();
-
-
-
- # Send the header
-
- $smtp->datasend("To: $mailto\n");
-
- $smtp->datasend("From: $mailfrom\n");
-
- $smtp->datasend("Subject: $subject\n");
-
- $smtp->datasend("\n");
-
-
-
- # Send the message
-
- $smtp->datasend("$text\n\n");
-
-
-
- # Send the termination string
-
- $smtp->dataend();
-
- }
-
-
-
- $smtp->quit;
阅读(538) | 评论(0) | 转发(0) |