失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Shell脚本和Python查看Nginx并发连接数 进程数和常驻内存占用情况

Shell脚本和Python查看Nginx并发连接数 进程数和常驻内存占用情况

时间:2021-11-13 23:52:26

相关推荐

Shell脚本和Python查看Nginx并发连接数 进程数和常驻内存占用情况

Shell脚本查看Nginx并发连接数、进程数和常驻内存占用情况

http://xiaoyongxing1./blog/static/64280752144302240/

该脚本针对网上已有解决方案(google一下nginx状态监控shell脚本),修改内存总数计算bug,修改字符错误。在CentOs5.7系统环境下测试可用。

#/bin/bashhost=`hostname`ip=`ifconfigeth0|grep'inetaddr'|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];thenecho"alert==>${host}@${ip}:httpconnection${http_req}>=300@${time_stamp}"elseecho"${host}@${ip}:httpconnection${http_req}@${time_stamp}"fi##监控nginx的进程nginx_proc=`ps-Cnginx-no-header|wc-l`time_stamp=`date"+%Y/%m/%d%T"`if[${nginx_proc}-ge100]thenecho"alert==>${host}@${ip}:nginxprocess${nginx_proc}>=100@${time_stamp}"elseecho"${host}@${ip}:nginxprocess${nginx_proc}@${time_stamp}"fi#监控nginx所占用的内存总数nginx_mem=`top-b-n1|grepnginx|gawk'{if($6~/m$/){sum+=$6*1024}else{sum+=$6}};END{printint(sum/1024)}'`time_stamp=`date"+%Y/%m/%d%T"`if[${nginx_mem}-ge500]thenecho"alert==>${host}@${ip}:nginxmemoryusage${nginx_mem}MB>=500@${time_stamp}"elseecho"${host}@${ip}:nginxmemory${nginx_mem}MB@${time_stamp}"fi

由此可推导出相应的python版:

#coding=utf-8__author__ = 'kenkao'import osdef 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()

如果觉得《Shell脚本和Python查看Nginx并发连接数 进程数和常驻内存占用情况》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。