shell監控腳本3

#!/bin/sh
#count memory_used_rate,disk_used_rate 
#@yuanwb 2015-5
 
#disk_used_rate
#Depend on real storage place the parameter 'Location' need to alter.
Location=/dev/sda1
Disk_Used_Rate1=$(df -h | grep $Location | awk '{print $5}')
free1=`echo $Disk_Used_Rate1|awk -F% '{print $1}'`
subject1="(info) web2 /dev/sda1 硬盤空間小於95%"
messagelog1="web2 /dev/sda1 硬盤空間使用率爲 $Disk_Used_Rate1 "
subject2="(warn) web2 /dev/sda1 硬盤空間大於95%"
messagelog2="web2 /dev/sda1 硬盤空間使用率爲 $Disk_Used_Rate1 "
if [ $free1 -lt 95 ];
then
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject1" -m "$messagelog1"
else
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject2" -m "$messagelog2"
fi

Location=/dev/sda2
Disk_Used_Rate2=$(df -h | grep $Location | awk '{print $5}')
echo $Disk_Used_Rate2
free1=`echo $Disk_Used_Rate2|awk -F% '{print $1}'`
subject1="(info) web2 /dev/sda2 硬盤空間小於95%"
messagelog1="web2 /dev/sda2 硬盤空間使用率爲 $Disk_Used_Rate2 "
subject2="(warn) web2 /dev/sda2 硬盤空間大於95%"
messagelog2="web2 /dev/sda2 硬盤空間使用率爲 $Disk_Used_Rate2 "
if [ $free1 -lt 95 ];
then
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject1" -m "$messagelog1"
else
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject2" -m "$messagelog2"
fi

Location=/dev/sdb1
Disk_Used_Rate3=$(df -h | grep $Location | awk '{print $5}')
echo $Disk_Used_Rate3
free1=`echo $Disk_Used_Rate3|awk -F% '{print $1}'`
subject1="(info) web2 /dev/sdb1 硬盤空間小於95%"
messagelog1="web2 /dev/sdb1 硬盤空間使用率爲 $Disk_Used_Rate3 "
subject2="(warn) web2 /dev/sdb1 硬盤空間大於95%"
messagelog2="web2 /dev/sdb1 硬盤空間使用率爲 $Disk_Used_Rate3 "
if [ $free1 -lt 95 ];
then
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject1" -m "$messagelog1"
else
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject2" -m "$messagelog2"
fi

Location=/dev/sdc1
Disk_Used_Rate3=$(df -h | grep $Location | awk '{print $5}')
echo $Disk_Used_Rate3
free1=`echo $Disk_Used_Rate3|awk -F% '{print $1}'`
subject1="(info) web2 /dev/sdc1 硬盤空間小於95%"
messagelog1="web2 /dev/sdc1 硬盤空間使用率爲 $Disk_Used_Rate3 "
subject2="(warn) web2 /dev/sdc1 硬盤空間大於95%"
messagelog2="web2 /dev/sdc1 硬盤空間使用率爲 $Disk_Used_Rate3 "
if [ $free1 -lt 95 ];
then
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject1" -m "$messagelog1"
else
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject2" -m "$messagelog2"
fi
 
#memory_used_rate
LoadMemory=$(cat /proc/meminfo | awk '{print $2}')
Total=$(echo $LoadMemory | awk '{print $1}')
Free1=$(echo $LoadMemory | awk '{print $2}')
Free2=$(echo $LoadMemory | awk '{print $3}')
Free3=$(echo $LoadMemory | awk '{print $4}')
 
Used=`expr $Total - $Free1 - $Free2 - $Free3`
Used_Rate=`expr  $Used/$Total*100 | bc -l`
Memory_Used_Rate=`expr  $Used_Rate/1 | bc`
echo $Memory_Used_Rate%
free1=`echo $Memory_Used_Rate%|awk -F% '{print $1}'`
subject1="(info) web2 內存使用率小於90%"
messagelog1="web2 內存使用率爲 $Memory_Used_Rate "
subject2="(error) web2 內存使用率大於90%"
messagelog2="web2 內存使用率爲 $Memory_Used_Rate "
if [ $free1 -lt 90 ];
then
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject1" -m "$messagelog1"
else
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject2" -m "$messagelog2"
fi
#!/bin/bash
b=`uptime|awk '{print $11}'|awk -F, '{print $1}'`
subject1="(info) web2 cpu負載小於2"
messagelog1="web2 cpu負載正常!!"
subject2="(warn) web2 cpu負載大於2小於8"
messagelog2="web2 cpu負載忙!!"
subject3="(error) web2 cpu負載大於8!!"
messagelog3="web2 cpu負載超負荷!!"
a=`echo ${b%.*}`
if [ $a -lt 2 ];
then
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject1" -m "$messagelog1"
elif [ $a -gt 2 -a $a -lt 8 ];
then
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject2" -m "$messagelog2"
else
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject3" -m "$messagelog3"
fi
#!/bin/bash
i=0
j=0
subject="(info) web2 web狀態監控"
messagelog="web2 web狀態監控成功!!"
subject1="(error) web2 web狀態監控"
messagelog1="web2 web狀態監控不成功!!"
while [[ $i -le 10 ]]
do
i=$((${i}+1))
wget http://172.20.101.54/test.php 
b=`cat test.php|grep "it is ok"|wc -l`
rm test.php -f
if [ $b -ne 0 ];
then
j=$((${j}+1))
else
exit 0
fi
done
if [ $j -ge 9 ];
then
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject" -m "$messagelog"
else
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject1" -m "$messagelog1"
fi
#!/bin/bash
httping -c20 -g http://172.20.101.54 > test.txt
b=`cat test.txt|grep avg|awk '{print $4}'|awk -F/ '{print $2}'`
subject1="(info) web2 延遲響應時間爲 $b ms"
messagelog="web2 延遲響應監控成功!!"
subject2="(warn) web2 延遲響應監控大於70ms"
messagelog1="web2 延遲響應時間爲 $b ms"
a=`echo ${b%.*}`
if [ $a -gt 70 ];
then
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject1" -m "$messagelog2"
else
/usr/local/bin/sendEmail -s 172.20.101.63 -o message-charset="UTF-8" -xu [email protected] -xp 1234565 -f [email protected] -t [email protected],[email protected],[email protected] -u "$subject1" -m "$messagelog"
fi


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章