Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / ups / apcupsd_pct @ 942bda31

Historique | Voir | Annoter | Télécharger (6 ko)

1
#!/usr/bin/env perl
2

    
3
use strict;
4
use warnings;
5
use Carp;
6
use Pod::Usage;
7

    
8
our $APCACCESS = $ENV{apcaccess} || "/sbin/apcaccess";
9
our $UPS_MODEL = $ENV{ups_model} || "ES 725";
10
our $VERSION   = 1.0;
11
my  %Graph;
12
my  %Metric;
13

    
14
MAIN: {
15
    decide_monitor_type();
16

    
17
    my $mode = $ARGV[0] || "fetch";
18
    $mode =~ /^-/ && pod2usage();
19

    
20
    ### $mode
21
    eval "do_${mode}();"
22
        or croak "do_${mode}: $@";
23

    
24
    ### end
25
    exit 0;
26
}
27

    
28
=begin comment
29

    
30
pct
31
  LOADPCT   is the percentage of load capacity as estimated by the UPS.
32
    15.0 Percent Load Capacity
33
  BCHARGE   is the percentage charge on the batteries.
34
    100.0 Percent
35

    
36
volt
37
  LINEV     is the current line voltage as returned by the UPS.
38
    102.0 Volts
39
  BATTV     is the battery voltage as supplied by the UPS.
40
    13.5 Volts
41

    
42
time
43
  TIMELEFT  is the remaining runtime left on batteries as estimated by the UPS.
44
    38.4 Minutes
45

    
46
pwr
47
  LOADPCT   is the percentage of load capacity as estimated by the UPS.
48
    15.0 Percent Load Capacity
49
  NOMPOWER
50
    330 Watts
51
  LOADMETRIC=LOADPCT/100*NOMPOWER gives realtime power consumption in WATTS
52

    
53
=end comment
54

    
55
=cut
56

    
57
sub decide_monitor_type {
58
    my $type = $0 =~ /_pct/  ? "pct"  :
59
               $0 =~ /_volt/ ? "volt" :
60
               $0 =~ /_time/ ? "time" :
61
               $0 =~ /_pwr/  ? "pwr"  : undef
62
                   or croak "unknown monitor type: $0";
63

    
64
    # common
65
    %Graph = (
66
        graph_title => "APC Status".($UPS_MODEL?" ($UPS_MODEL)":"")." - ",
67
        graph_category => "ups",
68
        graph_info     => "This graph shows information about your APC UPS",
69
        graph_args     => "--base 1000 --lower-limit 0",
70
       );
71

    
72
    if ($type eq "pct") {
73
        $Graph{graph_title}  .= "Percentage";
74
        $Graph{graph_vlabel}  = "%";
75
        %Metric =(
76
            LOADPCT => {
77
                label    => "load capacity pct",
78
            },
79
            BCHARGE => {
80
                label    => "charge on the batteries pct",
81
            },
82
           );
83
    } elsif ($type eq "volt") {
84
        $Graph{graph_title}  .= "Voltage";
85
        $Graph{graph_vlabel}  = "Volts";
86
        %Metric =(
87
            LINEV => {
88
                label    => "line voltage as returned by the UPS",
89
            },
90
            BATTV => {
91
                label    => "battery voltage as supplied by the UPS",
92
            },
93
           );
94
    } elsif ($type eq "time") {
95
        $Graph{graph_title} .= "Time";
96
        $Graph{graph_vlabel}  = "minutes";
97
        %Metric =(
98
            TIMELEFT => {
99
                label    => "remaining runtime left on batteries",
100
            },
101
           );
102
    } elsif ($type eq "pwr") {
103
        $Graph{graph_title} .= "Power";
104
        $Graph{graph_vlabel}  = "watts";
105
        %Metric =(
106
            LOADMETRIC => {
107
                label    => "absolute power consumption",
108
            },
109
           );
110
    }
111
}
112

    
113
sub do_fetch {
114
    ### do_fetch
115

    
116
    my @status_data = retrieve_apcupsd_status()
117
        or croak "failed: retrieve_apcupsd_status";
118
    ### status_data: \@status_data
119

    
120
    my $status = parse_status_data(@status_data);
121
    ### status: $status
122
    my $prod_status = proccess_status($status);
123

    
124
    my $FIELD;
125
    while (my($field,$attr) = each %Metric) {
126
        $field = lc $field;
127
        $FIELD = uc $field;
128
        printf "%s.value %.1f\n", $field, (exists $status->{$FIELD} ? ($status->{$FIELD} =~ /([\d]+\.?[\d]*)/) : ( exists $prod_status->{$FIELD} ? ( $prod_status->{$FIELD} =~ /([\d]+\.?[\d]*)/) : 0 ) );
129
    }
130

    
131
    return 1;
132
}
133

    
134
sub do_config {
135
    ### do_config
136

    
137
    while (my($k,$v) = each %Graph) {
138
        printf "%s %s\n", $k, $v;
139
    }
140
    while (my($field,$attr) = each %Metric) {
141
        $field = lc $field;
142
        while (my($k,$v) = each %$attr) {
143
            printf "%s.%s %s\n", $field, $k, $v;
144
        }
145
    }
146

    
147
    return 1;
148
}
149

    
150
sub do_autoconf {
151
    ### do_config
152
    print "yes\n";
153
}
154

    
155
sub retrieve_apcupsd_status {
156
    open my $apc, '-|', $APCACCESS
157
        or croak $!;
158
    my @status_data = <$apc>;
159
    close $apc;
160
    chomp @status_data;
161
    return @status_data;
162
}
163

    
164
sub proccess_status {
165
    my $prod = {};
166
    my($status) = @_;
167

    
168
    if (exists $status->{NOMPOWER} && exists $status->{LOADPCT}) {
169
        my $pwr_pct = sprintf "%.1f", ($status->{LOADPCT} =~ /([\d]+\.?[\d]*)/) ;
170
        my $nom_pwr = sprintf "%.1f", ($status->{NOMPOWER} =~ /([\d]+\.?[\d]*)/) ;
171
        $prod->{LOADMETRIC} = $pwr_pct/100 * $nom_pwr ;
172
    }
173

    
174
    return $prod;
175
}
176

    
177
sub parse_status_data {
178
    my $status = {};
179
    my($k,$v);
180
    for (@_) {
181
        ($k,$v) = split /\s*:\s*/, $_, 2;
182
        $status->{$k} = $v;
183
    }
184
    return $status;
185
}
186

    
187

    
188
__END__
189

    
190
=head1 NAME
191

    
192
B<apcupsd_pct>, B<apcupsd_volt>, B<apcupsd_time>, B<apcupsd_pwr>- munin plugin for APC UPS
193

    
194
=head1 SYNOPSIS
195

    
196
B<apcupsd_pct>  [ I<config>|I<fetch> ]
197

    
198
B<apcupsd_volt> [ I<config>|I<fetch> ]
199

    
200
B<apcupsd_time> [ I<config>|I<fetch> ]
201

    
202
B<apcupsd_pwr>  [ I<config>|I<fetch> ]
203

    
204
=head1 DESCRIPTION
205

    
206
munin plugin to monitor APC UPS via apcupsd by apcaccess.
207

    
208
=head1 INSTALLATION
209

    
210
  cp apcupsd_pct $MUNIN_LIBDIR/plugsin/
