| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use Getopt::Long; |
| 4 |
use IPC::Run qw< start >; |
| 5 |
use Time::HiRes qw< sleep >; |
| 6 |
|
| 7 |
|
| 8 |
# |
| 9 |
# main |
| 10 |
# ---- |
| 11 |
MAIN: { |
| 12 |
run() unless caller(); |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
# |
| 17 |
# run() |
| 18 |
# --- |
| 19 |
sub run { |
| 20 |
# default options |
| 21 |
my %options = ( |
| 22 |
count => 5, |
| 23 |
); |
| 24 |
|
| 25 |
# process comand line options |
| 26 |
Getopt::Long::Configure(qw< no_auto_abbrev no_ignore_case >); |
| 27 |
GetOptions(\%options, qw{ |
| 28 |
help|h! version|V! |
| 29 |
loop|l! count|c=i |
| 30 |
debug|d! format|as|F=s |
| 31 |
}) or pod2usage(1); |
| 32 |
|
| 33 |
# handle --help and --version |
| 34 |
$options{help} and pod2usage(2); |
| 35 |
$options{version} and version(); |
| 36 |
|
| 37 |
# debug mode forces normal output |
| 38 |
$options{format} = "human" if $options{debug}; |
| 39 |
|
| 40 |
# check the output format to use |
| 41 |
if (lc $options{format} eq "json") { |
| 42 |
if (eval "use JSON; 1") { |
| 43 |
# use JSON v2 API even with JSON v1 |
| 44 |
if (JSON->VERSION < 2.00) { |
| 45 |
no warnings; |
| 46 |
*to_json = *JSON::encode = \&JSON::objToJson; |
| 47 |
*from_json = *JSON::decode = \&JSON::jsonToObj; |
| 48 |
} |
| 49 |
} |
| 50 |
else { |
| 51 |
$options{format} = "human"; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
# read the rest of the arguments |
| 56 |
my ($first_oid, $extsnmp_bin, @extsnmp_args) = @ARGV; |
| 57 |
|
| 58 |
# prepare the command |
| 59 |
my @cmd = ( $extsnmp_bin, @extsnmp_args ); |
| 60 |
my ($in, $out, $err) = ("", "", ""); |
| 61 |
|
| 62 |
# execute the program |
| 63 |
my $process = start(\@cmd, \$in, \$out, \$err) |
| 64 |
or die "fatal: Can't execute '$extsnmp_bin'. Exit status: $?\n"; |
| 65 |
|
| 66 |
# wait for the program to initialise |
| 67 |
sleep .5; |
| 68 |
|
| 69 |
my $oid = $first_oid; |
| 70 |
my $count = $options{count}; |
| 71 |
my $i = 1; |
| 72 |
my @data; |
| 73 |
|
| 74 |
while ($process->pumpable) { |
| 75 |
$oid = $oid eq "NONE" ? $first_oid : $oid; |
| 76 |
$in = "getnext\n$oid\n"; |
| 77 |
print "--> getnext\n--> $oid\n" if $options{debug}; |
| 78 |
|
| 79 |
$process->pump until index($out, $/) > 0; |
| 80 |
($oid, my $type, my $value) = split /$\//, $out; |
| 81 |
$out =~ s/^/<-- /gm, print $out if $options{debug}; |
| 82 |
$out = ""; |
| 83 |
|
| 84 |
if ($oid eq "NONE") { |
| 85 |
if ($options{loop} and $count > 0) { |
| 86 |
$count--; |
| 87 |
|
| 88 |
if ($options{format} eq "json") { |
| 89 |
print to_json(\@data), $/; |
| 90 |
@data = (); |
| 91 |
} |
| 92 |
else { |
| 93 |
print "-" x 10, $/; |
| 94 |
} |
| 95 |
|
| 96 |
next |
| 97 |
} |
| 98 |
else { |
| 99 |
last |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if ($options{format} eq "csv") { |
| 104 |
print "$oid;$type;$value\n"; |
| 105 |
} |
| 106 |
elsif ($options{format} eq "json") { |
| 107 |
push @data, { oid => $oid, type => $type, value => $value }; |
| 108 |
} |
| 109 |
else { |
| 110 |
print "$oid ($type) = $value\n"; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
print to_json(\@data), $/ if $options{format} eq "json"; |
| 115 |
|
| 116 |
$process->finish; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
# |
| 121 |
# pod2usage() |
| 122 |
# --------- |
| 123 |
sub pod2usage { |
| 124 |
my ($level) = @_; |
| 125 |
|
| 126 |
if (eval { require Pod::Usage }) { |
| 127 |
Pod::Usage::pod2usage({ |
| 128 |
-exitval => 0, -verbose => $level, -noperldoc => 1, |
| 129 |
}); |
| 130 |
} |
| 131 |
else { |
| 132 |
require Pod::Text; |
| 133 |
Pod::Text::pod2text(__FILE__, \*STDOUT); |
| 134 |
exit; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
# |
| 140 |
# version() |
| 141 |
# ------- |
| 142 |
sub version { |
| 143 |
print "$::PROGRAM v$::VERSION\n"; |
| 144 |
exit; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
32272 |
| 149 |
|
| 150 |
__END__ |
| 151 |
|
| 152 |
=head1 NAME |
| 153 |
|
| 154 |
pseudo-walk - Manually walk the OID tree provided by a Net-SNMP extension |
| 155 |
|
| 156 |
=head1 SYNOPSIS |
| 157 |
|
| 158 |
pseudo-walk [--debug] [--loop] [--count N] [--format type] |
| 159 |
first-oid path/to/snmpext [snmpext args ..] |
| 160 |
|
| 161 |
pseudo-walk --help |
| 162 |
pseudo-walk --version |
| 163 |
|
| 164 |
|
| 165 |
=head1 OPTIONS |
| 166 |
|
| 167 |
B<Behaviour options> |
| 168 |
|
| 169 |
=over |
| 170 |
|
| 171 |
=item B<-l>, B<--loop> |
| 172 |
|
| 173 |
Run in loop mode. |
| 174 |
|
| 175 |
=item B<-c>, B<--count> I<number> |
| 176 |
|
| 177 |
Specify the number of times to loop over. Defaults to 5. |
| 178 |
|
| 179 |
=back |
| 180 |
|
| 181 |
B<Output options> |
| 182 |
|
| 183 |
=over |
| 184 |
|
| 185 |
=item B<-d>, B<--debug> |
| 186 |
|
| 187 |
Enable debug mode, printing the communication with the SNMP extension. |
| 188 |
|
| 189 |
=item B<-F>, B<--format> I<type> |
| 190 |
|
| 191 |
Specify how to print the data from the SNMP extension. |
| 192 |
Available types are C<csv>, C<json> and C<human>. Defaults to C<human>. |
| 193 |
|
| 194 |
=back |
| 195 |
|
| 196 |
B<Standard options> |
| 197 |
|
| 198 |
=over |
| 199 |
|
| 200 |
=item B<-h>, B<--help> |
| 201 |
|
| 202 |
Print this help screen and exit. |
| 203 |
|
| 204 |
=item B<-V>, B<---version> |
| 205 |
|
| 206 |
Print the program name and version and exit. |
| 207 |
|
| 208 |
=back |
| 209 |
|
| 210 |
|
| 211 |
=head1 DESCRIPTION |
| 212 |
|
| 213 |
This program is a tool to help developers of Net-SNMP C<pass_persist> |
| 214 |
extensions by manually walking the OID tree it provides, as Net-SNMP |
| 215 |
would do if queried by snmpwalk. |
| 216 |
|
| 217 |
|
| 218 |
=head1 AUTHOR |
| 219 |
|
| 220 |
SE<eacute>bastien Aperghis-Tramoni, C<< <sebastien at aperghis.net> >> |
| 221 |
|
| 222 |
|
| 223 |
=head1 COPYRIGHT & LICENSE |
| 224 |
|
| 225 |
Copyright 2008-2010 SE<eacute>bastien Aperghis-Tramoni, all rights reserved. |
| 226 |
|
| 227 |
This program is free software; you can redistribute it and/or modify it |
| 228 |
under the same terms as Perl itself. |
| 229 |
|
| 230 |
=cut |
| 231 |
|
| 232 |
|