📄 mrtg
字号:
# $$rcfg{'col3'}{$router}, $$rcfg{'rgb3'}{$router},\n\# $$rcfg{'col4'}{$router}, $$rcfg{'rgb4'}{$router},\n\# $$rcfg{'col5'}{$router}, $$rcfg{'rgb5'}{$router}"); # If they're using the "directory" option, we have to adjust the # path to the gifs. my $gifPath = "../" x ($$rcfg{'directory'}{$router} =~ tr|/|/|); if (defined $$cfg{icondir}) { $gifPath = $$cfg{icondir}; #lets make sure there is a trailing path separator $gifPath =~ s|/*$|/|; } print HTML<<TEXT;</TABLE><BR><HR><BR><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0> <TR> <TD WIDTH=63><A HREF="http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"><IMG BORDER=0 SRC="${gifPath}mrtg-l.gif" WIDTH=63 HEIGHT=25 ALT="MRTG"></A></TD> <TD WIDTH=25><A HREF="http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"><IMG BORDER=0 SRC="${gifPath}mrtg-m.gif" WIDTH=25 HEIGHT=25 ALT=""></A></TD> <TD WIDTH=388><A HREF="http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"><IMG BORDER=0 SRC="${gifPath}mrtg-r.gif" WIDTH=388 HEIGHT=25 ALT="Multi Router Traffic Grapher"></A></TD> </TR></TABLE><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0> <TR VALIGN=top> <TD WIDTH=88 ALIGN=RIGHT><FONT FACE="Arial,Helvetica" SIZE=2>TEXTprint HTML &$LOC("version")." $VERSION</FONT></TD>\n"; print HTML<<TEXT; <TD WIDTH=388 ALIGN=RIGHT><FONT FACE="Arial,Helvetica" SIZE=2> <A HREF="http://ee-staff.ethz.ch/~oetiker/">Tobias Oetiker</A> <A HREF="mailto:oetiker\@ee.ethz.ch"><oetiker\@ee.ethz.ch></A> TEXT print HTML &$LOC("and"); print HTML<<TEXT; <A HREF="http://www.bungi.com/">Dave Rand</A> <A HREF="mailto:dlr\@bungi.com"><dlr\@bungi.com></A></FONT> </TD></TR></TABLE>TEXT # We don't need this any more. undef $gifPath; if ($main::OS eq 'VMS') { print HTML " <HR NOSHADE>".&$LOC("Ported to OpenVMS Alpha by").". <NOBR><A HREF=\"http://www.cerberus.ch/\">Werner Berger</A> <A href=\"mailto:werner.berger\@cch.cerberus.ch\"> <werner.berger\@cch.cerberus.ch></A></NOBR>"; } if ($main::OS eq 'NT') { print HTML " <HR NOSHADE> ".&$LOC("Ported to WindowsNT by")." <NOBR><A HREF=\"http://www.testlab.orst.edu/\">Stuart Schneider</A> <A HREF=\"mailto:schneis\@testlab.orst.edu\"> <schneis\@testlab.orst.edu></A></NOBR>"; } if( $$cfg{'language'} && defined($lang2tran::LOCALE{"\L$$cfg{'language'}\E"}) && ($LOC != $lang2tran::LOCALE{"default"}) ) { if(defined($credits::LOCALE{"\L$$cfg{'language'}\E"})) { print HTML $credits::LOCALE{"\L$$cfg{'language'}\E"}; } else { print HTML $credits::LOCALE{'default'}; }; } print HTML $$rcfg{'pagefoot'}{$router} if defined $$rcfg{'pagefoot'}{$router}; print HTML <<TEXT;</BODY></HTML>TEXT close HTML; if ($$cfg{'writeexpires'} =~ /^y/i) { open(HTMLG, ">$$cfg{'workdir'}${main::SL}$$rcfg{'directory'}{$router}$router.html.meta"); print HTMLG "Expires: $expiration\n"; close(HTMLG); }}sub printusage { print <<USAGEDESC;Usage: mrtg <config-file>mrtg: Multi Router Traffic Grapher.If you want to know more about this tool, you might wantto read the docs. They came together with mrtg! Home: http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.htmlUSAGEDESC exit(1);}sub readtargets { my ($target, $cfg, $cfgfile) = @_; my($in,$out,$uptime,$name,$targ,$now,$i,$okfile,$cfgorig); # get a list of known ifDescrs, in order to cross check the # the values returned by querying the various targets ... my(%oldifDescrs,%newifDescrs); # remove any trailing . something from the cfg file name. $cfgorig=$cfgfile; $cfgfile =~ s/\.[^\.\/]+$//g; if(open (CFGOK,"<$cfgfile.ok")) { print STDERR "Reading ifDescs from $cfgfile.ok ...\n" if $main::DEBUG; while (<CFGOK>) { chomp; my($targ,$desc) = split (' = ', $_, 2); # ignore empty ifDescs ... $oldifDescrs{$targ}=$desc if $desc; } close CFGOK; } else { print STDERR "Could not read ifDescs from $cfgfile.ok ...\n" if $main::DEBUG; } open SR, ">>$$cfg{'workdir'}${main::SL}mrtg.dbg" if $main::SNMPDEBUG; print SR time," -- ", (join " | ", keys %$target), "\n" if $main::SNMPDEBUG; foreach $targ (sort keys %$target) { print "getting SNMP variables for target: $targ\n" if $main::DEBUG; if (exists $$target{$targ}{'command'}) { print "external snmpget: executing $$target{$targ}{'command'}\n" if $main::DEBUG; open (EXTERNAL , $$target{$targ}{'command'}."|") || warn "Can't fork to start \'".$$target{$targ}{'command'}."\': $!\n"; warn "Could not get any data from external command ". "'".$$target{$targ}{'command'}. "'\nMaybe the external command did not even start. ($!)\n\n" if eof EXTERNAL; chomp( $$target{$targ}{'in'}=<EXTERNAL>) unless eof EXTERNAL; chomp( $$target{$targ}{'out'}=<EXTERNAL>) unless eof EXTERNAL; chomp( $$target{$targ}{'uptime'}=<EXTERNAL>) unless eof EXTERNAL; chomp( $$target{$targ}{'name'}=<EXTERNAL>) unless eof EXTERNAL; close EXTERNAL; print "External Command returned: in '$$target{$targ}{'in'}'\n". " out '$$target{$targ}{'out'}'\n". " uptime '$$target{$targ}{'uptime'}'\n". " name '$$target{$targ}{'name'}'\n" if $main::DEBUG >2; # if there is no data set it to -1 so that the graph is # not generated if (! exists $$target{$targ}{'in'}) { $$target{$targ}{'in'}=-1; $$target{$targ}{'out'}=-1; } else { $$target{$targ}{'in'} =~ s/^\s*//; $$target{$targ}{'in'} =~ s/\s*$//; $$target{$targ}{'out'} =~ s/^\s*//; $$target{$targ}{'out'} =~ s/\s*$//; $$target{$targ}{'uptime'} =~ s/^\s*//; $$target{$targ}{'uptime'} =~ s/\s*$//; $$target{$targ}{'name'} =~ s/^\s*//; $$target{$targ}{'name'} =~ s/\s*$//; # do we have numbers in the external programs answer ? if ( $$target{$targ}{'in'} !~ /^\d+$/ ) { warn "Problem with Externale get '$targ':\n". " Expected an INTEGER for 'in' but got '$$target{$targ}{'in'}'\n\n"; $$target{$targ}{'in'} = int($$target{$targ}{'in'}); } if ( $$target{$targ}{'out'} !~ /^\d+$/ ) { warn "Problem with Externale get '$targ':\n". " Expected an INTEGER for 'out' but got '$$target{$targ}{'out'}'\n\n"; $$target{$targ}{'out'} = int($$target{$targ}{'out'}); } } if ($main::SNMPDEBUG) { $now=time; print SR "$now -- $targ ". "in: \"$$target{$targ}{'in'}\" ". "out: \"$$target{$targ}{'out'}\"\n"; } } else { if ($main::OS eq 'VMS') { die "FATAL ERROR: The VMS Port only supports 'external snmpget'\n"; } print "snmpget: $$target{$targ}{oid1} $$target{$targ}{oid2} ". "$$target{$targ}{router} ". "$$target{$targ}{community}\n" if $main::DEBUG;# for ($i=0;$i<2;$i++) { if (($$target{$targ}{'oid1'} =~ /^if.+\.(\d+)$/) || ($$target{$targ}{'oid1'} =~ /^1\.3\.6\.1\.2\.1\.2\.2\.1\.\d+\.(\d+)$/)){ # we ll do the interface check only if # the request is to an if OID. Otherwhise this does not make # sense. print STDOUT "1.2.1\n" if ($main::DEBUG > 100); ($in,$out,$uptime,$name,$newifDescrs{$targ}) = &snmpget($$target{$targ}{'community'}."@".$$target{$targ}{'router'}, $$target{$targ}{'oid1'}, $$target{$targ}{'oid2'}, 'sysUptime', 'sysName', "ifDescr.$1"); chomp($newifDescrs{$targ}); #some ifDescrs end with \n ... aargh } elsif (($$target{$targ}{'oid1'} =~ /^cache.+$/) || ($$target{$targ}{'oid1'} =~ /^1\.3\.6\.1\.4\.1\.3495\.1\.\d+(\.\d+)+$/)){ print STDOUT "1.4.1\n" if ($main::DEBUG > 100); ($in,$out) = &snmpget($$target{$targ}{'community'}."@".$$target{$targ}{'router'}, $$target{$targ}{'oid1'}, $$target{$targ}{'oid2'}); } else { print STDOUT "neither 1.2.1 nor 1.4.1: $$target{$targ}{'oid1'}\n" if ($main::DEBUG > 100); ($in,$out,$uptime,$name) = &snmpget($$target{$targ}{'community'}."@".$$target{$targ}{'router'}, $$target{$targ}{'oid1'}, $$target{$targ}{'oid2'}, 'sysUptime', 'sysName'); } if (($in >= 0) && (($$target{$targ}{'oid1'} =~ /ifOperHack/) || ($$target{$targ}{'oid1'} =~ /ifAdminHack/))) { $in = 0 unless $in == 1; print SR "SNMPGET: if*Hack in action: targ - '$targ' in - '$in'\n" if $main::SNMPDEBUG; } if (($$target{$targ}{'oid2'} =~ /ifOperHack/) || ($$target{$targ}{'oid2'} =~ /ifAdminHack/)) { $out = 0 unless $out == 1; print SR "SNMPGET: if*Hack in action: targ - '$targ' out - '$out'\n" if $main::SNMPDEBUG; }# if ($in != -1) {# last;# }# print "CANT GET $targ PROBLEM in:'$in' out:'$out'\n " if $main::DEBUG;# sleep(1); # try again in 1 seconds ... # } if ( not defined $in or not defined $out ) {# print STDERR "SNMPGET: Failed to reach target: \"$targ\". I tried multiple times! ".$$target{$targ}{'community'}."@".$$target{$targ}{'router'}."\n"; $in=-1; $out=-1; } else { # we only get here if there was data returned print "$targ --> in: $in out: $out name: $name\n" if $main::DEBUG; if (($oldifDescrs{$targ} ne '') && ($newifDescrs{$targ} ne '') && ($oldifDescrs{$targ} ne $newifDescrs{$targ})){ $in=-1,$out=-1; print STDERR <<WARN;Warning: There is something wrong with Target '$targ' * Its ifDescr used to be '$oldifDescrs{$targ}'* Now it is '$newifDescrs{$targ}'I will not update this graph for the moment. Maybe your Router haschanged the port to interface mapping. This can happen when newInterfaces are added to the router or when it is rebooted.You should alter your '$cfgfile' file to fix the mapping and thenremove the offending lines from your '$cfgfile.ok' file. Mrtg willthen assume that everything is OK and create new entries representing the new matching. ---------------------------------------------------------------------WARN ; } # Build the new ok files if ($oldifDescrs{$targ} ne '') { $okfile .= "$targ = $oldifDescrs{$targ}\n"; } elsif ($newifDescrs{$targ} ne '') { $okfile .= "$targ = $newifDescrs{$targ}\n"; } } # # sometimes we can only observe the router on the wrong side # of the fence ... if ($$target{$targ}{'ioswap'} eq '-') { $$target{$targ}{'in'}=$out; $$target{$targ}{'out'}=$in; } else { $$target{$targ}{'in'}=$in; $$target{$targ}{'out'}=$out; } $$target{$targ}{'uptime'}=$uptime; $$target{$targ}{'name'}=$name; if ($main::SNMPDEBUG) { $now=time; print SR "$now -- $$target{$targ}{'router'} ". " in: \"$in\" out: \"$out\"\n"; } } # now that the data is gatherd we take a time stamp ... # for simple targets we will pass this along to rateup and be happy $$target{$targ}{'time'}=time; } close SR if $main::SNMPDEBUG; # # store the new ifDescr File if(open CFGOK,">$cfgfile.ok") { print STDERR "Writing ifDescs to $cfgfile.ok ...\n" if $main::DEBUG; print CFGOK $okfile; close CFGOK; } else { print STDERR "Problem: Could not write ifDescs to $cfgfile.ok ...\n"; } #}# Search for interface corresponding to physical address on routersub getifindex { my ($router,$phys) = @_; my ($intf, $iphys, $bin); my (@phys); # Need to read router for first time if (!defined($main::phys2intf{$router})) { @phys = &snmpwalk ($router, "ifPhysAddress"); for (@phys) { ($intf,$bin) = split(/:/,$_,2); $iphys = unpack 'H*', $bin; $main::phys2intf{$router}{$iphys} = $intf; } } # Check for phys address present if (! defined($main::phys2intf{$router}{$phys})) { warn ("ERROR\nRequested physcal address <$phys> not found.\n"); return; } return $main::phys2intf{$router}{$phys};}# Change physical address from format 0-a3-03-ff-a5-d1 to 00a303ffa5d1sub formatphysaddress { my ($phys) = @_; my ($new); $phys = lc($phys); if ($phys =~ /-/) { for (split(/-/,$phys)) { $new .= sprintf ("%02x", hex $_); } } else { $new = $phys; } return $new;}sub demonize_me () { require 'POSIX.pm'; print "Daemonizing MRTG ...\n"; fork && exit; &POSIX::setsid; open STDOUT,'>/dev/null' or die;# open STDERR,'>/dev/null' or die; we still want the errors ... open STDIN, '</dev/null' or die; }sub lockit { my ($lockf
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -