File check_systemd_multiple of Package monitoring-plugins-systemd_multiple
#!/usr/bin/perl -w
#
# check_systemd_multiple [-m min] [-n max] [unit-regex]
#
# Author: RĂ¼diger Oertel
use POSIX;
use strict;
use Getopt::Long;
use vars qw($opt_h $opt_m $opt_n $opt_r $opt_v $PROGNAME %ERRORS);
%ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
$PROGNAME="check_systemd_multiple";
sub print_usage {
my ($err,$xit) = @_;
print "ERROR: $err\n\n" if $err;
print "Usage: $PROGNAME [-m min] [-n max] [unit_regex]\n";
print "\n";
print " -m, --min minimum number of units that should be found\n";
print " -n, --max maximum number of units that should be found\n";
print " -h, --help print this help\n\n";
print " -v, --verbose print executed commands\n\n";
print "status is WARNING if we have less than min or more than max\n";
print "running systemd units.\n";
print "status is failed if we have at least one failed unit\n";
exit $xit if $xit;
}
Getopt::Long::Configure('bundling');
GetOptions
("h" => \$opt_h, "help" => \$opt_h,
"m=i" => \$opt_m, "min=i" => \$opt_m,
"n=i" => \$opt_n, "max=i" => \$opt_n,
"r=s" => \$opt_r, "regex=s" => \$opt_r,
"v" => \$opt_v, "verbose" => \$opt_v);
if ($opt_h) {
print_usage("",$ERRORS{'UNKNOWN'});
}
($opt_r) || ($opt_r = shift @ARGV) || print_usage ("regex not given", $ERRORS{'UNKNOWN'});
# Execute the given command line and return anything it writes to STDOUT and/or
# STDERR. (from check_disk_smb by Michael Anthon)
sub output_and_error_of {
local *CMD;
local $/ = undef;
my $pid = open CMD, "-|";
if (defined($pid)) {
if ($pid) {
return <CMD>;
} else {
open STDERR, ">&STDOUT" and exec @_;
exit(1);
}
}
return undef;
}
my @cmd = ("systemctl", "--no-legend", "list-units", $opt_r);
print "RUNNING: ".join(" ",@cmd)."\n" if $opt_v;
my $res = output_and_error_of(@cmd) or exit $ERRORS{"UNKNOWN"};
my @lines = split /\n/, $res;
my $retcode = 0;
my $srvcount = 0;
my $failedlist;
my $failednum = 0;
for (@lines) {
my @curline = split /\s+/, $_;
shift(@curline) unless $curline[0];
if ($curline[2] eq "active" && $curline[3] eq "running") {
# all is good
} else {
$retcode |= 2;
$failedlist .= "$curline[0] $curline[2] $curline[3]\n";
my @jcmd = ("journalctl", "--no-pager", "-n", 10, "-u", $curline[0]);
my $res = output_and_error_of(@jcmd);
$failedlist .= $res if $res;
$failednum++;
}
$srvcount++;
}
$retcode |= 1 if ($opt_m && $srvcount < $opt_m);
$retcode |= 1 if ($opt_n && $srvcount > $opt_n);
my $retstring = "OK";
$retstring = "WARNING" if ($retcode & 1);
$retstring = "CRITICAL" if ($retcode & 2);
$opt_m ||= "";
$opt_n ||= "";
print "$retstring $srvcount services found | 'services'=$srvcount;;;$opt_m;$opt_n 'services_failed'=$failednum\n";
print "FAILED:\n".$failedlist if $failedlist;
exit $ERRORS{$retstring};