#!/bin/sh
#
# Plugin to monitor the rendering throughput of Renderd
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Copyright (c) 2007 - 2023 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 Renderd throughput by zoom'
  echo 'graph_args --base 1000 -l 0'
  echo 'graph_vlabel Metatiles per ${graph_period}'
  echo 'graph_category renderd'
  echo 'graph_info Displays the number of metatiles being rendered by renderd per ${graph_period}'
  echo 'z1.label zoom z0 - z8'
  echo 'z1.type DERIVE'
  echo 'z1.min 0'
  echo 'z1.draw AREA'
  echo 'z1.info Throughput of Metatiles for z0 - z8'
  echo 'z2.label zoom z9 - z12'
  echo 'z2.type DERIVE'
  echo 'z2.min 0'
  echo 'z2.draw STACK'
  echo 'z2.info Throughput of Metatiles for z9 - z12'
  echo 'z3.label zoom z13 - z14'
  echo 'z3.type DERIVE'
  echo 'z3.min 0'
  echo 'z3.draw STACK'
  echo 'z3.info Throughput of Metatiles for z13 - z14'
  echo 'z4.label zoom z15 - z16'
  echo 'z4.type DERIVE'
  echo 'z4.min 0'
  echo 'z4.draw STACK'
  echo 'z4.info Throughput of Metatiles for z15 - z16'
  echo 'z5.label zoom z17 - z18'
  echo 'z5.type DERIVE'
  echo 'z5.min 0'
  echo 'z5.draw STACK'
  echo 'z5.info Throughput of Metatiles for z17 - z18'
  echo 'z6.label zoom z19 - z20'
  echo 'z6.type DERIVE'
  echo 'z6.min 0'
  echo 'z6.draw STACK'
  echo 'z6.info Throughput of Metatiles for z19 - z20'

  exit 0
fi

req0=$(sed -e '/^ZoomRendered00/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req1=$(sed -e '/^ZoomRendered01/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req2=$(sed -e '/^ZoomRendered02/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req3=$(sed -e '/^ZoomRendered03/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req4=$(sed -e '/^ZoomRendered04/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req5=$(sed -e '/^ZoomRendered05/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req6=$(sed -e '/^ZoomRendered06/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req7=$(sed -e '/^ZoomRendered07/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req8=$(sed -e '/^ZoomRendered08/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req9=$(sed -e '/^ZoomRendered09/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req10=$(sed -e '/^ZoomRendered10/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req11=$(sed -e '/^ZoomRendered11/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req12=$(sed -e '/^ZoomRendered12/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req13=$(sed -e '/^ZoomRendered13/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req14=$(sed -e '/^ZoomRendered14/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req15=$(sed -e '/^ZoomRendered15/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req16=$(sed -e '/^ZoomRendered16/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req17=$(sed -e '/^ZoomRendered17/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req18=$(sed -e '/^ZoomRendered18/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req19=$(sed -e '/^ZoomRendered19/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req20=$(sed -e '/^ZoomRendered20/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)

if [ -z req19 ]; then req19=0; fi
if [ -z req20 ]; then req20=0; fi

echo "z1.value " $(expr $req0 + $req1 + + $req2 + $req3 + $req4 + $req5 + $req6 + $req7 + $req8)
echo "z2.value " $(expr $req9 + $req10 + + $req11 + $req12)
echo "z3.value " $(expr $req13 + $req14)
echo "z4.value " $(expr $req15 + $req16)
echo "z5.value " $(expr $req17 + $req18)
echo "z6.value " $(expr $req19 + $req20)
