http://xiaoyongxing1.blog.163.com/blog/static/642807522015144302240/
该脚本针对网上已有解决方案(google一下nginx状态监控shell脚本),修改内存总数计算bug,修改字符错误。在CentOs5.7系统环境下测试可用。
#/bin/bashhost=`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}-ge300];
then
echo"alert ==> ${host}@${ip}: http connection ${http_req} >= 300 @${time_stamp}"elseecho"${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}-ge100]
then
echo"alert ==> ${host}@${ip}: nginx process ${nginx_proc} >= 100 @${time_stamp}"elseecho"${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}-ge500]
then
echo"alert ==> ${host}@${ip}: nginx memory usage ${nginx_mem} MB >= 500 @${time_stamp}"elseecho"${host}@${ip}: nginx memory ${nginx_mem}MB @ ${time_stamp}"fi
由此可推导出相应的python版:
#coding=utf-8
__author__ = 'kenkao'
import os
def getNginxProcess():
p = os.popen('ps -C nginx -no-header | wc -l')
return p.read()
def getNginxMemory():
p = os.popen('top -b -n1 | grep nginx |gawk \'{if($6~/m$/) {sum+=$6*1024} else {sum+=$6} }; END {print int(sum/1024)}\'')
return p.read()
def getNginxConnect():
p = os.popen('netstat -nat|grep -i "80"|wc -l')
return p.read()
def getPhpfpmProcess():
p = os.popen('ps -C php-fpm -no-header | wc -l')
return p.read()
def getPhpfpmMemory():
p = os.popen('top -b -n1 | grep php-fpm |gawk \'{if($6~/m$/) {sum+=$6*1024} else {sum+=$6} }; END {print int(sum/1024)}\'')
return p.read()
if __name__ == '__main__':
print "NginxProcess:" + getNginxProcess()
print "NginxMemory:" + getNginxMemory()
print "NginxConnect:" + getNginxConnect()
print "PhpfpmProcess:" + getPhpfpmProcess()
print "PhpfpmMemory:" + getPhpfpmMemory()
posted on 2017-03-30 18:09 思月行云 阅读(442) 评论(0) 编辑 收藏 引用 所属分类: Nginx\Openresty