summaryrefslogtreecommitdiff
path: root/files/munin/apcupsd_pct
blob: 65e34ea5029a238ab3cf07e6ff1e930d07786c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env perl

use strict;
use warnings;
use Carp;
use Pod::Usage;

our $APCACCESS = $ENV{apcaccess} || "/sbin/apcaccess";
our $UPS_MODEL = $ENV{ups_model} || "ES 725";
our $VERSION   = 1.0;
my  %Graph;
my  %Metric;

MAIN: {
    decide_monitor_type();

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

    ### $mode
    eval "do_${mode}();"
        or croak "do_${mode}: $@";

    ### end
    exit 0;
}

=begin comment

pct
  LOADPCT   is the percentage of load capacity as estimated by the UPS.
    15.0 Percent Load Capacity
  BCHARGE   is the percentage charge on the batteries.
    100.0 Percent

volt
  LINEV     is the current line voltage as returned by the UPS.
    102.0 Volts
  BATTV     is the battery voltage as supplied by the UPS.
    13.5 Volts

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

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

=end comment

=cut

sub decide_monitor_type {
    my $type = $0 =~ /_pct/  ? "pct"  :
               $0 =~ /_volt/ ? "volt" :
               $0 =~ /_time/ ? "time" :
               $0 =~ /_pwr/  ? "pwr"  : undef
                   or croak "unknown monitor type: $0";

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

    if ($type eq "pct") {
        $Graph{graph_title}  .= "Percentage";
        $Graph{graph_vlabel}  = "%";
        %Metric =(
            LOADPCT => {
                label    => "load capacity pct",
            },
            BCHARGE => {
                label    => "charge on the batteries pct",
            },
           );
    } elsif ($type eq "volt") {
        $Graph{graph_title}  .= "Voltage";
        $Graph{graph_vlabel}  = "Volts";
        %Metric =(
            LINEV => {
                label    => "line voltage as returned by the UPS",
            },
            BATTV => {
                label    => "battery voltage as supplied by the UPS",
            },
           );
    } elsif ($type eq "time") {
        $Graph{graph_title} .= "Time";
        $Graph{graph_vlabel}  = "minutes";
        %Metric =(
            TIMELEFT => {
                label    => "remaining runtime left on batteries",
            },
           );
    } elsif ($type eq "pwr") {
        $Graph{graph_title} .= "Power";
        $Graph{graph_vlabel}  = "watts";
        %Metric =(
            LOADMETRIC => {
                label    => "absolute power consumption",
            },
           );
    }
}

sub do_fetch {
    ### do_fetch

    my @status_data = retrieve_apcupsd_status()
        or croak "failed: retrieve_apcupsd_status";
    ### status_data: \@status_data

    my $status = parse_status_data(@status_data);
    ### status: $status
    my $prod_status = proccess_status($status);

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

    return 1;
}

sub do_config {
    ### do_config

    while (my($k,$v) = each %Graph) {
        printf "%s %s\n", $k, $v;
    }
    while (my($field,$attr) = each %Metric) {
        $field = lc $field;
        while (my($k,$v) = each %$attr) {
            printf "%s.%s %s\n", $field, $k, $v;
        }
    }

    return 1;
}

sub do_autoconf {
    ### do_config
    print "yes\n";
}

sub retrieve_apcupsd_status {
    open my $apc, '-|', $APCACCESS
        or croak $!;
    my @status_data = <$apc>;
    close $apc;
    chomp @status_data;
    return @status_data;
}

sub proccess_status {
    my $prod = {};
    my($status) = @_;

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

    return $prod;
}

sub parse_status_data {
    my $status = {};
    my($k,$v);
    for (@_) {
        ($k,$v) = split /\s*:\s*/, $_, 2;
        $status->{$k} = $v;
    }
    return $status;
}


__END__

=head1 NAME

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

=head1 SYNOPSIS

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

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

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

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

=head1 DESCRIPTION

munin plugin to monitor APC UPS via apcupsd by apcaccess.

=head1 INSTALLATION

  cp apcupsd_pct $MUNIN_LIBDIR/plugsin/
  
  cd YOUR_MUNIN_PLUGINS_DIR
  (make symbolic links different name)
  ln -s $MUNIN_LIBDIR/plugsin/apcupsd_pct apcupsd_pct
  ln -s $MUNIN_LIBDIR/plugsin/apcupsd_pct apcupsd_volt
  ln -s $MUNIN_LIBDIR/plugsin/apcupsd_pct apcupsd_time
  ln -s $MUNIN_LIBDIR/plugsin/apcupsd_pwr apcupsd_pwr
  
  restart munin-node

=head1 REPOSITORY

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

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

patches and collaborators are welcome.

=head1 SEE ALSO

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

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

=head1 AUTHOR

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

=head1 CHANGELOG

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

=head1 COPYRIGHT & LICENSE

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

=cut

# for Emacsen
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# indent-tabs-mode: nil
# coding: utf-8
# End:

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