该脚本针对网上已有解决方案(google一下nginx状态监控shell脚本),修改内存总数计算bug,修改字符错误。在CentOs5.7系统环境下测试可用。
#/bin/bash
host=`hostname`
ip=`ifconfig eth0 | grep 'inet addr'|sed 's/.*addr://g'|sed 's/B.*//g'`
#监控nginx的连接数
http_req=`netstat -nat|grep -i "80"|wc -l `
time_stamp=`date "+%Y/%m/%d %T"`
if [ ${http_req} -ge 300 ];
then
echo "alert ==> ${host}@${ip}: http connection ${http_req} >= 300 @${time_stamp} "
else
echo "${host}@${ip}: http connection ${http_req} @ ${time_stamp}"
fi
##监控nginx的进程
nginx_proc=`ps -C nginx -no-header | wc -l `
time_stamp=`date "+%Y/%m/%d %T"`
if [ ${nginx_proc} -ge 100 ]
then
echo "alert ==> ${host}@${ip}: nginx process ${nginx_proc} >= 100 @${time_stamp} "
else
echo "${host}@${ip}: nginx process ${nginx_proc} @ ${time_stamp}"
fi
#监控nginx所占用的内存总数
nginx_mem=`top -b -n1 | grep nginx |gawk '{if($6~/m$/) {sum+=$6*1024} else {sum+=$6} }; END {print int(sum/1024)}' `
time_stamp=`date "+%Y/%m/%d %T"`
if [ ${nginx_mem} -ge 500 ]
then
echo "alert ==> ${host}@${ip}: nginx memory usage ${nginx_mem} MB >= 500 @${time_stamp} "
else
echo "${host}@${ip}: nginx memory ${nginx_mem}MB @ ${time_stamp}"
fi
本文转载自:http://www.guofang.me/article/index/aid/80