#!/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/