/[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 554 - (show annotations)
Sun Nov 28 20:35:02 2010 UTC (10 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 3121 byte(s)
use hostname from snmp

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26