📄 mrtg
字号:
#! /usr/bin/perl# -*- mode: Perl -*-BEGIN{$main::OS = 'UNIX';#$main::OS = 'NT';#$main::OS = 'VMS';################################################################### MRTG 2.8.12 Multi Router Traffic Grapher################################################################### Created by Tobias Oetiker <oetiker@ee.ethz.ch># and Dave Rand <dlr@bungi.com>## Code Strictification by Richard Bullington <rbulling@obscure.org>#################################################################### Distributed under the GNU copyleft#################################################################### # The path separator is a slash, backslash or semicolon, depending # on the platform. $main::SL = { UNIX=>'/', WINDOWS=>'\\', NT=>'\\', VMS=>'' }->{$main::OS}; # The search path separator is a colon or semicolon depending on the # operating system. $main::PS = { UNIX=>':', WINDOWS=>';', NT=>';', VMS=>':' }->{$main::OS}; # We need to find the place where mrtg is installed, and # then take the .pm and rateup programms from there. $main::binpath =""; if ($0 =~ /^(.+\Q${main::SL}\E)/) { $main::binpath="$1"; } else { foreach $pathname ( split ${main::PS}, $ENV{'PATH'}) { if ((($main::OS eq 'NT') && (-e "$pathname${main::SL}$0")) || (-x "$pathname${main::SL}$0")) { $main::binpath=$pathname; last; } } } die "ERROR: Can\'t find location of mrtg executable\n" unless $main::binpath; unshift (@INC,$main::binpath);}#### Finally, SNMPGet fully written in PERL5. # Thanks to Simon Leinen <simon@switch.ch># More on: http://www.switch.ch/misc/leinen/snmp/perl/##### There older perls tend to behave peculiar with# large integers ... require 5.003;######################################################################## use lib "path to a rrd module as instructed by RRDtool distro";#######################################################################use SNMP_Session "0.71";use BER "0.66";use SNMP_util "0.71";use locales_mrtg "0.01";use Config; use strict;$main::SNMPDEBUG =0;$main::STARTTIME = time; #save start time for comparisons with -Mif($main::OS eq 'UNIX'){ my($i) = 0; my($name); foreach $name (split(/ /, $Config{sig_name})) { $main::signo{$name} = $i; $main::signame[$i++] = $name; } $SIG{INT} = sub {unlink ${main::Cleanfile} if($main::Cleanfile);die "Bailout after SIG INT\n";}; $SIG{TERM} = sub {unlink ${main::Cleanfile} if($main::Cleanfile);die "Bailout after SIG TERM\n";};}$main::DEBUG=0;END { local($?, $!); unlink ${main::Cleanfile} if($main::Cleanfile);}&main;exit(0);sub main { # unbuffer stdout to see everything immediately $|=1 if $main::DEBUG; # read in the config file my ($routers, $cfg, $rcfg, $cfgfile) = readcfg(); # Check the config and create the target object my $target = cfgcheck($routers, $cfg, $rcfg); # Deamon Code my $last_time=0; my $curent_time; my $sleep_time; &demonize_me if $$cfg{'runasdaemon'} and $main::OS eq 'UNIX'; do { # Do this loop once for native mode and forever in daemon mode my $router; # lets make sure that there are not two mrtgs running in parallel. # so we lock on the cfg file. Nothing fancy, just a lockfile my $lockfile = $cfgfile."_l"; my $templock = $cfgfile."_l_" . $$ ; &lockit($lockfile,$templock); #if we run as daemin, we sleep in between collection cycles $sleep_time= ($$cfg{interval}*60)-(time-$last_time); if($sleep_time > 0 ){ #If greater than 0 the sleep that amount of time sleep ($$cfg{interval}*60); } elsif ($last_time != 0) { warn "Warning data collection did not complete within interval!\n"; } $last_time=time; # Use SNMP to populate the target object readtargets($target, $cfg, $cfgfile); # collect data for each router or pseudo target (`executable`) foreach $router (@$routers) { my($savetz) = $ENV{'TZ'}; if ($$rcfg{'timezone'}{$router} ne '') { $ENV{'TZ'} = $$rcfg{'timezone'}{$router} } my ($inlast, $outlast, $uptime, $name, $time) = getcurrent($target, $router, $rcfg); print "inlast $inlast outlast $outlast uptime $uptime". " name $name time $time\n" if $main::DEBUG; #abort, if the router is not responding. next if ($inlast == -1); my ($maxin, $maxout, $maxpercent, $avin, $avout, $avpercent, $cuin, $cuout, $cupercent); if ($$rcfg{'options'}{'dorelpercent'}{$router}) { ($maxin, $maxout, $maxpercent, $avin, $avout, $avpercent, $cuin, $cuout, $cupercent) = writegraphics($router, $cfg, $rcfg, $inlast, $outlast, $time); } else { ($maxin, $maxout ,$avin, $avout, $cuin, $cuout) = writegraphics($router, $cfg, $rcfg, $inlast, $outlast, $time); } &threshcheck($cfg,$rcfg,$cfgfile,$router,$cuin,$cuout); if ($$rcfg{'options'}{'dorelpercent'}{$router}) { print "maxin $maxin maxout $maxout maxpercent $maxpercent avin $avin ", "avout $avout avpercent $avpercent cuin $cuin cuout $cuout ", "cupercent $cupercent\n" if $main::DEBUG >2; } else { print "maxin $maxin maxout $maxout avin $avin ", "avout $avout cuin $cuin cuout $cuout\n" if $main::DEBUG >2; } # set the locale my($LOC); if( $$cfg{'language'} && defined($lang2tran::LOCALE{"\L$$cfg{'language'}\E"})) { $LOC=$lang2tran::LOCALE{"\L$$cfg{'language'}\E"}; } else { $LOC=$lang2tran::LOCALE{'default'}; }; writehtml($router, $cfg, $rcfg, $maxin, $maxout, $maxpercent, $avin, $avout, $avpercent, $cuin, $cuout, $cupercent, $uptime, $name, $LOC) if (!$$cfg{'userrdtool'} && !$$rcfg{'userrdtool'}{$router}); #put TZ things back in shape ... if ($savetz) {$ENV{'TZ'} = $savetz;} else {delete $ENV{'TZ'}}; } # OK we are done, remove the lock files ... close LOCK; unlink ($templock, $lockfile); # clean target if ($$cfg{'runasdaemon'}) { my $tnam; foreach $tnam (keys %{$target}) { $$target{$tnam}{'in'} = undef; $$target{$tnam}{'out'} = undef; $$target{$tnam}{'uptime'} = undef; $$target{$tnam}{'name'} = undef; $$target{$tnam}{'time'} = undef; } } } while ($$cfg{'runasdaemon'}) #In daemon mode run forever}sub quickcheck { my ($first,$second,$arg,$line) = @_; my %rules = (# General CFG 'workdir' => ['$arg && (-d $arg)','"Directory $arg does not exist"'], 'refresh' => ['int($arg) >= 300', '"$arg should be 300 seconds or more"'], 'interval' => ['int($arg) >= 5','"$arg should be more than 5 Minutes"'], 'writeexpires' => ['1','"Internal Error"'], 'icondir' => ['$arg','"Directory argument missing"'], 'language' => ['1','"Mrtg not localized for $arg - defaulting to english"'], 'loadmibs' => ['$arg','"No MIB Files specified"'], 'userrdtool' => ['1','"Internal Error"'], 'runasdaemon' => ['1','"Internal Error"'], 'nospacechar' => ['length($arg) == 1','"$arg must be one character long"'], # Per Router CFG 'target[]' => ['1','"Internal Error"'], #will test this later 'routeruptime[]' => ['1','"Internal Error"'], #will test this later 'maxbytes[]' => ['(($arg =~ /^[0-9]+$/) && ($arg <= (2**31)-1))', '"$arg must be a Number smaller than maxint"'], 'maxbytes1[]' => ['(($arg =~ /^[0-9]+$/) && ($arg < (2**31)-1))', '"$arg must be a Number smaller than maxint"'], 'maxbytes2[]' => ['(($arg =~ /^[0-9]+$/) && ($arg < (2**31)-1))', '"$arg must be a Number smaller than maxint"'], 'absmax[]' => ['($arg =~ /^[0-9]+$/)','"$arg must be a Number"'], 'title[]' => ['1','"Internal Error"'], #what ever the user chooses. 'directory[]' => ['1','"Internal Error"'], #what ever the user chooses. 'pagetop[]' => ['1','"Internal Error"'], #what ever the user chooses. 'pagefoot[]' => ['1','"Internal Error"'], #what ever the user chooses. 'addhead[]' => ['1','"Internal Error"'], #what ever the user chooses. 'unscaled[]' => ['$arg =~ /[dwmy]+/i','"Must be a string of [d]ay, [w]eek, [m]onth, [y]ear"'], 'weekformat[]' => ['$arg =~ /[UVW]/','"Must be either W, V, or U"'], 'withpeak[]' => ['$arg =~ /[dwmy]+/i','"Must be a string of [d]ay, [w]eek, [m]onth, [y]ear"'], 'suppress[]' => ['$arg =~ /[dwmy]+/i','"Must be a string of [d]ay, [w]eek, [m]onth, [y]ear"'], 'xsize[]' => ['((int($arg) >= 30) && (int($arg) <= 600))','"$arg must be between 30 and 600 pixels"'], 'ysize[]' => ['(int($arg) >= 30)','"Must be >= 30 pixels"'], 'ytics[]' => ['(int($arg) >= 1) ','"Must be >= 1"'], 'yticsfactor[]' => ['$arg =~ /[-+0-9.efg]+/','"Should be a numerical value"'], 'step[]' => ['(int($arg) >= 0)','"$arg must be > 0"'], 'timezone[]' => ['1','"Internal Error"'], 'options[]' => ['1','"Internal Error"'], 'colours[]' => ['1','"Internal Error"'], 'background[]' => ['1','"Internal Error"'], 'kilo[]' => ['($arg =~ /^[0-9]+$/)','"$arg must be a Integer Number"'], #define whatever k should be (1000, 1024, ???) 'kmg[]' => ['1','"Internal Error"'], 'ylegend[]' => ['1','"Internal Error"'], 'shortlegend[]' => ['1','"Internal Error"'], 'legend1[]' => ['1','"Internal Error"'], 'legend2[]' => ['1','"Internal Error"'], 'legend3[]' => ['1','"Internal Error"'], 'legend4[]' => ['1','"Internal Error"'], 'legend5[]' => ['1','"Internal Error"'], 'legendi[]' => ['1','"Internal Error"'], 'legendo[]' => ['1','"Internal Error"'], 'xzoom[]' => ['($arg =~ /^[0-9]+(?:\.[0-9]+)?$/)', '"$arg must be a Number xxx.xxx"'], 'yzoom[]' => ['($arg =~ /^[0-9]+(?:\.[0-9]+)?$/)', '"$arg must be a Number xxx.xxx"'], 'xscale[]' => ['($arg =~ /^[0-9]+(?:\.[0-9]+)?$/)', '"$arg must be a Number xxx.xxx"'], 'yscale[]' => ['($arg =~ /^[0-9]+(?:\.[0-9]+)?$/)', '"$arg must be a Number xxx.xxx"'], 'threshdir' => ['$arg && (-d $arg)','"Threshold directory $arg does not exist"'], 'threshmini[]' => ['1','"Internal Threshold Config Error"'], 'threshmino[]' => ['1','"Internal Threshold Config Error"'], 'threshmaxi[]' => ['1','"Internal Threshold Config Error"'], 'threshmaxo[]' => ['1','"Internal Threshold Config Error"'], 'threshdesc[]' => ['1','"Internal Threshold Config Error"'], 'threshprogi[]' =>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -