Projet

Général

Profil

Révision 32cd3f60

ID32cd3f6045e5e80d467f53496c2635c667a023bd
Parent d22e3dc0
Enfant c9bd24bd

Ajouté par pru.mike il y a presque 9 ans

Added plugin for Powercom UPSMON PRO software

Voir les différences:

plugins/sensors/upsmonpro_
1
#!/usr/bin/perl
2

  
3
=head1 NAME
4

  
5
upsmonpro_ - Munin plugin to monitor Powercom UPS via UPSMON PRO program L<http://www.pcm.ru/support/soft/>
6

  
7
=head1 INSTALLATION
8

  
9
  /etc/munin/plugins/upsmonpro_load -> /usr/share/munin/plugins/upsmonpro_
10
  /etc/munin/plugins/upsmonpro_status -> /usr/share/munin/plugins/upsmonpro_
11
  /etc/munin/plugins/upsmonpro_temp -> /usr/share/munin/plugins/upsmonpro_
12
  /etc/munin/plugins/upsmonpro_voltage -> /usr/share/munin/plugins/upsmonpro_
13

  
14
=head1 CONFIGURATION
15

  
16
Environment variables:
17

  
18
  host - UPSMON PRO server host, default localhost
19
  port - UPSMON PRO port, default 2601
20

  
21
=head1 MAGIC MARKERS
22

  
23
  #%# family=auto
24
  #%# capabilities=autoconf
25

  
26
=head1 AUTHOR
27

  
28
Copyright (C) 2017 pru.mike@gmail.com
29

  
30
=head1 LICENSE
31

  
32
GPLv2.
33

  
34
=cut
35

  
36
use strict;
37
use warnings;
38
use feature qw/say/;
39
use Munin::Plugin;
40
use IO::Socket::INET;
41
use Time::HiRes qw/usleep/;
42
use English qw/-no-math-vars/;
43
our $VERSION = '0.0.1';
44
$OUTPUT_AUTOFLUSH++;
45

  
46
our $DEFAULT_HOST = 'localhost';
47
our $DEFAULT_PORT = 2601;
48
our %TYPES        = (
49
  voltage => [qw/input output/],
50
  load    => [qw/battery_load battery_capacity/],
51
  temp    => [q/temp/],
52
  status  => [qw/power_failure low_battery voltage_status ups_status  battery_test/]
53
);
54
our @TYPES = keys %TYPES;
55

  
56
{
57
  no strict 'refs';
58
  for my $t (@TYPES) {
59
    for my $sub (qw/autoconf/) {
60
      *{ __PACKAGE__ . "::run_${sub}_$t" } = "run_$sub";
61
    }
62
    *{ __PACKAGE__ . "::run_default_$t" } = sub {
63
      run_default(@{ $TYPES{$t} });
64
    };
65
  }
66
}
67

  
68
{
69
  no strict 'refs';
70
  find_key($ARGV[0])->();
71
}
72

  
73
sub find_key {
74
  my $p = shift || "";
75
  my $type_re = join '|', @TYPES;
76
  unless ($Munin::Plugin::me =~ /upsmonpro_{1,}($type_re)$/) {
77
    die "Could not determine script type [@TYPES] ? name=" . $Munin::Plugin::me . "\n";
78
  }
79
  my $graph = $1;
80

  
81
  return 'run_' . ((grep { $p eq $_ } qw/autoconf config/) ? $ARGV[0] : 'default') . "_$graph";
82
}
83

  
84
sub run_config_voltage {
85
  print <<'END';
86
graph_title UPS Input/Output Voltage
87
graph_vlabel volt
88
graph_scale no
89
graph_category sensors
90
input.label input
91
input.info Input Voltage
92
input.type GAUGE
93
output.label output
94
output.info Output Voltage
95
output.type GAUGE
96
END
97
  exit(0);
98
}
99

  
100
sub run_config_temp {
101
  print <<'END';
102
graph_title UPS Temperature
103
graph_vlabel celsius
104
graph_scale no
105
graph_category sensors
106
temp.label temperature
107
temp.type GAUGE
108
END
109
  exit(0);
110
}
111

  
112
sub run_config_load {
113
  print <<'END';
114
graph_title UPS Battery Load/Capacity
115
graph_vlabel precent%
116
graph_scale no
117
graph_category sensors
118
battery_load.label battery_load
119
battery_load.type GAUGE
120
battery_capacity.label battery_capacity
121
battery_capacity.type GAUGE
122
END
123
  exit(0);
124
}
125

  
126
sub run_config_status {
127
  print <<'END';
128
graph_title UPS Statuses
129
graph_vlabel status
130
graph_scale no
131
graph_category sensors
132
power_failure.label power_failure
133
power_failure.type GAUGE
134
low_battery.label low_battery
135
low_battery.type GAUGE
136
voltage_status.label voltage_status
137
voltage_status.info 0 normal, 1 boost, 2 buck
138
voltage_status.type GAUGE
139
ups_status.label ups_status
140
ups_status.type GAUGE
141
battery_test.label battery_test
142
battery_test.type GAUGE
143
END
144
  exit(0);
145
}
146

  
147
sub run_default {
148
  my $host = $ENV{host} || $DEFAULT_HOST;
149
  my $port = $ENV{port} || $DEFAULT_PORT;
150
  my @val_list = @_;
151
  my $r = gather_data($host, $port);
152
  for (@val_list) {
153
    die "Wrong value: $_" if not exists $r->{$_};
154
    say "${_}.value $r->{$_}";
155
  }
156
}
157

  
158
sub run_autoconf {
159
  if (gather_data($DEFAULT_HOST, $DEFAULT_PORT)->{response} eq 'ok') {
160
    say "yes";
161
  } else {
162
    say "no ($DEFAULT_HOST:$DEFAULT_PORT not response)";
163
  }
164
  exit(0);
165
}
166

  
167
sub gather_data {
168
  my $host = shift;
169
  my $port = shift;
170
  my %data = map { ($_ => 'U') } map { @{ $TYPES{$_} } } @TYPES;
171
  $data{response} = 'failed';
172

  
173
  my $sock = IO::Socket::INET->new(
174
    PeerAddr => $host,
175
    Proto    => 'udp',
176
    PeerPort => $port,
177
    Blocking => 0
178
  ) or die "Cannot create socket: $@";
179

  
180
  my $pattern = pack 'AAAACAAA', split //, 'PCMG1END';
181
  $sock->send($pattern) or die "send to $host: $!";
182
  usleep(200);
183
  my $data;
184
  my $buf;
185
  while ($sock->read($buf, 32)) {
186
    $data .= $buf;
187
  }
188
  if (defined $data and $data =~ /^PCMR.*END$/) {
189
    my @data = unpack('C*', $data);
190
    %data = (
191
      response         => 'ok',
192
      input            => $data[6] + 256 * $data[5],
193
      output           => $data[8] + 256 * $data[7],
194
      battery_load     => $data[11],
195
      battery_capacity => $data[12],
196
      temp             => $data[15],
197
      power_failure    => ($data[18] ? 1 : 0),
198
      low_battery      => ($data[19] ? 1 : 0),
199

  
200
      #voltage_status: 0 = normal, 3 = buck, 2 = boost
201
      voltage_status => ($data[17] == 0 ? 0 : ($data[17] == 3 ? 2 : 1)),
202
      ups_status     => ($data[21]      ? 1 : 0),
203
      battery_test   => ($data[23]      ? 1 : 0),
204
    );
205
  }
206
  return \%data;
207
}

Formats disponibles : Unified diff