0: #!/usr/bin/perl
1: #
2: # This little program was written becuase I had a few freebsd
3: # machines that were not giving out the right information for
4: # snmpd's ifInOctets and ifOutOctets, thus remote monitoring
5: # for this information via snmpd was useless to me.
6: #
7: # After some heavy reading into some of the features ucd-snmpd
8: # provides I found a feature called "pass-through", which is a
9: # setting within the snmpd.conf that allows you to (unlike the
10: # exec option), pass entire mib tree's to an external program or
11: # script. Hence I can take data that is normally generated by
12: # snmpd (and in my case the failure to generate), and make it
13: # spit out the RIGHT information.
14: #
15: # This script uses the .1.3.6.1.2.1.2.2.1.10 and .16 oid tree's
16: # which are the default inoctets and outoctets. It reads the
17: # information it needs to get from netstat. This supports both
18: # "get" requests and "getnext" requests.
19: #
20: # From what I have seen, this is *bsd only (due to the way it
21: # gets its information from netstat), yet can easily be changed
22: # to work with other unix variants..
23: #
24: # In order to utilize this script, in your specified snmpd.conf
25: # put these lines somewhere in the file:
26: #
27: # pass .1.3.6.1.2.1.2.2.1.16 /local/bin/snmp-pass.pl
28: # pass .1.3.6.1.2.1.2.2.1.10 /local/bin/snmp-pass.pl
29: #
30: # For quicker responses and a little faster output, try
31: # compiling this program using perlcc.
32: #
33: # Mark Thomas <mrk@ackers.net>
34:
35: use strict;
36:
37: my $oidreplace = ".1.3.6.1.2.1.2.2.1";
38: my $in = 10;
39: my $out = 16;
40: my $bytes = 0;
41: my $gorn = $ARGV[0];
42: my $oid = $ARGV[1];
43:
44: if ($gorn eq "-g") {
45:
46: if ($oid =~ /$oidreplace/) {
47: my $num = $oid;
48: $num =~ s/.*\.(\d+)\.(\d+$)/$1 $2/g;
49: my $inorout = $1;
50: my $num = $2;
51: my(@ints) = get_int_info();
52: my $num_ints = @ints;
53: if ($num > $num_ints) {
54: print "$num to big\n";
55: exit 0;
56: }
57: my $num_get = $num - 1;
58: if ($num_get < 0) {
59: print "$num to small\n";
60: exit;
61: }
62: if ($inorout eq $in) {
63: $bytes = get_in_bytes($ints[$num_get]);
64: }
65: elsif ($inorout eq $out) {
66: $bytes = get_out_bytes($ints[$num_get]);
67: }
68: else {
69: exit 0;
70: }
71: do_output($bytes,$oid);
72: }
73: else {
74: exit;
75: }
76:
77: }
78: elsif ($gorn eq "-n") {
79: if ($oid =~ /$oidreplace/) {
80: my(@ints) = get_int_info();
81: my $num_ints = @ints;
82: if ($oid =~ /.1.3.6.1.2.1.2.2.1.10$/) {
83: my $yoid = "$oidreplace.10.1";
84: $bytes = get_in_bytes($ints[0]);
85: do_gn_output($bytes,$yoid);
86: }
87: elsif ($oid =~ /.1.3.6.1.2.1.2.2.1.16$/) {
88: my $yoid = "$oidreplace.16.1";
89: $bytes = get_out_bytes($ints[0]);
90: do_gn_output($bytes,$yoid);
91: }
92: elsif ($oid =~ /$oidreplace.\d+.\d+$/) {
93: my $num = $oid;
94: $num =~ s/.*\.(\d+)\.(\d+$)/$1 $2/g;
95: my $inorout = $1;
96: my $num = $2;
97: my $numforint = $num;
98: my $numforoid = $num + 1;
99: if ($num+1 > $num_ints) {
100: print "$num to big\n";
101: exit 0;
102: }
103: if ($inorout eq $in) {
104: $bytes = get_in_bytes($ints[$num]);
105: }
106: elsif ($inorout eq $out) {
107: $bytes = get_out_bytes($ints[$num]);
108: }
109: my $yoid = "$oidreplace.$inorout.$numforoid";
110: do_output($bytes,$yoid);
111: } else { exit 0; }
112: } else { exit 0; }
113: }
114: else { exit; }
115:
116: sub do_output {
117:
118: my($data,$yoid) = @_;
119: if ($data eq "") {
120: $data = 0;
121: }
122: print "$yoid\n";
123: print "counter\n";
124: print "$data\n";
125: exit 0;
126: }
127:
128: sub get_int_info {
129:
130: my $ifconfig = `/sbin/ifconfig -l 2>/dev/null`;
131: $ifconfig =~ s/ /,/g;
132: $ifconfig =~ s/\n//g;
133: my(@ints) = split(/,/, $ifconfig);
134: return @ints;
135:
136: }
137:
138: sub get_in_bytes {
139:
140: my ($int_name) = $_[0];
141: my @netstat = `/usr/bin/netstat -b -I $int_name`;
142: my $good_inf = $netstat[1];
143: $good_inf =~ s/\://g;
144: $good_inf =~ s/\D+/ /g;
145: $good_inf =~ s/^\s//g;
146: my($nu,$nu,$nu,$nu,$nu,$nu,$nu,$inbytes,$nu,$nu,$outbytes,$nu) = split(/ /, $good_inf);
147: return $inbytes;
148: }
149:
150: sub get_out_bytes {
151: my ($int_name) = $_[0];
152: my @netstat = `/usr/bin/netstat -b -I $int_name`;
153: my $good_inf = $netstat[1];
154: $good_inf =~ s/\://g;
155: $good_inf =~ s/\D+/ /g;
156: $good_inf =~ s/^\s//g;
157: my($nu,$nu,$nu,$nu,$nu,$nu,$nu,$inbytes,$nu,$nu,$outbytes,$nu) = split(/ /, $good_inf);
158: return $outbytes;
159: }
In reply to Using ucd-snmpd's passthrough option by cleen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |