Under Bin
Vim sms_monitor.sh
#!/bin/bash
CD ' DirName $ '
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 $} ' | Awk-f ' "' {print '} '
}
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= "Disk Alarm" production server $IP disk alarm,
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 Volume: space occupancy: $2%.
}
For I in $a
Do
Number= ' echo $i |awk-f% ' {print $} '
Name= ' echo $i |awk-f% ' {print $} '
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= "Disk Alarm" production server $IP disk alarm,
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 Volume: The inode percentage is: $2%.
}
For I in $b
Do
Number2= ' echo $i |awk-f% ' {print $} '
Name2= ' echo $i |awk-f% ' {print $} '
Value2= ' get_value2 $name 2 $number 2 '
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 '} '
If ["$loss"-ne 0];then
Ping_over= "Connectivity Alarm" production server "$ip" ping alarm, packet loss rate is $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 Alarm" production server "$ip", the last 15 minutes of CPU load average more than 70%, for $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 >" | BC)
If [$iowait _decimal_compare-eq 1];then
iowait_load_over= "iowait Alarm" production server "$ip", iowait more than 10%, for $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 Alarm Message"} '
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, monitoring no exceptions. ">> $audit _file
Fi
conf under
Vim Conf.json
{
"IP": "192.168.1.1",
"Ping_target": "192.168.1.228",
"Ping_count": "4",
"Sms_server": "192.168.1.228:6400"
}
This article from "11945344" blog, declined reprint!
Shell Script SMS Monitoring script (disk, CPU, etc.)