且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

nagios监控nginx status

更新时间:2022-09-25 18:30:24

网上已经有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,如需转载请自行联系原作者