1.3.1.3 短信报警,监控内存使用量
内存的使用量,是另一个反应当前服务器性能和负载的指标。使用free.sh对内存使用量进行采集,使用mail_free.sh对内存的使用量进行监控,实现短信报警。具体程序如下:
free.sh程序
#!/bin/bash
#author longxibendi
#blog http://blog.csdn.net/longxibendi
#function get the percentage of memory usage
fr="`free`";
free| grep Mem | awk -v f="$fr" '
($3/$2 >= 0.5) {print f ;}
'
unsetfr
mail_free.sh程序
#!/bin/bash
#author longxibendi
#blog http://blog.csdn.net/longxibendi
#function to send email to longxibendi@139.com if the percentage of memoryusage greater than or equal
iftest ! -z "`sh free.sh`" ;then
/usr/local/bin/sendEmail-f monitor_sys@163.com -t longxibendi@139.com -s smtp.163.com -u "memorywarnning" -xu monitor_sys -xp 123456789 -m "` sh free.sh| awk -va="****" ' { print $1,a,$2,a,$3,a,$4,a,$5,a,$6;}'`"
Fi
1.3.1.4 短信报警,监控磁盘使用量
主要对磁盘的分区使用量进行监控,通过程序df.sh对磁盘使用量进行数据收集,通过mail_df.sh对df.sh收集的数据进行分析,如果发现超过阈值,进行短信报警。设置的阈值为70%。即磁盘分区使用量超过70%进行报警。程序如下。
df.sh程序
#!/bin/bash
#author longxibendi
#blog http://blog.csdn.net/longxibendi
#function find which disk‘s Available amount less than 20%
df -hm | awk '
( $3/$2 >= 0.7 ) { printf "%s\n" ,$0; }
'
mail_df.sh程序
#!/bin/bash
#author longxibendi
#blog http://blog.csdn.net/longxibendi
#function sendmail to longxibendi@139.com if the amount of available disk less than 20%
#if[ `./df.sh | wc -l ` >= 1 ] ; then
if [ `./df.sh | wc -l ` > 0 ] ; then
/usr/local/bin/sendEmail -f monitor_sys@163.com -t longxibendi@139.com-s smtp.163.com -u "disk warning" -xu monitor_sys -xp 123456789 -m "`./df.sh | awk ' { print $5,$6}' ; date ` "
fi
1.3.1.5 短信报警,监控磁盘的读写速度
读速度监控以及短信报警,使用程序mail_io_read.sh。当发现磁盘读速度小于每秒100Blocks时,执行短信报警。程序如下。
mail_io_read.sh程序
#!/bin/bash
#author longxibendi
#blog http://blog.csdn.net/longxibendi
#function SMS and Email alert if IO error
if[ `/usr/bin/iostat | grep sda | head -n 1 | awk '{printf("%d",$3);}'`-le 100 ] ;then
/usr/local/bin/sendEmail -f monitor_sys@163.com -t longxibendi@139.com-s smtp.163.com -u "IO warning" -xu monitor_sys -xp 123456789 -m " `iostat | sed -e '1,5d' -e '8,21d'|awk -v a="||" '{print $1,a,$2,a,$3,a,$4,a,$5,a,$6;}
'`";
fi
写速度监控以及短信报警,使用程序mail_io_write.sh。当发现磁盘写速度小于每秒100Blocks时,执行短信报警。程序如下
mail_io_write.sh程序
#!/bin/bash
#author longxibendi
#blog http://blog.csdn.net/longxibendi
#function SMS and Email alert if IO error
if[ `/usr/bin/iostat | grep sda | head -n 1 | awk '{printf("%d",$4);}'`-le 100 ] ;then
/usr/local/bin/sendEmail -f monitor_sys@163.com -t longxibendi@139.com-s smtp.163.com -u "IO warning" -xu monitor_sys -xp 123456789 -m "`iostat | sed -e '1,5d' -e '8,21d'|awk -v a="||" '{print $1,a,$2,a,$3,a,$4,a,$5,a,$6;}
'`";
fi
1.3.1.6 短信报警,监控网卡的状态
提供网络服务器的服务器,必须有网卡,一旦网卡出现问题,如果只有一块出口网卡,那么预示着本机的服务也终止了。所以网卡的监控非常重要。下面使用mail_eth0.sh对网卡进行状态监控。程序如下。
mail_eth0.sh
!/bin/bash
#author longxibendi
#blog http://blog.csdn.net/longxibendi
#function SMS and Email alert
if[ `sar -n DEV 1 1 | grep Average | sed -e '2d' -e '4d' | tail -n 1 | awk'{printf("%d",$5);}'` -eq 0 ];then
/usr/local/bin/sendEmail -f monitor_sys@163.com -t longxibendi@139.com-s smtp.163.com -u "eth0 error" -xu monitor_sys -xp 123456789 -m "`date `" ;
fi
1.4 故障转移
故障转移是指,当一个节点或者一个服务发生故障,比如主机僵死,进程僵死之后,能够有相应的监控系统发现故障,并在较短的时间内启动备用机器,实现故障转移。这是保证大型网站的高可用性最为重要的措施和方法之一。故障转移有不同的几个级别,比如主机操作系统的故障转移,数据库的故障转移,应用进程的故障转移等。本节主要给出应用进程的故障转移,以检测Nginx进程为例。
1.4.1 WebServer故障转移解决方案
运行环境如图8-4-1所示。

图8-4-1
如图8-4-1所示,在IP:172.29.141.112和172.29.141.113上同时部署两个web server Nginx服务器,并且部署故障转移程序monitor_nginx程序互相监控对方的Nginx运行情况。当发现另一方的Nginx服务器停止之后,立刻更改本机IP为对方IP,发送短信报警然后启用本机的Nginx程序来提供服务。详细部署情况如下所示:
172.29.141.112 部署 Nginx 与 FastCGI(对外开放)
172.29.141.113 部署 Nginx 与 FastCGI(作为热备)
172.29.141.115 部署 MySQL(或者MySQL-Proxy)
