forked from dkrizic/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_homematic
executable file
·210 lines (190 loc) · 6 KB
/
check_homematic
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
#!/usr/bin/perl -w
# Checks Homematic CCU or Homegear for battery state and unreachable devices
# Requires perl and RPC:XML
# Written by Darko Krizic <darko@krizic.net>
use strict;
use Getopt::Long;
use RPC::XML;
use RPC::XML::Client;
use Data::Dumper;
sub usage() {
print <<EOF;
Usage: check_homematic [-v] --host=<hostname> --port=<port> --mode=<lowbat|unreach> --level=<critical|warning>
host: The Homematic XML RPC host
port: The port number of the XML RPC host
mode: Check for "lowbat", "unreach", "srvmsg" (for Service Messages) or configuration "pending"
level: If a lowbat or unreach occurs, is it a warning or critical
EOF
exit 255;
}
my $verbose = 0;
my $host = undef;
my $port = undef;
my $mode = undef;
my $level = undef;
my $ccu = 0;
GetOptions(
'verbose|v!' => \$verbose,
'host|h=s' => \$host,
'port|p=i' => \$port,
'mode|m=s' => \$mode,
'level|l=s' => \$level,
);
usage() unless defined $host;
usage() unless defined $port;
usage() unless defined $mode;
usage() unless defined $level;
usage() unless $level eq 'warning' || $level eq 'critical';
usage() unless $mode eq 'lowbat' || $mode eq 'unreach' || $mode eq 'pending' || $mode eq "srvmsg";
my $url = "http://$host:$port";
my $cli = RPC::XML::Client->new($url);
my $ret = 1;
my $retname = 'WARNING';
if( $level eq 'critical' ) {
$ret = 2;
$retname = 'CRITICAL';
}
if( $mode eq 'srvmsg' ) {
print "Connecting to $url\n" if $verbose;
my $resp = $cli->send_request("getServiceMessages" );
print Dumper( $resp ) if $verbose;
my %state;
if( ref( $resp ) eq 'RPC::XML::array' ) {
foreach my $entry (@{$resp}) {
print Dumper( $entry ) if $verbose;
my $address = @{$entry}[0]->value;
my $state = @{$entry}[1]->value;
push @{$state{$state}}, $address;
#print "$address = $state\n";
}
}
print Dumper( \%state ) if $verbose;
if( scalar keys %state == 0 ) {
print "OK: No service message\n";
exit 0;
}
my @state;
foreach my $state ( keys %state ) {
push @state, "$state (".join(", ", @{$state{$state}}).")";
}
print "$retname: Devices have service messages: ".join(", ", @state)."\n";
exit $ret;
}
my %address;
{
print "Connecting to $url\n" if $verbose;
my $resp = $cli->send_request('listDevices');
print Dumper( $resp ) if $verbose;
foreach my $device (@{$resp}) {
#print Dumper( $device );
my $id;
if( $device->{'ID'} ) {
$id = $device->{'ID'}->value;
}
my $address = $device->{'ADDRESS'}->value;
my $deviceDesc = $cli->send_request;
my $channel = undef;
if( $address =~ m/:/ ) {
($address,$channel) = split(":",$address);
$address{$address}->{'channel'} = [] unless defined $address{$address}->{'channel'};;
push @{$address{$address}->{'channel'}},$channel;
} else {
if( $id ) {
$address{$address}->{'id'} = $id;
}
}
}
}
printf "Reading details for %i devices\n",scalar keys %address if $verbose;
foreach my $address (keys %address) {
my $id = $address;
my @msg;
if( $address{$address}->{'id'} ){
$id = $address{$address}->{'id'};
push @msg, RPC::XML::int->new( $id );
push @msg, RPC::XML::int->new( 0 );
push @msg, RPC::XML::string->new( 'VALUES' );
} else {
push @msg, RPC::XML::string->new( $id.":0" );
push @msg, RPC::XML::string->new( 'VALUES' );
push @msg, RPC::XML::int->new( 0 );
}
my $resp = $cli->send_request('getParamset',@msg);
print Dumper( $resp ) if $verbose;
$address{$address}->{'unreach'} = $resp->value->{'UNREACH'};
$address{$address}->{'lowbat'} = $resp->value->{'LOWBAT'};
$address{$address}->{'pending'} = $resp->value->{'CONFIG_PENDING'};
}
printf "Reading device informaiton for %i devices\n", scalar keys %address if $verbose;
foreach my $address (keys %address ) {
if( $address{$address}->{'id'} ) {
my $id = $address{$address}->{'id'};
my @msg;
push @msg, RPC::XML::int->new( $id );
my $resp = $cli->send_request('getDeviceInfo', @msg );
print Dumper( $resp ) if $verbose;
if( $resp->value && $resp->value->{'NAME'} ) {
$address{$address}->{'name'} = $resp->value->{'NAME'};
}
}
}
foreach my $address (keys %address) {
my $outname = $address;
my $name = $address{$address}->{'name'};
if( $name ) {
$outname = "$name <$address>";
}
$address{$address}->{'outname'} = $outname;
}
my @lowbat = ();
my $batcount = 0;
my @unreach = ();
my @pending = ();
print Dumper("devices", \%address) if $verbose;
foreach my $address (keys %address) {
if( defined $address{$address}->{'lowbat'} ) {
$batcount++;
if( $address{$address}->{'lowbat'} ne 0 ) {
push @lowbat,$address{$address}->{'outname'};
}
}
if( defined $address{$address}->{'unreach'} && $address{$address}->{'unreach'} ne 0 ) {
push @unreach,$address{$address}->{'outname'};
}
if( defined $address{$address}->{'pending'} && $address{$address}->{'pending'} ne 0 ) {
push @pending,$address{$address}->{'outname'};
}
}
my $lowbat = scalar @lowbat;
my $unreach = scalar @unreach;
my $pending = scalar @pending;
printf "%i devices have low battery\n", $lowbat if $verbose;
printf "%i devices are unreachable\n", $unreach if $verbose;
printf "%i devices have pending configurations\n", $pending if $verbose;
if( $mode eq 'lowbat' ) {
my $perf = "'Low battery'=$lowbat";
if( $lowbat eq 0 ) {
printf "OK: All %i battery powered devices with sufficient batteries | $perf\n", $batcount;
exit 0;
}
printf "$retname: %i of %i devices (%s) have battery problems | $perf\n", $lowbat, $batcount, join(", ", @lowbat);
exit $ret;
} elsif( $mode eq 'unreach' ) {
my $perf = "'Unreachable'=$unreach";
if( $unreach eq 0 ) {
printf "OK: All %i devices reachable | $perf\n", scalar keys %address;
exit 0;
}
printf "$retname: %i of %i devices (%s) unreachable | $perf\n", $unreach, scalar keys %address, join(", ", @unreach);
exit $ret;
} elsif ( $mode eq 'pending' ) {
my $perf = "'Pending'=$pending";
if( $pending eq 0 ) {
printf "OK: No of %i devices have pending configuration | $perf\n", scalar keys %address;
exit 0;
}
printf "$retname: %i of %i devices (%s) have pending configurations | $perf\n", $pending, scalar keys %address, join(", ", @pending);
exit $ret;
}
print "ERROR: Unknown mode $mode\n";
exit 2;