plugin-snmp__supplies: snmp__supplies

File snmp__supplies, 4.0 kB (added by sveinung.marvik@ntnu.no, 6 years ago)

Plugin

Line 
1 #!/usr/bin/perl -w
2 #
3 # Copyright (C) Rune Nordboe Skillingstad, Sveinung Marvik
4 # Reports supplies (ie. toner level) on printers adhering to RFC1759
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2 dated June,
9 # 1991.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #
20 #
21 # $Log$
22 #
23 #%# family=snmpauto
24 #%# capabilities=snmpconf
25
26 use strict;
27 use Net::SNMP;
28
29 my $DEBUG     = $ENV{'DEBUG'} || 0;
30 my $MAXLABEL  = 20;
31
32 my $host      = $ENV{host}      || undef;
33 my $port      = $ENV{port}      || 161;
34 my $community = $ENV{community} || "public";
35 my $iface     = $ENV{interface} || undef;
36 my $timeout   = $ENV{timeout}   || 1;
37 my $warning   = $ENV{warning}   || 10;
38 my $critical  = $ENV{critical|| 5;
39
40 my %cache;
41 my %supplies;
42
43 if(defined $ARGV[0] and $ARGV[0] eq "snmpconf") {
44     print "require 1.3.6.1.2.1.43.11.1.1.6.1.1\n";
45     print "require 1.3.6.1.2.1.43.11.1.1.8.1.1 ^\\d\n";
46     print "require 1.3.6.1.2.1.43.11.1.1.9.1.1 ^\\d\n";
47     exit 0;
48 }
49
50 if($0 =~ /^(?:|.*\/)snmp_([^_]+)_supplies$/) {
51     $host  = $1;
52     if($host =~ /^([^:]+):(\d+)$/) {
53         $host = $1;
54         $port = $2;
55     }
56 }
57 elsif(!defined($host)) {
58     print "# Debug: $0 -- $1\n" if $DEBUG;
59     die "# Error: couldn't understand what I'm supposed to monitor.";
60 }
61
62 my ($session, $error) = Net::SNMP->session(
63     -hostname  => $host,
64     -community => $community,
65     -port      => $port,
66     -timeout   => $timeout
67 );
68
69 if(!defined ($session)) {
70     die "Croaking: $error";
71 }
72
73 &get_multiple ($session, "1.3.6.1.2.1.43.11.1.1.6", "desc");
74 &get_multiple ($session, "1.3.6.1.2.1.43.11.1.1.8", "max");
75 &get_multiple ($session, "1.3.6.1.2.1.43.11.1.1.9", "level");
76 &wash_data;
77
78 # Configure
79 if(defined $ARGV[0] and $ARGV[0] eq "config") {
80     print "host_name $host\n";
81     print "graph_title Supply Level\n";
82     print "graph_args --base 1000 -l 0 --upper-limit 100\n";
83     print "graph_vlabel %\n";
84     print "graph_scale no";
85     print "graph_category other\n";
86     print "graph_info This graph represents supplies level\n";
87    
88     foreach my $supply (keys(%supplies)) {
89         print "supply$supply.label ".(length($supplies{$supply}{desc})<=$MAXLABEL ? $supplies{$supply}{desc} : substr($supplies{$supply}{desc},0,($MAXLABEL-3)))."...";
90         print "\n";
91         print "supply$supply.draw LINE1\n";
92         print "supply$supply.info ".$supplies{$supply}{desc}."\n";
93         print "supply$supply.warning $warning:100\n";
94         print "supply$supply.critical $critical:100\n";
95     }
96    
97     exit 0;
98 }
99
100 # Values
101 if (keys(%supplies) > 0) {
102     foreach my $supply (keys(%supplies)) {
103         printf "supply%s.value %.2f\n",$supply,
104           ($supplies{$supply}{level}/$supplies{$supply}{max})*100;
105     }
106 }
107
108 sub get_multiple {
109     my $handle = shift;
110     my $oid    = shift;
111     my $type   = shift;
112    
113     print "# Getting table $oid...\n" if $DEBUG;
114    
115     my $response = $handle->get_table($oid);
116
117     if(!defined($response)) {
118         return "";
119     } else {
120         foreach my $key (keys(%{$response})) {
121             $supplies{&keyname($key)}{$type} = $response->{$key};
122             print "$key -> ".$response->{$key}."\n" if $DEBUG;
123         }
124     }
125 }
126
127 sub keyname {
128     my $key = shift;
129     if (defined $cache{$key}) {
130         return $cache{$key};
131     }
132    
133     my $tkey = $key;
134
135     $tkey =~ s/.*(\d+\.\d+)$/$1/;
136     $tkey =~ s/\./_/;
137     $cache{$key} = $tkey;
138
139     return $tkey;
140 }
141
142 sub wash_data {
143     # Get rid of supply-levels reporting negative values
144     foreach my $supply (keys (%supplies)) {
145         if ($supplies{$supply}{level} < 0) {
146             delete $supplies{$supply};
147             print "# Deleting entry $supply: supply level unknown.\n" if $DEBUG;
148         }
149     }
150 }