在你拥有多台服务器,并且需要时刻关注服务器运行是否稳定正常的时候,就需要一个服务器状态监控。这里可以用iis7服务器监控工具实时监控,IIS7服务器监控工具,操作简单,删除系统缓存,重启服务器,修改服务器账号密码,修复服务器复制功能等,也可以一键开启关闭MYSQL和503错误的监控,省去了繁琐的操作步骤,一键完成。也可以直接修改远程端口范围,省去繁琐步骤。
于是乎使用脚本定时发送服务器状态到统计接口的需求就应运而生。
#!/bin/bash
# 保存参数
P1=$1
# 每5分钟
CRONCMD="*/5 * * * * "
# 记录最后一次运行的时间
now_time=`date '+%F %T'`
echo "lasttime $now_time $P1" > /usr/cron.log
DOMAIN='monitor.domain.com'
# 检查并置入定时器
function check_cron(){
RET=`crontab -l| grep "/usr/monitor.sh"`
if [[ "$RET" != "" ]];then #发现了任务
echo "monitor任务已存在$CRONPATH"
else
echo "monitor任务不存在"
basepath=$(cd `dirname $0`; pwd)
FILEPATH=$basepath/monitor.sh
if [ "$FILEPATH" != "/usr/monitor.sh" ];then
if [ -f $FILEPATH ];then
echo "发现脚本位置$FILEPATH"
mv $FILEPATH /usr/monitor.sh
chmod 777 /usr/monitor.sh
echo "增加cron任务->/var/spool/cron/monitor"
echo "$CRONCMD /usr/monitor.sh"
echo "$CRONCMD /usr/monitor.sh -crontab" >> /var/spool/cron/root
echo "定时已完成"
else
echo "脚本不存在,位置:"$FILEPATH
fi
fi
fi
}
# 监控数据并发送到服务器
function monitor_main(){
# 获取设备名称
hostname=`hostname`
# 获取mac地址
mac=`/sbin/ifconfig |grep -o "[a-f0-9A-F]\\([a-f0-9A-F]\\:[a-f0-9A-F]\\)\\{5\\}[a-f0-9A-F]"|head -n 1`
if [[ "$mac" == "" ]];then
mac=`ip address |grep -o "[a-f0-9A-F]\\([a-f0-9A-F]\\:[a-f0-9A-F]\\)\\{5\\}[a-f0-9A-F]"`;
mac=${mac//00:00:00:00:00:00};
mac=${mac//ff:ff:ff:ff:ff:ff};
mac=`echo $mac |grep -o "[a-f0-9A-F]\\([a-f0-9A-F]\\:[a-f0-9A-F]\\)\\{5\\}[a-f0-9A-F]"|head -n 1`
fi
#获取cpu使用率
cpuUsage=`top -b -n1 | awk -F '[ %]+' 'NR==3 {print $2}'`
# Cpu(s):
RET=`echo $cpuUsage| grep "Cpu"`
if [[ "$RET" != "" ]];then #发现了任务
cpuUsage=`top -b -n1 | awk -F '[ %]+' 'NR==3 {print $3}'`
fi
#获取磁盘使用率
data_name="/"
diskUsage=`df -h | grep $data_name | awk -F '[ %]+' '{print $5}'`
#logFile=/tmp/monitor.log
#获取内存情况
mem_total=`free -m | awk -F '[ :]+' 'NR==2{print $2}'`
#获取内存使用
mem_used=`free -m | awk -F '[ :]+' 'NR==2{print $3}'`
#统计内存使用率
mem_used_persent=`awk 'BEGIN{printf "%.0f\n",('$mem_used'/'$mem_total')*100}'`
#获取报警时间
now_time=`date '+%F %T'`
URLPATH="http://$DOMAIN/Monitor/API/push"
PARAMS="hostname=$hostname"
PARAMS="$PARAMS&mac=$mac"
PARAMS="$PARAMS&cpu=$cpuUsage"
PARAMS="$PARAMS&disk=$diskUsage"
PARAMS="$PARAMS&memsum=$mem_total"
PARAMS="$PARAMS&memused=$mem_used"
PARAMS="$PARAMS&mem=$mem_used_persent"
PARAMS="$PARAMS<ime=$now_time"
echo $URLPATH
echo $PARAMS
ret=`curl $URLPATH -H 'Accept: application/json, text/plain, */*' -H 'Referer: http://127.0.0.1/' -H 'Origin: http://127.0.0.1:8080' -H 'User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Mobile Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded' --data "$PARAMS" --compressed`
echo $ret
}
function main(){
# 根据参数,确定是否需要校验定时器cron运行
if [ "$P1" == "-uninstall" ];then
echo "卸载定时器"
RET=`crontab -l| grep "/usr/monitor.sh"`
if [[ "$RET" != "" ]];then #发现了任务
sed -i '/monitor.sh/d' /var/spool/cron/root
echo "已删除任务 /usr/monitor.sh"
else
echo "未发现任务"
fi
exit
fi
if [ "$P1" == "-update" ];then
if [ -f '/usr/monitor.sh' ];then
rm -f '/usr/monitor.sh'
fi
cd /usr/
wget http://$DOMAIN/Public/shell/monitor.sh
sh /usr/monitor.sh -uninstall
sh /usr/monitor.sh
exit
fi
if [ "$P1" == "-h" ];then
echo "sh monitor.sh -update"
echo "sh monitor.sh -uninstall"
echo "sh monitor.sh -crontab"
exit
fi
if [ "$P1" == "-crontab" ];then
echo "on crontab now"
monitor_main
exit
else
monitor_main
check_cron
exit
fi
}
main
使用方法
wget http://monitor.domain.com/Public/shell/monitor.sh
sh monitor.sh
以上脚本如果在个别服务器中运行不正常,需要自己调整获取参数。