Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin for PostgreSQL 9.0+ streaming replication lag
- Loading branch information
1 parent
a650f79
commit ce1e7f9
Showing
1 changed file
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!@@PERL@@ | ||
# -*- cperl -*- | ||
# vim: ft=perl | ||
# | ||
# Copyright (C) 2010 Magnus Hagander, Redpill Linpro AB | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; version 2 dated June, | ||
# 1991. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
# 02110-1301 USA. | ||
|
||
=head1 NAME | ||
postgres_streaming_ - Plugin to monitor PostgreSQL streaming replication lag. | ||
=head1 CONFIGURATION | ||
The configuration for the database connections need to be made in | ||
pg_service.conf (see the PostgreSQL documentation). To specify which | ||
cluster to monitor, link this plugin to postgres_lag_<master>:<slave>, where | ||
<master> and <slave> are the names of the services specified in | ||
pg_service.conf. | ||
=head1 SEE ALSO | ||
=head1 MAGIC MARKERS | ||
#%# family=manual | ||
=head1 AUTHOR | ||
Magnus Hagander <magnus@hagander.net>, Redpill Linpro AB | ||
=head1 COPYRIGHT/License. | ||
Copyright (c) 2010 Magnus Hagander, Redpill Linpro AB | ||
All rights reserved. This program is free software; you can | ||
redistribute it and/or modify it under the terms of the GNU General | ||
Public License as published by the Free Software Foundation; version 2 | ||
dated June, 1991. | ||
=cut | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use bigint; | ||
|
||
use DBI; | ||
use DBD::Pg; | ||
|
||
if (defined $ARGV[0] && $ARGV[0] ne '') { | ||
if ($ARGV[0] eq 'autoconf') { | ||
print "no (autoconf not supported)\n"; | ||
exit(1); | ||
} | ||
elsif ($ARGV[0] eq 'config') { | ||
print "graph_title PostgreSQL replication lag\n"; | ||
print "graph_vlabel Lag (kb xlog)\n"; | ||
print "graph_category PostgreSQL\n"; | ||
print "graph_info PostgreSQL streaming replication lag\n"; | ||
print "graph_args --base 1024\n"; | ||
print "receive.label Receive delay\n"; | ||
print "receive.type GAUGE\n"; | ||
print "receive.draw LINE1\n"; | ||
print "receive.min 0\n"; | ||
print "apply.label Apply delay\n"; | ||
print "apply.type GAUGE\n"; | ||
print "apply.draw LINE1\n"; | ||
print "apply.min 0\n"; | ||
exit(0); | ||
} | ||
else { | ||
print "Unknown command: '$ARGV[0]'\n"; | ||
exit(1); | ||
} | ||
} | ||
|
||
# Process! | ||
unless ($0 =~ /postgres_streaming_([^:]+):([^:]+)$/) { | ||
print "Invalid link: $0. Must be postgres_streaming_<master>:<slave>\n"; | ||
exit(2); | ||
} | ||
my $master = $1; | ||
my $slave = $2; | ||
|
||
my $dbmaster = DBI->connect("DBI:Pg:service=$master") or die "Could not connect to master at $master\n"; | ||
my $dbslave = DBI->connect("DBI:Pg:service=$slave") or die "Could not connect to slave at $slave\n"; | ||
|
||
my $masterdata = $dbmaster->selectall_arrayref("SELECT pg_current_xlog_location()") or die "Could not query for xlog location on master\n"; | ||
my $slavedata = $dbslave->selectall_arrayref("SELECT pg_last_xlog_receive_location(), pg_last_xlog_replay_location()\n") or die "Could not query for xlog locations on slave\n"; | ||
|
||
$dbmaster->disconnect(); | ||
$dbslave->disconnect(); | ||
|
||
my $master_num = CalculateNumericalOffset($masterdata->[0]->[0]); | ||
my $receive_delay = ($master_num - CalculateNumericalOffset($slavedata->[0]->[0])); | ||
my $replay_delay = ($master_num - CalculateNumericalOffset($slavedata->[0]->[1])); | ||
|
||
print "receive.val $receive_delay\n"; | ||
print "apply.val $replay_delay\n"; | ||
|
||
exit(0); | ||
|
||
|
||
sub CalculateNumericalOffset | ||
{ | ||
my $stringofs = shift; | ||
|
||
my @pieces = split /\//, $stringofs; | ||
die "Invalid offset: $stringofs" unless ($#pieces == 1); | ||
|
||
# First part is logid, second part is record offset | ||
return (hex("ffffffff") * hex($pieces[0])) + hex($pieces[1]); | ||
} |