summaryrefslogtreecommitdiff
path: root/databases/pgcluster/files/check_pgcluster.pl
blob: 8d40d60b75a87a236346f7d61b6241bab53346a5 (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
#!/usr/bin/perl -w
#
# Copyright (c) 2004  IMG SRC, Inc.  All rights reserved.
#
# $Id: check_pgcluster.pl,v 1.4 2004/03/03 11:51:06 kuriyama Exp $
#
# Plugin for nagios.
#
# Prepare pgr_current_replicator() function before using.
#
# % psql -U pgsql -d template1
# template1=# create function pgr_current_replicator () returns text as 'pgr_current_replicator' language internal with (isStrict);
# CREATE FUNCTION
# template1=#
#
# define command{
# 	command_name	check_pgcluster
# 	command_line	$USER1$/check_pgcluster -H $HOSTADDRESS$ -p $ARG1$ -w $ARG2$
# 	}
#
# define service{
# 	use		generic-service
# 	host_name	cluster1.example.org
# 	service_description	PGCLUSTER
# 	check_command	check_pgcluster!5432!replicator.example.org:8777
# }

use strict;
use Getopt::Std;
use DBI;

my ($ret, %O, @r) = (0);
getopts('H:p:U:P:w:', \%O);

$O{p} ||= 5432;
$O{U} ||= "";
$O{P} ||= "";
usage() if (not $O{H} or not $O{w});

my $dbh = DBI->connect("dbi:Pg:dbname=template1;host=$O{H};port=$O{p}",
		       $O{U}, $O{P}, { PrintError => 0 });

if ($dbh) {
  my $sth = $dbh->prepare("select pgr_current_replicator()") or die;
  $sth->execute or die;
  @r = $sth->fetchrow_array;
  $sth->finish;

  $dbh->disconnect;

  if (not defined $r[0] or length($r[0]) < 1) {
    $ret = 2;
  } elsif ($r[0] ne $O{w}) {
    $ret = 1;
  }

} else {
  $ret = 2;
}

my %STATUS = (2 => "CRITICAL", 1 => "WARNING", 0 => "OK");
printf "PGCLUSTER %s: %s\n", $STATUS{$ret}, $r[0] || "";
exit $ret;

# ============================================================
sub usage {
  print "Usage: check_pgcluster -H host [-p dbport] [-U dbuser] [-P dbpass] -w <primary replication server:port>\n";
  exit(3);
}