I find it extremely odd that on a forum where the posting of code is often a necessity, certain characters would get stripped. But I digress…
According to ngraph.cfg, I am using single mode
I forgot to specify, I am using Nagios_Grapher 1.3-2 from the RPM, and rrdtool version 1.0.49. I wanted to upgrade to a later version of rrdtool, but I’m not sure how to do it without an rpm for SuSE
I have tried eveything in the suggestions above, all with the same basic results.
I wonder why my ngraph file has the extension of . ncfg instead of .cfg as in the documentation. If I change the name of the file to ngraph.cfg, I lose the error messages, but I get empty graphs on all services, including PING.
THEN if I then change the references (there are 2) to ngraph.ncfg to ngraph.cfg in my NagiosGrapher.pm, I get graphs with data for PING, but error pages on the other services (Load and Users) Basically back to square one.
When I say “error pages”, I am referring to the errors that are in red in my second post in this thread
In a suggestion above, there was code that referred to :AVERAGE ", which I didnt have
In it’s place, my code had $type "
I removed the space after $type, and got error messages. I also replaced $type " with AVERAGE ", with and without the space, and still got error messages.
It seems that the space is necessary?
Here is my latest code from rrd2-graphs.cgi
Lets assume that where there should be double backslashes in the code, that they ARE indeed there, and the forum is stripping them.
#! /usr/bin/perl -w
rrd2-graph.cgi - Returns an Image from an RRDDB using the ngraph configfile
Copyright © 2004, NETWAYS GmbH, Gerd Mueller, Marius Hein
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; either version 2
of the License, or (at your option) any later version.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
use strict;
use lib ‘/usr/lib/nagios/nagiosgrapher’;
use CGI qw(:standard);
use IO::Handle;
use RRDs;
use XML::Simple;
#use Data::Dumper;
use NagiosGrapher;
use GD;
use Time::Local;
use vars qw (
$path
$rrdtool
$configfile
$height
$width
$host
$service
$title
$end
$start
@lines
@values
@graphs
@prints
%tmp
@blocks
@tmp
$ERR
$x_scale
$page_act
$xml_hash
$filename
$nagios_config
$type
@options
$debug
);
CGI Query Params
$host=param(“host”);
$service=param(“service”);
$title=param(“title”);
$end=param(“end”);
$start=param(“start”);
$width=param(“width”);
$height=param(“height”);
$page_act=param(“act_page”);
$type=uc(param(“type”)) if (defined(param(“type”)));
$type=‘AVERAGE’ unless (defined($type));
Configuration
NagiosGrapher::time_need(type=>‘start’);
NagiosGrapher::read_ngrapher_cfg();
NagiosGrapher::time_need(type=>‘stop’, msg => $title. ’ parsing config’);
NagiosGrapher::time_need(type=>‘start’);
NagiosGrapher::read_ngrapher_def();
NagiosGrapher::time_need(type=>‘stop’, msg => $title. ’ parsing graphs’);
$path=NagiosGrapher::get_ngraphercfg_value(‘rrdpath’);
$configfile=NagiosGrapher::get_ngraphercfg_value(‘ngraphcfg’);
$nagios_config=NagiosGrapher::get_ngraphercfg_value(‘nagios_config’);
makes some error in the Apache LOG …
undef $page_act if (length($page_act) <= 0);
if(!NagiosGrapher::AuthCheck($nagios_config,$host,$ENV{‘REMOTE_USER’})) {
print STDOUT “Content-type: image/png\n”;
print STDOUT “Content-disposition: filename=accessdenied.png\n\n”;
print_error(“Sorry you are not allowed to view this graph!\nYour Nagios System”);
exit 0;
}
Setting up default values…
$start="–start=".$start if(defined($start));
$end="–end=".$end if(defined($end));
$height=255 if (!defined($height) || $height eq “”);
$width=595 if (!defined($width) || $width eq “”);
$x_scale = " ";
@options=(’-aPNG’);
Adding some cool options …
my $rrd_background = NagiosGrapher::get_ngraphercfg_value(‘rrd_color_background’);
push @options, ‘-cBACK#’.$rrd_background if (defined $rrd_background);
my $rrd_font = NagiosGrapher::get_ngraphercfg_value(‘rrd_color_font’);
push @options, ‘-cFONT#’.$rrd_font if (defined $rrd_font);
my $rrd_arrow = NagiosGrapher::get_ngraphercfg_value(‘rrd_color_arrow’);
push @options, ‘-cARROW#’.$rrd_arrow if (defined $rrd_arrow);
my $rrd_frame = NagiosGrapher::get_ngraphercfg_value(‘rrd_color_frame’);
push @options, ‘-cFRAME#’.$rrd_frame if (defined $rrd_frame);
my $rrd_grid = NagiosGrapher::get_ngraphercfg_value(‘rrd_color_grid’);
push @options, ‘-cGRID#’.$rrd_grid if (defined $rrd_grid);
push @options, ‘-cMGRID#’.$rrd_grid if (defined $rrd_grid);
my $rrd_canvas = NagiosGrapher::get_ngraphercfg_value(‘rrd_color_canvas’);
push @options, ‘-cCANVAS#’.$rrd_canvas if (defined $rrd_canvas);
Setting up some date as comment.
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=localtime(time());
my $date=sprintf("%02d:%02d/%02d.%02d.%04d]",$hour,$min,$mday,$mon+1,$year+1900);
if (NagiosGrapher::get_nmgrapherdef($service)) {
NagiosGrapher::time_need(type=>‘start’);
my (%units, @dsnames);
my $graph_type = ‘’;
my %nmgraph = NagiosGrapher::get_nmgrapherdef($service);
if ($nmgraph{colors}) {
NagiosGrapher::set_colors($nmgraph{colors});
}
$xml_hash = NagiosGrapher::get_ngrapherxml();
while (my($host, $val) = each(%{ $xml_hash->{host} })) {
if ($host =~ m/$nmgraph{hosts}/) {
while (my($key, $service) = each(%{ $val->{service} })) {
if ($service->{service_name} =~ m/$nmgraph{services}/) {
@blocks = NagiosGrapher::get_ngrapherdef($service->{service_name}, ‘graph’);
@blocks = reverse(@blocks) if (defined($nmgraph{order}) && $nmgraph{order} =~ m/reverse/i);
# print Dumper \@blocks;
foreach (@blocks) {
next if(!($_->{graph_value} =~ m/$nmgraph{graph_values}/) and $nmgraph{graph_values} ne "");
$units{$_->{service_name}} = $_->{graph_units} unless (exists($units{$_->{service_name}}));
my $dsname = NagiosGrapher::get_random_letters(10);
my $color = NagiosGrapher::get_next_color();
if ($nmgraph{graph_type} =~ m/line/i) {
$graph_type = 'LINE2';
push @values, 'DEF:'. $dsname. '='. $path. $host. '/'. $service->{rrd_file}. ':'. $_->{graph_value}. ':'. $type. ' ';
push @graphs, $graph_type. ':'. $dsname. $color. ':'. ''. $host. '] '. $_->{graph_legend}. "\\J";
}
elsif ($nmgraph{graph_type} =~ m/stack/i) {
$graph_type = 'AREA';
$graph_type = "STACK" if (@graphs >= 1);
push @dsnames, $dsname;
push @values, 'DEF:'. $dsname. '='. $path. $host. '/'. $service->{rrd_file}. ':'. $_->{graph_value}. ':'. $type. ' ';
push @graphs, $graph_type. ':'. $dsname. $color. ':'. ''. $host. '] '. $_->{graph_legend}. "\\J";
}
elsif ($nmgraph{graph_type} =~ m/area/i) {
$graph_type = 'AREA';
push @values, 'DEF:'. $dsname. '='. $path. $host. '/'. $service->{rrd_file}. ':'. $_->{graph_value}. ':'. $type. ' ';
my $old_ds = $dsname;
$dsname = NagiosGrapher::get_random_letters(10);
push @values, 'CDEF:'. $dsname. '='. $old_ds. ' ';
push @graphs, $graph_type. ':'. $old_ds. $color. ':'. ''. $host. '] '. $_->{graph_legend}. "\\J";
push @graphs, 'LINE1'. ':'. $dsname. '#000000'. " ";
}
}
}
}
}
}
if ($nmgraph{graph_type} =~ m/stack/i) {
my $cdef = $dsnames[0]. ',';
shift (@dsnames);
$cdef .= join(',+,', @dsnames). ',+';
push @values, 'CDEF:'. 'sum1'. '='. $cdef;
push @graphs, 'LINE1'. ':'. 'sum1'. '#000000'. " ";
}
NagiosGrapher::time_need(type=>'stop', msg => $title. ' creating multigraph def\'s');
# print Dumper \@graphs;
print STDOUT "Content-type: image/png\n";
my $fname = $nmgraph{service_name};
$fname =~ s/\s+/_/g;
print STDOUT "Content-disposition: filename=". $fname."_". $title. ".png\n\n";
$x_scale = join(' / ', values(%units));
NagiosGrapher::time_need(type=>'start');
chop(@values);
RRDs::graph("-", @options, "-h$height", "-w$width","$start", "--title=MultiGraph: ". $nmgraph{service_name}, "-v $x_scale", @values, @graphs, "COMMENT:Graphs");
NagiosGrapher::time_need(type=>'stop', msg => $title. ' drawing multigraph');
}
else {
NagiosGrapher::time_need(type=>‘start’);
# Parsing the Array...
@blocks = NagiosGrapher::get_ngrapherdef($service);
# Reading XML from index file...
$xml_hash = NagiosGrapher::get_ngrapherxml();
# fetch the data...
if (@blocks > 0) {
foreach (@blocks) {
# Paging
next if ($page_act && !($_->{page} eq $page_act));
# Adding real ds
push (@values, "DEF:". %$_->{graph_value}. "=$path". "$host/". $xml_hash->{host}->{$host}->{service}->{$service}->{rrd_file}. ":". %$_->{graph_value}. ":$type ") unless(exists(%$_->{type}) && %$_->{type} =~ /cdef|gprint|hrule/i);
push (@graphs, %$_->{rrd_plottype}. ":". %$_->{graph_value}. "#". %$_->{rrd_color}. ":". %$_->{graph_legend}. "\\J") unless ( (exists(%$_->{hide}) && %$_->{hide} =~ /yes/i) || (exists(%$_->{type}) && %$_->{type} =~ /cdef|gprint|hrule/i) );
# Adding CDEF's
if (exists(%$_->{type}) && %$_->{type} =~ /cdef/i) {
push(@values, "CDEF:". %$_->{graph_value}. "=". %$_->{graph_calc});
push(@graphs, %$_->{rrd_plottype}. ":". %$_->{graph_value}. "#". %$_->{rrd_color}. ":". %$_->{graph_legend}. "\\J") unless ( (exists(%$_->{hide}) && %$_->{hide} =~ /yes/i) );
}
# Adding GPRINTs
if (exists(%$_->{type}) && %$_->{type} =~ /gprint/i) {
push(@prints, "GPRINT:". %$_->{print_source}. ":". uc(%$_->{print_function}). ":". %$_->{print_description}. "\\: ". %$_->{print_format}. "\\n");
}
# Adding HRULEs
if (exists(%$_->{type}) && %$_->{type} =~ /hrule/i) {
push(@graphs, "HRULE:". %$_->{hrule_value}. "#". %$_->{rrd_color});
}
unless (exists(%$_->{type}) && %$_->{type} =~ /gprint/i) {
$x_scale.=" / " if(defined($x_scale) && $x_scale ne "" && !$x_scale =~ /.*?$_->{graph_units}/i && (%$_->{graph_units} ne ""));
$x_scale.=%$_->{graph_units} unless ($x_scale =~ /.*?$_->{graph_units}/i);
}
}
}
#VLINES
if($start eq "--start=-5400") {
# mark hours
push(@graphs, "VRULE:".timelocal(0, 0, $hour, $mday, $mon, $year)."#FF0000");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=localtime(time()-3600);
push(@graphs, "VRULE:".timelocal(0, 0, $hour, $mday, $mon, $year)."#FF0000");
} elsif ($start eq "--start=-129600") {
# mark days
push(@graphs, "VRULE:".timelocal(0, 0, 0, $mday, $mon, $year)."#FF0000");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=localtime(time()-86400);
push(@graphs, "VRULE:".timelocal(0, 0, 0, $mday, $mon, $year)."#FF0000");
} elsif ($start eq "--start=-907200") {
# mark weeks
my $monday_seconds=($wday-1)*24*60*60;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=localtime(time()-$monday_seconds);
push(@graphs, "VRULE:".timelocal(0, 0, 0, $mday, $mon, $year)."#FF0000");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=localtime(time()-604800);
$monday_seconds=($wday-1)*24*60*60;
my $monday=(localtime(time()-$monday_seconds-604800))[3];
push(@graphs, "VRULE:".timelocal(0, 0, 0, $monday, $mon, $year)."#FF0000");
} elsif ($start eq "--start=-4017600") {
# mark month
push(@graphs, "VRULE:".timelocal(0, 0, 0, 1, $mon, $year)."#FF0000");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=localtime(time()-2419200);
push(@graphs, "VRULE:".timelocal(0, 0, 0, 1, $mon, $year)."#FF0000");
# calendarweek workaround
push(@options,'--x-grid=DAY:1:WEEK:1:WEEK:1:604800:Week %V');
} elsif ($start eq "--start=-48211200") {
# mark year
push(@graphs, "VRULE:".timelocal(0, 0, 0, 1, 0, $year)."#FF0000");
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=localtime(time()-29030400);
push(@graphs, "VRULE:".timelocal(0, 0, 0, 1, 0, $year)."#FF0000");
}
NagiosGrapher::time_need(type=>'stop', msg => $title. ' creating graph def\'s');
# Make the output ...
STDOUT->autoflush(1);
print STDOUT "Content-type: image/png\n";
$filename = $xml_hash->{host}->{$host}->{service}->{$service}->{rrd_file};
$filename =~ s/\.rrd//i;
print STDOUT "Content-disposition: filename=". $filename. "_". $title. ".png\n\n";
NagiosGrapher::time_need(type=>'start');
chop(@values);
RRDs::graph("-", @options, "-h$height", "-w$width","$start", "--title=$host(". $xml_hash->{host}->{$host}->{service}->{$service}->{service_title}. ") - $title Graph", "-v $x_scale", @values, @graphs, @prints, "COMMENT:Graphs");
NagiosGrapher::time_need(type=>'stop', msg => $title. ' drawing graph');
}
$ERR = RRDs::error;
if ($ERR) {
my $msg;
$msg.= “Some error occured:\n”;
$msg.= “~~~~~~~~~~~~~~~~~~~\n\n”;
$msg.= “RRD Error: $ERR\n”;
$msg.= “Options:\n”;
foreach (@options) {
$msg.= " $\n";
}
$msg.= “Values:\n”;
foreach (@values) {
$msg.= " $\n";
}
$msg.= “\nGraphs:\n”;
foreach (@graphs) {
$msg.= " $\n";
}
$msg.= “\nGPrints:\n”;
foreach (@prints) {
$msg.= " $\n";
}
$msg.= “\n$date\n”;
print_error($msg);
}
Default exit and return true
exit (0);
sub print_error {
my ($msg) = @_;
my @lines = split(/\n/,$msg);
my $lheight = @lines;
my $im = new GD::Image($width,13*$lheight);
# allocate some colors
my $white = $im->colorAllocate(255,255,255);
my $black = $im->colorAllocate(0,0,0);
my $red = $im->colorAllocate(255,0,0);
# make the background transparent and interlaced
$im->transparent($white);
$im->interlaced('true');
my $line=0;
foreach(@lines) {
$im->string(gdSmallFont,0,$line,$_,$red);
$line+=12;
}
binmode STDOUT;
print $im->png;
}
1;
Again, thanks for your patience, and I hope I was able to explain everything clearly.
Looking at that code, I might as well be looking at hyroglyphics
Thanks for making it clearer to me
.
I am beginning to suspect that this is a problem with rrdtool… :shock:
Edited Thu Nov 03 2005, 12:48PM ]