summaryrefslogtreecommitdiff
path: root/databases/nagios-check_redis/files/check_redis
blob: 812c50adabd48d62ebed02353bb1e5c0ed351407 (plain) (blame)
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
#!/usr/bin/env perl

use warnings;
use strict;
use Getopt::Long qw(:config gnu_getopt);
use Redis;
use Nagios::Plugin;

my $VERSION="0.1";
my $np;

$np = Nagios::Plugin->new(usage => "Usage: %s [--host|-H <host>] [--port|-p <port>] [ -c|--critical=<threshold> ] [ -w|--warning=<threshold>] [-?|--usage] [-V|--version] [-h|--help] [-v|--verbose] [-t|--timeout=<timeout>]",
                          version => $VERSION,
                          blurb => 'This plugin checks the availability of a redis server, expecting that a slave server is sync with master, and the replication delay is not too high.',
                          license => "Brought to you AS IS, WITHOUT WARRANTY, under GPL. (C) Remi Paulmier <remi.paulmier\@gmail.com>",
                          shortname => "CHECK_REDIS",
                         );

$np->add_arg(spec => 'host|H=s',
             help => q(Check the host indicated in STRING),
             required => 0,
             default => 'localhost',
            );

$np->add_arg(spec => 'port|p=i',
             help => q(Use the TCP port indicated in INTEGER),
             required => 0,
             default => 4730,
            );

$np->add_arg(spec => 'critical|c=s',
             help => q(Exit with CRITICAL status if replication delay is greater than INTEGER),
             required => 0,
             default => 10,
            );

$np->add_arg(spec => 'warning|w=s',
             help => q(Exit with WARNING status if replication delay is greater than INTEGER),
             required => 0,
             default => 1,
            );

$np->getopts;
my $ng = $np->opts;

# manage timeout
alarm $ng->timeout;

# host & port
my $host = $ng->get('host');
my $port = $ng->get('port');

# verbosity
my $verbose = $ng->get('verbose');

my $redis;
eval {
	$redis = Redis->new( server => "$host:$port", debug => 0);
};
if ($@) {
	$np->nagios_exit( CRITICAL, "Can't connect to $host:$port" );
}

my $info = $redis->info();
my $code = OK;
my $msg = "Everything is OK";

$redis->ping || $np->nagios_exit( CRITICAL, "Can't ping server $host:$port" );

if ($info->{'role'} eq "slave") {
	$code = $np->check_threshold(check => $info->{'master_last_io_seconds_ago'});
	$msg = ("redis replication is late (" .
	        $info->{'master_last_io_seconds_ago'} .
	        "s)"
	       ) if $code != OK;

	if ($info->{'master_sync_in_progress'} != 0) {
		$msg = "redis replication sync is in progress";
		$code = CRITICAL;
	}
}

$np->nagios_exit( $code, $msg );