File check_logrotate of Package monitoring-plugins-logrotate
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
my $skip_systemd = "";
my $statusdir = "/var/lib/misc";
my @logdirs = ();
my $maxage = "1";
my $help = "";
GetOptions(
"n|skip-systemd" => \$skip_systemd,
"t|statusdir=s" => \$statusdir,
"d|logdir=s" => \@logdirs,
"m|maxage=s" => \$maxage,
"h|help" => \$help,
);
push @logdirs, "/var/log" unless @logdirs;
my $output = "";
my @output_long = ();
my $line = "";
my %status_bits = ( 'OK' => 0,
'WARNING' => 1,
'CRITICAL' => 2,
'UNKNOWN' => 4
);
my %status = ( 'OK' => 0,
'WARNING' => 1,
'CRITICAL' => 2,
'UNKNOWN' => 3
);
my $exit_status = 0;
usage() if $help;
usage() if ( ! @logdirs || !$statusdir || ! -d $logdirs[0] || ! -d $statusdir );
if (! $skip_systemd) {
my $status = "";
open(SYSTEMCTL, "systemctl status logrotate.service|");
while (<SYSTEMCTL>) {
next unless (/^\s*Process: .*\(.*, status=([^,=\)]*)/);
$status = $1;
if ($status =~ /\/FAILURE/) {
$exit_status ||= $status_bits{CRITICAL};
push @output_long, "last service run failed";
}
}
close (SYSTEMCTL);
open(SYSTEMCTL, "systemctl status logrotate.timer|");
while (<SYSTEMCTL>) {
next unless (/^\s*Active: ([a-z]*)/);
$status = $1;
if ($status =~ /inactive/) {
$exit_status ||= $status_bits{CRITICAL};
push @output_long, "timer is inactive";
}
}
close (SYSTEMCTL);
}
if (-e "$statusdir/logrotate.status") {
my @s = stat "$statusdir/logrotate.status";
my $mtime = 0;
$mtime = $s[9] if @s && $s[9];
my $age = time - $mtime;
if ($age > 86400 * ($maxage + 1)) {
$exit_status ||= $status_bits{CRITICAL};
push @output_long, "status file is outdated";
} elsif ($age > 86400 * $maxage) {
$exit_status ||= $status_bits{WARNING};
push @output_long, "status file is outdated";
}
} else {
$exit_status ||= $status_bits{CRITICAL};
push @output_long, "status file is missing";
}
my @sfiles;
my @lt = localtime(time);
my $todaystring = sprintf "%04d%02d%02d", $lt[5]+1900,$lt[4]+1,$lt[3];
for my $logdir (@logdirs) {
opendir(LOGDIR,"$logdir");
while(readdir(LOGDIR)) {
chomp();
next unless /-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
# skip if today, might be still compressing
next if /-$todaystring$/;
$exit_status ||= $status_bits{WARNING};
push @sfiles,$_;
}
closedir(LOGDIR);
}
if (@sfiles) {
push @output_long, "found suspicious file ".join(",",@sfiles);
}
# FINALS
my $out = "OK";
for (qw(CRITICAL WARNING UNKNOWN)) {
if ($exit_status & $status_bits{$_}) {
$out = $_;
last;
}
}
print join("; ", $out, @output_long)."\n";
$exit_status = $status{$out};
exit( $exit_status );
sub usage {
print <<EOU;
Usage: $0 [-h] [-n] [-d logdir] [-t statusdir] [-m maxage]
-h print this help
-n --no-systemd
Skip checking systemd service
-d, --logdir STRING
Directory to check for logfiles (default /var/log)
May be passed multiple times.
-t --statusdir STRING
Directory for logrotate.status file (default /var/lib/misc)
-m --maxage number
Max allowed age for status file in days (default 1)
EOU
exit $status{UNKNOWN};
}