shell脚本之短信监控脚本(磁盘、cpu等等)

释放双眼,带上耳机,听听看~!

bin下

vim sms_monitor.sh

#!/bin/bash

cd

1
1`

dirname $0

1
1`

timestamp=

1
1`

date +"%F %H:%M:%S"

1
1`

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=

1
1`

find $json_path -type f

1
1`

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=

1
1`

get_value_1 ip

1
1`

ping_target=

1
1`

get_value_1 ping_target

1
1`

ping_count=

1
1`

get_value_1 ping_count

1
1`

sms_server=

1
1`

get_value_1 sms_server

1
1`

#检查磁盘容量使用情况

disk=【磁盘告警】生产服务器$ip磁盘告警,

a=

1
1`

df -h | awk  '+$(NF-1)>70 {print $(NF-1)$NF}' | grep -v 192 | grep -v 172

1
1`

if [ -n "$a" ];then

disk_flag=true

fi

get_value () {

echo $1卷:空间占用率为:$2%。

}

for i in $a

do

number=

1
1`

echo $i|awk -F%  '{print $1}'

1
1`

name=

1
1`

echo $i|awk -F%  '{print $2}'

1
1`

value=

1
1`

get_value $name $number

1
1`

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=

1
1`

df -hi | awk  '+$(NF-1)>70 {print $(NF-1)$NF}' | grep -v 192 | grep -v 172

1
1`

if [ -n "$b" ];then

inode_flag=true

fi

get_value2 () {

echo $1卷:inode百分比为:$2%。

}

for i in $b

do

number2=

1
1`

echo $i|awk -F% '{print $1}'

1
1`

name2=

1
1`

echo $i|awk -F% '{print $2}'

1
1`

value2=

1
1`

get_value2 $name2 $number2

1
1`

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=

1
1`

ping $ping_target -c $ping_count | grep % | tr ',' ' ' | awk  '{print $6}' | awk -F "%" '{print $1}'

1
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=

1
1`

grep 'model name' /proc/cpuinfo | wc -l

1
1`

define_cpu_load_average=

1
1`

echo | awk "{print $cpu_count*0.7}"

1
1`

cpu_load_average=

1
1`

uptime | awk -F "[ ,]" '{print $NF}'

1
1`

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=

1
1`

iostat | awk 'NR==4{print $4}'

1
1`

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"

}

转载于:https://blog.51cto.com/11955344/1984018

给TA打赏
共{{data.count}}人
人已打赏
安全技术安全运维

Windows服务器如何发现被黑

2018-5-20 12:24:31

安全技术

网站制作需要素材的实用网站

2021-12-21 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索