nagios监控nginx status

简介:

网上已经有nagios 的 nginx的脚本了,有通过本地拿数据通过check_nrpe 传输给nagios的,也有pl写的通过get status 页面来获得数据的。

我自己也做了个。通过wget nginx status页面,来拆分数据·来实现报警功能的。至于出图,暂时还没有做测试。稍后会把出图数据以及pnp模版发布上来。

脚本如下:

nginx $> cat check_nginx.sh
#!/bin/bash
PROGNAME=`basename $0`
VERSION="Version 1.0"
AUTHOR="2010.11.18-www.nginxs.com"

ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

print_version() {
        echo "$VERSION $AUTHOR"
}

print_help() {
        print_version $PROGNAME  $VERSION
        echo "$PROGNAME is a Nagios plugin to monitor nginx status"
        echo "Use of wget nginxstatus page"
        echo "When using optional warning/critical thresholds all values except"
        echo "Usage parameters:"
        echo ""
        echo "$PROGNAME [-u|--url] [-p|--path] [-w/--warning] [-c/--critical]"
        echo ""
        echo "Options:"
                echo "  --url|-u)"
                echo "       Sets nginx status url"
                echo ""
                echo "  --path|-p)"
                echo "       Sets nginx status url path"
                echo ""
                echo "  --warning|-w)"
                echo "       Sets a warning level for nginx Active connections. Default is: off"
                echo ""
                echo "  --critical|-c)"
                echo "       Sets a critical level for nginx Active connections. Default is: off"
        echo ""
        echo "Example:"
        echo "http://www.nginxs.com/status"
        echo "./check_nginx.sh -u www.nginxs.com -p /status -w 10000 -c 15000"
        exit $ST_UK
}

while test -n "$1";do
   case "$1" in
        --help|-h)
                print_help
                exit $ST_UK
                ;;
        --url|-u)
                url=$2
                shift
                ;;
        --path|-p)
                path=$2
                shift
                ;;
        --warning|-w)
                warn=$2
                shift
                ;;
        --critical|-c)
                crit=$2
                shift
                ;;
        *)
                echo "Unknown argument: $1"
                print_help
                exit $ST_UK
                ;;
    esac
    shift
done

if [ -z $url ];then
        echo "Must Sets --url|-u) Parameters"
        exit $ST_UK
elif [ -z $path ];then
        echo "Must sets --path|-p) Parameters"
        echo "Please look help"
        echo "$PROGNAME --help"
        exit $ST_UK
fi

if [ -n "$warn" -a -n "$crit" ];then
        if [ $warn -ge $crit ];then
                echo "Please adjust your warning/critical thresholds. The warning must be lower than the critical level!"
                exit $ST_UK
        fi
fi

do_status() {
wget -qNO /tmp/nginx.html ${url}${path}
ActiveConn=`awk -F: {'print $2'} /tmp/nginx.html |head -1`
serveraccepts=`awk {'print $1'} /tmp/nginx.html |tail -n 2|head -1`
handled=`awk {'print $2'} /tmp/nginx.html |tail -n 2|head -1`
requests=`awk {'print $3'} /tmp/nginx.html |tail -n 2|head -1`
reading=`tail -n 1 /tmp/nginx.html|awk {'print $2'}`
writing=`tail -n 1 /tmp/nginx.html|awk {'print $4'}`
waiting=`tail -n 1 /tmp/nginx.html|awk {'print $6'}`
}
do_output() {
        output="ActiveConn:${ActiveConn},serveraccepts:${serveraccepts},handled:${handled},requests:${requests},reading:${reading},writing:${writing},waiting:${waiting}"
}
do_perfdata() {
        perfdata="'ActiveConn'=${ActiveConn},'serveraccepts'=${serveraccepts},'handled'=${handled},'requests'=${requests},'reading'=${reading},'writing'=${writing},'waiting'=${waiting}"
}

do_status
do_output
do_perfdata

