shell腳本之短信監控腳本(磁盤、cpu等等)
bin下
vim sms_monitor.sh
#!/bin/bash
cd `dirname $0`
timestamp=`date +"%F %H:%M:%S"`
disk_flag=false
inode_flag=false
ping_flag=false
cpu_flag=false
iowait_flag=false
sms_flag=false
disk_over=""
inode_over=""
ping_over=""
cpu_load_average_over=""
iowait_load_over=""
#創建和檢查log文件
default_file=../log/runtime.log
audit_file=../log/audit.log
exec 2>>$default_file #腳本未知錯誤保存
#讀取json配置文件
json_path=../conf
json_file=`find $json_path -type f `
get_value_1 () {
keytmp=$1\"\:\"
cat $json_file |sed s#"^M"##g |tr ‘\n‘ ‘ ‘ |sed s#[[:space:]]##g |awk -F "$keytmp" ‘{print $2}‘ | awk -F ‘"‘ ‘{print $1}‘
}
ip=`get_value_1 ip`
ping_target=`get_value_1 ping_target`
ping_count=`get_value_1 ping_count`
sms_server=`get_value_1 sms_server`
#檢查磁盤容量使用情況
disk=【磁盤告警】生產服務器$ip磁盤告警,
a=`df -h | awk ‘+$(NF-1)>70 {print $(NF-1)$NF}‘ | grep -v 192 | grep -v 172`
if [ -n "$a" ];then
disk_flag=true
fi
get_value () {
echo $1卷:空間占用率為:$2%。
}
for i in $a
do
number=`echo $i|awk -F% ‘{print $1}‘`
name=`echo $i|awk -F% ‘{print $2}‘`
value=`get_value $name $number`
disk_over=$disk_over$value
done
if [ "$disk_flag" = true ];then
disk_monitor=$disk$disk_over
elif [ "$disk_flag" = false ];then
disk_monitor=$disk_over
fi
#檢查磁盤inode使用情況
inode=【磁盤告警】生產服務器$ip磁盤告警,
b=`df -hi | awk ‘+$(NF-1)>70 {print $(NF-1)$NF}‘ | grep -v 192 | grep -v 172`
if [ -n "$b" ];then
inode_flag=true
fi
get_value2 () {
echo $1卷:inode百分比為:$2%。
}
for i in $b
do
number2=`echo $i|awk -F% ‘{print $1}‘`
name2=`echo $i|awk -F% ‘{print $2}‘`
value2=`get_value2 $name2 $number2`
inode_over=$inode_over$value2
done
if [ "$inode_flag" = true ];then
inode_monitor=$inode$inode_over
elif [ "$inode_flag" = false ];then
inode_monitor=$inode_over
fi
#檢查丟包率
loss=`ping $ping_target -c $ping_count | grep % | tr ‘,‘ ‘ ‘ | awk ‘{print $6}‘ | awk -F "%" ‘{print $1}‘`
if [ "$loss" -ne 0 ];then
ping_over=【連通性告警】生產服務器"$ip"ping告警,丟包率為$loss%。
ping_flag=true
fi
if [ "$ping_flag" = true ];then
ping_monitor=$ping_over
elif [ "$ping_flag" = false ];then
ping_monitor=$ping_over
fi
#檢查CPU負載情況
cpu_count=`grep ‘model name‘ /proc/cpuinfo | wc -l`
define_cpu_load_average=`echo | awk "{print $cpu_count*0.7}"`
cpu_load_average=`uptime | awk -F "[ ,]" ‘{print $NF}‘`
cpu_decimal_compare=$(echo "$cpu_load_average > $define_cpu_load_average" | bc)
if [ $cpu_decimal_compare -eq 1 ];then
cpu_load_average_over="【cpu告警】生產服務器"$ip",最近15分鐘的cpu load average超過70%,為$cpu_load_average。"
cpu_flag=true
fi
if [ "$cpu_flag" = true ];then
cpu_load_average_monitor=$cpu_load_average_over
elif [ "$cpu_flag" = false ];then
cpu_load_average_monitor=$cpu_load_average_over
fi
#檢查磁盤讀寫情況
iowait_load=`iostat | awk ‘NR==4{print $4}‘`
iowait_decimal_compare=$(echo "$iowait_load > 10" | bc)
if [ $iowait_decimal_compare -eq 1 ];then
iowait_load_over=【iowait告警】生產服務器"$ip",iowait超過10%,為$iowait_load%。
iowait_flag=true
fi
if [ "$iowait_flag" = true ];then
iowait_load_monitor=$iowait_load_over
elif [ "$iowait_flag" = false ];then
iowait_load_monitor=$iowait_load_over
fi
#拼接sms內容
if [ $disk_flag = true -o $inode_flag = true -o $ping_flag = true -o $cpu_flag = true -o $iowait_flag = true ];then
sms_flag=true
fi
if [ $sms_flag = true ];then
sms_msg=$disk_monitor$inode_monitor$ping_monitor$cpu_load_average_monitor$iowait_load_monitor
sms_json=‘{"msg":"‘$sms_msg‘","ip":"‘$ip‘","timestamp":"‘$timestamp‘","tags":"production","remark":"sms報警消息"}‘
curl -i -X POST -H "‘Content-type‘:‘text/html‘, ‘charset‘:‘utf-8‘, ‘Accept‘: ‘text/plain‘" -d "${sms_json}" "http://$sms_server/ots-manager/sp/sms/monitor/send" >>$default_file
else
echo "$timestamp,監控沒有異常。" >>$audit_file
fi
conf下
vim conf.json
{
"ip":"192.168.1.1",
"ping_target":"192.168.1.228",
"ping_count":"4",
"sms_server":"192.168.1.228:6400"
}
本文出自 “11945344” 博客,謝絕轉載!
shell腳本之短信監控腳本(磁盤、cpu等等)