-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_fhem-update
executable file
·125 lines (90 loc) · 2.71 KB
/
check_fhem-update
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
#!/usr/bin/perl
##################
#
# Check Fhem update
#
# Usage: ./check_fhem-update --host FHEM_HOSTNAME
#
# Copyright (C) 2023 Bernd Arnold
# https://github.com/wopfel/monitoring-plugins
#
# Sample output:
# Warning: Main Fhem program needs update. 571 modules need update.
# Ok: Everything is up to date.
#
##################
use strict;
use warnings;
use LWP::UserAgent;
use Data::Dumper;
use Getopt::Long;
my $opt_host;
my $opt_verbose;
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,
)
or do { print STDERR "Error in command line argument processing.\n"; exit 1; };
if ( ! $opt_host ) {
print STDERR "Error: option --host missing.\n";
exit 4;
}
# Get CSRF token
my $ua = LWP::UserAgent->new;
my $response = $ua->get( "http://$opt_host:8083/" );
my $token = $response->header('X-FHEM-csrfToken');
unless ( $response->is_success ) {
print "UNKNOWN: " . $response->status_line;
exit 3;
}
print $response->decoded_content if $opt_verbose;
print Dumper( $token ) if $opt_verbose;
$ua = LWP::UserAgent->new;
my $url = "http://$opt_host:8083/fhem?cmd=update%20check&XHR=1";
$response = $ua->post( $url, [ 'fwcsrf' => $token ] );
my $output = $response->decoded_content;
print Dumper( $output ) if $opt_verbose;
my $update_count = scalar grep /^UPD /, split /\n/, $output;
print Dumper( $update_count ) if $opt_verbose;
my $update_count_modules = scalar grep /^UPD FHEM\/\d\d_/, split /\n/, $output;
print Dumper( $update_count_modules ) if $opt_verbose;
my $update_count_main = scalar grep /^UPD \.\/fhem\.pl$/, split /\n/, $output;
print Dumper( $update_count_main ) if $opt_verbose;
# nothing to do...
if ( $update_count == 0 and $output !~ /^nothing to do\.\.\.$/m ) {
# Ensure the message appears when no updates found
print "UNKNOWN: No updates found, but message 'nothing to do' missing.\n";
exit 3;
}
##
$answer .= "Main Fhem program needs update. " if $update_count_main;
$answer .= "$update_count_modules modules need update. " if $update_count_modules;
if ( $update_count == 0 ) {
$answer .= "Everything is up to date. ";
} else {
$warn_count++;
}
#
# Build output
#
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