/[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

Contents of /bin/snmp-printer.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 552 - (show annotations)
Sun Nov 28 20:25:22 2010 UTC (13 years, 4 months ago) by dpavlin
File MIME type: text/plain
File size: 3021 byte(s)
create directories for new printers

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26