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
运行效果,测试时每两分钟检查一次
![](http://images.cnitblog.com/blog/293671/201411/251210397468186.png)
邮件通知
![](http://images.cnitblog.com/blog/293671/201411/251210409968471.png)
脚本内容
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)
数组定义: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
运行效果,测试时每两分钟检查一次
![](http://images.cnitblog.com/blog/293671/201411/251210397468186.png)
邮件通知
![](http://images.cnitblog.com/blog/293671/201411/251210409968471.png)
脚本内容
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)
附件列表
相关文章推荐
- Linux监控脚本监控后台进程,当进程死掉后重新启动服务
- Linux监控Tomcat服务定时任务脚本
- linux服务(web/mysql/disk/memory)监控和邮件报警脚本
- Linux基础之-利用shell脚本实现自动监控系统服务
- linux应用服务状态——脚本监控方法
- linux Web服务程序监控shell脚本
- linux服务器监控脚本
- Linux基础之-利用shell脚本实现自动监控系统服务
- linux bash脚本监控启动停止weblogic服务
- linux运维学习shell脚本监控nginx服务
- Linux下脚本自动监控Tomcat服务
- 关于Linux下监控WEB服务
- 在Linux系统下启动服务(包含main的java类)脚本(sockos.sh)
- 三个监控linux网卡流量脚本
- linux 监控服务器常用脚本
- 通过Nagios监控Weblogic服务(续)--优化调整脚本
- nagios监控linux主机和主机上的web服务
- 监控SQUID服务是否运行脚本
- 负载、CPU、内存、磁盘、Http服务、Tcp服务监控脚本
- nagios监控linux主机及服务信息