首页 > nagios > python写的nginx的nagios插件

python写的nginx的nagios插件

2010年12月9日 eric 发表评论 阅读评论

闲的蛋疼学习python urllib2模块,想到了可以写nginx status模块,就试着写了一下,随后测试下了,比shell写的执行速度要快些,数据如下

第一次:                    第二次:
check_nginx.sh                          check_nginx.sh
real    0m0.453s                         real    0m0.264s
user    0m0.020s                        user    0m0.030s
sys     0m0.030s                        sys     0m0.020s

check_nginx.py                         check_nginx.py
real    0m0.244s                        real    0m0.244s
user    0m0.030s                       user    0m0.020s
sys     0m0.020s                        sys     0m0.010s

由此可以看出 python速度要快些。

下面帖源码:

#!/usr/bin/python
# check_nginx is a Nagios to monitor nginx statu
# my blog: http://www.nginxs.com
# The version is 1.0.0
# MSN: yangzi2008@126.com

import string
import urllib2
import getopt
import sys

def usage():
   print """check_nginx is a Nagios to monitor nginx status
   Usage:

   check_nginx [-h|--help][-u|--url][-p|--path][-w|--warning][-c|--critical]

   Options:
          --help|-h)
                         print check_nginx help.
                  --url|-u)
                                 Sets nginx status url.
                  --path|-p)
                          Sets nginx status url path.
                  --warning|-w)
                                  Sets a warning level for nginx Active connections. Default is: off
                  --critical|-c)
                              Sets a critical level for nginx Active connections. Default is: off
        Example:
                  The url is www.nginxs.com/status
                  ./check_nginx -u www.nginxs.com -p /status -w 1000 -c 2000"""

   sys.exit(3)

try:
    options,args = getopt.getopt(sys.argv[1:],"hu:p:w:c:",["help","url=","path=","warning=","critical="])

except getopt.GetoptError:
   usage()
   sys.exit(3)

for name,value in options:
    if name in ("-h","--help"):
       usage()
    if name in ("-u","--url"):
       url = "http://"+value
    if name in ("-p","--path"):
       path = value
    if name in ("-w","--warning"):
       warning = value
    if name in ("-c","--critical"):
       critical = value
try:
   req = urllib2.Request(url+path)
   response = urllib2.urlopen(req)
   the_page = response.readline()
   a = the_page.split()
   ActiveConn = a[2]
   the_page1 = response.readline()
   the_page2 = response.readline()
   b = the_page2.split()
   serveraccepts = b[0]
   handled = b[1]
   requests = b[2]
   the_page3 = response.readline()
   c = the_page3.split()
   reading = c[1]
   writing = c[3]
   waiting = c[5]
   output = 'ActiveConn:%s,serveraccepts:%s,handled:%s,requests:%s,reading:%s,writing:%s,waiting:%s' % (ActiveConn,serveraccepts,handled,requests,reading,writing,waiting)
   perfdata = 'ActiveConn:%s,serveraccepts:%s,handled:%s,requests:%s,reading:%s,writing:%s,waiting:%s' % (ActiveConn,serveraccepts,handled,requests,reading,writing,waiting)

except Exception:
   print "NGINX STATUS unknown: Error while getting Connection"
   sys.exit(3)

if 'warning' in dir() and 'critical' in dir():
   if ActiveConn >= warning:
      print 'WARNING - %s|%s' % (output,perfdata)
      sys.exit(2)
   elif ActiveConn >= critical:
      print 'CRITICAL - %s|%s' % (output,perfdata)
      sys.exit(1)
   else:
      print 'OK - %s|%s' % (output,perfdata)
      sys.exit(0)
else:
   print 'OK - %s|%s' % (output,perfdata)
   sys.exit(0)

代码就这些,应该不需要对代码加什么解释吧·如果有人需要,我可以每行代码加注视
-w 和-c (warning 和critical) 默认是关闭的。

使用方法:

nginx $> ./check_nginx -u status.tuokou.com -p / -u www.nginxs.com -p / -w 10000 -c 20000
OK -ActiveConn:1,serveraccepts:49177,handled:49177,requests:71099,reading:0,writing:1,waiting:0|ActiveConn:1,serveraccepts:49177,handled:49177,requests:71099,reading:0,writing:1,waiting:0

下载 check_nginx
check_nginx

转载请注明:文章转载自:Eric's linux and nginx! (http://www.nginxs.com)
本文地址:http://www.nginxs.com/linux/368.html

分类: nagios 标签: , ,
订阅评论
2+3= (必填)