#!/bin/sh
#
# $Id: munin-plugin,v 1.5 2007-01-26 07:26:01 finnarne-guest Exp $
#
# Author: Finn-Arne Johansen 
# Date:   2006-01-29

PLUGINNAME=$(basename $0)
LOGNAME=/var/log/slbackup/$PLUGINNAME.log

if [ "$1" = "config" ] ; then 
  echo "graph_title slbackup status"
  echo "graph_args --base 1000 -l 0"
  echo "graph_vlabel count"
  echo "graph_scale no"
  echo "graph_category disk"
  echo "success.label success"
  echo "failed.label failed"
  echo "failed.critical 1"
  echo "lastrun.label last run"
  echo "lastrun.warning 25"
  echo "lastrun.critical 30"
  echo "graph_info Show the number of failed and successfull backup set from the last run"
  exit 0
fi

# Fetch log that is not empty (prevent broken plugin when logfile
# is rotated)
if [ -s $LOGNAME ] ; then 
  CAT=cat
elif [ -r $LOGNAME.1.gz ] ; then 
  CAT=zcat
  LOGNAME=$LOGNAME.1.gz
fi

# Fetch were last backup was started
LAST=$($CAT $LOGNAME | grep -n "Starting slbackup:" | tail -1 | cut -f1 -d:)

# count failed and successfull backups during last backup
if [ "$LAST" ] ; then 
  echo -n "failed.value "
  $CAT $LOGNAME | tail -n +$LAST | \
    grep "Failed backing up client" | wc -l
  echo -n "success.value "
  $CAT $LOGNAME | tail -n +$LAST | \
    grep "Successfully finished backing up client" | wc -l
else
  # or trigger an error if last backup is not found
  echo "failed.value 1"
  echo "success.value 0"
fi

# Find when last backup ended
LASTRUN="$($CAT $LOGNAME | sed -ne "s/- Finished slbackup.//p" | tail -1 )" 

if [ -z "$LASTRUN" ] ; then   
  echo lastrun.value 0
else   
  # report number of hours since last backup
  echo -n "lastrun.value ";   
  cat << EOF | bc 
scale=2
($(date +%s) - $(date -d "$LASTRUN" +%s)) / 3600
EOF
fi

