-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheck_nodeos_block_time
More file actions
executable file
·88 lines (65 loc) · 1.92 KB
/
check_nodeos_block_time
File metadata and controls
executable file
·88 lines (65 loc) · 1.92 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use JSON;
use LWP::UserAgent;
use DateTime;
use DateTime::Format::ISO8601;
my $url;
my $warn_limit;
my $crit_limit = 10.0;
my $ok = GetOptions
('url=s' => \$url,
'warn=f' => \$warn_limit,
'crit=f' => \$crit_limit);
if( not $ok or scalar(@ARGV) > 0 or not $url )
{
print STDERR "Usage: $0 --url=URL [options...]\n",
"The script checks block_time from a nodeos instance and compares it to host time.\n",
"Options:\n",
" --url=URL EOS API endpoint\n",
" --warn=X optional warning level, float in seconds\n",
" --crit=X \[$crit_limit\] critical level, float in seconds\n";
exit 1;
}
my $ua = LWP::UserAgent->new
(keep_alive => 1,
ssl_opts => { verify_hostname => 0 });
$ua->timeout(10);
$ua->env_proxy();
my $res = $ua->get($url . '/v1/chain/get_info');
if( not $res->is_success )
{
bailout('HTTP error', $res->decoded_content);
}
my $content = $res->decoded_content;
my $result = eval { decode_json($content) };
bailout('JSON parsing error', $@) if $@;
my $block_time = $result->{'head_block_time'};
bailout('Error', 'Cannot find head_block_time in the responce') unless defined($block_time);
my $bt = eval {DateTime::Format::ISO8601->parse_datetime($block_time)};
$bt->set_time_zone('UTC');
bailout('Date/time parsing error', $@) if $@;
my $now = DateTime->now('time_zone' => 'UTC');
my $diff = $now->subtract_datetime_absolute($bt)->in_units('nanoseconds')/1.0e9;
my $exitcode = 0;
my $status = 'OK';
if( $diff > $crit_limit )
{
$exitcode = 2;
$status = 'CRITICAL';
}
elsif( defined($warn_limit) and $diff > $warn_limit )
{
$exitcode = 1;
$status = 'WARNING';
}
printf("BLOCKTIME %s - %fs difference|time_diff=%f\n", $status, $diff, $diff);
exit($exitcode);
sub bailout
{
my ($error, $msg) = @_;
printf("%s: %s\n", $error, $msg);
exit(2);
}