#!/bin/bash
# Wildcard-plugin to monitor Firebird database transaction stats. To monitor a
# database, link firebird_<db> to this file. E.g.
#    ln -s /usr/share/munin/plugins/firebird_ /etc/munin/plugins/firebird_employee
#
# ...will monitor firebird database "employee"
#
# "employee" must be an alias configured in the firebird aliases.conf file
#
# You will also need to set 
#
# [firebird_employee]
# user root
#
# somewhere in your /etc/munin/plugins-conf.d/


case $1 in
	config)
		cat << 'EOM'
graph_title Firebird Transaction Stats
graph_order oldest_trans oldest_active oldest_snapshot next_transaction oldest_trans_gap1 oldest_trans_gap2
graph_args --base 1000
graph_scale no
graph_category firebird
oldest_trans.label Oldest transaction - OIT
oldest_trans.graph no
oldest_active.label Oldest active
oldest_active.graph no
oldest_snapshot.label Oldest snapshot
oldest_snapshot.graph no
next_transaction.label Next transaction
next_transaction.graph no
oldest_trans.type GAUGE
oldest_active.type GAUGE
oldest_snapshot.type GAUGE
next_transaction.type GAUGE
oldest_trans_gap1.label Next Transaction - OIT
oldest_trans_gap1.value GAUGE
oldest_trans_gap1.warning 100
oldest_trans_gap1.critical 1000
oldest_trans_gap2.label Next Transaction - Oldest Snapshot
oldest_trans_gap2.value GAUGE
oldest_trans_gap2.warning 100
oldest_trans_gap2.critical 1000
EOM
exit 0;;
esac

db=$(echo $0 | awk -F'_' '{print $2}')

gstat=$(which gstat 2> /dev/null)

${gstat:=/opt/firebird/bin/gstat} -h ${db} | awk -F'[\t]+' \
	 '{ sub(/^ */,"");
		if ($2 == "Oldest transaction") 
		{ 
			oldest_trans=$3;
			print "oldest_transaction.value " $3
		}
		if ($2 == "Oldest active")
		{
			oldest_active=$3 
			print "oldest_active.value " $3
		}
		if ($2 == "Oldest snapshot") 
		{
			oldest_snapshot=$3 
			print "oldest_snapshot.value " $3
		}
		if ($2 == "Next transaction")
		{
			next_transaction=$3 
			print "next_transaction.value " $3
			print "oldest_trans_gap1.value " $3 - oldest_trans
			print "oldest_trans_gap2.value " $3 - oldest_snapshot
		}

         }'
