-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_systemd.pl
More file actions
executable file
·320 lines (265 loc) · 10.4 KB
/
check_systemd.pl
File metadata and controls
executable file
·320 lines (265 loc) · 10.4 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/perl -w
# check_systemd.pl
#---------------------------------------------------------------------------
# Author(s): hbrennhaeuser
# Created: Jan 23, 2022
# Last modified: Feb 06, 2025
# License: GPL v3
# Version: 1.2.1
#
# License information:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Changelog:
# v1.2.1 - hbrennhaeuser, Feb 03, 2025
# Fix GetOptions -w and -c
# Fix wrong ActiveEnterTimestamp human readable format
# v1.2.0 - hbrennhaeuser, Feb 03, 2025
# Add optional check of ActiveEnterTimestamp
# v1.1.0 - hbrennhaeuser, Oct 10, 2023
# Remove support for --output=json (remove JSON::Parse requirement)
# v1.0.4 - hbrennhaeuser, Oct 04, 2023
# Minor Changes
# v1.0.0 - hbrennhaeuser, 04 Oct, 2023
# Rename from check_systemctl.pl to check_systemd.pl
# Major rewrite aiming to
# ... match features and behavior of Friedrich/check_systemd v2.0.9
# ... be able to run on more systems without requiring installation of additional libraries
# ... provide better code quality
#
#---------------------------------------------------------------------------
use warnings;
use strict;
use 5.010;
# This plugin is intentionally using as few libraries as possible.
use Getopt::Long;
use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
our $VERSION = '1.2.1';
our $SHORTNAME = "SYSTEMD";
our %MP_CODES = (
OK => 0,
WARNING => 1,
CRITICAL => 2,
UNKNOWN => 3,
);
our %MP_TEXTS = reverse %MP_CODES;
my $ec = $MP_CODES{OK};
my $et;
my $pd;
sub print_help {
print("check_systemd.pl $VERSION
(C) hbrennhaeuser 2025
This plugin is licensed under the terms of the GNU General Public License Version 3,you will find a copy of this license in the LICENSE file included in the source package.
Nagios / Icinga compatible monitoring plugin to check systemd for failed units!
SYNTAX: check_systemd.pl [-u <UNIT> [-w <sec>] [-c <sec>] | -e <UNIT>] [-l] [-v] [-h]
-u, --unit <UNIT> - Specific full unit name to be checked
-e, --exclude <REGEX> - Exclude units using regex. May be specified multiple times. Does not apply if --unit is being used.
-l, --legacy - [Unused] This argument does nothing. It remains for backwards-compatibility-reasons
-w, --warning <sec> - Check seconds since units ActiveEnterTimestamp, only available when --unit is specified.
-c, --critical <sec> - Check seconds since units ActiveEnterTimestamp, only available when --unit is specified.
-v, --verbose - Enable verbose output
-h, --help - Print this message
");
exit($MP_CODES{UNKNOWN});
}
# --
my $opt = {};
Getopt::Long::Configure(qw( gnu_getopt ignore_case_always ));
Getopt::Long::GetOptions(
'unit|u=s' => \$opt->{'unit'},
'exclude|e=s@' => \$opt->{'exclude'},
'legacy|l' => \$opt->{'legacy'},
'warning|w=s' => \$opt->{'warning'},
'critical|c=s' => \$opt->{'critical'},
'verbose|v' => \$opt->{'verbose'},
'help|h' => \$opt->{'help'},
);
print_help() if (defined($opt->{'help'}));
plugin_die("Warning threshold must be an integer.") if (defined($opt->{'warning'}) && $opt->{'warning'} !~ /^\d+$/);
plugin_die("Critical threshold must be an integer.") if (defined($opt->{'critical'}) && $opt->{'critical'} !~ /^\d+$/);
# ========================
sub set_ec {
my ($ec_new) = @_;
$ec = $ec_new if ($ec_new > $ec);
}
sub get_new_ec {
my ($ec_old, $ec_new) = @_;
return $ec_new if ($ec_new > $ec_old);
return $ec_old;
}
sub check_exclude {
my ($excludes, $unit) = @_;
my $out = 0;
foreach my $tmp_exclude (@$excludes) {
if ($unit =~ m/$tmp_exclude/gixms) {
$out = 1;
}
}
return $out;
}
sub get_systemd_units {
my $cmd = "systemctl list-units --all --full --no-pager";
my @out = `$cmd`;
my @units_all;
foreach my $line (@out) {
$line =~ s/[^[:ascii:]]//gxms; # Only return ascii-characters to $line
$line =~ s/\s+/\ /gxms; # Replace multiple whitespaces with one
$line =~ s/^\*+//gxms; # Remove leading star
$line =~ s/^\s+//gxms; # Remove leading whitespaces
if (! $line =~ m/\s*/xms) {
my %entry;
my ($unit, $load, $active, $sub, $desc) = ('','','','','');
my @line_split = split(/\s+/, $line);
$unit = $line_split[0]; shift (@line_split);
$load = $line_split[0]; shift (@line_split);
$active = $line_split[0]; shift (@line_split);
$sub = $line_split[0]; shift (@line_split);
$desc = join(' ', @line_split);
if (defined($unit) && defined($load) && defined($active) && defined($sub) &&
$unit ne '' && $load =~ /\S+/ixms && $active =~ /\S+/ixms && $sub ne '') {
$entry{'unit'} = $unit;
$entry{'active'} = $active;
$entry{'sub'} = $sub;
$entry{'description'} = $desc;
$entry{'load'} = $load;
#TODO: Validation
push(@units_all, \%entry);
}
}
}
return (\@units_all);
}
sub get_systemd_unit_parameters {
my ($unit_name) = @_;
my $cmd = "systemctl show $unit_name --property=ActiveState,ActiveEnterTimestamp,ActiveEnterTimestampMonotonic";
my @out = `$cmd`;
my %unit_params;
foreach my $line (@out) {
$line =~ s/[^[:ascii:]]//gxms; # Only return ascii-characters to $line
$line =~ s/\s+/\ /gxms; # Replace multiple whitespaces with one
$line =~ s/^\s+//gxms; # Remove leading whitespaces
if ($line =~ m/^(\S+)=(.+)/) {
$unit_params{$1} = $2;
}
}
return \%unit_params;
}
sub format_duration {
my $seconds = shift;
my $hours = int($seconds / 3600);
my $minutes = int(($seconds % 3600) / 60);
my $secs = $seconds % 60;
return sprintf("%02dh:%02dm:%02ds", $hours, $minutes, $secs);
}
sub plugin_die {
my ($et) = @_;
plugin_exit($MP_CODES{UNKNOWN}, $et);
}
sub plugin_exit {
my ($ec, $et, $pd) = @_;
$ec = $MP_CODES{UNKNOWN} unless defined $MP_CODES{$ec};
print("$SHORTNAME $MP_TEXTS{$ec}: ");
print($et);
print("\n|$pd") if($pd);
print("\n");
exit $ec;
}
sub verb {
my ($text) = @_;
print STDERR "[DEBUG] $text\n" if ($opt->{'verbose'});
}
# ========================
my ($units) = get_systemd_units();
if ($opt->{'unit'}) {
if (my @units = grep { $_->{unit} eq $opt->{'unit'} } @$units) {
my $unit_active_state = $units[0]->{active};
$et = $opt->{'unit'}.' is '.$unit_active_state.'!';
if ($unit_active_state eq 'failed') {
set_ec($MP_CODES{CRITICAL});
}
if ($unit_active_state eq 'active' && (defined($opt->{'warning'}) || defined($opt->{'critical'}))) {
verb("evaluating ActiveEnterTimestamp");
my $unit_params = get_systemd_unit_parameters($opt->{'unit'});
my $active_enter_timestamp = $unit_params->{'ActiveEnterTimestampMonotonic'};
my $ec_tmp = $MP_CODES{OK};
my $time_since_boot = clock_gettime(CLOCK_MONOTONIC);
my $activeTime = ($time_since_boot * 1_000_000 - $active_enter_timestamp) / 1_000_000;
$activeTime = int($activeTime);
verb("activetime: ${activeTime}s");
my $activeTime_pretty = format_duration($activeTime);
if (defined($opt->{'critical'}) && $opt->{'critical'} > $activeTime){
set_ec($MP_CODES{CRITICAL});
$ec_tmp = get_new_ec($ec_tmp, $MP_CODES{CRITICAL});
}
if (defined($opt->{'warning'}) && $opt->{'warning'} > $activeTime){
set_ec($MP_CODES{WARNING});
$ec_tmp = get_new_ec($ec_tmp, $MP_CODES{WARNING});
}
$et .= " ActiveEnterTime $MP_TEXTS{$ec_tmp}, ${activeTime_pretty} ago.";
# $pd .= " 'activeTime'=${activeTime}s;;;0;";
$pd .= " 'activeTime'=${activeTime}s;";
$pd .= defined($opt->{'warning'}) ? "$opt->{'warning'};" : ";";
$pd .= defined($opt->{'critical'}) ? "$opt->{'critical'};" : ";";
$pd .= "0;"
}
} else {
plugin_die($opt->{'unit'}.' could not be found!');
}
} else {
my $units_excluded = [];
my $units_active = [];
my $units_inactive = [];
my $units_failed = [];
my $units_unknown = [];
foreach my $unit (@$units) {
if (check_exclude($opt->{'exclude'}, $unit->{unit})) {
push(@{$units_excluded}, $unit);
next;
}
if ($unit->{active} eq 'active') {
push(@{$units_active}, $unit);
} elsif ($unit->{active} eq 'inactive') {
push(@{$units_inactive}, $unit);
} elsif ($unit->{active} eq 'failed') {
push(@{$units_failed}, $unit);
} else {
push(@{$units_unknown}, $unit);
}
}
if (@$units_failed >= 1) {
set_ec($MP_CODES{CRITICAL});
}
$et .= @$units_failed.' failed units!';
foreach my $unit (@$units_failed) {
$et .= "\n".$unit->{unit}.': failed';
}
$pd .= " 'count_units'=".@$units;
$pd .= " 'units_active'=".@$units_active;
$pd .= " 'units_inactive'=".@$units_inactive;
$pd .= " 'units_failed'=".@$units_failed;
$pd .= " 'units_unknown'=".@$units_unknown;
$pd .= " 'units_excluded'=".@$units_excluded;
}
if ($ec == $MP_CODES{OK}) {
$et = 'SYSTEMD OK: '.$et;
} elsif ($ec == $MP_CODES{WARNING}) {
$et = 'SYSTEMD WARNING: '.$et;
} elsif ($ec == $MP_CODES{CRITICAL}) {
$et = 'SYSTEMD CRITICAL: '.$et;
} else {
$et = 'SYSTEMD UNKNOWN: '.$et;
}
$et = $et.' |'.$pd if ($pd);
print($et."\n");
exit($ec);