All pastes #16161 Raw Edit

Miscellany

public text v1 · immutable
#16161 ·published 2005-06-28 21:06 UTC
rendered paste body
use strict;
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
);

# Configuration
NagiosGrapher::read_ngrapher_cfg();
NagiosGrapher::read_ngrapher_def();

$path=NagiosGrapher::get_ngraphercfg_value('rrdpath');

$configfile=NagiosGrapher::get_ngraphercfg_value('ngraphcfg');
$nagios_config=NagiosGrapher::get_ngraphercfg_value('nagios_config');

# 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));

# 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);

# Parsing the Array...
@blocks = NagiosGrapher::get_ngrapherdef($service);

# Reading XML from index file...
$xml_hash = NagiosGrapher::get_ngrapherxml();

# 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);

# 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}->{servic
e}->{$service}->{rrd_file}. ":". %$_->{graph_value}. ":$type") unless(exists(%$_->{type}) && %$_->{type} =
~ /cdef|gprint|hrule/i);
        push (@graphs, %$_->{rrd_plottype}. ":". %$_->{graph_value}. "#". %$_->{rrd_color}. ":". %$_->{gra
ph_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");
}

# 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";


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, "CO
MMENT: NETWAYS GmbH - $date\\r");
$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;
}