#!/bin/sh
#
# Plugin to monitor the time spent rendering per zoom by 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 time spent by zoom'
  echo 'graph_args --base 1000 -l 0'
  echo 'graph_vlabel time spent per ${graph_period}'
  echo 'graph_category renderd'
  echo 'graph_info Displays the amount of time renderd has spent rendering tiles of a given zoom per ${graph_period}'
  echo 'zoomtime1.label zoom z0 - z8'
  echo 'zoomtime1.type DERIVE'
  echo 'zoomtime1.min 0'
  echo 'zoomtime1.cdef zoomtime1,1000,/'
  echo 'zoomtime1.draw AREA'
  echo 'zoomtime1.info Time for Metatiles z0 - z8'
  echo 'zoomtime2.label zoom z9 - z12'
  echo 'zoomtime2.type DERIVE'
  echo 'zoomtime2.min 0'
  echo 'zoomtime2.cdef zoomtime2,1000,/'
  echo 'zoomtime2.draw STACK'
  echo 'zoomtime2.info Time for Metatiles for z9 - z12'
  echo 'zoomtime3.label zoom z13 - z14'
  echo 'zoomtime3.type DERIVE'
  echo 'zoomtime3.min 0'
  echo 'zoomtime3.cdef zoomtime3,1000,/'
  echo 'zoomtime3.draw STACK'
  echo 'zoomtime3.info Time for Metatiles for z13 - z14'
  echo 'zoomtime4.label zoom z15 - z16'
  echo 'zoomtime4.type DERIVE'
  echo 'zoomtime4.min 0'
  echo 'zoomtime4.cdef zoomtime4,1000,/'
  echo 'zoomtime4.draw STACK'
  echo 'zoomtime4.info Time for Metatiles for z15 - z16'
  echo 'zoomtime5.label zoom z17 - z18'
  echo 'zoomtime5.type DERIVE'
  echo 'zoomtime5.min 0'
  echo 'zoomtime5.cdef zoomtime5,1000,/'
  echo 'zoomtime5.draw STACK'
  echo 'zoomtime5.info Time for Metatiles for z17 - z18'
  echo 'zoomtime6.label zoom z19 - z20'
  echo 'zoomtime6.type DERIVE'
  echo 'zoomtime6.min 0'
  echo 'zoomtime6.cdef zoomtime6,1000,/'
  echo 'zoomtime6.draw STACK'
  echo 'zoomtime6.info Time for Metatiles for z19 - z20'

  exit 0
fi

req0=$(sed -e '/^TimeRenderedZoom00/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req1=$(sed -e '/^TimeRenderedZoom01/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req2=$(sed -e '/^TimeRenderedZoom02/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req3=$(sed -e '/^TimeRenderedZoom03/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req4=$(sed -e '/^TimeRenderedZoom04/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req5=$(sed -e '/^TimeRenderedZoom05/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req6=$(sed -e '/^TimeRenderedZoom06/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req7=$(sed -e '/^TimeRenderedZoom07/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req8=$(sed -e '/^TimeRenderedZoom08/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req9=$(sed -e '/^TimeRenderedZoom09/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req10=$(sed -e '/^TimeRenderedZoom10/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req11=$(sed -e '/^TimeRenderedZoom11/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req12=$(sed -e '/^TimeRenderedZoom12/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req13=$(sed -e '/^TimeRenderedZoom13/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req14=$(sed -e '/^TimeRenderedZoom14/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req15=$(sed -e '/^TimeRenderedZoom15/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req16=$(sed -e '/^TimeRenderedZoom16/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req17=$(sed -e '/^TimeRenderedZoom17/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req18=$(sed -e '/^TimeRenderedZoom18/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req19=$(sed -e '/^TimeRenderedZoom19/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)
req20=$(sed -e '/^TimeRenderedZoom20/!d' -e 's/.*: //' -e q /run/renderd/renderd.stats)

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

echo "zoomtime1.value " $(expr $req0 + $req1 + + $req2 + $req3 + $req4 + $req5 + $req6 + $req7 + $req8)
echo "zoomtime2.value " $(expr $req9 + $req10 + + $req11 + $req12)
echo "zoomtime3.value " $(expr $req13 + $req14)
echo "zoomtime4.value " $(expr $req15 + $req16)
echo "zoomtime5.value " $(expr $req17 + $req18)
echo "zoomtime6.value " $(expr $req19 + $req20)
