#!/bin/bash
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

WHAT="$1"
PORT="${2:-80}"
HOSTNAME="${3:-127.0.0.1}"
if [ -z "$WHAT" ];then
   echo "$0 reader|writer|keepalive|accepts|handled|requests"
   exit 1
fi

wget -q --user-agent "ZABBIX" --timeout=5 -O- "http://$HOSTNAME:$PORT/basic_status"|awk -v get=$WHAT '
    /Active connections:/{
     if (get == "total"){
            printf("%s",$3);
	      }
    }
    /^ [0-9][0-9]* [0-9][0-9]* [0-9][0-9]*/{
         if (get == "accepts"){
            printf("%s",$1);
	      }
         if (get == "handled"){
            printf("%s",$2);
	      }
         if (get == "requests"){
            printf("%s",$3);
	      }
    }
    /Reading: .* Writing: .* Waiting: .*/{
         if (get == "reader"){
            printf("%s",$2);
	      }
         if (get == "writer"){
            printf("%s",$4);
	      }
         if (get == "keepalive"){
            printf("%s",$6);
	      }

     }
'

