1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #!/bin/bash
# 01.获取主机资源 function sys_info() { # 系统信息 Date=`date +%Y-%m-%d` Date_time=`date "+%Y-%m-%d__%H:%M:%S"` Host_name=`hostname` IP_addr=`grep IPADDR /etc/sysconfig/network-scripts/ifcfg-ens*|awk -F"=" '{print $2}'` # 主机资源 Disk_rate_sys=`df -h|grep \/\$|awk -F" " '{ print $5 }'` Disk_rate_sys2=${Disk_rate_sys%%%} Disk_rate_data=`df -h|grep \/var/lib/containers\$|awk -F" " '{ print $5 }'` Disk_rate_data2=${Disk_rate_data%%%} CPU_free=`vmstat |tail -1|awk -F" " '{print $15}'` Load_ave15=`uptime |awk '{ print $NF }'|awk -F" " '{ print $1 }'` Load_ave15_1=`expr $Load_ave15` Mem_Total_kb=`cat /proc/meminfo | grep "MemTotal"|awk -F":" '{print $2}'|awk '{print $1}'` Mem_Free_kb=`cat /proc/meminfo | grep "MemFree"|awk -F":" '{print $2}'|awk '{print $1}'` Mem_Total=`expr $Mem_Total_kb / 1024` Mem_Free=`expr $Mem_Free_kb / 1024` # 微信接口 CropID='xxxxxx' Secret='xxxxxx' GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}') PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" To_User="xxxxxx" To_Party="1" Agent_ID="xxxxx" # 日志目录 if [ ! -d "/tmp/logs_check" ]; then mkdir /tmp/logs_check; fi Logs_file='/tmp/logs_check' # 运行业务 Project_mode="DEV镜像仓库" }
# 02.主机资源磁盘、CPU、MEM、LOAD # 2.1 判断根磁盘使用率超额 #系统盘使用率 function sys_disk() { if [ $Disk_rate_sys2 -gt 80 ] then echo "$Date_time 磁盘使用率超额" >> $Logs_file/disk_rate.$Date.log /usr/bin/curl --data-ascii '{ "touser": "'${To_User}'", "toparty": "'${To_Party}'","msgtype": "text","agentid": "'${Agent_ID}'","text": {"content": "'获取时间:${Date_time}'\n'运行业务:${Project_mode}'\n'内网地址:${IP_addr}'\n'主机名称:${Host_name}'\n'磁盘使用:目录/为${Disk_rate_sys}'"},"safe":"0"}' $PURL else echo "$Date_time 磁盘使用率良好:$Disk_rate_sys" >> $Logs_file/disk_rate.$Date.log fi
#数据盘使用率
if [ ${Disk_rate_data2} -gt 50 ] then echo "$Date_time 磁盘使用率超额" >> $Logs_file/disk_rate.$Date.log /usr/bin/curl --data-ascii '{ "touser": "'${To_User}'", "toparty": "'${To_Party}'","msgtype": "text","agentid": "'${Agent_ID}'","text": {"content": "'获取时间:${Date_time}'\n'运行业务:${Project_mode}'\n'内网地址:${IP_addr}'\n'主机名称:${Host_name}'\n'磁盘使用:目录/data为${Disk_rate_data}'"},"safe":"0"}' $PURL else echo "$Date_time 磁盘使用率良好:$Disk_rate_data" >> $Logs_file/disk_rate.$Date.log fi }
# 2.3 判断CPU空闲值过低 function sys_cpu() { if [ $CPU_free -lt 10 ] then echo "$Date_time CPU空闲率少于10%" >> $Logs_file/CPU_free.$Date.log /usr/bin/curl --data-ascii '{ "touser": "'${To_User}'", "toparty": "'${To_Party}'","msgtype": "text","agentid": "'${Agent_ID}'","text": {"content": "'获取时间:${Date_time}'\n'运行业务:${Project_mode}'\n'远程地址:${SSH_addr}'\n'内网地址:${IP_addr}'\n'主机名称:${Host_name}'\n'CPU空闲:${CPU_free}%'"},"safe":"0"}' $PURL else echo "$Date_time CPU空闲率良好:$CPU_free%" >> $Logs_file/CPU_free.$Date.log fi }
# 2.4 判断服务器15分钟负载过高 function sys_load() { if [ `expr $Load_ave15_1 \> 0` -gt 1 ] then echo "$Date_time 服务器15分钟负载过高:$Load_ave15%" >> $Logs_file/Load_ave15.$Date.log /usr/bin/curl --data-ascii '{ "touser": "'${To_User}'", "toparty": "'${To_Party}'","msgtype": "text","agentid": "'${Agent_ID}'","text": {"content": "'获取时间:${Date_time}'\n'运行业务:${Project_mode}'\n'远程地址:${SSH_addr}'\n'内网地址:${IP_addr}'\n'主机名称:${Host_name}'\n'主机负载:${Load_ave15}%'"},"safe":"0"}' $PURL else echo "$Date_time 服务器15分钟负载良好:$Load_ave15%" >> $Logs_file/Load_ave15.$Date.log fi }
# 3 调用函数 sys_info sys_disk sys_cpu sys_load
|