1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
#!@@PERL@@ @@PERLOPTS@@
# Copyright 1999, 2000, 2001 (c) Thomas Erskine <@@AUTHOR@@>
# See the COPYRIGHT file with the distribution.
# snmp-route-collector - a remstats collector for BGP4 routing info via SNMP
# $Id: snmp-route-collector.pl,v 1.12 2001/08/31 21:07:20 remstats Exp $
# - - - Configuration - - -
use strict;
# What is this program called, for error-messages and file-names
$main::prog = 'snmp-route-collector';
# Which collector is this
$main::collector = 'snmp-route';
# Where is the default configuration dir
$main::config_dir = '@@CONFIGDIR@@';
# - - - Version History - - -
(undef, $main::version) = split(' ', '$Revision: 1.12 $');
# - - - Setup - - -
use lib '.', '@@LIBDIR@@', '@@RRDLIBDIR@@';
require "remstats.pl";
use Getopt::Std;
use SNMP_util "0.69";
use RRDs;
require "snmpstuff.pl";
# Parse the command-line
my %opt = ();
my @hosts;
getopts('c:d:f:FhH:u', \%opt);
if (defined $opt{'h'}) { &usage; } # no return
if (defined $opt{'c'}) { $main::community = $opt{'c'}; }
if (defined $opt{'d'}) { $main::debug = $opt{'d'}; } else { $main::debug = 0; }
if (defined $opt{'f'}) { $main::config_dir = $opt{'f'}; }
if (defined $opt{'F'}) { $main::force_collection = 1; } else { $main::force_collection = 0; }
if( defined $opt{'H'}) { @hosts = split(',', $opt{'H'}); }
if (defined $opt{'u'}) { $main::use_uphosts = 0; } else { $main::use_uphosts = 1; }
&read_config_dir($main::config_dir, 'general', 'oids', 'groups',
'rrds', 'host-templates', 'hosts');
%main::uphosts = &get_uphosts if ($main::use_uphosts);
# No buffering when debugging
if ($main::debug) { $| = 1; }
&snmp_load_oids;
# - - - Mainline - - -
my ($host, $ip, $realrrd, $wildrrd, $wildpart, $fixedrrd, $start_time, $run_time);
$start_time = time();
$main::entries_collected = $main::entries_used = $main::requests = 0;
my $tmpfile = $main::config{DATADIR} .'/LAST/'. $main::collector .'.'. $$;
my $lastfile = $main::config{DATADIR} .'/LAST/'. $main::collector;
open (TMP, ">$tmpfile") or &abort("can't open $tmpfile: $!");
unless( @hosts) { @hosts = keys %{$main::config{HOST}}; }
foreach $host (@hosts) {
next if ($host eq '_remstats_');
if ($main::use_uphosts and not defined $main::uphosts{$host}) {
&debug("$host is down(uphosts); skipped") if ($main::debug);
next;
}
$ip = &get_ip($host);
next unless (defined $ip);
&debug("doing host $host ".×tamp) if ($main::debug);
$main::data_from_host = 0;
foreach $realrrd (@{$main::config{HOST}{$host}{RRDS}}) {
($wildrrd, $wildpart, undef, $fixedrrd) = &get_rrd($realrrd);
unless (defined $main::config{COLLECTOR}{$main::collector}{$wildrrd}) {
&debug(" $realrrd is not collected by $main::collector; skipped") if ($main::debug>1);
next;
}
# Check whether it's at all time to collect data
unless ($main::force_collection or
&check_collect_time($host, $wildrrd, $fixedrrd)) {
&debug(" not time yet for $realrrd($wildrrd): skipped") if ($main::debug>1);
next;
}
&debug(" doing rrd $realrrd($wildrrd)") if ($main::debug);
# Collect it
&collect_host( $host, $realrrd, $wildrrd, $wildpart);
}
}
# Now remstats instrumentation info
my $now = time;
$run_time = $now - $start_time;
print <<"EOD_INSTRUMENTATION";
_remstats_ $now ${main::collector}-collector:requests $main::requests
_remstats_ $now ${main::collector}-collector:collected $main::entries_collected
_remstats_ $now ${main::collector}-collector:used $main::entries_used
_remstats_ $now ${main::collector}-collector:runtime $run_time
EOD_INSTRUMENTATION
close(TMP) or &abort("can't open $tmpfile: $!");
rename $tmpfile, $lastfile or &abort("can't rename $tmpfile to $lastfile: $!");
exit 0;
#----------------------------------------------------------- collect_host ---
sub collect_host {
my ($host, $realrrd, $wildrrd, $wildpart) = @_;
my ($comhost, $routeinfo, $prefix, $prefixlen, $peer, $best, $now);
$comhost = &get_comhost($host, $wildrrd);
return unless (defined $comhost);
&debug(" using $comhost for $host") if ($main::debug);
# Only walk a given host once, since the info for all peers is intermixed.
unless (defined $main::route_host and $main::route_host eq $host and defined @main::routes) {
$main::route_host = $host;
@main::routes = snmpwalk( $comhost, 'bgp4PathAttrBest');
++$main::requests;
undef %main::routes;
undef %main::best;
&debug(" read ". $#main::routes ." routes from $host") if ($main::debug);
}
unless (defined $main::routes[0]) {
&debug(" no data for bgp4PathAttrBest; skipped") if ($main::debug);
return;
}
# Count all the info into hashes
unless (defined %main::routes) {
$main::entries_collected += $#main::routes + 1;
foreach $routeinfo (@main::routes) {
if ($routeinfo =~ /^(\d+\.\d+\.\d+\.\d+)\.(\d+)\.(\d+\.\d+\.\d+\.\d+):(\d+)$/) {
$prefix = $1;
$prefixlen = $2;
$peer = $3;
$best = $4;
&debug(" collected $peer($best) for $prefix/$prefixlen") if ($main::debug>2);
if (defined $main::routes{$peer}) { $main::routes{$peer}++; }
else { $main::routes{$peer} = 1; $main::best{$peer} = 0; }
$main::best{$peer}++ if ($best == 2);
}
else {
&error("unknown routing info for $host: $routeinfo; skipped");
}
}
if ($main::debug) {
foreach $peer (keys %main::routes) {
&debug("$peer: $main::routes{$peer}, best $main::best{$peer}");
}
}
}
# Now say what we've got for this one
if (defined $main::routes{$wildpart}) {
$now = time;
print $host .' '. $now .' routes-'. $wildpart .' '. $main::routes{$wildpart} ."\n";
print $host .' '. $now .' routes-best-'. $wildpart .' '. $main::best{$wildpart} ."\n";
$main::entries_used += 2;
print TMP $host .' '. $now .' routes-'. $wildpart .' '. $main::routes{$wildpart} ."\n";
print TMP $host .' '. $now .' routes-best-'. $wildpart .' '. $main::best{$wildpart} ."\n";
$main::data_from_host = 1;
}
else {
&debug("no routes for $host $realrrd") if ($main::debug);
}
}
#----------------------------------------------------------------- usage ---
sub usage {
print STDERR <<"EOD_USAGE";
$main::prog version $main::version
usage: $main::prog [options]
where options are:
-c ccc use 'ccc' for the read community string; overrides host
-d nnn enable debugging output at level 'nnn'
-f fff use 'fff' for config-dir [$main::config_dir]
-F force collection even if it's not time
-h show this help
-H HHH only try hosts from 'HHH', a comma-separated list
-u ignore uphosts file
EOD_USAGE
exit 0;
}
#----------------------------------------------------------------- debug ---
sub debug {
my $msg = join('', @_);
if ($main::debug) { print STDERR "DEBUG: $msg\n"; }
0;
}
#----------------------------------------------------------------- error ---
sub error {
my $msg = join('', @_);
print STDERR "$main::prog: ERROR: $msg\n";
0;
}
#------------------------------------------------------------------ abort ---
sub abort {
my $msg = join('', @_);
print STDERR "$main::prog: ABORT: $msg\n";
exit 1;
}
#------------------------------------------------- keep_strict_happy ---
sub keep_strict_happy {
$main::community = 0;
}
|