-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_certmon_nagios.pl
executable file
·63 lines (51 loc) · 1.33 KB
/
check_certmon_nagios.pl
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
#!/usr/bin/perl -w
# Nagios plugin that checks status of a k8s-certmon endpoint (https://github.com/olesku/k8s-certmon).
# Author: Ole Fredrik Skudsvik <[email protected]>
# nagios: -epn
use strict;
use LWP::UserAgent();
use JSON::Parse ':all';
my ($url) = @ARGV;
defined $url or die("Usage:\n " . $0 . " <url>\n");
my %handlers = (
200 => \&h_200,
201 => \&h_201,
202 => \&h_202
);
my $ua = LWP::UserAgent->new(timeout => 10);
my $resp = $ua->get($url);
if ($resp->is_success) {
if (exists($handlers{$resp->code})) {
my $json_data = parse_json_safe($resp->content);
if (!$json_data) {
printf("CRITICAL: Invalid JSON response from '%s'.\n", $url);
exit(2);
}
$handlers{$resp->code}($json_data);
} else {
printf("CRITICAL: Got invalid status code '%s'.\n", $resp->status_line, $url);
exit(2);
}
} else {
printf("CRITICAL: Request to %s failed.\n", $url);
exit(2);
}
# No certificate errors or warnings found.
sub h_200 {
printf("OK: All certificates is valid.\n");
exit(0);
}
# Warnings found.
sub h_201 {
my $resp = shift;
my $warnings = join(', ', @{$resp->{'warnings'}});
printf("WARNING: %s\n", $warnings);
exit(1);
}
# Critical issues found.
sub h_202 {
my $resp = shift;
my $errors = join(', ', @{$resp->{'errors'}});
printf("Critical: %s\n", $errors);
exit(2);
}