#!/usr/bin/perl # # Author: M.Fuerstenau, Oce Printing Systems GmbH, Poing, Germany # # Purpose: # # c2n.cgi is a filter/wrapper for the frontend of Cacti to include it in Nagios. # # Prerequisites: # - guest account in cacti must be enabled # - CGI.pm # - libwww-perl # - a database user how is able to read all tables in the Cacti database # # Changes: # # 15.05.2008 - Version 1.0 # # 24.09.2008 - Version 1.1 # - renamed from cacti.cgi to c2n.cgi # - c2n.cgi can now handle multiple installations of Cacti with seperate databases. # # 11.10.2008 - Version 1.2 # - Minor code revision - removed some nested if/else # - Some minor bugs using the calendar fixed # - Allow/added the zoom function where possible # - Added some lines in the interface filter to remove unnessesary grey lines. # # 15.01.2009 - Version 1.3 # - Minor bug fix - /cacti was hardcoded in 47 lines (oh yeah - the good old copy & paste) # Therefore defining another URI in global.php than /cacti would cause porblems. # Replaced by the variable $CACTI_URI. Enter your URI here. # Thanks to Thomas Wollner for detecting this bug. use strict; use CGI qw/:cgi-lib/; use LWP::Simple; use DBI; use Date::Calc qw(:all); use File::Basename; use vars qw($DOC $DATE_TO_EPOCH $DSN $DATABASE $DBHOST $DBPORT $DBH $ID @ROW $STH $SQLSTATEMENT); use vars qw($SERVER $HOST $DBPASS $DBLOGIN $DATE_FROM_EPOCH $DATE_TO $DATE_FROM $SCRIPT_NAME $SCRIPT_NAME_BASE); use vars qw(%formParameter $URL2FETCH $QUERY_PARMS $NAGIOS); use vars qw($CACTI_INSTANCE $CALLING_SERVER %URL_PART1 %DBHOST %DBLOGIN %DBPASS %DATABASE %DBPORT); # Main part ------------------------------------------------------------------------ &ReadParse(*formParameter); $HOST = $formParameter{'host'}; # $HOST is the name of the host you are # asking performance graphs for. $NAGIOS = $formParameter{'nagios'}; # $NAGIOS is a variable identifying only # the part of the script which has to be used $CACTI_INSTANCE = $formParameter{'cacti_instance'}; # This variable is only a value for # identifying the server and the database # cacti is running $CALLING_SERVER = $ENV{'SERVER_NAME'}; # $CALLING_SERVER is the calling host. $SCRIPT_NAME = $ENV{'SCRIPT_NAME'}; $SCRIPT_NAME_BASE = basename($SCRIPT_NAME); $QUERY_PARMS = $ENV{'QUERY_STRING'}; # The complete query string $CACTI_URI = "cacti"; # The URI of Cacti as defined in Cacti # in global.php # This is needed for the reverse proxy %URL_PART1 = ( 0 => "$CALLING_SERVER", 1 => "$CALLING_SERVER/ci1" ); %DBHOST = ( 0 => "mysqlsrv", 1 => "cacti_probe" ); %DBLOGIN = ( 0 => "stat4nag", 1 => "stat4nag" ); %DBPASS = ( 0 => "stat", 1 => "stat" ); %DATABASE = ( 0 => "cacti", 1 => "cacti" ); %DBPORT = ( 0 => "3306", 1 => "3306" ); $SERVER = $URL_PART1{$CACTI_INSTANCE}; $DBHOST = $DBHOST{$CACTI_INSTANCE}; $DBLOGIN = $DBLOGIN{$CACTI_INSTANCE}; $DBPASS = $DBPASS{$CACTI_INSTANCE}; $DATABASE = $DATABASE{$CACTI_INSTANCE}; $DBPORT = $DBPORT{$CACTI_INSTANCE}; # First call - Main status page # $NAGIOS is an identifier for the page wanted if ($NAGIOS eq 1) { get_host_id(); print "Content-type: text/html\n\n"; $URL2FETCH = "http://$SERVER/$CACTI_URI/graph_view.php?action=preview&host_id=$ID&graph_template_id=0&filter="; $DOC = LWP::Simple::get $URL2FETCH; die "Couldn't get it!" unless defined $DOC; # Removing all unwanted scrap, rewriting URL/URI and so on using som regular expressions $DOC =~ s/style=\'background-color: #f5f5f5; border: 1px solid #bbbbbb;\'//iso; $DOC =~ s/href=\'graph.php\?/href=\'nagios=2\&cacti_instance=$CACTI_INSTANCE\&function=graph.php\&/g; $DOC =~ s/href=\'nagios=2\&cacti_instance=$CACTI_INSTANCE\&function=graph.php\&action=zoom/href=\'cacti_instance=$CACTI_INSTANCE\&function=graph.php\&action=zoom/g; $DOC =~ s///isog; $DOC =~ s///isog; $DOC =~ s/href=\'/href=\'http:\/\/$CALLING_SERVER$SCRIPT_NAME\?/g; $DOC =~ s///isog; $DOC =~ s/src=\'/src=\'http:\/\/$SERVER\/$CACTI_URI\//g; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/table>//iso; $DOC =~ s/
.*?<\/table>//iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s/Presets:<\/strong>//iso; $DOC =~ s/
.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s///iso; $DOC =~ s/< href=\"images/href=\"http:\/\/$SERVER\/$CACTI_URI\/images/isog; $DOC =~ s/src=\"include/src=\"http:\/\/$SERVER\/$CACTI_URI\/include/g; $DOC =~ s/src=\"images/src=\"http:\/\/$SERVER\/$CACTI_URI\/images/g; $DOC =~ s/href=\"include/href=\"\/$CACTI_URI\/include/g; $DOC =~ s/href=\"images/href=\"\/$CACTI_URI\/images/g; $DOC =~ s/graph_view.php\?action=preview\&graph_template_id=0\&page=/nagios=3\&cacti_instance=$CACTI_INSTANCE\&function=graph_view.php\&action=preview\&host_id=$ID\&graph_template_id=0\&page=/isog; $DOC =~ s/
/
/; print "$DOC"; exit; } if ($NAGIOS eq 2) { print "Content-type: text/html\n\n"; $QUERY_PARMS =~ s/^.*function=//; $QUERY_PARMS =~ s/php\&?/php\?/; $URL2FETCH = "http://$SERVER/$CACTI_URI/$QUERY_PARMS"; $DOC = LWP::Simple::get $URL2FETCH; die "Couldn't get it!" unless defined $DOC; # Removing all unwanted scrap, rewriting URL/URI and so on using som regular expressions $DOC =~ s///isog; $DOC =~ s///isog; $DOC =~ s/href=\'graph.php\?/href=\'nagios=2\&cacti_instance=$CACTI_INSTANCE\&function=graph.php\&/g; $DOC =~ s///isog; $DOC =~ s/href=\'/href=\'http:\/\/$CALLING_SERVER$SCRIPT_NAME\?/g; $DOC =~ s/src=\'/src=\'http:\/\/$SERVER\/$CACTI_URI\//g; $DOC =~ s/
.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/table>//iso; $DOC =~ s/
.*?<\/table>//iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s/src=\"include/src=\"http:\/\/$SERVER\/$CACTI_URI\/include/g; $DOC =~ s/src=\"images/src=\"\http:\/\/$SERVER\/$CACTI_URI\/images/g; $DOC =~ s/href=\"include/href=\"http:\/\/$SERVER\/$CACTI_URI\/include/g; $DOC =~ s/href=\"images/href=\"\http:\/\/$SERVER\/$CACTI_URI\/images/g; $DOC =~ s/graph_view.php\?action=preview\&graph_template_id=0\&page=/nagios=3\&cacti_instance=$CACTI_INSTANCE\&function=graph_view.php\&action=preview\&host_id=$ID\&graph_template_id=0\&page=/isog; print "$DOC"; exit; } if ($NAGIOS eq 3) { my $PAGE=$formParameter{'page'}; my $HOSTID=$formParameter{'host_id'}; print "Content-type: text/html\n\n"; $URL2FETCH = "http://$SERVER/$CACTI_URI/graph_view.php?action=preview&host_id=$HOSTID&graph_template_id=0&page=$PAGE"; $DOC = LWP::Simple::get $URL2FETCH; die "Couldn't get it!" unless defined $DOC; $DOC =~ s/style=\'background-color: #f5f5f5; border: 1px solid #bbbbbb;\'//iso; $DOC =~ s/style=\'background-color: #f5f5f5; border: 1px solid #bbbbbb;\'//iso; $DOC =~ s/href=\'graph.php\?/href=\'nagios=2\&cacti_instance=$CACTI_INSTANCE\&function=graph.php\&/g; $DOC =~ s/href=\'nagios=2\&cacti_instance=$CACTI_INSTANCE\&function=graph.php\&action=zoom/href=\'cacti_instance=$CACTI_INSTANCE\&function=graph.php\&action=zoom/g; $DOC =~ s///isog; $DOC =~ s///isog; $DOC =~ s/href=\'/href=\'http:\/\/$CALLING_SERVER$SCRIPT_NAME\?/g; $DOC =~ s///isog; $DOC =~ s/src=\'/src=\'http:\/\/$SERVER\/$CACTI_URI\//g; $DOC =~ s/
.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/table>//iso; $DOC =~ s/
.*?<\/table>//iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s/Presets:<\/strong>//iso; $DOC =~ s/
.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s///iso; $DOC =~ s/< href=\"images/href=\"http:\/\/$SERVER\/$CACTI_URI\/images/isog; $DOC =~ s/src=\"include/src=\"http:\/\/$SERVER\/$CACTI_URI\/include/g; $DOC =~ s/src=\"images/src=\"http:\/\/$SERVER\/$CACTI_URI\/images/g; $DOC =~ s/href=\"include/href=\"http:\/\/$SERVER\/$CACTI_URI\/include/g; $DOC =~ s/href=\"images/href=\"http:\/\/$SERVER\/$CACTI_URI\/images/g; $DOC =~ s/.*?<\/form>//iso; $DOC =~ s/graph_view.php\?action=preview\&graph_template_id=0\&page=/nagios=3\&cacti_instance=$CACTI_INSTANCE\&function=graph_view.php\&action=preview\&host_id=$HOSTID\&graph_template_id=0\&page=/isog; $DOC =~ s/Previous<\/a>/Previous<\/a>/iso; print "$DOC"; exit; } if ($NAGIOS eq 4) { my $PAGE=$formParameter{'page'}; my $HOSTID=$formParameter{'host_id'}; $DATE_FROM = $formParameter{'date_from'}; $DATE_TO = $formParameter{'date_to'}; $DATE_FROM_EPOCH = $formParameter{'date_from_epoch'}; $DATE_TO_EPOCH = $formParameter{'date_to_epoch'}; print "Content-type: text/html\n\n"; $URL2FETCH = "http://$SERVER/$CACTI_URI/graph_view.php?action=preview&host_id=$HOSTID&graph_template_id=0&page=$PAGE"; $DOC = LWP::Simple::get $URL2FETCH; die "Couldn't get it!" unless defined $DOC; $DOC =~ s/style=\'background-color: #f5f5f5; border: 1px solid #bbbbbb;\'//iso; $DOC =~ s/style=\'background-color: #f5f5f5; border: 1px solid #bbbbbb;\'//iso; $DOC =~ s/href=\'graph.php\?/href=\'nagios=2\&cacti_instance=$CACTI_INSTANCE\&function=graph.php\&/g; $DOC =~ s/href=\'nagios=2\&cacti_instance=$CACTI_INSTANCE\&function=graph.php\&action=zoom/href=\'cacti_instance=$CACTI_INSTANCE\&function=graph.php\&action=zoom/g; $DOC =~ s///isog; $DOC =~ s///isog; $DOC =~ s/href=\'/href=\'http:\/\/$CALLING_SERVER$SCRIPT_NAME\?/g; $DOC =~ s///isog; $DOC =~ s/src=\'/src=\'http:\/\/$SERVER\/$CACTI_URI\//g; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/table>//iso; $DOC =~ s/
.*?<\/table>//iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s/Presets:<\/strong>//iso; $DOC =~ s/
.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s///iso; $DOC =~ s/title=\'Graph Begin Timestamp\' size=\'14\' value=\'.*?\'/title=\'Graph Begin Timestamp\' size=\'14\' value=\'$DATE_FROM\'/g; $DOC =~ s/title=\'Graph End Timestamp\' size=\'14\' value=\'.*?\'/title=\'Graph End Timestamp\' size=\'14\' value=\'$DATE_TO\'/g; $DOC =~ s/graph_start=.*?&/graph_start=$DATE_FROM_EPOCH&/g; $DOC =~ s/graph_end=.*?\/'/graph_end=$DATE_TO_EPOCH\'/g; $DOC =~ s/< href=\"images/href=\"http:\/\/$SERVER\/$CACTI_URI\/images/isog; $DOC =~ s/src=\"include/src=\"http:\/\/$SERVER\/$CACTI_URI\/include/g; $DOC =~ s/src=\"images/src=\"http:\/\/$SERVER\/$CACTI_URI\/images/g; $DOC =~ s/href=\"include/href=\"http:\/\/$SERVER\/$CACTI_URI\/include/g; $DOC =~ s/href=\"images/href=\"http:\/\/$SERVER\/$CACTI_URI\/images/g; $DOC =~ s/.*?<\/form>//iso; $DOC =~ s/$SCRIPT_NAME_BASE\?graph_view.php\?action=preview\&graph_template_id=0\&page=/$SCRIPT_NAME_BASE\?nagios=4\&cacti_instance=$CACTI_INSTANCE\&date_from=$DATE_FROM&date_to=$DATE_TO\&date_from_epoch=$DATE_FROM_EPOCH\&date_to_epoch=$DATE_TO_EPOCH\&function=graph_view.php&action=preview\&host_id=$HOSTID\&graph_template_id=0\&page=/isog; $DOC =~ s/graph_view.php\?action=preview\&graph_template_id=0\&page=/$SCRIPT_NAME_BASE\?nagios=4\&date_from=$DATE_FROM&date_to=$DATE_TO\&date_from_epoch=$DATE_FROM_EPOCH\&date_to_epoch=$DATE_TO_EPOCH\&function=graph_view.php&action=preview\&host_id=$HOSTID\&graph_template_id=0\&page=/isog; print "$DOC"; exit; } else { my $CAL_SET=$formParameter{'cal_select'}; if ($CAL_SET eq "") { $CAL_SET=0; } if ($CAL_SET eq 0) { my $LOCAL_GRAPH_ID=$formParameter{'local_graph_id'}; my $RRA_ID=$formParameter{'rra_id'}; my $GRAPH_START=$formParameter{'graph_start'}; my $GRAPH_END=$formParameter{'graph_end'}; print "Content-type: text/html\n\n"; # The rra-id determines which type/graphic has to be zoomed if ($RRA_ID eq 0) { $URL2FETCH = "http://$SERVER/$CACTI_URI/graph.php?action=zoom&local_graph_id=$LOCAL_GRAPH_ID&rra_id=0&graph_start=$GRAPH_START&graph_end=$GRAPH_END"; } else { $URL2FETCH = "http://$SERVER/$CACTI_URI/graph.php?action=zoom&local_graph_id=$LOCAL_GRAPH_ID&rra_id=$RRA_ID&view_type="; } $DOC = LWP::Simple::get $URL2FETCH; die "Couldn't get it!" unless defined $DOC; $DOC =~ s/src=\'/src=\'http:\/\/$SERVER\/$CACTI_URI\//g; $DOC =~ s/.*?<\/table>//iso; $DOC =~ s/
.*?<\/table>//iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s/src=\"include/src=\"http:\/\/$SERVER\/$CACTI_URI\/include/g; $DOC =~ s/src=\"images/src=\"http:\/\/$SERVER\/$CACTI_URI\/images/g; $DOC =~ s/
//iso; $DOC =~ s/var cURLBase = \"graph.php/var cURLBase = \"http:\/\/$CALLING_SERVER$SCRIPT_NAME\?cacti_instance=$CACTI_INSTANCE\&function=graph.php/g; print "$DOC"; exit; } else { print "Content-type: text/html\n\n"; # Now it is a little bit tricky. The calendar function. All submission of parameters # is done by get. Unfortuntely using the calendar a part is being send using post. # Therefore some paramters are available in $QUERY_PARMS but cannot be splitted up by # ReadParse and must be splitted up manually below. $HOST = $QUERY_PARMS; $HOST =~ s/^host=//; $HOST =~ s/&.*$//; $CACTI_INSTANCE = $QUERY_PARMS; $CACTI_INSTANCE =~ s/^.*cacti_instance=//; # And another phenomenon I haven't solved. Although these vars have # been defined as global in the beginning they are not set here. # Therefore I had to set them seperately here. Bullhsit. $DBHOST = $DBHOST{$CACTI_INSTANCE}; $SERVER = $URL_PART1{$CACTI_INSTANCE}; $DBLOGIN = $DBLOGIN{$CACTI_INSTANCE}; $DBPASS = $DBPASS{$CACTI_INSTANCE}; $DATABASE = $DATABASE{$CACTI_INSTANCE}; $DBPORT = $DBPORT{$CACTI_INSTANCE}; get_host_id(); $DATE_FROM = $formParameter{'date1'}; $DATE_TO = $formParameter{'date2'}; $DATE_FROM_EPOCH = conv_date($DATE_FROM); $DATE_TO_EPOCH = conv_date($DATE_TO); $URL2FETCH = "http://$SERVER/$CACTI_URI/graph_view.php?action=preview&host_id=$ID&graph_template_id=0&filter="; $DOC = LWP::Simple::get $URL2FETCH; die "Couldn't get it!" unless defined $DOC; # Removing all unwanted scrap, rewriting URL/URI and so on using som regular expressions $DOC =~ s/style=\'background-color: #f5f5f5; border: 1px solid #bbbbbb;\'//iso; $DOC =~ s/href=\'graph.php\?/href=\'nagios=2\&cacti_instance=$CACTI_INSTANCE\&function=graph.php\&/g; $DOC =~ s/href=\'nagios=2\&cacti_instance=$CACTI_INSTANCE\&function=graph.php\&action=zoom/href=\'cacti_instance=$CACTI_INSTANCE\&function=graph.php\&action=zoom/g; $DOC =~ s///isog; $DOC =~ s///isog; $DOC =~ s/href=\'/href=\'http:\/\/$CALLING_SERVER$SCRIPT_NAME\?/g; $DOC =~ s///isog; $DOC =~ s/src=\'/src=\'http:\/\/$SERVER\/$CACTI_URI\//g; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s/.*?<\/table>//iso; $DOC =~ s/
.*?<\/table>//iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s/<< Previous<\/strong>/  <\/strong>/g; $DOC =~ s/Next >><\/strong>/  <\/strong>/g; $DOC =~ s///iso; $DOC =~ s///iso; $DOC =~ s/Presets:<\/strong>//iso; $DOC =~ s/
.*?<\/td>//iso; $DOC =~ s/.*?<\/td>//iso; $DOC =~ s///iso; $DOC =~ s/title=\'Graph Begin Timestamp\' size=\'14\' value=\'.*?\'/title=\'Graph Begin Timestamp\' size=\'14\' value=\'$DATE_FROM\'/g; $DOC =~ s/title=\'Graph End Timestamp\' size=\'14\' value=\'.*?\'/title=\'Graph End Timestamp\' size=\'14\' value=\'$DATE_TO\'/g; $DOC =~ s/graph_start=.*?&/graph_start=$DATE_FROM_EPOCH&/g; $DOC =~ s/graph_end=.*?\'/graph_end=$DATE_TO_EPOCH\'/g; $DOC =~ s/src=\"include/src=\"http:\/\/$SERVER\/$CACTI_URI\/include/g; $DOC =~ s/src=\"images/src=\"http:\/\/$SERVER\/$CACTI_URI\/images/g; $DOC =~ s/href=\"include/href=\"http:\/\/$SERVER\/$CACTI_URI\/include/g; $DOC =~ s/href=\"images/href=\"http:\/\/$SERVER\/$CACTI_URI\/images/g; $DOC =~ s/$SCRIPT_NAME_BASE\?graph_view.php\?action=preview\&graph_template_id=0\&page=/$SCRIPT_NAME_BASE\?nagios=4\&cacti_instance=$CACTI_INSTANCE\&date_from=$DATE_FROM&date_to=$DATE_TO\&date_from_epoch=$DATE_FROM_EPOCH\&date_to_epoch=$DATE_TO_EPOCH\&function=graph_view.php&action=preview\&host_id=$ID\&graph_template_id=0\&page=/isog; $DOC =~ s//
/; print "$DOC"; exit; } } sub error { print "Content-type: text/html\n\n"; print "\n"; print "\n"; print "\n"; print "Performance Data Error\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
\n"; print "
\n"; print "
\n"; print "
\n"; print "
\n"; print "
\n"; print "

Sorry - no performance data for this hostavailable

\n"; print "\n"; print "\n"; } sub get_host_id { # Connect to the database ---------------------------------------------------------- $DSN = "DBI:mysql:database=$DATABASE;host=$DBHOST;port=$DBPORT"; $DBH = DBI->connect($DSN, $DBLOGIN, $DBPASS, { PrintError => 0, ### Don't report errors via warn( ) RaiseError => 1 ### Do report errors via die( ) } ); # Constuct a SQL statement ---------------------------------------------------------- # get the id for the host delivered by nagios $SQLSTATEMENT = "SELECT id FROM host where description = \"$HOST\""; # ----- Prepare a SQL statement for execution $STH = $DBH->prepare( $SQLSTATEMENT ); # ----- Execute the statement in the database $STH->execute(); # ----- Retrieve the returned rows of data while ( @ROW = $STH->fetchrow_array( ) ) { # Put the hashed password in the var $ID = $ROW[0]; } $DBH->disconnect; if ($ID eq "") { error(); exit 1; } } sub conv_date { my $DATE2CONV = $_[0]; my $YEAR = "$DATE2CONV"; $YEAR =~ s/-.*//; my $MONTH = "$DATE2CONV"; $MONTH =~ s/^.*?-//; $MONTH =~ s/-.*//; my $DAY = "$DATE2CONV"; $DAY =~ s/^.*?-//; $DAY =~ s/^.*?-//; $DAY =~ s/ .*//; my $HOUR = "$DATE2CONV"; $HOUR =~ s/^.*? //; $HOUR =~ s/...$//; my $MIN = "$DATE2CONV"; $MIN =~ s/^.*?://; $DATE2CONV = Mktime($YEAR,$MONTH,$DAY, $HOUR,$MIN,0); return $DATE2CONV; }