211
  
212
  cd YOUR_MUNIN_PLUGINS_DIR
213
  (make symbolic links different name)
214
  ln -s $MUNIN_LIBDIR/plugsin/apcupsd_pct apcupsd_pct
215
  ln -s $MUNIN_LIBDIR/plugsin/apcupsd_pct apcupsd_volt
216
  ln -s $MUNIN_LIBDIR/plugsin/apcupsd_pct apcupsd_time
217
  ln -s $MUNIN_LIBDIR/plugsin/apcupsd_pwr apcupsd_pwr
218
  
219
  restart munin-node
220

    
221
=head1 REPOSITORY
222

    
223
L<http://github.com/hirose31/munin-apcupsd>
224

    
225
  git clone git://github.com/hirose31/munin-apcupsd.git
226

    
227
patches and collaborators are welcome.
228

    
229
=head1 SEE ALSO
230

    
231
L<http://exchange.munin-monitoring.org/plugins/apcupsd_pct/details>
232

    
233
L<http://munin.projects.linpro.no/wiki/HowToWritePlugins>,
234
L<http://munin.projects.linpro.no/wiki/protocol-config>
235

    
236
=head1 AUTHOR
237

    
238
HIROSE, Masaaki E<lt>hirose31 _at_ gmail.comE<gt>
239

    
240
=head1 CHANGELOG
241

    
242
    * 10/11/2010 - basos - added support for absolute power display
243

    
244
=head1 COPYRIGHT & LICENSE
245

    
246
This program is free software; you can redistribute it and/or modify it
247
under the same terms as Perl itself.
248

    
249
=cut
250

    
251
# for Emacsen
252
# Local Variables:
253
# mode: cperl
254
# cperl-indent-level: 4
255
# indent-tabs-mode: nil
256
# coding: utf-8
257
# End:
258

    
259
# vi: set ts=4 sw=4 sts=0 :