首页 > 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. 2017年10月20日22:17 | #1

    Hey just wanted to give you a quick heads up. The text in your post
    seem to be running off the screen in Internet explorer.

    I’m not sure if this is a formatting issue or something to do with browser compatibility but I
    thought I’d post to let you know. The layout look great though!

    Hope you get the problem fixed soon. Many thanks

  2. 2017年10月21日20:05 | #2

    hello!,I really like your writing so a lot! share we keep in touch extra approximately your
    article on AOL? I need a specialist in this area to solve my problem.
    May be that is you! Looking ahead to look you.

  3. 2017年10月23日10:07 | #3

    Currently it sounds like BlogEngine is the top blogging platform available right now.
    (from what I’ve read) Is that what you are using on your blog?

  4. 2017年10月23日12:26 | #4

    Cialis Am Gunstigsten viagra Mail Order Tab Amoxicilina In Internet Xenical 120mg To Buy Online

  5. 2017年10月23日14:18 | #5

    Very shortly this web page will be famous among all blogging and site-building
    viewers, due to it’s fastidious articles

  6. 2017年10月26日19:02 | #6

    Nice blog here! Also your web site loads up fast!
    What web host are you using? Can I get your affiliate link to your host?
    I wish my site loaded up as fast as yours lol

  7. 2017年10月27日16:30 | #7

    It is perfect time to make a few plans for the longer term and it’s time
    to be happy. I’ve learn this put up and if I may just I want to recommend you some
    attention-grabbing issues or suggestions. Maybe you could write subsequent articles referring to this article.
    I wish to learn more issues about it!

  8. 2017年10月27日19:47 | #8

    I do not even know the way I ended up here, but I
    assumed this put up was good. I do not understand who
    you’re however definitely you are going to a well-known blogger
    for those who aren’t already. Cheers!

  9. 2017年10月28日11:04 | #9

    I’ve been browsing online greater than three hours lately, yet
    I never found any fascinating article like yours.
    It’s beautiful price sufficient for me. Personally, if all site owners and bloggers made just right content as you
    did, the internet will likely be a lot more helpful than ever before.

  10. 2017年10月28日20:08 | #10

    Propecia En Ligne 1mg viagra Cheapviagra From India Viagra Meltabs

  11. 2017年10月29日01:27 | #11

    Greetings from Los angeles! I’m bored to tears at work so I decided
    to check out your site on my iphone during lunch break.
    I love the information you present here and can’t wait
    to take a look when I get home. I’m amazed at how fast
    your blog loaded on my phone .. I’m not even using
    WIFI, just 3G .. Anyways, good site!

  12. 2017年10月29日14:05 | #12

    Hello there! This post could not be written any better!

    Reading this post reminds me of my good old room
    mate! He always kept chatting about this. I will forward this write-up to him.
    Pretty sure he will have a good read. Many thanks for sharing!

  13. 2017年10月30日01:03 | #13

    Levitra 20 Mg Problemi Amoxicillin Maximum Dose cialis price Generic Keflex Pills Buy Albuterol Inhaler In Canada

  14. 2017年10月30日03:00 | #14

    Hello, I enjoy reading all of your post. I like to
    write a little comment to support you.

  15. 2017年10月30日13:50 | #15

    Howdy! I could have sworn I’ve visited this
    website before but after looking at a few of the articles I realized it’s new to me.
    Nonetheless, I’m certainly happy I came across it and I’ll
    be book-marking it and checking back often!

  16. 2017年10月31日00:04 | #16

    I’m impressed, I must say. Rarely do I come across a blog that’s both
    equally educative and interesting, and let me tell you,
    you’ve hit the nail on the head. The issue is something that not enough folks
    are speaking intelligently about. I am very happy I came across this during my hunt for something regarding this.

  17. 2017年10月31日12:55 | #17

    hi!,I like your writing so much! proportion we communicate more approximately your
    post on AOL? I require a specialist in this space to resolve my problem.
    May be that’s you! Looking forward to peer you.

  18. 2017年11月1日02:39 | #18

    Levitra Vs Viagra cialis Isotretinoin With Free Shipping Visa

  19. 2017年11月1日09:57 | #19

    Why viewers still make use of to read news papers when in this technological world everything is
    accessible on web?

  20. 2017年11月1日10:36 | #20

    I’m not that much of a online reader to be honest but your sites really nice, keep it up!
    I’ll go ahead and bookmark your website to come back later.
    Many thanks

  21. 2017年11月2日03:01 | #21

    Hi there! I simply would like to give you a big thumbs up for your great information you have got right here on this post.
    I will be coming back to your web site for more soon.

  22. 2017年11月2日14:46 | #22

    you’re in point of fact a excellent webmaster. The site loading speed is amazing.
    It kind of feels that you are doing any distinctive
    trick. In addition, The contents are masterwork.
    you have done a excellent activity in this matter!

  23. 2017年11月3日05:12 | #23

    Howdy! I know this is kinda off topic nevertheless I’d figured I’d ask.
    Would you be interested in trading links or maybe
    guest writing a blog article or vice-versa? My
    site goes over a lot of the same topics as yours and I think we could greatly benefit from each other.
    If you’re interested feel free to send me an email. I look forward
    to hearing from you! Great blog by the way!

  24. 2017年11月4日04:06 | #24

    Howdy very nice site!! Man .. Beautiful .. Wonderful ..
    I’ll bookmark your blog and take the feeds also? I’m satisfied
    to seek out so many helpful information right here within the
    publish, we need develop more techniques on this regard, thank you
    for sharing. . . . . .

  25. 2017年11月4日18:52 | #25

    Woah! I’m really digging the template/theme of this blog.

    It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness
    and visual appearance. I must say you have done a very good job with this.
    Additionally, the blog loads super quick for me on Firefox.
    Excellent Blog!

  26. 2017年11月6日23:53 | #26

    Hi! I’m at work browsing your blog from my new iphone 4!
    Just wanted to say I love reading through
    your blog and look forward to all your posts! Carry on the excellent work!

  27. 2017年11月10日10:49 | #27

    hi!,I like your writing very a lot! share we keep up a correspondence extra approximately your post on AOL?
    I require an expert in this house to solve my problem.

    May be that is you! Taking a look ahead to see you.

  28. 2017年11月12日04:01 | #28

    Hello There. I found your blog using msn. This is an extremely well
    written article. I will be sure to bookmark it and come
    back to read more of your useful information. Thanks for the post.
    I’ll certainly return.

  29. 2017年11月12日09:20 | #29

    Green Line Phmcy Propecia Fiabilidad Other Names For Amoxicillin viagra Cephalexin For Dogs Soide Effects

  30. 2017年11月12日14:30 | #30

    Hi! This is kind of off topic but I need some guidance from an established blog.
    Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about setting up my own but I’m not sure where
    to begin. Do you have any tips or suggestions?
    Thanks

  31. 2017年11月12日21:23 | #31

    It’s a pity you don’t have a donate button! I’d certainly donate to this brilliant
    blog! I guess for now i’ll settle for bookmarking and adding your RSS feed
    to my Google account. I look forward to brand new updates and will
    share this website with my Facebook group. Talk soon!

  32. 2017年11月13日14:43 | #32

    WOW just what I was searching for. Came here by searching for
    hello

  33. 2017年11月13日17:44 | #33

    Hello just wanted to give you a quick heads up. The words in your article seem to be running
    off the screen in Ie. I’m not sure if this is a formatting issue or something to
    do with web browser compatibility but I thought I’d post to let
    you know. The style and design look great though! Hope you get the problem resolved soon. Thanks

  34. 2017年11月14日11:53 | #34

    Do you know of falling courtyard panel in the
    house?

  35. 2017年11月15日18:40 | #35

    Marvelous, what a weblog it is! This web site provides helpful facts to us,
    keep it up.

  36. 2017年11月17日13:31 | #36

    Colchicine 0.5mg Where Can I Buy Flagyl Er Acheter Kamagra Gel viagra My Canadian Pharcharmy Online Want To Buy Direct Macrobid Low Price

  37. 2017年11月20日21:31 | #37

    Wow! Finally I got a website from where I know how to truly take valuable data concerning my study and knowledge.

  38. 2017年11月22日03:33 | #38

    Prednisolone Nextday levitra shipped from us Levitra Generico Online Prezzo Propecia Laboratorio

  39. 2017年11月22日04:51 | #39

    i need money right now
    3000 dollar loan
    loans for bad credit direct lenders
    easy online payday loans
    castle payday loans

  40. 2017年11月28日23:38 | #40

    Does your website have a contact page? I’m having trouble locating it but, I’d like to send you
    an email. I’ve got some ideas for your blog you might be interested
    in hearing. Either way, great site and I
    look forward to seeing it improve over time.

  41. 2017年12月1日10:00 | #41

    I must thank you for the efforts you’ve put in penning this website.
    I’m hoping to see the same high-grade content from
    you later on as well. In fact, your creative writing abilities has
    encouraged me to get my very own website now ;)

  42. 2017年12月1日11:07 | #42

    online payday loans
    payday advance
    online loans
    cash advance
    cash advance loans

  43. 2017年12月8日15:38 | #43

    best us casinos online
    play casino online
    casino games
    slot games
    slot machines

  44. 2017年12月16日01:11 | #44

    Great beat ! I would like to apprentice at the same time
    as you amend your website, how can i subscribe for a weblog web site?
    The account helped me a acceptable deal. I had been tiny bit familiar of this your broadcast provided bright clear idea

  45. 2017年12月20日11:20 | #45

    online casino real money
    online casino
    casino games list
    casino online
    casino games

  46. 2018年1月2日20:16 | #46

    new online casino us players
    casino real money
    best online casino keno

  47. 2018年1月3日04:37 | #47

    loans for bad credit
    payday loans no credit
    loans for bad credit
    loans no credit check
    payday loans online no credit check

  48. 2018年1月12日12:12 | #48

    bloque bicicleta autocad
    autocad 2017
    autodesk autocad educational download

  49. 2018年1月14日17:43 | #49

    loans no credit
    payday loans no credit
    payday loans no credit check
    loans no credit check
    payday loans no credit check

  50. 2018年1月14日19:09 | #50

    casino games
    online casino
    casino online
    casino games
    casino online

评论分页
订阅评论
3+7= (必填)