首页 > 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 标签: , ,
  1. 2018年1月21日20:32 | #1

    It’s not my first time to pay a visit this website,
    i am browsing this web site dailly and take nice information from here every day.

  2. 2018年1月23日10:46 | #2

    Great article. I will be dealing with many of these issues as well..

  3. 2018年1月23日15:07 | #3

    usa online slots real money
    casino games online real money
    download free casino slot games
    online casino game
    absolutely free slots for fun

  4. 2018年1月24日09:31 | #4

    casino online
    real money casino
    online slots
    real money casino
    real money casino

  5. 2018年1月26日17:11 | #5

    payday loans online
    payday loans online
    payday loans
    payday loans
    payday loans

  6. 2018年1月29日10:07 | #6

    It’s a pity you don’t have a donate button! I’d without
    a doubt donate to this excellent blog! I guess
    for now i’ll settle for book-marking and adding your RSS feed to my Google account.

    I look forward to fresh updates and will share this site with
    my Facebook group. Chat soon!

  7. 2018年2月2日07:48 | #7

    online casino gambling
    slot online
    free online casino
    online casino gambling
    top rated free online casino games

  8. 2018年2月3日22:30 | #8

    Pretty nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed
    browsing your blog posts. After all I will be subscribing
    to your feed and I hope you write again soon!

  9. 2018年2月4日10:40 | #9

    A dry cleansing agent is unfold on the carpet.

  10. 2018年2月9日01:56 | #10

    lose weight fast
    weight loss pills for men
    lose weight fast
    prescription diet pills
    new weight loss medication

  11. 2018年2月10日07:44 | #11

    Yes! Finally something about ig.

  12. 2018年2月12日17:09 | #12

    Hit Black Friday in a Black Friday get together bus!

  13. 2018年2月13日01:57 | #13

    vegas casino games
    online casino no deposit bonus
    top rated free online casino games
    casino games slots free heart of vegas
    casino games slots free heart of vegas

  14. 2018年2月13日09:51 | #14

    These on-line advertising methods are timeless.

  15. 2018年2月14日04:31 | #15

    casino games slots free heart of vegas
    pogo casino slots
    casino games free
    casino online loc us
    online gambling casino

  16. 2018年2月15日10:21 | #16

    What day is your garbage and yard waste collected?

评论分页
1 ... 3 4 5 368
订阅评论
2+8= (必填)