[root@localhost ~]# cat han.sh
#!/bin/bash
#监控用户登录
IP=`ifconfig eth0 | sed -n '2'p | awk '{print $2}' | awk -F':' '{print $2}'`
Usermonitor () {
LoginUser=`uptime | awk '{print $4}'`
if [ $LoginUser -ge 4 ]
then
Critical="当前系统登录用户人数超过4人,具体人数为:$LoginUser个,请确认操作者人数。"
echo "$Critical"
status=0
else
echo "loginuser ok"
status=2
fi
}
#监控内存
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
echo "$Critical"
elif [ $MemFreeS -lt 20 ]
then
Warning="系统可用内存小于20%,实际可用内存为:$MemFreeB ,请查看。"
WarningT="内存报警"
status=1
echo "$Warning"
else
echo "Mem OK"
status=2
fi
}
#监控分区空间大小
DiskMonitorG () {
Disk=`df -h | awk NR==2 | awk '{print $1}'`
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="$Disk 分区使用率超过90%,实际已使用 $DiskGB ,请处理。"
status=0
echo "$Critical"
elif [ $DiskGS -gt 80 -a $DiskGS -lt 90 ]
then
Warning="$Disk 分区使用率超过80%,实际已使用 $DiskGB , 请查看。"
WarningT="$Disk 分区报警"
status=1
echo "$Warning"
else
echo "$Disk Ok"
status=2
fi
}
DiskMonitorA () {
Apply=`df -h | awk NR==2 | awk '{print $1}'`
ApplyB=`df -h | awk NR==3 | awk '{print $5}'`
ApplyS=`df -h | awk NR==3 | awk '{print $5}' | awk -F% '{print $1}'`
if [ $ApplyS -gt 90 ]
then
Critical="$Apply 分区使用率超过90%,实际已使用 $ApplyB ,请处理."
status=0
echo "$Critical"
elif [ $ApplyS -gt 80 -a $ApplyS -lt 90 ]
then
Warning="$Apply 分区使用率超过80%,实际已使用 $ApplyB ,请查看。"
WarningT="$Apply 分区报警"
status=1
echo "$Warning"
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
echo "$Critical"
elif [ $CPULoad1 -gt 3 -a $CPULoad1 -lt 5 ]
then
Warning="CPU负载警告, $Warning "
WarningT="CPU负载报警"
status=1
echo "$Warning"
else
echo "CPU OK"
status=2
fi
}
#监控服务状态
ServerMonitor () {
#服务状态监控
timeout=10
makfails=2
fails=0
success=0
NUM=`curl --connect-timeout 15 -s www.**.com`
NUM1=`echo $NUM | grep -Po '(?<=B2\-).*(?=\<\/a)'` #匹配网站的备案号或许可证
MATCH=20130776
if [ "$NUM1" != "$MATCH" ]
then
Critical="`date +%Y%m%d-%H:%M`: $NUM"
status=0
else
echo "web ok "
status=2
fi
}
#发送报警邮件
for n in Usermonitor MemMonitor DiskMonitorG DiskMonitorA CPULoad ServerMonitor
do
$n
if [ $status -eq 0 ]
then
echo "$n is $Critical" |mutt -s "$IP $n $Critical" hz7726@163.com
elif [ $status -eq 1 ]
then
echo "$n is $Warning" |mutt -s "$IP $n $Warning" hz7726@163.com
else
echo "ok"
fi
done
运行结果如下:
阅读(1274) | 评论(0) | 转发(1) |