]> git.openstreetmap.org Git - chef.git/blob - cookbooks/munin/files/default/plugins/apcpdu_
7fada28972a5cbdabc9dae9d15c2488dbe348e4a
[chef.git] / cookbooks / munin / files / default / plugins / apcpdu_
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2009 - Rune Nordbøe Skillingstad
4 # Copyright (c) 2009 - Kai Ove Gran
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 #       snmp_host_apcpdu - For APC PDUs. Graphs total current throughput.
22 #
23 #%# family=snmpauto
24 #%# capabilities=snmpconf
25
26 use strict;
27 use Net::SNMP;
28
29 my $DEBUG     = $ENV{DEBUG}     || 0;
30 my $host      = $ENV{host}      || undef;
31 my $port      = $ENV{port}      || 161;
32 my $community = $ENV{community} || "public";
33 my $iface     = $ENV{interface} || undef;
34 my $timeout   = $ENV{timeout}   || 1;
35 my $snmp_ver  = $ENV{version}   || 1;
36
37 if($0 =~ /^(?:|.*\/)apcpdu_([^_]+)$/) {
38     $host = $1;
39     if($host =~ /^([^:]+):(\d+)$/) {
40         $host = $1;
41         $port = $2;
42     }
43 } elsif(!defined($host)) {
44     die "# Error: couldn't understand what I'm supposed to monitor."; 
45 }
46
47 my @oidsList =  (
48     '1.3.6.1.4.1.318.1.1.12.2.3.1.1.2.1',
49     '1.3.6.1.4.1.318.1.1.12.2.2.1.1.3.1',
50     '1.3.6.1.4.1.318.1.1.12.1.7.0'    
51 );
52
53 if(defined $ARGV[0] and $ARGV[0] eq "snmpconf") {
54     for(my $i = 0; $i < @oidsList; $i++) {
55         print "require " . $oidsList[$i] . "[0-9]\n"; 
56     }
57     exit 0;
58 }
59
60 my ($session, $error) = Net::SNMP->session(
61     -hostname  => $host,
62     -community => $community,
63     -port      => $port,
64     -timeout   => $timeout,
65     -version   => $snmp_ver,
66 );
67
68 if(!defined ($session)) {
69     die "# Croaking: $error";
70 }
71
72 if($ARGV[0] and $ARGV[0] eq "config") {
73     my $name = &get_single($session, '1.3.6.1.4.1.318.1.1.4.3.3.0');
74     if($name) {
75 #       print "host_name $host\n" unless $host eq 'localhost';
76         print "graph_title Current for $name\n";
77         print "graph_args --base 1000 -l 0\n";
78         print "graph_vlabel Amps\n";
79         print "graph_category Ups\n";
80         print "graph_info This graph shows the total throughput of the PDU\n";
81         print "graph_order load threshold rating\n";
82         print "load.label Load\n";
83         print "load.type GAUGE\n";
84         print "load.info Current load in amps.\n";
85         print "load.draw LINE2\n";
86         print "threshold.label Threshold\n";
87         print "threshold.type GAUGE\n";
88         print "threshold.info Near overload threshold.\n";
89         print "threshold.draw LINE2\n";
90         print "rating.label Rating\n";
91         print "rating.type GAUGE\n";
92         print "rating.info Rating (Max amps).\n";
93         print "rating.draw LINE2\n";
94     }
95     exit 0;                                 
96 }
97
98 my @response = &get_multiple($session, @oidsList);
99
100 printf "load.value %.02f\n", $response[0] / 10;
101 printf "threshold.value %d\n", $response[1];
102 printf "rating.value %d\n", $response[2];
103
104
105 sub get_multiple {
106     my $handle = shift;
107     my @oids = @_;
108
109     my @result;
110     foreach my $oid (@oids) {
111         push(@result, &get_single($handle,$oid));
112     }
113     chomp @result;
114     return @result;
115 }
116
117 sub get_single {
118     my ($handle, $oid) = @_;
119     
120     my $response = $handle->get_request ($oid);
121     if(!defined $response->{$oid}) {
122         print "# No response\n" if $DEBUG;
123         return "";
124     } else {
125         print "# Got response \"".$response->{$oid}."\"\n" if $DEBUG;
126         return $response->{$oid};
127     }
128 }