/[pxelator]/bin/snmp-printer.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /bin/snmp-printer.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 551 - (hide annotations)
Sun Nov 28 19:52:58 2010 UTC (13 years, 4 months ago) by dpavlin
File MIME type: text/plain
File size: 3009 byte(s)
pull info from SNMP enabled printers and dump JSON

1 dpavlin 551 #!/usr/bin/perl
2     use warnings;
3     use strict;
4    
5     # pull info from SNMP enabled printers and dump JSON
6    
7     use SNMP::Multi;
8     use Data::Dump qw(dump);
9    
10     my $dir = 'json/printer';
11    
12     use JSON;
13     sub save_json {
14     my ( $path, $json ) = @_;
15     $path = "$dir/$path";
16     open(my $fh, '>', $path);
17     print $fh encode_json $json;
18     close($fh);
19     warn "# $path ", -s $path, " bytes\n";
20     }
21    
22     my $debug = $ENV{DEBUG} || 0;
23    
24     my $community = 'public';
25     my @printers = qw(
26     10.60.0.20
27    
28     10.60.3.15
29     10.60.3.17
30    
31     10.60.3.19
32     10.60.3.21
33    
34     10.60.3.23
35     10.60.3.25
36    
37     10.60.3.27
38     10.60.3.29
39    
40     10.60.3.31
41     10.60.3.33
42    
43     10.60.3.35
44     10.60.3.37
45     );
46    
47     @printers = @ARGV if @ARGV;
48    
49     # remove final .1 since we are using bulkwalk to get values!
50     my %vars = qw[
51     info iso.3.6.1.2.1.1.1.0
52     name iso.3.6.1.2.1.43.5.1.1.16.1
53     serial iso.3.6.1.2.1.43.5.1.1.17.1
54     pages iso.3.6.1.2.1.43.10.2.1.4.1
55     @message iso.3.6.1.2.1.43.18.1.1.8
56     @consumable_name iso.3.6.1.2.1.43.11.1.1.6.1
57     @consumable_max iso.3.6.1.2.1.43.11.1.1.8.1
58     @consumable_curr iso.3.6.1.2.1.43.11.1.1.9.1
59     @tray_max iso.3.6.1.2.1.43.8.2.1.9.1
60     @tray_capacity iso.3.6.1.2.1.43.8.2.1.10.1
61     @tray_name iso.3.6.1.2.1.43.8.2.1.13.1
62     @tray_dim_x iso.3.6.1.2.1.43.8.2.1.4.1
63     @tray_dim_y iso.3.6.1.2.1.43.8.2.1.5.1
64     ];
65    
66     my $oid2name;
67     my @vars;
68     while ( my ($name,$oid) = each %vars ) {
69     $oid =~ s/\.[0-1]$// if $name !~ /^\@/;
70     push @vars, [ $oid ];
71     $oid2name->{$oid} = $name;
72     }
73     my @oids = sort { length $a <=> length $b } keys %$oid2name;
74     warn "# vars = ",dump(@vars) if $debug;
75    
76     my $sm = SNMP::Multi->new(
77     Method => 'bulkwalk',
78     Community => $community,
79     Requests => SNMP::Multi::VarReq->new(
80     hosts => [ @printers ],
81     vars => [ @vars ],
82     ),
83     Timeout => 1,
84     Retries => 0,
85     ) or die $SNMP::Multi::error;
86    
87     warn "# working on: ", join(' ', @printers),$/;
88    
89     my $resp = $sm->execute() or die $sm->error();
90    
91     my $collected;
92    
93     foreach my $host ( $resp->hosts ) {
94     my $status;
95    
96     foreach my $result ( $host->results ) {
97     if ( $result->error ) {
98     warn "ERROR: $host ", $result->error;
99     next;
100     }
101    
102     warn "## result = ", dump($result) if $debug;
103    
104     foreach my $v ( $result->varlists ) {
105     foreach my $i ( @$v ) {
106     my ( $oid, undef, $val, $fmt ) = @$i;
107     if ( my $name = $oid2name->{$oid} ) {
108     $status->{$name} = $val;
109     } else {
110     my $oid_base;
111     foreach ( @oids ) {
112     my $oid_part = substr($oid,0,length($_));
113     if ( $oid_part eq $_ ) {
114     $oid_base = $oid_part;
115     last;
116     }
117     }
118    
119     my $name = $oid2name->{$oid_base} || die "no name for $oid in ",dump( $oid2name );
120     if ( $name =~ s/^\@// ) {
121     push @{ $status->{$name} }, $val;
122     } else {
123     $status->{$name} = $val;
124     }
125     }
126     }
127     }
128    
129     }
130    
131     foreach my $group ( grep { /\w+_\w+/ } keys %$status ) {
132     my ( $prefix,$name ) = split(/_/,$group,2);
133     foreach my $i ( 0 .. $#{ $status->{$group} } ) {
134     $status->{$prefix}->[$i]->{$name} = $status->{$group}->[$i];
135     }
136     delete $status->{$group};
137     }
138    
139     print "$host = ",dump($status);
140     save_json $host => $status;
141     $collected->{$host} = $status;
142     }
143    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26