机器性能预警系统(cpu,process,内存,硬盘监控)并钉钉机器人报警,
里边都有讲解注释,不是很清楚请留言,共同学习~
最终报警及检查的文件:FinalCheck.sh
1 #!/bin/bash 2 # 如果第二个参数是'@'符号,就@手机号对应的群成员 3 phone=13889612345 5 # message 是要发送到钉钉的信息,中间不要有空格 6 IP=`ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'` 7 function httpRequest() 8 { 9 #curl 请求 将access_token换成自己的 10 curl -H "Content-type: application/json" -X POST -d '{"msgtype": "text","text": {"content": "主人:'$IP$message'"}, "at": {"atMobiles": ["'$phone'"], "isAtAll": false}}' https://oapi.dingtalk.com/robot/send?access_token=8efdb5626113cd3e8a67f6fea7a2d95b69a69d9af74bb7c2fc8229e8f49a849d 11 } 12 13 #检查硬盘 14 cat disk.log| while read line 15 do 16 curDiskUsePer=`echo $line | awk '{printf "%d",$1}'` 17 if [ $curDiskUsePer -gt 80 ];then 18 message="硬盘快满了,当前占用$curDiskUsePer%" 19 httpRequest 20 fi 21 done 22 #检查内存 23 cat memory.log| while read line 24 do 25 curDiskUsePer=`echo $line | awk '{printf "%d",$1}'` 26 if [ $curDiskUsePer -gt 8 ];then 27 message="内存不够了,剩余$curDiskUsePer个G" 28 httpRequest 29 fi 30 done 31 32 #检查进程cpu占用率 33 cat cpu_process.log| while read line 34 do 35 curDiskUsePer=`echo $line | awk '{printf "%d",$1}'` 36 if [ $curDiskUsePer -gt 3 ];then 37 message="进程占用cpu太高,当前为$curDiskUsePer%" 38 httpRequest 39 fi 40 done 41 42 exit 0
总CPU检测脚本:cpu_total.sh
1 #!/bin/sh 2 # 3 rm -rf /home/xsn/MachineHealthCheck/memory.log 4 free -m | awk -F '[ :]+' 'NR==2{print ($4+$6+$7)/1000}' >> /home/xsn/MachineHealthCheck/memory.log 5 6 #echo "CPU Usage:${CPU_USAGE}%"$time >> /home/xsn/MachineHealthCheck/cpu.log
进程CPU占用最高的CPU使用率:cpu_process.sh
1 #!/bin/sh 2 # 3 rm -rf /home/xsn/MachineHealthCheck/cpu_process.log 4 ps aux --sort=-pcpu|head -2 | grep -v USER | grep -v mysql | awk -F '[ ]+' 'NR!=0{print $3}'>> /home/xsn/MachineHealthCheck/cpu_process.log 5 6 #echo "CPU Usage:${CPU_USAGE}%"$time >> /home/xsn/MachineHealthCheck/cpu.log
内存检测:memory.sh
1 #!/bin/sh 2 # 3 rm -rf /home/xsn/MachineHealthCheck/memory.log 4 free -m | awk -F '[ :]+' 'NR==2{print ($4+$6+$7)/1000}' >> /home/xsn/MachineHealthCheck/memory.log 5 6 #echo "CPU Usage:${CPU_USAGE}%"$time >> /home/xsn/MachineHealthCheck/cpu.log
硬盘检测:disk.sh
1 #!/bin/sh 2 # 3 rm -rf /home/xsn/MachineHealthCheck/disk.log 4 df -THP | awk -F '[ ]+' 'NR!=1{print $6}' >> /home/xsn/MachineHealthCheck/disk.log