-
Notifications
You must be signed in to change notification settings - Fork 8
/
check_log_seen_today
executable file
·160 lines (133 loc) · 4.01 KB
/
check_log_seen_today
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
#!/usr/bin/env perl
# check_log_seen_today - Check that matching log entries have been seen today
#
# Copyright (c) 2015 Amadeus Germany GmbH
#
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
use strict;
use warnings;
use autodie;
use File::ReadBackwards;
use DateTime;
use DateTime::Format::Strptime;
### START PLUGIN LOADER
sub load_module {
my $module;
eval "require $_" and $module = $_ and last for @_;
$module->import();
return $module;
}
my $plugin_module;
BEGIN {
$plugin_module = load_module('Monitoring::Plugin', 'Nagios::Plugin', 'Nagios::Monitoring::Plugin');
}
### END PLUGIN LOADER
my $np = $plugin_module->new(
usage => "Usage: %s --help",
version => '1.5.0',
plugin => 'check_log_seen_today',
blurb => 'Check that matching log entries have been seen today',
license => 'MIT',
);
$np->add_arg(
spec => 'logfile|l=s',
help => 'File to check',
required => 1,
);
$np->add_arg(
spec => 'time-format|t=s',
help => 'strptime(3) format of the timestamp',
required => 1,
);
$np->add_arg(
spec => 'count|c=i',
help => 'Amount of entries that are expected',
default => 1,
);
$np->add_arg(
spec => 'regex|r=s',
help => 'Regular expression to search for',
required => 1,
);
$np->add_arg(
spec => 'start-time|s=s',
help => 'Start time of the search',
default => '00:00:00',
);
$np->getopts;
my $bw = File::ReadBackwards->new($np->opts->logfile) or
$np->nagios_exit(
UNKNOWN,
sprintf('Could not read logfile: "%s" (%s)', $np->opts->logfile, $!));
my $seen = 0;
sub strptime {
my $pattern = shift;
my $string = shift;
my $obj = DateTime::Format::Strptime->new(
pattern => $pattern,
on_error => 'undef',
);
my $result = $obj->parse_datetime($string) or
$np->nagios_exit(
UNKNOWN,
sprintf('Could not parse "%s" as time with "%s": %s',
$string, $obj->pattern, $obj->errmsg));
return $result;
}
my $strp = DateTime::Format::Strptime->new(
pattern => $np->opts->get('time-format'),
on_error => 'undef',
);
my $time_strp = DateTime::Format::Strptime->new(
pattern => '%H:%M:%S',
on_error => 'undef',
);
my $start_time = DateTime->today()->add_duration(
strptime('%H:%M:%S', $np->opts->get('start-time'))->subtract_datetime(DateTime->new(year=>1)));
my $regex = $np->opts->regex;
while (defined(my $log_line = $bw->readline)) {
my $log_date = $strp->parse_datetime($log_line) or
$np->nagios_exit(
UNKNOWN,
sprintf('Could not parse timestamp of line "%s" with "%s": %s',
$log_line, $strp->pattern, $strp->errmsg));
last if ($log_date < $start_time);
$seen++ if $log_line =~ /$regex/;
}
$np->add_perfdata(
label => 'count',
value => $seen,
critical => $np->opts->count,
min => 0,
);
$np->add_message(
OK,
sprintf('Found %d of %d expected entries from today matching "%s"',
$seen, $np->opts->count, $regex)
);
if ($seen < $np->opts->count) {
$np->add_message(
CRITICAL,
sprintf('Only found %d of %d expected entries from today matching "%s"',
$seen, $np->opts->count, $regex));
}
my ($code, $message) = $np->check_messages();
$np->nagios_exit($code, $message);