#!/usr/bin/perl
#
# Plugin to monitor the amount of data sent by KDC server.
# Based on kdc-log-analyze.pl script from heimdal.
#
# Contributed by Jan Rękorajski <baggins@pld-linux.org>
#
# Example configuration:
#
#  [heimdal_kdc_*]
#	env.logdir /var/log
#	env.logfile secure
#
use strict;
use Munin::Plugin;

my $LOGDIR  = $ENV{'logdir'}  || '/var/log';
my $LOGFILE = $ENV{'logfile'} || 'secure';

my $pos = undef;
my $bw  = 0;

sub parseLogfile {
    my ($fname, $start) = @_;

    my ($LOGFILE,$rotated) = tail_open($fname,$start);

    my $line;

    while (<$LOGFILE>) {
	chomp ($_);

	if (/sending ([0-9]+) bytes to IPv[46]:([0-9\.:a-fA-F]+)/) {
		$bw += $1;
	}
    }
    return tail_close($LOGFILE);
}

if ( $ARGV[0] and $ARGV[0] eq "autoconf" ) {
    print "no\n";
    exit 0;
}

if ( $ARGV[0] and $ARGV[0] eq "config" ) {
    print "graph_title Heimdal KDC bandwidth\n";
    print "graph_args --base 1024\n";
    print "graph_vlabel bytes / \${graph_period}\n";
    print "graph_scale yes\n";
    print "graph_category Heimdal\n";
    print "bw.label Bytes sent\n";
    print "bw.type ABSOLUTE\n";
    print "bw.min 0\n";
    exit 0;
}

my $logfile = "$LOGDIR/$LOGFILE";

if (! -f $logfile) {
    print "bw.value U\n";
    exit 1;
}

($pos) = restore_state();

if (!defined($pos)) {
    # No state file present.  Avoid startup spike: Do not read log
    # file up to now, but remember how large it is now, and next
    # time read from there.

    $pos = (stat $logfile)[7]; # File size
} else {
    $pos = parseLogfile ($logfile, $pos);
}

print "bw.value $bw\n";

save_state($pos);

# vim:syntax=perl
