#!/usr/local/bin/perl -wT # # view.cgi - browse the routers/interfaces and display with rtgplot # Copyright (C) 2006 - Anthony Tonns # # 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. # # # initially written # ATonns Mon Aug 7 11:54:25 EDT 2006 # use strict; use CGI qw/-debug :standard :html3 :cgi-lib/; use CGI::Carp qw(fatalsToBrowser); use DBI; use Time::Local; ### customizable variables ### my $config = '/usr/local/rtg/etc/rtg.conf'; my $relative_image_directory = "/rtg"; # # the width of images shown my $width = "570"; my $height = "240"; ### static variables ### my $VERSION = "0.7.4"; ### CODE ### # start CGI output print header; # get the database connection properties from rtg.conf my %dbconfig; my $conf; open($conf,$config) || die "cannot open config file '$config':"; while(<$conf>) { my $line = $_; chomp $line; next if $line =~ /^#/; next if $line =~ /^$/; my ($key,$val) = split(/\s+/,$line,2); if ( $key =~ /^DB_/ ) { $dbconfig{$key} = $val; } } close $conf; # connect to the database my $dbh = DBI->connect( "DBI:mysql:database=$dbconfig{DB_Database};host=$dbconfig{DB_Host}", "$dbconfig{DB_User}", "$dbconfig{DB_Pass}", {'RaiseError' => 1} ); # parse the CGI variables my %p = Vars; my $title = "RTG:"; my $output; # we didn't get a router id if ( ! $p{rid} ) { $output .= "Monitored Devices: ".br; my $statement; $statement .= "SELECT rid, name "; $statement .= "FROM router "; # my @routers; my $ary_ref = $dbh->selectall_arrayref($statement); foreach my $row (@{$ary_ref}) { my $rid = $row->[0]; my $name = $row->[1]; push(@routers,li(a({-href=>"?rid=$rid"},$name))); } $output .= ul(@routers); } # we just got the router, but not an interface elsif ( $p{rid} && ! $p{iid} ) { my $statement; $statement = "SELECT name "; $statement .= "FROM router "; $statement .= "WHERE rid=$p{rid} "; my $hash_ref = $dbh->selectrow_hashref($statement); my $router = $hash_ref->{name}; $title .= $router; $statement = "SELECT id, name, description "; $statement .= "FROM interface "; $statement .= "WHERE rid=$p{rid} "; $statement .= "ORDER BY id "; # my @interfaces; push(@interfaces,li(a({-href=>"?rid=$p{rid}&iid=all"},"ALL interfaces")."[NOTE: may be slow for large switches/routers]")); my $ary_ref = $dbh->selectall_arrayref($statement); my $count = 0; foreach my $row (@{$ary_ref}) { my $iid = $row->[0]; my $name = $row->[1]; my $description = $row->[2]; push(@interfaces,li(a({-href=>"?rid=$p{rid}&iid=$iid"},"$name ($description)"))); $count++; } $output .= "System: $router".br; $output .= "Interfaces: $count".br; $output .= "Page Generated: ".localtime(time).br; $output .= hr; $output .= ul(@interfaces); } # we got the router elsif ( $p{rid} && $p{iid} ) { my $base_args = "rtgplot.cgi?units=bits/s&factor=8&filled=yes"; my $now = time; my $one_day = $now - (60 * 60 * 24); my $one_week = $now - (60 * 60 * 24 * 7); my $one_month = $now - (60 * 60 * 24 * 30); my $statement; $statement = "SELECT name "; $statement .= "FROM router "; $statement .= "WHERE rid=$p{rid} "; my $router_hash_ref = $dbh->selectrow_hashref($statement); my $router = $router_hash_ref->{name}; $title .= $router; $output .= "System: $router".br; my %data; if ( $p{iid} eq "all" ) { $statement = "SELECT id, name, description, speed "; $statement .= "FROM interface "; $statement .= "WHERE rid=$p{rid} "; $statement .= "ORDER BY id "; my $ary_ref = $dbh->selectall_arrayref($statement); my $count = 0; my $last; my @rows; foreach my $row (@{$ary_ref}) { $count++; my $iid = $row->[0]; my $interface = $row->[1]; my $description = $row->[2]; my $speed = $row->[3]/1000000; my $graph_args = "$base_args&t1=ifInOctets_$p{rid}&t2=ifOutOctets_$p{rid}&iid=$iid"; my $day_args = "$graph_args&begin=$one_day&end=$now"; my $this = td( a({-href=>"?rid=$p{rid}&iid=$iid"}, img({-src=>"$day_args",-border=>0,-width=>$width,-height=>$height}) ).br. b("$router: $interface ($description)").br ); if ( $count % 2 ) { unshift(@rows,Tr($last,$this)); } $last = $this; } if ( ! $count % 2 ) { unshift(@rows,Tr($last,"")); } $output .= "Interfaces: $count".br; $output .= "Page Generated: ".localtime(time).br; $output .= hr; $output .= table({-border=>0},\@rows); $output .= hr; } else { $statement = "SELECT name, description, speed "; $statement .= "FROM interface "; $statement .= "WHERE id=$p{iid} "; $statement .= "AND rid=$p{rid} "; my $interface_hash_ref = $dbh->selectrow_hashref($statement); my $interface = $interface_hash_ref->{name}; my $description = $interface_hash_ref->{description}; my $speed = $interface_hash_ref->{speed}/1000000; $output .= "Interface: $interface".br; $output .= "Description: $description".br; $output .= "Speed: $speed Mbps".br; $output .= "Page Generated: ".localtime(time).br; $output .= hr; my $graph_args = "$base_args&t1=ifInOctets_$p{rid}&t2=ifOutOctets_$p{rid}&iid=$p{iid}"; my $day_args = "$graph_args&begin=$one_day&end=$now"; my $week_args = "$graph_args&begin=$one_week&end=$now"; my $month_args = "$graph_args&begin=$one_month&end=$now"; $output .= b("Day View").br; $output .= img({-src=>"$day_args",-width=>$width,-height=>$height}).br; $output .= b("$router: $interface ($description)").br; $output .= hr; $output .= b("Week View").br; $output .= img({-src=>"$week_args",-width=>$width,-height=>$height}).br; $output .= b("$router: $interface ($description)").br; $output .= hr; $output .= b("Month View").br; $output .= img({-src=>"$month_args",-width=>$width,-height=>$height}).br; $output .= b("$router: $interface ($description)").br; $output .= hr; } } else { # something went bad $output .= "Something went wrong. Please ".a({-href=>"?"},"start over").".".br; } # start the HTML print start_html(-title=>$title,-background=>"$relative_image_directory/rtgback.png"); print a({-href=>"http://rtg.sourceforge.net/"},img {-src=>"$relative_image_directory/rtg.png",-border=>0}),p; print hr; print $output; # clean up database handle, incase this gets mod_perl'd in the future $dbh->disconnect; # clean up HTML print a({-href=>"http://rtg.sourceforge.net/"},'RTG') . "Version $VERSION"; print end_html;