欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

shell监控系统资源并通过短信报警的脚本

程序员文章站 2023-11-09 13:44:40
目标分析: 所需的监控资源: 1、登陆用户数 2、cpu负载 3、服务探测 4、硬盘空间(根分区、应用分区、备份分区) 5、内存资源 短信接口、邮件接口 格式上传至...

目标分析:

所需的监控资源:

1、登陆用户数
2、cpu负载
3、服务探测
4、硬盘空间(根分区、应用分区、备份分区)
5、内存资源

短信接口、邮件接口

格式上传至附件

脚本:

#!/bin/bash
#监控用户登录
usermonitor () {
loginuser=`uptime | awk '{print $6}'`
if [ $loginuser -ge 2 ]
then
critical="当前系统登录用户人数超过1人,具体人数为:$loginuser 个,请确认操作者人数。"
status=0
else
echo "loginuser ok"
status=1
fi
013.
}
#监控内存
memmonitor () {
memtotal=`free -m | grep mem | awk -f: '{print $2}' | awk '{print $1}'`
memfree=`free -m | grep cache | awk nr==2 | awk '{print $4}'`
memfreeb=`awk 'begin{printf "%.2f%\n",'$memfree/$memtotal\*100'}'`
memfrees=`awk 'begin{printf "%.f",'$memfree/$memtotal\*100'}'`
if [ $memfrees -lt 10 ]
then
critical="系统可用内存小于10%,实际可用内存为:$memfreeb ,请处理。"
status=0
elif [ $memfrees -lt 20 ]
then
warning="系统可用内存小于20%,实际可用内存为:$memfreeb ,请查看。"
warningt="内存报警"
status=1
else
echo "mem ok"
status=2
fi
}
#监控分区空间大小
diskmonitorg () {
#根分区
diskgb=`df -h | awk nr==2 | awk '{print $5}'`
diskgs=`df -h | awk nr==2 | awk '{print $5}' | awk -f% '{print $1}'`
if [ $diskgs -gt 90 ]
then
critical="根分区使用率超过90%,实际已使用 $diskgb ,请处理。"
status=0
elif [ $diskgs -gt 80 -a $diskgs -lt 90 ]
then
warning="根分区使用率超过80%,实际已使用 $diskgb , 请查看。"
warningt="根分区报警"
status=1
else
echo "diskgb ok"
status=2
fi
}
diskmonitora () {
#应用分区
applyb=`df -h | awk nr==4 | awk '{print $5}'`
applys=`df -h | awk nr==4 | awk '{print $5}' | awk -f% '{print $1}'`
if [ $applys -gt 90 ]
then
critical="应用分区使用率超过90%,实际已使用 $applyb ,请处理."
status=0
elif [ $applys -gt 80 -a $applys -lt 90 ]
then
warning="应用分区使用率超过80%,实际已使用 $applyb ,请查看。"
warningt="应用分区报警"
status=1
else
echo "apply ok"
status=2
fi
}
#监控cpu负载
cpuload () {
cpuload1=`uptime | awk '{print $10}' | awk -f. '{print $1}'`
cpuload2=`uptime`
if [ $cpuload1 -gt 5 ]
then
critical="cpu负载过高,请即使处理。 $cpuload2 "
status=0
elif [ $cpuload1 -gt 3 -a $cpuload1 -lt 5 ]
then
warning="cpu负载警告, $warning "
warningt="cpu负载报警"
status=1
else
echo "cpu ok"
status=2
fi
}
#监控服务状态
servermonitor () {
#服务状态监控
timeout=10
makfails=2
fails=0
success=0
while true
do
/usr/bin/wget --timeout=$timeout --tries=1 http://192.168.20.84/ -q -o /dev/null
if [ $? -ne 0 ]
then
let fails=fails+1
success=0
else
fails=0
let success=1
fi
if [ $success -ge 1 ]
then
exit 0
fi
if [ $fails -ge 1 ]
then
critical="应用服务出现故障,请紧急处理!"
echo $critical | mutt -s "服务down" hao.lulu@chinaebi.com
exit -1
fi
done
}
#发送报警短信、报警邮件
for n in usermonitor memmonitor diskmonitorg diskmonitora cpuload servermonitor
do
$n
if [ $status -eq 0 ]
then
curl "http://172.20.36.118/app/tms.do?trancode=tm0311&;content=$critical"
elif [ $status -eq 1 ]
then
curl "http://172.20.36.118/app/tms.do?trancode=tm0310&;title=$warningt&content=warning"
else
echo "ok"
fi
done

总结

以上所述是小编给大家介绍的shell监控系统资源并通过短信报警的脚本,希望对大家有所帮助