use strict;
my $BASE_DIR = "/usr/share/nagiostat";
my $CONFIG_FILE = "/etc/nagios/nagiostat.conf"; my $DEBUG_LOG_FILE = "/var/spool/nagiostat/debug.log"; my $DEBUGLEVEL = 1; my $DEBUGOUTPUT = 0;
require 'shellwords.pl';
my $DEBUG_TIMESTAMP=0;
if( $ARGV[0] eq "-t") { print STDERR "nagiostat: Testing configuration-file..\n";
$DEBUGLEVEL=3;
$DEBUGOUTPUT=1; my $c = &read_config();
abort();
} elsif( $ARGV[0] eq "-p") { &parse_perfdata();
} else {
if( exists $ENV{'GATEWAY_INTERFACE'}) { $DEBUGOUTPUT=2; &run_as_cgi();
} else { print STDERR "nagiostat: usage:
-t Test configuration-file
-p Parse/import performance-data (used when called from nagios)
";
}
}
abort();
sub abort {
if( $DEBUG_TIMESTAMP!=0) {
debug( 1, "");
}
exit;
}
sub run_as_cgi {
use CGI;
my $cgi = new CGI;
my $graph_name = $cgi->param( "graph_name");
my $graph_iteration = $cgi->param( "graph_iteration");
if( $graph_iteration eq "") {
print "Content-type: text/html\nExpires: 0\n\n";
} else {
print "Content-type: image/gif\nExpires: 0\n\n";
}
my $config = read_config();
if( $graph_name eq "") {
display_htmltemplate( $config->{'htmltemplatepath'}."/".$config->{'graphindextemplate'}, $graph_name, $config);
} else { if( ! exists $config->{'graphs'}->{$graph_name}) {
debug( 1, "ERROR: Graph '$graph_name' does not exist!");
exit;
} elsif( $graph_iteration eq "") {
if( ! -r $config->{'htmltemplatepath'}."/".$config->{'graphs'}->{$graph_name}->{'htmltemplate'}) {
debug( 1, "ERROR: HTML-template '".($config->{'htmltemplatepath'}."/".$config->{'graphs'}->{$graph_name}->{'htmltemplate'})."' is not readable by effective userid!");
exit;
}
display_htmltemplate( $config->{'htmltemplatepath'}."/".$config->{'graphs'}->{$graph_name}->{'htmltemplate'}, $graph_name, $config);
} else {
my $rrdtool_cmdline = $config->{'rrdtoolpath'}." graph - ".join( " ", @{$config->{'plottemplates'}->{ $config->{'graphs'}->{$graph_name}->{'plottemplate'} } });
my $rrdarchive = $config->{'rrdarchivepath'}."/".$config->{'graphs'}->{$graph_name}->{'rrdfilename'};
$rrdtool_cmdline =~ s/\$f/$rrdarchive/g;
my $t_start = $config->{'graphtimetemplates'}->{ $config->{'graphs'}->{$graph_name}->{'graphtimetemplate'} }->[$graph_iteration]->{'starttime'};
$rrdtool_cmdline =~ s/\$s/$t_start/g;
my $t_end = $config->{'graphtimetemplates'}->{ $config->{'graphs'}->{$graph_name}->{'graphtimetemplate'} }->[$graph_iteration]->{'endtime'};
$rrdtool_cmdline =~ s/\$e/$t_end/g;
my $t_descr = $config->{'graphtimetemplates'}->{ $config->{'graphs'}->{$graph_name}->{'graphtimetemplate'} }->[$graph_iteration]->{'description'};
$rrdtool_cmdline =~ s/\$d/$t_descr/g;
print `$rrdtool_cmdline`;
}
}
}
sub display_htmltemplate {
my( $filename, $graph_name, $config) = @_;
if( -r $filename) {
open( HTML, $filename);
while( <HTML>) {
s/\$(\w+)/my $t=sub {
my $varname = $_[0];
if( $varname eq "GRAPHNAME") { if( $config->{'graphs'}->{$graph_name}->{'title'} ne "") {
return( $config->{'graphs'}->{$graph_name}->{'title'});
} else {
return( "Graph ".$graph_name);
}
} elsif( $varname eq "CURRENTTIME") { return( localtime());
} elsif( $varname eq "GRAPHINDEX" || $varname eq "GRAPHINDEX_ONEROW") { my $return_html;
foreach my $gn ( sort keys %{$config->{'graphs'}}) {
$return_html.=(($varname eq "GRAPHINDEX")?"<LI>":"").
"<A HREF=\"?graph_name=$gn\">".($config->{'graphs'}->{$gn}->{'title'})."<\/A>". (($varname eq "GRAPHINDEX_ONEROW")?" ":"");
}
return( $return_html);
} elsif( $varname eq "GRAPH_AUTOGENERATE") { ## return HTML-code for displaying the actual graph-images
my $iteration_id=0;
my $return_html;
foreach my $time ( @{ $config->{'graphtimetemplates'}->{ $config->{'graphs'}->{$graph_name}->{'graphtimetemplate'} } }) {
$return_html.="<P>".($time->{'description'})."<BR><IMG SRC=\"?graph_name=$graph_name&graph_iteration=$iteration_id\">";
$iteration_id++;
}
return( $return_html);
} else { return( "##UNKNOWN-VARIABLE##");
}
}; &$t($1)/eig; print;
}
close( HTML);
} else {
print "ERROR: HTML-template '$filename' does not exist or is not readable by effective userid.";
}
}
sub parse_perfdata {
$DEBUG_TIMESTAMP=0;
my $config = read_config();
my $rrd_updates;
my( $LASTCHECK, $HOSTNAME, $SERVICEDESCR, $SERVICESTATE, $OUTPUT, $PERFDATA) = split( /\|!!\|/, $ARGV[1]);
debug( 3, "**INCOMING PERFDATA:\n LASTCHECK=$LASTCHECK\n HOSTNAME=$HOSTNAME\n SERVICEDESCR=\"$SERVICEDESCR\"\n SERVICESTATE=\"$SERVICESTATE\"\n OUTPUT=\"$OUTPUT\"\n PERFDATA=\"$PERFDATA\"");
my $host_and_descr_found;
foreach my $host_regex ( keys %{$config->{'regexes'}}) {
foreach my $service_regex ( keys %{$config->{'regexes'}->{$host_regex}}) {
if( ($HOSTNAME =~ m/$host_regex/i) && ($SERVICEDESCR =~ m/$service_regex/i) ) { $host_and_descr_found=1;
foreach my $insert_value ( @{$config->{'regexes'}->{$host_regex}->{$service_regex}} ) {
foreach my $regex ( @{ $config->{'valueregextemplates'}->{$insert_value->{'regextemplate'}} }) {
my $regex_string = $regex->{'regex'};
if( $regex->{'regex_what'} eq "output") { if( $OUTPUT =~ m/$regex_string/) {
debug( 3, " +VALUE: ".$1);
push( @{$rrd_updates->{$insert_value->{'rrdarchive'}}->{'value'}}, $1);
push( @{$rrd_updates->{$insert_value->{'rrdarchive'}}->{'dsaname'}}, $regex->{'dsaname'});
$rrd_updates->{$insert_value->{'rrdarchive'}}->{'rrdcreatetemplate'} = $insert_value->{'rrdcreatetemplate'}; } else {
debug( 2, "**WARNING: No match for value with regex on output '$regex_string'.");
}
} else { if( $PERFDATA =~ m/$regex_string/) {
debug( 3, " +VALUE: ".$1);
push( @{$rrd_updates->{$insert_value->{'rrdarchive'}}->{'value'}}, $1);
push( @{$rrd_updates->{$insert_value->{'rrdarchive'}}->{'dsaname'}}, $regex->{'dsaname'});
$rrd_updates->{$insert_value->{'rrdarchive'}}->{'rrdcreatetemplate'} = $insert_value->{'rrdcreatetemplate'}; } else {
debug( 2, "**WARNING: No match for value with regex on perfdata '$regex_string'.");
}
}
}
}
}
}
}
if( !$host_and_descr_found) {
debug( 2, "**WARNING: Hostname and description didn't match any of the regexes in the config-file.");
} else {
foreach my $archive ( keys %{$rrd_updates}) {
debug( 3, " =INSERT into '$archive': ".join( ",", @{$rrd_updates->{$archive}->{'value'}} )." DSA-names=".join( ",", @{$rrd_updates->{$archive}->{'dsaname'}}) );
my $rrdarchive_filename = $config->{'rrdarchivepath'}."/".$archive;
if( ! -e $rrdarchive_filename) {
my $rrdtool_cmdline = $config->{'rrdtoolpath'}." create ".$rrdarchive_filename." ".(join( " ", @{$config->{'rrdcreatetemplates'}->{$rrd_updates->{$archive}->{'rrdcreatetemplate'}}}));
debug( 2, "**CREATING: $rrdarchive_filename, cmdline='".$rrdtool_cmdline."'.");
`$rrdtool_cmdline`;
}
if( ! -w $rrdarchive_filename) { debug( 1, "!!ERROR: RRD-archive '".$rrdarchive_filename."' does not exist or is not writable by effective UID."); abort();
}
my $rrdtool_cmdline = $config->{'rrdtoolpath'}." update ".$rrdarchive_filename.
" --template ".join( ":", @{$rrd_updates->{$archive}->{'dsaname'}}).
" $LASTCHECK:".join( ":", @{$rrd_updates->{$archive}->{'value'}});
debug( 3, " !RRDCMDLINE: ".$rrdtool_cmdline);
my $result = `$rrdtool_cmdline`;
if( $result ne "") {
debug( 1, "!!RESULT: $result");
}
}
}
}
sub read_config {
my $config;
open( CONFIG, $CONFIG_FILE);
my( $line_counter);
while( <CONFIG>) {
$line_counter++;
chomp;
my( @args) = &shellwords( $_);
my $orig_confline = $_;
$args[0] = uc( $args[0]);
if( $args[0] eq "INSERTVALUE") { shift @args;
my( $rrd_filename, $rrdcreatetemplate, $hostname_regex, $servicedescr_regex, $regex_template) = @args;
if( ! exists $config->{'rrdcreatetemplates'}->{$rrdcreatetemplate}) {
debug( 1, "!!ERROR: RRDCreateTemplate '$rrdcreatetemplate' is not defined but refered to in line $line_counter."); abort();
}
if( $hostname_regex !~ m/^\/(.*)\/$/) { debug( 1, "!!ERROR: Hostname regex should be enclosed in slashes, i.e. /HOSTNAME/ and optionally enclosed in quotes if needed. Conf-line $line_counter."); abort();
} else {
$hostname_regex = $1;
}
if( $servicedescr_regex !~ m/^\/(.*)\/$/) { debug( 1, "!!ERROR: Service-description regex should be enclosed in slashes, i.e. /SERVICEDESCR/ and optionally enclosed in quotes if needed. Config-line $line_counter.");
abort();
} else {
$servicedescr_regex = $1;
}
if( ! exists $config->{'valueregextemplates'}->{$regex_template}) { debug( 1, "!!ERROR: VALUEREGEXTEMPLATE '$regex_template' is not defined on line $line_counter."); abort();
}
push( @{$config->{'regexes'}->{$hostname_regex}->{$servicedescr_regex}}, {
'rrdarchive' => $rrd_filename,
'rrdcreatetemplate' => $rrdcreatetemplate,
'regextemplate' => $regex_template
} );
} elsif( $args[0] =~ m/^(\s*)#/ || $args[0] eq "") { } elsif( $args[0] eq "RRDTOOLPATH") { $config->{'rrdtoolpath'} = $args[1];
} elsif( $args[0] eq "PLOTTEMPLATE") { shift @args;
my( $name, @params) = @args;
if( $name eq "") {
debug( 1, "!!ERROR: PLOTTEMPLATE-name must be specified on line $line_counter."); abort();
}
if( exists $config->{'plottemplates'}->{$name}) {
debug( 1, "!!ERROR: PLOTTTEMPLATE-names must be uniqe. Duplicate name found on line: $line_counter."); abort();
}
$config->{'plottemplates'}->{$name} = [ @params];
} elsif( $args[0] eq "GRAPHTIMETEMPLATE") { shift @args;
my( $name, @params) = @args;
if( $name eq "") {
debug( 1, "!!ERROR: GRAPHTIMETEMPLATE-name must be specified on line $line_counter."); abort();
}
if( exists $config->{'graphtimetemplates'}->{$name}) {
debug( 1, "!!ERROR: GRAPHTIMETEMPLATE-names must be uniqe. Duplicate name found on line: $line_counter."); abort();
}
foreach my $time_template (@params) {
my( $t_start, $t_end, @t_descr) = split( /:/, $time_template);
my $t_descr = join( ":", @t_descr); if( $t_start eq "" || $t_end eq "") {
debug( 1, "!!ERROR: GRAPHTIMETEMPLATE, each time-definition should be defined as '<starttime>:<endtime>:<description>' on line $line_counter.");
}
push( @{$config->{'graphtimetemplates'}->{$name}}, {
'starttime' => $t_start,
'endtime' => $t_end,
'description' => $t_descr
});
}
} elsif( $args[0] eq "RRDCREATETEMPLATE") { shift @args;
my( $name, @params) = @args;
if( $name eq "") {
debug( 1, "!!ERROR: RRDCREATETEMPLATE-name must be specified on line $line_counter."); abort();
}
if( exists $config->{'rrdcreatetemplates'}->{$name}) {
debug( 1, "!!ERROR: RRDCREATETEMPLATE-names must be uniq. Duplicate name found on line: $line_counter."); abort();
}
$config->{'rrdcreatetemplates'}->{$name} = [ @params];
} elsif( $args[0] eq "VALUEREGEXTEMPLATE") { shift @args;
my( $template_name, @regexes) = @args;
if( $template_name eq "") {
debug( 1, "!!ERROR: VALUEREGEXTEMPLATE-name must be specified on line $line_counter."); abort();
}
foreach my $r (@regexes) {
if( $r !~ m/^(output|perfdata):(\w+):\/(.*)\/$/) {
debug( 1, "!!ERROR: Value-regex should be formatted as 'output:dsaname:/regex/' or 'perfdata:dsaname:/regex/' depending on in which field to extract the data. The value should be within parantheses in the regex. Config-line $line_counter.");
abort();
} else {
my( $regex_what, $dsa_name, $regex) = ( $1, $2, $3);
push( @{$config->{'valueregextemplates'}->{$template_name}}, {
'regex_what' => $regex_what,
'regex' => $regex,
'dsaname' => $dsa_name
} );
}
}
} elsif( $args[0] eq "RRDARCHIVEPATH") { $config->{'rrdarchivepath'} = $args[1];
} elsif( $args[0] eq "HTMLTEMPLATEPATH") { $config->{'htmltemplatepath'} = $args[1];
} elsif( $args[0] eq "GRAPHINDEXTEMPLATE") { $config->{'graphindextemplate'} = $args[1];
} elsif( $args[0] eq "GRAPH") { if( $args[1] eq "") {
debug( 1, "!!ERROR: GRAPH-name must be specified on line $line_counter."); abort();
}
if( ! exists $config->{'graphtimetemplates'}->{$args[3]}) {
debug( 1, "!!ERROR: Unknown GRAPHTIMETEMPLATE on line $line_counter."); abort();
}
if( ! exists $config->{'plottemplates'}->{$args[4]}) {
debug( 1, "!!ERROR: Unknown PLOTTEMPLATE on line $line_counter."); abort();
}
if( exists $config->{'graphs'}->{$args[1]}) {
debug( 1, "!!ERROR: Graphnames must be uniqe. Duplicate name found on line: $line_counter.");
abort();
} else {
$config->{'graphs'}->{$args[1]} = {
'graphname' => $args[1],
'rrdfilename' => $args[2],
'graphtimetemplate' => $args[3],
'plottemplate' => $args[4],
'htmltemplate' => $args[5],
'title' => $args[6]
};
}
} else {
debug( 1, "!!ERROR: Unknown config-file directive on line $line_counter: '".$args[0]."'");
}
}
close( CONFIG);
if( ! -x $config->{'rrdtoolpath'}) {
debug( 1, "!!ERROR: RRDTOOLPATH does not point to executable rrdtool-binary.");
abort();
}
if( ! -x $config->{'rrdarchivepath'}) {
debug( 1, "!!ERROR: RRDARCHIVEPATH, '".($config->{'rrdarchivepath'})."' is not readable by effective userid."); abort();
}
if( ! -x $config->{'htmltemplatepath'}) {
debug( 1, "!!ERROR: HTMLTEMPLATEPATH, '".($config->{'htmltemplatepath'})."' is not readable by effective userid."); abort();
}
return( $config);
}
sub debug {
my( $level, $msg) = @_;
if( $DEBUGLEVEL >= $level) {
if( !$DEBUG_TIMESTAMP) {
$DEBUG_TIMESTAMP=1;
debug( 1, scalar localtime());
}
if( $DEBUGOUTPUT == 0) {
open( DEBUGOUTPUT, ">>".$DEBUG_LOG_FILE);
print DEBUGOUTPUT $msg."\n";
close( DEBUGOUTPUT);
} elsif( $DEBUGOUTPUT == 2) {
print "<BR><PRE>$msg</PRE>";
} else {
print STDERR $msg."\n";
}
}
}