更新check_nginx插件

简介:

新增功能:nginx BasicAuth 认证功能,
去掉了显示:serveraccepts handled requests
只显示主要的: ActiveConn reading writing waiting
优化代码可以省去不需要的参数。

支持

源码:

# -*- coding: utf-8 -*-
#!/usr/bin/python
# check_nginx is a Nagios to monitor nginx statu
# my blog: http://www.nginxs.com
# The version is 1.0.1
# 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][-u|--user][-p|--passwd][-w|--warning][-c|--critical]

   Options:
          --help|-h)
            print check_nginx help.
          --url|-U)
            Sets nginx status url.
          --path|-P)
            Sets nginx status url path. Default is: off
          --user|-u)
            Sets nginx status BasicAuth user. Default is: off
          --passwd|-p)
            Sets nginx status BasicAuth passwd. Default is: off
          --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 -u eric -p nginx -w 1000 -c 2000
            if dont't have password:
            ./check_nginx -U www.nginxs.com -P /status -w 1000 -c 2000
            if don't have path and password:
            ./check_nginx -U www.nginxs.com -w 1000 -c 2000"""

   sys.exit(3)

try:
    options,args = getopt.getopt(sys.argv[1:],"hU:P:u:p:w:c:",["help","url=","path=","user=","passwd=","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 ("-u","--user"):
       user = value
    if name in ("-p","--passwd"):
       passwd = value
    if name in ("-w","--warning"):
       warning = value
    if name in ("-c","--critical"):
       critical = value
try:
   if 'path' in dir():
      req = urllib2.Request(url+path)
   else:
      req = urllib2.Request(url)
   if 'user' in dir() and 'passwd' in dir():
      passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
      passman.add_password(None, url+path, user, passwd)
      authhandler = urllib2.HTTPBasicAuthHandler(passman)
      opener = urllib2.build_opener(authhandler)
      urllib2.install_opener(opener)
   response = urllib2.urlopen(req)
   the_page = response.readline()
   conn = the_page.split()
   ActiveConn = conn[2]
   the_page1 = response.readline()
   the_page2 = response.readline()
   the_page3 = response.readline()
   response.close()
   b = the_page3.split()
   reading = b[1]
   writing = b[3]
   waiting = b[5]
   output = 'ActiveConn:%s,reading:%s,writing:%s,waiting:%s' % (ActiveConn,reading,writing,waiting)
   perfdata = 'ActiveConn:%s,reading:%s,writing:%s,waiting:%s' % (ActiveConn,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)

使用方法:

./check_nginx -U www.nginxs.com -P /status -ueric -pnginx
OK - ActiveConn:1,reading:0,writing:1,waiting:0|ActiveConn:1,reading:0,writing:1,waiting:0
./check_nginx -U stat.nginxs.com
OK - ActiveConn:1,reading:0,writing:1,waiting:0|ActiveConn:1,reading:0,writing:1,waiting:0
 

本文转自Deidara 51CTO博客,原文链接:http://blog.51cto.com/deidara/485502,如需转载请自行联系原作者
相关文章
|
3月前
|
存储 应用服务中间件 数据库
C++文件服务器项目—Nginx+FastDFS插件—5
C++文件服务器项目—Nginx+FastDFS插件—5
40 0
|
4月前
|
Kubernetes Cloud Native Java
云原生|kubernetes|ingress-nginx插件部署(kubernetes-1.23和最新版controller-1.6.4)
云原生|kubernetes|ingress-nginx插件部署(kubernetes-1.23和最新版controller-1.6.4)
272 0
|
4月前
|
Kubernetes 应用服务中间件 nginx
云原生|kubernetes|ingress-nginx插件部署以及简单的应用(修订版---适用于kubernetes-1.18-1.21)
云原生|kubernetes|ingress-nginx插件部署以及简单的应用(修订版---适用于kubernetes-1.18-1.21)
89 0
|
域名解析 Kubernetes Cloud Native
云原生|kubernetes|ingress-nginx插件部署以及简单的应用(修订版)(二)
云原生|kubernetes|ingress-nginx插件部署以及简单的应用(修订版)
292 0
云原生|kubernetes|ingress-nginx插件部署以及简单的应用(修订版)(二)
|
域名解析 Kubernetes 负载均衡
云原生|kubernetes|ingress-nginx插件部署以及简单的应用(修订版)(一)
云原生|kubernetes|ingress-nginx插件部署以及简单的应用(修订版)
420 0
|
运维 监控
Zabbix监控插件-Nginx、TCP、Redis、Memcached
2014年写的zabbix监控插件,主要监控TCP的11种状态,Nginx的状态,Redis的和Memcached的状态。
309 0
Zabbix监控插件-Nginx、TCP、Redis、Memcached
|
应用服务中间件 视频直播 nginx
使用 nginx 和 rtmp 插件搭建视频直播和点播服务器
使用 nginx 和 rtmp 模块 ,可以很容易地搭建一个视频直播和点播服务器出来。 首先,看一下最经典的参考文献: How to set up your own private RTMP server using nginx 1. 安装 nginx 和 rtmp 模块 有关 nginx 的编译和安装比较简单,这里就不介绍了,看参考文献。
3134 0
|
24天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
27 0
|
23天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
92 0
|
3天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
12 0