脚本需求:
公司在A省接入了一批数据流量,小博参与写一套接入流量的c语言服务,需要时刻监控公司接入的流量的状态和服务是否core掉的情况
#/bin/bash
# author : wuyingzhouzhou# time : 2014-11-21
# 网卡数据接入流-服务程序-磁盘 监控脚本
date=$(date -d "today" +"%Y-%m-%d_%H:%M:%S")
phone=( phone1 phone2 )
# 下面的数组,index是端口号,value 是 监控的ip,网卡,区市
server_list=( [22]=192.168.1.101:dna0:C市 [9122]=192.168.1.102:dna1:B市 [9222]=192.168.1.106:dna1:A市 )server_name=( server client )
server_list2=( [9022]=222.68.194.41:radius_server:redis-server:D市 [9222]=222.68.194.41:radius_server:redis-server:D市2 )
function sendmail(){
message='警告!:'$1' , 请及时处理!'
for var in ${phone[@]}
do
curl "http://2.smsfx.sinaapp.com/send.php?tel=18310119242&pwd=wuying423568&aim=$var&text=$message"
done
}
// 检查redis存储服务器的状况
function checkradius(){
for index in ${!server_list2[*]}
do
ip=`echo ${server_list2[$index]}|awk -F ':' '{print $1}'`
radius=`echo ${server_list2[$index]}|awk -F ':' '{print $2}'`
redis=`echo ${server_list2[$index]}|awk -F ':' '{print $3}'`
address=`echo ${server_list2[$index]}|awk -F ':' '{print $4}'`
rdnum=`ssh -p $index root@$ip ps -ef|grep $radius|wc -l`
if [ $? -eq 0 ]
then
if [ $rdnum -lt 1 ]
then
sendmail $address$ip':'$index'的'$radius'服务dump了'
fi
fi
renum=`ssh -p $index root@$ip ps -ef|grep $redis|wc -l`
if [ $? -eq 0 ]
then
if [ $renum -lt 1 ]
then
sendmail $address$ip':'$index'的'$redis'服务dump了'
fi
fi
done
}
//检查接入流量服务器的状态
function checkstream (){
for index in ${!server_list[*]}
do
ip=`echo ${server_list[$index]}|awk -F ':' '{print $1}'`
netcard=`echo ${server_list[$index]}|awk -F ':' '{print $2}'`
address=`echo ${server_list[$index]}|awk -F ':' '{print $3}'`
stream_1=`ssh -p $index root@$ip ifconfig $netcard|sed -n "7"p|awk '{print $2}'|awk -F ':' '{print $2}'`
if [ $? -ne 0 ];then
continue
fi
sleep 5
stream_2=`ssh -p $index root@$ip ifconfig $netcard|sed -n "7"p|awk '{print $2}'|awk -F ':' '{print $2}'`
if [ $? -ne 0 ];then
continue
fi
if [ $stream_1 -ge $stream_2 ];then
sendmail $address$ip':'$index"的流量断了"
fi
done
}
//检查接入流量服务器的服务程序的状态
function checkserver(){
for index in ${!server_list[*]}
do
ip=`echo ${server_list[$index]}|awk -F ':' '{print $1}'`
netcard=`echo ${server_list[$index]}|awk -F ':' '{print $2}'`
address=`echo ${server_list[$index]}|awk -F ':' '{print $3}'`
for list in ${server_name[@]}
do
num=`ssh -p $index root@$ip ps -ef|grep $list|wc -l`
if [ $? -eq 0 ];then
if [ $num -ne 1 ];then
sendmail $address$ip':'$index'的'$list'服务dump了'
fi
fi
done
done
}
//检查磁盘状态
function checkdisk(){
for index in ${!server_list[*]}
do
ip=`echo ${server_list[$index]}|awk -F ':' '{print $1}'`
address=`echo ${server_list[$index]}|awk -F ':' '{print $3}'`
for line in `ssh -p $index root@$ip df -P|awk -F ' ' '{print $6":"$5}'|grep '%'`
do
dir=`echo $line|cut -d ':' -f1`
percent=`echo $line|cut -d ':' -f2`
cmp=`echo $line|cut -d ':' -f2|sed 's/%//'`
if [ $cmp -ge 80 ];then
sendmail $address$ip':'$index'的'$dir'目录已经达到:'$percent
fi
done
done
}
checkstream
checkserver
checkdisk
checkradius
===========================================================================================================================
小博能力有限,记录下自己的工作生活点滴吧