把下面的代码保存成weixin.sh(代码里有中文的改成你自己的信息,微信对接信息请先https://work.weixin.qq.com/ 到这里注册获取)
存放到/www/server/panel/script这个目录里。
然后在你的宝塔管理控制台添加计划任务
#!/bin/bash
#!/bin/bash
# 01.获取主机资源
function sys_info()
{
# 系统信息
Date=`date +%Y-%m-%d`
Date_time=`date "+%Y-%m-%d__%H:%M:%S"`
Host_name=`qmun`
IP_net_dev="eth0"
IP_addr="10.0.0.200"
# 主机资源
Disk_rate_sys=`df -h|grep \/\$|awk -F" " '{ print $5 }'`
Disk_rate_sys2=${Disk_rate_sys%%%}
Disk_rate_data=`df -h|grep \/boot\$|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='企业id号'
Secret='安全key'
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="接收用户名"
To_Party="1"
Agent_ID="1000001"
# 日志目录
if [ ! -d "/tmp/logs_check" ]; then mkdir /tmp/logs_check; fi
Logs_file='/tmp/logs_check'
# 远程地址
SSH_addr="gisquest.com"
# 运行业务
Project_mode="监测"
}
# 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'远程地址:${SSH_addr}'\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
}
# 2.3 判断CPU空闲值过低
function sys_cpu()
{
if [ $CPU_free -lt 1 ]
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 3 ]
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
}
# 03.检查服务
# 3.1 检查mysql服务存活
function mysql_status()
{
ps -ef|grep mysql |grep -v grep
if [ $? -eq 0 ]
then
Date_time311=`date "+%Y-%m-%d__%H:%M:%S"`
echo "$Date_time311:mysql is up" >> $Logs_file/mysql_pro.$Date.log
else
Date_time312=`date "+%Y-%m-%d__%H:%M:%S"`
echo "$Date_time312:mysql服务第一次停止" >> $Logs_file/mysql.$Date.log
/usr/bin/curl --data-ascii '{ "touser": "'${To_User}'", "toparty": "'${To_Party}'","msgtype": "text","agentid": "'${Agent_ID}'","text": {"content": "'获取时间:${Date_time312}'\n'运行业务:${Project_mode}'\n'远程地址:${SSH_addr}'\n'内网地址:${IP_addr}'\n'主机名称:${Host_name}'\n'发生事件:mysql第一次停止'"},"safe":"0"}' $PURL
/etc/init.d/mysqld restart
bash /www/server/panel/script/rememory.sh
Os_code=$?
if [ $Os_code -eq 0 ]
then
Date_time313=`date "+%Y-%m-%d__%H:%M:%S"`
echo "$Date_time313:mysql_rec is up" >> $Logs_file/mysql.$Date.log
/usr/bin/curl --data-ascii '{ "touser": "'${To_User}'", "toparty": "'${To_Party}'","msgtype": "text","agentid": "'${Agent_ID}'","text": {"content": "'获取时间:${Date_time313}'\n'运行业务:${Project_mode}'\n'远程地址:${SSH_addr}'\n'内网地址:${IP_addr}'\n'主机名称:${Host_name}'\n'发生事件:mysql服务恢复'"},"safe":"0"}' $PURL
elif [ $Os_code -ne 0 ]
then
Date_time314=`date "+%Y-%m-%d__%H:%M:%S"`
echo "$Date_time314:mysql重启失败" >> $Logs_file/mysql.$Date.log
/usr/bin/curl --data-ascii '{ "touser": "'${To_User}'", "toparty": "'${To_Party}'","msgtype": "text","agentid": "'${Agent_ID}'","text": {"content": "'获取时间:${Date_time314}'\n'运行业务:${Project_mode}'\n'远程地址:${SSH_addr}'\n'内网地址:${IP_addr}'\n'主机名称:${Host_name}'\n'发生事件:mysql服务停止'"},"safe":"0"}' $PURL
fi
fi
}
# 10 调用函数
sys_info
sys_disk
sys_cpu
sys_load
mysql_status
文章评论