-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_delock
212 lines (162 loc) · 4.91 KB
/
check_delock
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
#!/usr/bin/perl
##################
#
# Check a Delock Tasmota 11827
#
# Usage: ...
#
# Copyright (C) 2021 Bernd Arnold
# https://github.com/wopfel/monitoring-plugins
#
##################
use strict;
use warnings;
require LWP::UserAgent;
use JSON;
use Data::Dumper;
use Getopt::Long;
# Sub routines
sub print_help();
my $opt_verbose;
my $opt_host;
my $opt_max_energy_1day;
my $opt_help;
my $opt_power_status = "on";
my $answer = "";
my $crit_count = 0;
my $warn_count = 0;
my $status = "Unknown";
my @perfdata = ();
my @additional = ();
#
# Process command line options
#
GetOptions( "h|hostname=s" => \$opt_host,
"v|verbose" => \$opt_verbose,
"max1day=f" => \$opt_max_energy_1day,
"s|powerstatus=s" => \$opt_power_status,
"help" => \$opt_help,
)
or do { print STDERR "Error in command line argument processing. Try '$0 -help'.\n"; exit 4; };
if ( $opt_help ) {
print_help();
exit 4; # Outside of the official monitoring plugin's exit codes
}
if ( ! $opt_host ) {
print STDERR "Error: option -host missing.\n";
exit 4;
}
if ( $opt_power_status ) {
if ( $opt_power_status eq "on" or $opt_power_status eq "off" or $opt_power_status eq "ignore" ) {
# Everything's fine
} else {
print STDERR "Error: wrong value '$opt_power_status' for option -powerstatus.\n";
exit 4;
}
$opt_power_status = "\U$opt_power_status"; # to uppercase
}
#
# Query device
#
my $ua = LWP::UserAgent->new;
my $response = $ua->get( "http://$opt_host/cm?cmnd=Status 0" );
if ( $response->is_success ) {
# print $response->decoded_content; # or whatever
}
else {
print "UNKNOWN: $response->status_line";
exit 3;
}
print $response->decoded_content if $opt_verbose;
my $response_json = decode_json( $response->decoded_content );
print Dumper( $response_json ) if $opt_verbose;
if ( my $friendlyname = $response_json->{'Status'}->{'FriendlyName'}->[0] ) {
$answer .= "$friendlyname. "
}
#
# Check results
#
# Get value from POWER, or POWER1 if POWER is undefined
my $powerstatus = $response_json->{'StatusSTS'}->{'POWER'} // $response_json->{'StatusSTS'}->{'POWER1'};
$powerstatus = "\U$powerstatus"; # to uppercase
if ( $opt_power_status eq "IGNORE" ) {
push @additional, "[OK] Power status is $powerstatus (ignored)";
} elsif ( $powerstatus ne $opt_power_status ) {
$crit_count++;
push @additional, "[CRITICAL] Power status is $powerstatus";
} else {
push @additional, "[OK] Power status is $powerstatus";
}
$answer .= "Power status: $powerstatus. ";
push @perfdata, sprintf("powerstate=%d", $powerstatus eq "ON" ? 1 : 0) if defined $powerstatus;
# Total energy
my $total = $response_json->{'StatusSNS'}->{'ENERGY'}->{'Total'};
if ( defined $total ) {
push @perfdata, "total=$total";
}
# Today's energy
my $today = $response_json->{'StatusSNS'}->{'ENERGY'}->{'Today'};
if ( defined $today ) {
push @perfdata, "today=$today";
$answer .= "Today $today kWh. ";
}
# Yesterday's energy
my $yesterday = $response_json->{'StatusSNS'}->{'ENERGY'}->{'Yesterday'};
if ( defined $yesterday ) {
push @perfdata, "yesterday=$yesterday";
}
# Check for maximum energy
if ( defined $opt_max_energy_1day ) {
if ( $today > $opt_max_energy_1day ) {
$crit_count++;
push @additional, "[CRITICAL] Max. energy for one day ($opt_max_energy_1day) exceeds today's consumption ($today)";
} else {
push @additional, "[OK] Max. energy for one day ($opt_max_energy_1day) is lower than today's consumption ($today)";
}
}
# Get version
my $version = $response_json->{'StatusFWR'}->{'Version'};
if ( defined $version ) {
push @additional, "Version $version";
}
# Current power
my $power = $response_json->{'StatusSNS'}->{'ENERGY'}->{'Power'};
$answer .= "$power W now. ";
push @perfdata, "power=$power";
###
if ( $crit_count ) {
$status = "Critical";
} elsif ( $warn_count ) {
$status = "Warning";
} else {
$status = "Ok";
}
print "$status: ";
print "$answer\n";
print "| " . join(" ", @perfdata) . "\n" if @perfdata;
print join("\n", @additional) . "\n" if @additional;
exit 2 if $status eq "Critical";
exit 1 if $status eq "Warning";
exit 0 if $status eq "Ok";
exit 3; # 'Unknown' return code
#
# Print help
#
sub print_help() {
print STDERR <<~"END"
Check Tasmota Delock state.
Usage: $0 [--max1day ENERGY]
Example: $0
Critical when device state is OFF
Example: $0 --max1day 1.2
Critical when today's energy exceeds 1.2 kWh,
or when device state is OFF
Example: $0 --powerstatus ignore
Just return results, ignore device state
Parameters:
-h, --help Show this help text
-v, --verbose Verbose output (for debugging)
-s, --powerstatus Check power status (on, off, ignore, default: $opt_power_status)
--max1day ENERGY Limit the today's energy
END
}