if [ -n "warn" -a -n "$crit" ];then
        if [ $ActiveConn -ge $warn -a $ActiveConn -lt $crit ];then
                echo  "WARNING - $output |$perfdata"
                exit $ST_WR
        elif [ $ActiveConn -ge $crit ];then
                echo  "CRITICAL - $output|$perfdata"
                exit $ST_CR
        else
                echo  "OK - $output|$perfdata"
                exit $ST_OK
        fi
else
        echo "OK - $output|$perfdata"
        exit $ST_OK
fi

使用说明:

nginx $> ./check_nginx.sh -h
Version 1.0 2010.11.18-www.nginxs.com
check_nginx.sh is a Nagios plugin to monitor nginx status
Use of wget nginxstatus page
When using optional warning/critical thresholds all values except
Usage parameters:

check_nginx.sh [-u|--url] [-p|--path] [-w/--warning] [-c/--critical]

Options:
  --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:
http://www.nginxs.com/status
./check_nginx.sh -u www.nginxs.com -p /status -w 10000 -c 15000
nginx $> ./check_nginx.sh -u stat.123.com -p / -w 10000 -c 15000
OK - ActiveConn: 2 ,serveraccepts:61241,handled:61241,requests:107647,reading:0,writing:1,waiting:1|'ActiveConn'=2 ,'serveraccepts'=61241,'handled'=61241,'requests'=107647,'reading'=0,'writing'=1,'waiting'=1

此脚本我做了两份,一份是通过 wget nginx status页面·来分析数据,另一个是通过 curl 页面来分析数据的·所以我上传两个版本供大家选择

wget nginx status :
check_nginx.sh

curl ngin status
check_curl_nginx.sh

 

本文转自Deidara 51CTO博客,原文链接:http://blog.51cto.com/deidara/429213,如需转载请自行联系原作者
相关文章
|
运维 监控 应用服务中间件
【运维知识进阶篇】zabbix5.0稳定版详解3(监控Nginx+PHP服务状态信息)(二)
【运维知识进阶篇】zabbix5.0稳定版详解3(监控Nginx+PHP服务状态信息)(二)
237 0
|
监控 数据可视化 Java
又一款Nginx 管理可视化神器!通过界面完成配置监控,一条龙!
又一款Nginx 管理可视化神器!通过界面完成配置监控,一条龙!
996 0
又一款Nginx 管理可视化神器!通过界面完成配置监控,一条龙!
|
4月前
|
消息中间件 NoSQL Kafka
基于Kafka的nginx日志收集分析与监控平台(3)
基于Kafka的nginx日志收集分析与监控平台(3)
|
4月前
|
消息中间件 监控 Kafka
基于Kafka的nginx日志收集分析与监控平台(2)
基于Kafka的nginx日志收集分析与监控平台(2)
|
4月前
|
消息中间件 负载均衡 应用服务中间件
基于Kafka的nginx日志收集分析与监控平台(1)
基于Kafka的nginx日志收集分析与监控平台(1)
|
运维 监控 网络协议
【运维知识进阶篇】集群架构-Nginx常用模块(目录索引+状态监控+访问控制+访问限制)(下)
【运维知识进阶篇】集群架构-Nginx常用模块(目录索引+状态监控+访问控制+访问限制)(下)
87 0
|
运维 监控 应用服务中间件
【运维知识进阶篇】集群架构-Nginx常用模块(目录索引+状态监控+访问控制+访问限制)(上)
【运维知识进阶篇】集群架构-Nginx常用模块(目录索引+状态监控+访问控制+访问限制)
104 0
|
监控 应用服务中间件 BI
如何使用 Zabbix 监控 Nginx?
如何使用 Zabbix 监控 Nginx?
309 6
|
监控 数据可视化 应用服务中间件
吹爆,Nginx 可视化!配置监控一条龙
nginx-gui这个东西真的要吹一波,太好用了,而且源码公开。对于我这样一个作为Linux菜鸟的Java开发来说,这个可视化配置工具简直是福音!
|
监控 应用服务中间件 nginx
【2022】Nginx基础状态监控
【2022】Nginx基础状态监控
80 0
【2022】Nginx基础状态监控