Merge branch 't42'
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 2 Mar 2019 07:44:15 +0000 (08:44 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 2 Mar 2019 07:44:15 +0000 (08:44 +0100)
debian-install.sh
snmp-supply [new file with mode: 0755]
snmp-supply.pl [new file with mode: 0755]

index c948ecc..c9e0254 100755 (executable)
@@ -1 +1,4 @@
-sudo apt-get install cups printer-driver-fujixerox cups-ipp-utils printer-driver-cups-pdf
+sudo apt-get install cups printer-driver-fujixerox cups-ipp-utils printer-driver-cups-pdf snmp snmp-mibs-downloader
+
+# enable mibs
+grep -i '^mibs :' /etc/snmp/snmp.conf && perl -p -i -n -e 's/^mibs : .*/mibs +ALL/' /etc/snmp/snmp.conf
diff --git a/snmp-supply b/snmp-supply
new file mode 100755 (executable)
index 0000000..010c08b
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh -e
+
+SEP=, ./snmp-supply.pl $* | column -s , -t | less --no-init --quit-if-one-screen --chop-long-lines
+
diff --git a/snmp-supply.pl b/snmp-supply.pl
new file mode 100755 (executable)
index 0000000..d044848
--- /dev/null
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use autodie;
+use Data::Dump qw(dump);
+
+my $ip = shift @ARGV || '10.60.3.35';
+my $sep = $ENV{SEP} || "\t";
+
+my $stat = { _take => 1 };
+
+my $cache = "/dev/shm/$ip.snmp";
+my $snmp;
+if ( -e $cache ) {
+       open($snmp, '<', $cache);
+       warn "# cache $cache";
+} else {
+       my $cmd = "snmpwalk -v1 -cpublic $ip | tee /dev/shm/$ip.snmp";
+       warn "# $cmd";
+       open($snmp, '-|', $cmd);
+}
+
+while(<$snmp>) {
+       chomp;
+
+       if ( m/Supplies/ ) {
+               $stat->{_take} = 1;
+       } else {
+               $stat->{_take} = 0;
+       }
+
+       if ( $stat->{_take} && m/::([^\.]+)\.(.+) = (\w+): (.+)/ ) {
+               my ($name,$id,$type,$val) = ( $1,$2,$3,$4 );
+               $stat->{$id}->{$name} = $val;
+               $stat->{_ids}->{$id}++;
+
+               if ( $stat->{_order}->[-1] ne $name ) {
+                       push @{ $stat->{_order} }, $name;
+               }
+       }
+
+}
+
+warn "# stat = ",dump($stat);
+
+my @order = @{ $stat->{_order} };
+warn "# order = ",dump( \@order );
+
+my @ids = keys %{ $stat->{_ids} };
+
+warn "# ids = ",dump( \@ids );
+
+print join($sep,'IP', 'ID', @order),"\n";
+foreach my $id ( @ids ) {
+       print join($sep, $ip, $id, map { $stat->{$id}->{$_} } @order),"\n";
+}
+