阅读背景:

Nagios监控nginx服务详细过程

来源:互联网 
  1. #!/bin/sh

  2. PROGNAME=`basename
    1. #!/bin/sh

    2. PROGNAME=`basename $0`
    3. VERSION=\\\"Version 1.1,\\\"
    4. AUTHOR=\\\"tim man\\\"

    5. ST_OK=0
    6. ST_WR=1
    7. ST_CR=2
    8. ST_UK=3
    9. hostname=\\\"localhost\\\"
    10. port=80
    11. path_pid=/var/run
    12. name_pid=\\\"nginx.pid\\\"
    13. status_page=\\\"nginx_status\\\"
    14. pid_check=1
    15. secure=0

    16. print_version() {
    17.     echo \\\"$VERSION $AUTHOR\\\"
    18. }

    19. print_help() {
    20.     print_version $PROGNAME $VERSION
    21.     echo \\\"\\\"
    22.     echo \\\"$PROGNAME is a Nagios plugin to check whether nginx is running.\\\"
    23.     echo \\\"It also parses the nginx\\\'s status page to get requests and\\\"
    24.     echo \\\"connections per second as well as requests per connection. You\\\"
    25.     echo \\\"may have to alter your nginx configuration so that the plugin\\\"
    26.     echo \\\"can access the server\\\'s status page.\\\"
    27.     echo \\\"The plugin is highly configurable for this reason. See below for\\\"
    28.     echo \\\"available options.\\\"
    29.     echo \\\"\\\"
    30.     echo \\\"$PROGNAME -H localhost -P 80 -p /var/run -n nginx.pid \\\"
    31.     echo \\\" -s nginx_statut -o /tmp [-w INT] [-c INT] [-S] [-N]\\\"
    32.     echo \\\"\\\"
    33.     echo \\\"Options:\\\"
    34.     echo \\\" -H/--hostname)\\\"
    35.     echo \\\" Defines the hostname. Default is: localhost\\\"
    36.     echo \\\" -P/--port)\\\"
    37.     echo \\\" Defines the port. Default is: 80\\\"
    38.     echo \\\" -p/--path-pid)\\\"
    39.     echo \\\" Path where nginx\\\'s pid file is being stored. You might need\\\"
    40.     echo \\\" to alter this path according to your distribution. Default\\\"
    41.     echo \\\" is: /var/run\\\"
    42.     echo \\\" -n/--name_pid)\\\"
    43.     echo \\\" Name of the pid file. Default is: nginx.pid\\\"
    44.     echo \\\" -N/--no-pid-check)\\\"
    45.     echo \\\" Turn this on, if you don\\\'t want to check for a pid file\\\"
    46.     echo \\\" whether nginx is running, e.g. when you\\\'re checking a\\\"
    47.     echo \\\" remote server. Default is: off\\\"
    48.     echo \\\" -s/--status-page)\\\"
    49.     echo \\\" Name of the server\\\'s status page defined in the location\\\"
    50.     echo \\\" directive of your nginx configuration. Default is:\\\"
    51.     echo \\\" nginx_status\\\"
    52.     echo \\\" -S/--secure)\\\"
    53.     echo \\\" In case your server is only reachable via SSL, use this\\\"
    54.     echo \\\" this switch to use HTTPS instead of HTTP. Default is: off\\\"
    55.     echo \\\" -w/--warning)\\\"
    56.     echo \\\" Sets a warning level for requests per second. Default is: off\\\"
    57.     echo \\\" -c/--critical)\\\"
    58.     echo \\\" Sets a critical level for requests per second. Default is:\\\"
    59.     echo \\\" off\\\"
    60.     exit $ST_UK
    61. }

    62. while test -n \\\"$1\\\"; do
    63.     case \\\"$1\\\" in
    64.         -help|-h)
    65.             print_help
    66.             exit $ST_UK
    67.             ;;
    68.         --version|-v)
    69.             print_version $PROGNAME $VERSION
    70.             exit $ST_UK
    71.             ;;
    72.         --hostname|-H)
    73.             hostname=$2
    74.             shift
    75.             ;;
    76.         --port|-P)
    77.             port=$2
    78.             shift
    79.             ;;
    80.         --path-pid|-p)
    81.             path_pid=$2
    82.             shift
    83.             ;;
    84.         --name-pid|-n)
    85.             name_pid=$2
    86.             shift
    87.             ;;
    88.         --no-pid-check|-N)
    89.             pid_check=0
    90.             ;;
    91.         --status-page|-s)
    92.             status_page=$2
    93.             shift
    94.             ;;
    95.         --secure|-S)
    96.             secure=1
    97.             ;;
    98.         --warning|-w)
    99.             warning=$2
    100.             shift
    101.             ;;
    102.         --critical|-c)
    103.             critical=$2
    104.             shift
    105.             ;;
    106.         *)
    107.             echo \\\"Unknown argument: $1\\\"
    108.             print_help
    109.             exit $ST_UK
    110.             ;;
    111.         esac
    112.     shift
    113. done

    114. get_wcdiff() {
    115.     if [ ! -z \\\"$warning\\\" -! -z \\\"$critical\\\" ]
    116.     then
    117.         wclvls=1

    118.         if [ ${warning} -ge ${critical} ]
    119.         then
    120.             wcdiff=1
    121.         fi
    122.     elif [ ! -z \\\"$warning\\\" --z \\\"$critical\\\" ]
    123.     then
    124.         wcdiff=2
    125.     elif [ -z \\\"$warning\\\" -! -z \\\"$critical\\\" ]
    126.     then
    127.         wcdiff=3
    128.     fi
    129. }

    130. val_wcdiff() {
    131.     if [ \\\"$wcdiff\\\" = 1 ]
    132.     then
    133.         echo \\\"Please adjust your warning/critical thresholds. The warning \\\\
    134. must be lower than the critical level!\\\"
    135.         exit $ST_UK
    136.     elif [ \\\"$wcdiff\\\" = 2 ]
    137.     then
    138.         echo \\\"Please also set a critical value when you want to use \\\\
    139. warning/critical thresholds!\\\"
    140.         exit $ST_UK
    141.     elif [ \\\"$wcdiff\\\" = 3 ]
    142.     then
    143.         echo \\\"Please also set a warning value when you want to use \\\\
    144. warning/critical thresholds!\\\"
    145.         exit $ST_UK
    146.     fi
    147. }

    148. check_pid() {
    149.     if [ -f \\\"$path_pid/$name_pid\\\" ]
    150.     then
    151.         retval=0
    152.     else
    153.         retval=1
    154.     fi
    155. }

    156. get_status() {
    157.     if [ \\\"$secure\\\" = 1 ]
    158.     then
    159.         wget_opts=\\\"-O- -q -t 3 -T 3 --no-check-certificate\\\"
    160.         #out1=`/usr/bin/wget ${wget_opts} http://${hostname}:${port}/${status_page}`
    161.        out1=`/usr/bin/wget -O- --t 3 -T 3 http://localhost:80/nginx_status`
    162.      sleep 1
    163.     out2=`/usr/bin/wget -O- --t 3 -T 3 http://localhost:80/nginx_status`
    164.     else 
    165.         wget_opts=\\\"-O- -q -t 3 -T 3\\\"
    166.     out1=`/usr/bin/wget -O- --t 3 -T 3 http://localhost:80/nginx_status` 
    167.     sleep 1
    168.         out2=`/usr/bin/wget -O- --t 3 -T 3 http://localhost:80/nginx_status`
    169.     fi
    170.     if [ -z \\\"$out1\\\" --z \\\"$out2\\\" ]
    171.     then
    172.         echo \\\"out1:$out1 out2:$out2, UNKNOWN - Local copy/copies of $status_page is empty.\\\"
    173.     exit $ST_UK
    174.     fi
    175. }

    176. get_vals() {
    177.     tmp1_reqpsec=`echo ${out1}|awk \\\'{print $10}\\\'`
    178.     tmp2_reqpsec=`echo ${out2}|awk \\\'{print $10}\\\'`
    179.     reqpsec=`expr $tmp2_reqpsec - $tmp1_reqpsec`

    180.     tmp1_conpsec=`echo ${out1}|awk \\\'{print $9}\\\'`
    181.     tmp2_conpsec=`echo ${out2}|awk \\\'{print $9}\\\'`
    182.     conpsec=`expr $tmp2_conpsec - $tmp1_conpsec`

    183.     reqpcon=`echo \\\"scale=2; $reqpsec / $conpsec\\\" | bc -l`
    184.     if [ \\\"$reqpcon\\\" = \\\".99\\\" ]
    185.     then
    186.         reqpcon=\\\"1.00\\\"
    187.     fi
    188. }

    189. do_output() {
    190.     output=\\\"nginx is running. $reqpsec requests per second, $conpsec connections per second ($reqpcon requests per connection)\\\"
    191. }

    192. do_perfdata() {
    193.     perfdata=\\\"\\\'reqpsec\\\'=$reqpsec \\\'conpsec\\\'=$conpsec \\\'conpreq\\\'=$reqpcon\\\"
    194. }

    195. # Here we 
    196. get_wcdiff
    197. val_wcdiff

    198. if [ ${pid_check} = 1 ]
    199. then
    200.     check_pid
    201.     if [ \\\"$retval\\\" = 1 ]
    202.     then
    203.         echo \\\"There\\\'s no pid file for nginx. Is nginx running? Please also make sure whether your pid path and name is correct.\\\"
    204.         exit $ST_CR
    205.     fi
    206. fi

    207. get_status
    208. get_vals
    209. do_output
    210. do_perfdata

    211. if [[ -n \\\"$warning\\\" ]] && [[ -n \\\"$critical\\\" ]]
    212. then
    213.     if [[ \\\"$reqpsec\\\" -ge \\\"$warning\\\" ]] && [[ \\\"$reqpsec\\\" -lt \\\"$critical\\\" ]]
    214.     then
    215.         echo \\\"WARNING - ${output} | ${perfdata}\\\"
    216.     exit $ST_WR
    217.     elif [ \\\"$reqpsec\\\" -ge \\\"$critical\\\" ]
    218.     then
    219.         echo \\\"CRITICAL - ${output} | ${perfdata}\\\"
    220.     exit $ST_CR
    221.     else
    222.         echo \\\"OK - ${output} | ${perfdata} ]\\\"
    223.     exit $ST_OK
    224.     fi
    225. else
    226.     echo \\\"OK - ${output} | ${perfdata}\\\"
    227.     exit $ST_OK
    228. fi

    `



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: