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"

}