-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_json_jq_timestamp
58 lines (41 loc) · 1.36 KB
/
check_json_jq_timestamp
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
#!/bin/perl
# Quick-and-dirty at the moment
# Uses jq for parsing json
# Retrieve a json file, and check if the date variable is not older than 120 seconds
# out.json:
# {
# "date": "2024-09-08T15:07:13Z",
# ...
# }
use strict;
use warnings;
require LWP::UserAgent;
use IPC::Run qw(run);
# developed on command line:
# curl https://server/out.json | jq -r '(now - (.date | fromdate)) as $age | if $age > 3600 then "CRITICAL: older than 1 hour: \(.date)" elif $age > 120 then "WARNING: older than 120 seconds: \(.date)" else "OK: younger than 120 seconds: \(.date)" end'
my $url = $ARGV[0];
my $ua = LWP::UserAgent->new;
my $response = $ua->get( $url );
if ( ! $response->is_success ) {
print "UNKNOWN: $response->status_line";
exit 3;
}
my $curlout = $response->decoded_content;
#print $curlout;
my ($out, $err);
my $jqcmd = q# (now - (.date | fromdate)) as $age | if $age > 3600 then "CRITICAL: older than 1 hour: \(.date)" elif $age > 120 then "WARNING: older than 120 seconds: \(.date)" else "OK: younger than 120 seconds: \(.date)" end | . + " | age=\($age|round)s" #;
run [ 'jq', '-r', $jqcmd ], \$curlout, \$out, \$err;
#print $out;
if ( $out =~ /^OK: / ) {
print $out;
exit 0;
} elsif ( $out =~ /^WARNING: / ) {
print $out;
exit 1;
} elsif ( $out =~ /^CRITICAL: / ) {
print $out;
exit 2;
} else {
print $out;
exit 3;
}