您的位置:首页 > 运维架构 > Linux

linux服务监控脚本

2014-11-21 14:19 489 查看
配置需要监控的服务器
数组定义:host_ports=(host_name=host_port=uri_path)
host_name为容易识别的服务器名称
host_port为服务器ip和服务端口
uri_path为经济的请求路径
为脚本运行的服务器开放防火墙端口,内网ip和外网ip皆可,但要与host_port一致
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp -s 121.45.111.17 --dport 8090 -j ACCEPT

监控结果记录日志
先记录日期
echo `date +'%Y-%m-%d %H:%M:%S'` > $log

选择notify_host,需要提供发送邮件的接口,所以前面定义数组时将有邮件接口的host放在前面,找到第一个运行正常的host即可作为notify_host
if [ -z $notify_host ]; then notify_host=$host_port fi
检查完后检查日志文件,大于1行则发邮件通知
curl http://$notify_host/admin/monitor.html?log=$log > /dev/null 2>&1

crontab定时运行监控脚本

crontab配置,每20分钟检查一次
*/20 * * * * /soft/monitor.sh >> /soft/logs/monitor-cron.log

运行效果,测试时每两分钟检查一次



邮件通知



脚本内容

log=/soft/logs/monitor.log
echo `date +'%Y-%m-%d %H:%M:%S'`
echo `date +'%Y-%m-%d %H:%M:%S'` > $log

host_ports=(
caifuxiang-slave-test=121.45.111.17:8080=/monitor.html
ghcaiyuan-slave-test=121.45.111.17:8080=/monitor.html
caifuxiang-app=121.45.111.17:8080=/monitor.html
caifuxiang-app2=121.45.111.17:8080=/monitor.html
ghcaiyuan-app=121.45.111.17:8080=/monitor.html
ghcaiyuan-app2=121.45.111.17:8080=/monitor.html
caifuxiang-master-upload=121.45.111.17:8090=/upload/crossdomain.xml
ghcaiyuan-master-upload=121.45.111.17:8090=/upload/crossdomain.xml
)
#echo ${host_ports[*]}

for ((i=0;i<${#host_ports[@]};++i))
do
host_name=`echo ${host_ports[i]}|cut -d "=" -f 1`
host_port=`echo ${host_ports[i]}|cut -d "=" -f 2`
uri_path=`echo ${host_ports[i]}|cut -d "=" -f 3`
#echo $host_port
if curl http://$host_port$uri_path > /dev/null 2>&1
then
#echo "$host_name is ok"
if [ -z $notify_host ]; then
notify_host=$host_port
fi
else
echo "$host_name is not ok"
echo "$host_name is not ok" >> $log
fi
done

log_lines=`cat $log|wc -l`
#echo "$log lines: $log_lines"
if [ $log_lines -gt 1 ]; then
echo "send notify email using: $notify_host, log: $log"
#curl http://$notify_host/admin/monitor.html?log=$log > /dev/null 2>&1
log_content=`cat $log` #post log_content to notify_host, cause it can't get file $log
curl -d "log=$log_content" http://$notify_host/admin/monitor.html > /dev/null 2>&1

else
echo "everything is ok"
fi

来自为知笔记(Wiz)

附件列表

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: