-
Notifications
You must be signed in to change notification settings - Fork 8
/
check_daggre.pl
73 lines (53 loc) · 2.26 KB
/
check_daggre.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
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/perl -w
use strict;
use warnings;
use WWW::Mechanize;
use Getopt::Long;
use JSON -support_by_pp;
my $daggre_uri = undef;
my $timeout = undef;
sub exit_critical {
my $message = shift;
print "CRITICAL: $message\n";
exit 2;
}
sub exit_unknown {
my $message = shift;
print "UNKNOWN: $message\n";
exit 3;
}
sub exit_ok {
my $message = shift;
print "OK: $message\n";
exit 0;
}
GetOptions ( "uri=s" => \$daggre_uri ,
"timeout=i" => \$timeout) || exit_unknown("usage: $0 --uri http://212.247.189.107/g?a={auth}&o={instance-id}&latest=cpu_usage_ms --timeout 5");
exit_unknown("usage: $0 --uri http://212.247.189.107/g?a={auth}&o={instance-id}&latest=cpu_usage_ms --timeout 5") unless
defined($daggre_uri) && defined($timeout);
my $mech = WWW::Mechanize->new();
exit_unknown("error constructing WWW::Mechanize browser object") unless defined($mech);
$mech->timeout($timeout) || exit_unknown("error setting timeout");
$mech->add_header('Accept-Encoding' => undef);
eval {
$mech->get($daggre_uri) || exit_critical("error fetching daggre data");
exit_critical("no content on given url") unless defined($mech->content);
my $json = new JSON;
exit_unknown("error constructing json object") unless defined($json);
my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($mech->content);
exit_unknown("decoding of json failed") unless defined($json_text);
use DateTime;
use DateTime::Format::ISO8601;
my $time = DateTime->now(time_zone=>'GMT');
my $last = DateTime::Format::ISO8601->parse_datetime($json_text->{last}->{start_of_span});
my $interval = DateTime::Duration->new( days => 0, hours => 0, minutes => 15 );
unless ($last + $interval > $time)
{
exit_critical("No recent stats are available: last is " . $json_text->{last}->{start_of_span});
}
};
if ($@) {
my $exception = $@;
exit_critical($exception);
}
exit_ok("daggre has stats for the last 15 mins");