#!/bin/bash source /usr/lib/nagios/plugins/utils.sh PROGNAME=`basename $0` EXTCODE=$STATE_OK status_query_default="/status" status_port_default="80" active_warn_tr_default=1000 active_crit_tr_default=2000 DEBUGLOG="/tmp/nagios.frontend_conn_states.log" print_usage() { echo "Usage: $PROGNAME -H your.host.with.nginx.ru -w INT -c INT [-P 80] [-Q /status] [-F]" echo "Options:" echo "-H - Hostname where nginx server is running" echo "-w - warning treshhold, integer [Default: $active_warn_tr_default]" echo "-c - critical treshhold, integer [Default: $active_crit_tr_default]" echo "NOTICE: warning & critical are compared to 5 minute interval value" echo "-Q - Query string, like /status [Default: $status_query_default]" echo "-P - daemon port [Default: $status_port_default ]" echo "-F - show performance data" } print_help() { echo $PROGNAME $REVISION echo "" print_usage echo "" echo "Will show connection states info based on status module counters" echo "" exit $UNKNOWN } ### Parse Arguments while getopts ":hV:H:w:c:Q:P:F" Option; do case $Option in h) print_help exit 0 ;; V) print_revision $PROGNAME $REVISION exit 0 ;; H) status_host=${OPTARG} ;; w) active_warn_tr=${OPTARG} ;; c) active_crit_tr=${OPTARG} ;; Q) status_query=${OPTARG} ;; P) status_port=${OPTARG} ;; F) show_perfdata="yes" ;; *) print_help exit 0 ;; esac done shift $(($OPTIND - 1)) if [[ "x$status_host" == "x" ]] then echo "No hostname specified, hostname should be like your.host.with.nginx.ru" exit $STATE_UNKNOWN fi if [[ "x$status_query" == "x" ]] then status_query=$status_query_default fi if [[ "x$status_port" == "x" ]] then status_port=$status_port_default fi if [[ "x$active_warn_tr" == "x" ]] then active_warn_tr=$active_warn_tr_default fi if [[ "x$active_crit_tr" == "x" ]] then active_crit_tr=$active_crit_tr_default fi c=0 if ! let c+=$active_warn_tr 2>/dev/null;then echo "warning value must be integer";exit_unknown="yes";fi c=0 if ! let c+=$active_crit_tr 2>/dev/null;then echo "critical value must be integer";exit_unknown="yes" ;fi if [[ "x$exit_unknown" == "xyes" ]] then exit $STATE_UNKNOWN fi c=0 #exit 0 status_url="${status_host}:${status_port}${status_query}" #URL=$1 #echo $status_url #exit 0 reslt=$(lynx --dump $status_url 2>&1) reslt_code=$? #echo "return $reslt_code" if [[ "x$reslt_code" != "x0" ]] then if [[ $reslt_code -eq 1 ]] then echo "Unable to access $status_url, lynx error is:$reslt" exit $STATE_CRITICAL else echo "lynx returned $reslt_code as return code, message is:$reslt" exit $STATE_UNKNOWN fi fi if [[ "x$reslt" != "x" ]] then speeddata=$(echo "$reslt" |grep -E "(Active connections:\ |Reading:)") speeddata=$(echo $speeddata|cut -d\ -f 3,5,7,9) EXITTEXT=$(echo -n $speeddata) DATA_TMP=`mktemp /tmp/nagios_check.XXXXXXXX` echo $speeddata > $DATA_TMP #echo "result is:$reslt" >> $DEBUGLOG read fe_conn_active fe_conn_reading fe_conn_writing fe_conn_waiting fe_rest < $DATA_TMP rm -rf $DATA_TMP #echo "1min:${libwebserver_req_1m}" >> $DEBUGLOG #echo "5min:${libwebserver_req_5m}" >> $DEBUGLOG #echo "15min:${libwebserver_req_15m}" >> $DEBUGLOG k=0 for i in $fe_conn_active $fe_conn_reading $fe_conn_writing $fe_conn_waiting do let k+=1 c=1 #echo "i is $i" valuecheck=$(let "c+=$i" 2>&1) valretcode=$? if [[ ( $valretcode != 0 ) || (($c < 0)) ]] then #echo "fuck! $c" exit_unknown="yes" fe_conn_states[$k]=0 else fe_conn_states[$k]=$i fi done #here should be min & max check, and 1m / 5m / 15m difference check if [[ ("x$show_perfdata" == "xyes") && ("x$speeddata" != "x") && ("x$exit_unknown" != "xyes") ]] then #exit 0 TEXTPERFDATA="|'active'=${fe_conn_states[1]};$active_warn_tr;$active_crit_tr;0; 'reading'=${fe_conn_states[2]};;;0; 'writing'=${fe_conn_states[3]};;;0; 'waiting'=${fe_conn_states[4]};;;0;" #echo "exit text: $EXITTEXT" >> $DEBUGLOG fi if [[ "x$exit_unknown" == "xyes" ]] then echo "RAW TEXT:$EXITTEXT" exit $STATE_UNKNOWN else ERRTEXT="OK: ${fe_conn_states[1]} < $active_warn_tr" if [[ ${fe_conn_states[2]} -ge $active_warn_tr ]] then EXTCODE=$STATE_WARNING ERRTEXT="warning treshhold: ${fe_conn_states[1]} > $active_warn_tr" fi if [[ ${fe_conn_states[1]} -ge $active_crit_tr ]] then EXTCODE=$STATE_CRITICAL ERRTEXT="critical treshhold: ${fe_conn_states[2]} > $active_crit_tr" fi #this should be redesigned echo "active connections count $ERRTEXT $TEXTPERFDATA" exit $EXTCODE fi else echo "status is empty, whattafuck?" exit $STATE_UNKNOWN fi