Skip to content

Instantly share code, notes, and snippets.

@lazyfrosch
Last active April 21, 2020 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazyfrosch/8bce6c794ab2830c93e868a486188928 to your computer and use it in GitHub Desktop.
Save lazyfrosch/8bce6c794ab2830c93e868a486188928 to your computer and use it in GitHub Desktop.
check_logfiles for the slightly magical usage, when files are named with the current date, but we can't expect a static filename
## Config
$seekfilesdir = '/var/tmp';
my $logdir = '/var/log/app';
my $search_defaults = {
tag => 'prefix',
criticalpatterns => 'someerror',
options => 'sticky=3600,allyoucaneat',
};
## Internal
use POSIX qw(strftime);
@searches = ();
my $date = strftime "%y%m%d", localtime;
my $tag = $search_defaults->{'tag'};
my $prefix = "${tag}${date}";
my $dh;
if (! opendir($dh, $logdir)) {
print("Could not open ${logdir} - $!");
exit(3);
}
while (my $file = readdir($dh)) {
if (! -f $file || $file !~ /^${prefix}/) {
next;
}
my $search = { %$search_defaults };
$search->{'tag'} .= $date;
$search->{'logfile'} = "${logdir}\\$file";
push @searches, $search;
}
if (@searches == 0) {
print("OK - No files found for ${tag}${date}*");
exit(0);
}
# Debugging
#use Data::Dumper;
#print Dumper(\@searches);
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment