#!/bin/sh
#
# Plugin to monitor the response codes of tiles returned by mod_tile
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Copyright (c) 2007 - 2020 by mod_tile contributors (see AUTHORS file)
#
# 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, see http://www.gnu.org/licenses/.

if [ "$1" = "config" ]; then

  echo 'graph_title mod_tile responses by zoom layer'
  echo 'graph_args --base 1000 -l 0'
  echo 'graph_vlabel responses per ${graph_period}'
  echo 'graph_category mod_tile'
  echo 'z1.label z1-8'
  echo 'z1.draw AREA'
  echo 'z1.type DERIVE'
  echo 'z1.min 0'
  echo 'z2.label z9-12'
  echo 'z2.draw STACK'
  echo 'z2.type DERIVE'
  echo 'z2.min 0'
  echo 'z3.label z13-14'
  echo 'z3.draw STACK'
  echo 'z3.type DERIVE'
  echo 'z3.min 0'
  echo 'z4.label z15-16'
  echo 'z4.draw STACK'
  echo 'z4.type DERIVE'
  echo 'z4.min 0'
  echo 'z5.label z17-18'
  echo 'z5.draw STACK'
  echo 'z5.type DERIVE'
  echo 'z5.min 0'
  echo 'z6.label z19-20'
  echo 'z6.draw STACK'
  echo 'z6.type DERIVE'
  echo 'z6.min 0'
  exit 0
fi

data=$(wget -q http://localhost/mod_tile -O -)

resp_0=$(expr match "$data" '.*NoRespZoom00: \([0-9]*\)')
resp_1=$(expr match "$data" '.*NoRespZoom01: \([0-9]*\)')
resp_2=$(expr match "$data" '.*NoRespZoom02: \([0-9]*\)')
resp_3=$(expr match "$data" '.*NoRespZoom03: \([0-9]*\)')
resp_4=$(expr match "$data" '.*NoRespZoom04: \([0-9]*\)')
resp_5=$(expr match "$data" '.*NoRespZoom05: \([0-9]*\)')
resp_6=$(expr match "$data" '.*NoRespZoom06: \([0-9]*\)')
resp_7=$(expr match "$data" '.*NoRespZoom07: \([0-9]*\)')
resp_8=$(expr match "$data" '.*NoRespZoom08: \([0-9]*\)')
resp_9=$(expr match "$data" '.*NoRespZoom09: \([0-9]*\)')
resp_10=$(expr match "$data" '.*NoRespZoom10: \([0-9]*\)')
resp_11=$(expr match "$data" '.*NoRespZoom11: \([0-9]*\)')
resp_12=$(expr match "$data" '.*NoRespZoom12: \([0-9]*\)')
resp_13=$(expr match "$data" '.*NoRespZoom13: \([0-9]*\)')
resp_14=$(expr match "$data" '.*NoRespZoom14: \([0-9]*\)')
resp_15=$(expr match "$data" '.*NoRespZoom15: \([0-9]*\)')
resp_16=$(expr match "$data" '.*NoRespZoom16: \([0-9]*\)')
resp_17=$(expr match "$data" '.*NoRespZoom17: \([0-9]*\)')
resp_18=$(expr match "$data" '.*NoRespZoom18: \([0-9]*\)')
resp_19=$(expr match "$data" '.*NoRespZoom19: \([0-9]*\)')
resp_20=$(expr match "$data" '.*NoRespZoom20: \([0-9]*\)')

if [ -z "$resp_19" ]; then
  resp_19=0
fi
if [ -z "$resp_20" ]; then
  resp_20=0
fi

echo "z1.value " $(expr $resp_0 + $resp_1 + + $resp_2 + $resp_3 + $resp_4 + $resp_5 + $resp_6 + $resp_7 + $resp_8)
echo "z2.value " $(expr $resp_9 + $resp_10 + + $resp_11 + $resp_12)
echo "z3.value " $(expr $resp_13 + $resp_14)
echo "z4.value " $(expr $resp_15 + $resp_16)
echo "z5.value " $(expr $resp_17 + $resp_18)
echo "z6.value " $(expr $resp_19 + $resp_20)
