#!/usr/bin/perl
#
# Copyright (C) 2006-2009 Rodolphe Quiédeville <rodolphe@quiedeville.org>
#
# 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; version 2 dated June,
# 1991.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#
# Magic markers (used by munin-node-configure and some installation scripts):
#%# family=auto
#%# capabilities=autoconf

use strict;
use warnings;

use Munin::Plugin;


my $missing_module = undef;
$missing_module = "File::ReadBackwards" unless (eval "require File::ReadBackwards");

my @logs = qw/update graph html limits/;
my $logdir = ($ENV{'logdir'} || $ENV{'MUNIN_LOGDIR'} || '/var/log/munin');

if ($ENV{'fields'}) {
	@logs = split(/ +/, $ENV{'fields'});
}

sub uses_graph_cron {
    my $log_file = $_[0];
    my $bw = File::ReadBackwards->new($log_file) or
                            die "can't read 'log_file' $!";
    if (defined(my $log_line = $bw->readline)) {
        if ($log_line =~ ('.*graphing is cgi, do nothing$')) {
            return 0;
        } else {
            return 1;
        }
    } else {
        return 1;
    }
}


if ($ARGV[0] and $ARGV[0] eq 'autoconf') {
    my $munin_update_location =
                "$Munin::Common::Defaults::MUNIN_LIBDIR/munin-update";

    if (! -e $munin_update_location) {
        print "no (munin-update was not found at $munin_update_location)\n";
    } elsif (! -x $munin_update_location) {
        print "no ($munin_update_location was found, but is not executable)\n";
    } elsif (defined($missing_module)) {
        print "no (missing Perl module: '$missing_module')\n";
    } else {
        print "yes\n";
    }
    exit 0;
}

if ($ARGV[0] and $ARGV[0] eq "config") {
    print "graph_title Munin processing time\n",
          "graph_info This graph shows the run time of the four different processes making up a munin-master run.  Munin-master is run from cron every 5 minutes and we want each of the programmes in munin-master to complete before the next instance starts. If munin-update uses too long time to run please see the munin-update graph to determine which host is slowing it down.\n",
          "graph_args --base 1000 -l 0\n",
          "graph_scale yes\n",
          "graph_vlabel seconds\n",
          "graph_category munin\n";
    foreach my $log (@logs) {
        print "$log.label munin $log\n";
	print "$log.draw AREASTACK\n";

	next unless $log eq "update" || $log eq "graph";

        print_thresholds("$log", undef, undef, 240, 285);
    }
    exit 0;
}


if (defined($missing_module)) {
    die "Failed to run due to missing Perl module '$missing_module'";
}

foreach my $log (@logs) {
    my $logfile = "$logdir/munin-$log.log";
    my $time = 'U';

    if (! -r $logfile) {
        print "$log.extinfo Can't open $logfile for reading\n";
        print "$log.value $time\n";
        next;
    }

    my $bw = File::ReadBackwards->new("$logdir/munin-$log.log") or
                            die "can't read 'log_file' $!";
    my $found_previous_finished = 0;
    while(defined(my $log_line = $bw->readline) && $found_previous_finished == 0) {
        if ($log_line =~ (/(finished|generated) \((\d+\.\d+)\ssec\)$/)) {
            $time = $2;
            $found_previous_finished = 1;
        }
    }

    if ($log ne "graph" || uses_graph_cron("$logdir/munin-$log.log")) {
        print "$log.value $time\n";
    }
}
