#!/usr/bin/perl
use strict;
#
# $Id: rrdutil.pl,v 2.15 2005/06/09 20:46:55 matt Exp $
#
=head1 NAME
rrdutil - a simple host monitoring app based on rrdtool
=head1 SYNOPSIS
RRDutil automates the creation of rrdtool based databases,
populates them with data, and then displays the data in
a HTML format for convenient display and printing.
=head1 DESCRIPTION
RRDutil is a tool to collect, store, and graph runtime
information about Unix servers running net-snmp. It
depends on Net::SNMP, RRDs (rrdtool), and CGI.pm.
=cut
use lib "lib";
use RRDutil; my $rrdutil = RRDutil->new;
use RRDutil::Graphs; my $graphs = RRDutil::Graphs->new;
use RRDutil::HTML; my $html = RRDutil::HTML->new;
use RRDutil::SNMP; my $snmp = RRDutil::SNMP->new;
use RRDutil::Utility; my $utility = RRDutil::Utility->new;
use vars qw/ $opt_a $opt_b $opt_d $opt_v $action $debug $VERSION %MonitorHash /;
#$VERSION = sprintf "%d.%02d", q$Revision: 2.15 $ =~ /(\d+)/g;
$VERSION = 2.20;
my $conf = $utility->parse_config( {file=>"rrdutil.conf",debug=>0} );
$debug = $conf->{'debug'};
my $datadir = $conf->{'datadir'};
my $htmldir = $conf->{'htmldir'};
my $timeout = $conf->{'snmp_timeout'};
my $allchecked = $conf->{'select_all_by_default'};
my $hostlist = $rrdutil->get_hosts($conf);
my $monitors = $rrdutil->parse_config($hostlist, $conf);
my @MonitorTypes = qw( cpu disk storage info load net memory www mail mysql mbmon apache pf);
for ( @MonitorTypes ) { $MonitorHash{$_} = 1 };
sub process_cgi($);
sub process_shell;
$|++;
$conf->{'debug'} = 1 if $debug;
$conf->{'htmldir'} = $htmldir;
$conf->{'datadir'} = $datadir;
$conf->{'timeout'} = $timeout;
$conf->{'allchecked'} = $allchecked;
$conf->{'bits'} = 1 if $opt_b;
$conf->{'monitorhash'} = \%MonitorHash;
$conf->{'mtypes'} = \@MonitorTypes;
process_shell($conf) unless ( $ENV{'GATEWAY_INTERFACE'} );
use CGI qw(:standard);
use CGI::Carp qw( fatalsToBrowser );
process_cgi($conf);
exit 0;
=head1 INSTALLATION
Download (free) from The Network People store:
http://www.tnpi.biz/store/product_info.php?cPath=2&products_id=41
To install this module type the following:
perl Makefile.PL
make
make test
make install
To install the CGI
make cgi
For upgrades
make snmp - install the latest snmpd.conf
make conf - install mysqlstatus.pl and apastat.pl
If your cgi-bin is not in a standard place for your OS,
simply copy the rrdutil.pl file to cgi-bin/rrdutil.cgi.
=cut
# -----------------------------------------------------------------------------
# No user servicable parts below this line! #
# -----------------------------------------------------------------------------
BEGIN
{
use RRDutil::Perl; my $perl = RRDutil::Perl->new;
$perl->module_load( {module=>"Date::Format", ports_name=>"p5-TimeDate", ports_group=>"devel"} );
$perl->module_load( {module=>"RRDs", ports_name=>"rrdtool", ports_group=>"net"} );
};
sub usage
{
print "\nusage:
$0 -a update|graph
optional flags:
-b bits instead of (default) bytes
-d days (useful when generating images on command line)
-v verbose \n\n";
}
sub process_cgi($)
{
my ($conf) = (@_);
my $refresh = $conf->{'http_refresh_minutes'}; $refresh ||= 5;
my $host = param('hostname');
my $host_s = [ $host ];
print header('text/html');
$host ||= "localhost";
my $avail = $rrdutil->get_graph_types( $host, $monitors, $conf );
my $days = param('days'); $days ||= 1;
$conf->{'days'} = $days;
$html->request($hostlist, $avail, $refresh, $allchecked);
my $Graphs = $rrdutil->get_requested_graphs($conf, @MonitorTypes);
print "process_cgi: being called\n
" if $debug;
$graphs->generate($host_s, $conf, $monitors, $Graphs);
$html->graphs($host_s, $Graphs, $conf, $monitors);
$html->footer($VERSION);
};
sub process_shell($)
{
my ($conf) = @_;
use Getopt::Std;
getopts('a:bd:v');
$conf->{'days'} = $opt_d if $opt_d;
$debug = 0;
if ($opt_v)
{
$debug = 1; $conf->{'debug'} = 1;
print "Debugging Enabled\n";
};
if (! $opt_a || ( $opt_a ne "update" && $opt_a ne "graph" ) )
{
usage();
exit 1;
} else {
$action = $opt_a;
};
my $pidfile = "/var/run/rrdutil.pid";
if ( $conf->{'var_run_dir'} ) { $pidfile = $conf->{'var_run_dir'} . "/rrdutil.pid"; };
$pidfile = $utility->check_pidfile($pidfile);
unless ($pidfile) {
print "Yikes, I couldn't create $pidfile!\n";
exit 1;
};
if ($action eq "update") { $snmp->update_rrds($hostlist, $monitors, $conf) }
elsif ($action eq "graph") { $graphs->generate($hostlist, $conf, $monitors) };
unlink $pidfile or warn "WARNING: couldn't delete $pidfile: $!\n";
# exit 0 when happy (like most daemons)
exit 0;
};
=head1 AUTHOR
Matt Simerson
Rob Lensen
=head1 BUGS
None known. Report any to author.
=head1 TODO
Add more bounds checking on inputs, especially via CGI
Modify config file via HTML interface
Poll systems via SNMP to assist in HTML setup
=head1 SEE ALSO
http://www.tnpi.biz/internet/manage/rrdutil/
head1 COPYRIGHT
Copyright (c) 2003-2005, The Network People, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that t
he following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the followi
ng disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the foll
owing disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the The Network People, Inc. nor the names of its contributors may be used to endorse or p
romote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut