3 # Script: pgbouncer_follower.pl
4 # Copyright: 22/04/2012: v1.0.1 Glyn Astill <glyn@8kb.co.uk>
5 # Requires: Perl 5.10.1+, PostgreSQL 9.0+ Slony-I 2.0+
7 # This script is a command-line utility to monitor Slony-I clusters
8 # and reconfigure pgbouncer to follow replication sets.
10 # This script is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # This script is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this script. If not, see <http://www.gnu.org/licenses/>.
25 use experimental 'smartmatch';
28 use Getopt::Long qw/GetOptions/;
29 use Digest::MD5 qw/md5 md5_hex md5_base64/;
32 use Time::HiRes qw/usleep/;
33 use sigtrap 'handler' => \&cleanExit, 'HUP', 'INT','ABRT','QUIT','TERM';
34 Getopt::Long::Configure qw/no_ignore_case/;
38 use constant false => 0;
39 use constant true => 1;
41 my $g_usage = 'Pass configuration file: pool_follower.pl -f <configuration_path> [-D] ';
43 my $g_pidfile = "/tmp/pgbouncer_follower_%mode.pid";
44 my $g_logfile = "/tmp/pgbouncer_follower_%mode.log";
45 my $g_poll_interval = 1000;
48 my $g_clname = "replication";
51 my @g_cluster; # no_id, no_comment, no_prov, orig_sets, conninfo, dbname, host, port
52 my $g_status_file = "/tmp/pgbouncer_follower_%mode.status";
53 my $g_conf_template = "/etc/pgbouncer/pgbouncer_%mode.template";
54 my $g_conf_target = "/etc/pgbouncer/pgbouncer_%mode.ini";
55 my $g_reload_command = "/etc/init.d/pgbouncer_%mode reload";
57 my $g_all_databases=false;
59 my ($year, $month, $day, $hour, $min, $sec);
61 my $g_host = hostname;
62 my ($g_addr)=inet_ntoa((gethostbyname(hostname))[4]);
63 my $g_origins_only = false;
64 my $g_best_config = false;
67 die $g_usage unless GetOptions(\%opt, 'config_file|f=s', 'daemon|D',) and keys %opt and ! @ARGV;
69 unless (getConfig($opt{config_file})){
70 print ("There was a problem reading the configuration.\n");
74 printLogLn($g_logfile, "DEBUG: Logging to my '$g_logfile'");
75 printLogLn($g_logfile, "\t Watching sets $g_clsets in Slony-I cluster '$g_clname' polling every ${g_poll_interval}ms");
76 printLogLn($g_logfile, "\t Following " . ($g_all_databases ? "all databases" : "replicated database only") . " on an '$g_mode' node for the above replicated sets");
77 printLogLn($g_logfile, "\t Template config '$g_conf_template' Target config '$g_conf_target'");
78 printLogLn($g_logfile, "\t Reload command is '$g_reload_command'");
79 printLogLn($g_logfile, "\t Status stored in '$g_status_file'");
80 printLogLn($g_logfile, "\t Using local address for '$g_host' as '$g_addr'");
81 if (($g_max_lag > 0) && ($g_mode = 'ro')) {
82 printLogLn($g_logfile, "\t Max lag for read only targets will be $g_max_lag seconds");
84 #printLogLn($g_logfile, "\t '$g_user' as '$g_pass'");
87 if (defined($opt{daemon})) {
88 printLogLn($g_logfile, "pgbouncer_follower starting up");
89 if (writePID($g_pidfile)) {
93 printLogLn($g_logfile, "DEBUG: Sleeping for ${g_poll_interval}ms");
95 usleep($g_poll_interval * 1000);
106 if (defined($opt{daemon})) {
107 printLogLn($g_logfile, "pgbouncer_follower shutting down");
108 removePID($g_pidfile);
115 my $conninfo_read = 0;
117 foreach my $conninfo (@g_conninfos) {
120 @g_cluster = loadCluster($g_clname, $conninfo, $g_user, $g_pass, $g_addr, $g_clsets);
122 printLogLn($g_logfile, "DEBUG: ($conninfo_read) Cluster with " . scalar(@g_cluster) . " nodes read from conninfo: $conninfo");
123 foreach (@g_cluster) {
124 printLogLn($g_logfile, "DEBUG: Node #" . @$_[0] . " DETAIL: " . @$_[1] . " " . @$_[2] . " " . (@$_[3] // "<NONE>") . " " . @$_[4] . " " . (@$_[5] // "<NONE>") . " " . @$_[6] . " " . @$_[7] . " " . @$_[8] . " " . @$_[9] . " " . @$_[10] . " " . @$_[11]);
129 printLogLn($g_logfile, "ERROR: Failed using conninfo: $conninfo DETAIL: $@");
131 elsif($g_best_config) {
133 printLogLn($g_logfile, "DEBUG: Found current origin to read config from");
138 unless (checkCluster($g_status_file)) {
140 printLogLn ($g_logfile, "DEBUG: Cluster status unchanged");
144 printLogLn ($g_logfile, "Cluster status changed");
145 $node_to = generateConfig($g_conf_template, $g_conf_target, $g_mode, $g_all_databases, $g_clsets);
146 if (reloadConfig($g_reload_command)) {
147 printLogLn ($g_logfile, "Pool repointed to node #$node_to");
153 my $reload_command = shift;
155 if(length($reload_command // '')) {
156 printLogLn($g_logfile, "Running '$reload_command'");
158 open(RELOAD, "-|", $reload_command . " 2>&1");
160 printLogLn($g_logfile, $_);
163 printLogLn($g_logfile, "Reload command has been run.");
166 printLogLn($g_logfile, "ERROR: Failed to run reload command DETAIL: $@");
174 my $template = shift;
177 my $all_databases = shift;
188 my $target_port = 5432;
189 my $target_is_origin;
190 my $target_auth = "";
193 printLogLn($g_logfile, "DEBUG: All databases = " . ($g_all_databases ? 'true' : 'false'));
196 if ($g_auth_user ne "") {
197 $target_auth = " auth_user=" . $g_auth_user;
200 if (open(INFILE, "<", $template)) {
201 if (open(OUTFILE, ">", $target)) {
202 print OUTFILE "# Configuration file autogenerated at " . getRuntime() . " from $template\n";
204 if (m/\[databases]/) {
206 # Try and choose a node; we always assign the origin initially regardless of rw/ro status
207 # when in ro mode and if we then find a suitable subscriber we'll reassign to it.
208 foreach my $node (@g_cluster) {
211 # If the node is lagging anyway skip it
212 if (($g_mode eq 'ro') && ($g_max_lag > 0) && ($node->[11])) {
213 printLogLn ($g_logfile, "Lag on node $node->[0] exceeds $g_max_lag seconds");
217 if ($clsets ne 'all') {
218 @sets_to_follow = split(',', $clsets);
219 if (defined($node->[3])) {
220 @sets_origin = split(',', $node->[3]);
225 if (defined($node->[5])) {
226 @sets_subscribed = split(',', $node->[5]);
229 undef @sets_subscribed;
233 if (($clsets eq 'all' && defined($node->[3])) || (@sets_to_follow && @sets_origin && checkProvidesAllSets(\@sets_to_follow, \@sets_origin))) {
234 if (defined($node->[8])) {
235 $target_db = $node->[7];
236 $target_host = $node->[8];
237 $target_node_id = $node->[0];
238 $target_sets = $node->[3];
239 $target_is_origin = true;
241 if (defined($node->[9])) {
242 $target_port = $node->[9];
248 elsif (($mode eq "ro") && (($clsets eq 'all') || (@sets_to_follow && @sets_subscribed && checkProvidesAllSets(\@sets_to_follow, \@sets_subscribed)))) {
249 if (defined($node->[8])) {
250 $target_db = $node->[7];
251 $target_host = $node->[8];
252 $target_node_id = $node->[0];
253 $target_sets = ($node->[5] // $node->[3]);
254 $target_is_origin = false;
256 if (defined($node->[9])) {
257 $target_port = $node->[9];
262 if (defined($target_host)) {
263 $_ = "# Configuration for " . ($target_is_origin ? "origin" : "subscriber") . " of sets $target_sets node #$target_node_id $target_host:$target_port\n" . $_;
265 printLogLn ($g_logfile, "DEBUG: Configuration for " . ($target_is_origin ? "origin" : "subscriber") . " of sets $target_sets node #$target_node_id $target_host:$target_port");
267 if ($all_databases) {
268 $_ =~ s/(\[databases\])/$1\n\* = host=$target_host port=$target_port$target_auth/;
271 $_ =~ s/(\[databases\])/$1\n$target_db = host=$target_host port=$target_port dbname=$target_db$target_auth/;
275 $_ = "# Could not find any node providing sets $g_clsets in mode $mode\n";
276 printLogLn ($g_logfile, "DEBUG: Could not find any node providing sets $g_clsets in mode $mode");
285 print ("ERROR: Can't open file $target\n");
290 print ("ERROR: Can't open file $template\n");
292 return $target_node_id;
298 my $current_state = md5_hex('INIT');
300 foreach (@g_cluster) {
301 if (!$g_origins_only || defined($_->[3])) {
302 $current_state = md5_hex(($current_state // "") . $_->[0] . $_->[2] . (defined($_->[3]) ? 't' : 'f') . $_->[6] . $_->[11]);
304 printLogLn($g_logfile, "DEBUG: Node " . $_->[0] . " detail = " . $_->[2] . (defined($_->[3]) ? 't' : 'f') . $_->[6] . $_->[11]);
310 if (open(CLUSTERFILE, "<", $infile)) {
311 $previous_state = <CLUSTERFILE>;
315 printLogLn ($g_logfile, "ERROR: Can't open file $infile for reading");
319 unless (-f $infile && ($current_state eq $previous_state)) {
321 printLogLn($g_logfile, "DEBUG: Writing to status file");
323 if (open(CLUSTERFILE, ">", $infile)) {
324 print CLUSTERFILE $current_state;
328 printLogLn ($g_logfile, "ERROR: Can't open file $infile for writing");
332 if ((($previous_state // "") ne "") && ($current_state ne $previous_state)){
341 my $conninfo = shift;
356 $g_best_config = false;
357 $dsn = "DBI:Pg:$conninfo};";
360 $dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
361 $qw_clname = $dbh->quote_identifier("_" . $clname);
363 $query = "SELECT $qw_clname.getModuleVersion()";
364 $sth = $dbh->prepare($query);
366 ($version) = $sth->fetchrow;
369 $query = "WITH x AS (
372 COALESCE(b.sub_provider, 0) AS no_prov,
373 NULLIF(array_to_string(array(SELECT set_id FROM $qw_clname.sl_set WHERE set_origin = a.no_id" .
374 ($clsets ne "all" ? " AND set_id IN (" . substr('?, ' x scalar(split(',', $clsets)), 0, -2) . ")" : "")
375 . " ORDER BY set_id), ','), '') AS origin_sets,
376 CASE " . ((substr($version,0,3) >= 2.2) ? "WHEN a.no_failed THEN 'FAILED' " : "") . "WHEN a.no_active THEN 'ACTIVE' ELSE 'INACTIVE' END AS no_status,
377 string_agg(CASE WHEN b.sub_receiver = a.no_id AND b.sub_forward AND b.sub_active" .
378 ($clsets ne "all" ? " AND b.sub_set IN (" . substr('?, ' x scalar(split(',', $clsets)), 0, -2) . ")" : "")
379 . " THEN b.sub_set::text END, ',' ORDER BY b.sub_set) AS prov_sets,
380 COALESCE(c.pa_conninfo,(SELECT pa_conninfo FROM $qw_clname.sl_path WHERE pa_server = $qw_clname.getlocalnodeid(?) LIMIT 1)) AS no_conninfo
381 FROM $qw_clname.sl_node a
382 LEFT JOIN $qw_clname.sl_subscribe b ON a.no_id = b.sub_receiver AND b.sub_set <> 999
383 LEFT JOIN $qw_clname.sl_path c ON c.pa_server = a.no_id AND c.pa_client = $qw_clname.getlocalnodeid(?)
384 LEFT JOIN $qw_clname.sl_set d ON d.set_origin = a.no_id
385 GROUP BY b.sub_provider, a.no_id, a.no_comment, c.pa_conninfo, a.no_active
386 ORDER BY (COALESCE(b.sub_provider, 0) = 0) DESC, a.no_id ASC
389 CASE WHEN x.no_conninfo ilike '%dbname=%' THEN(regexp_matches(x.no_conninfo, E'dbname=(.+?)\\\\M', 'ig'))[1] END AS database,
390 CASE WHEN x.no_conninfo ilike '%host=%' THEN(regexp_matches(x.no_conninfo, E'host=(.+?)(?=\\\\s|\$)', 'ig'))[1] END AS host,
391 CASE WHEN x.no_conninfo ilike '%port=%' THEN(regexp_matches(x.no_conninfo, E'port=(.+?)\\\\M', 'ig'))[1] ELSE '5432' END AS port,
392 (no_id = $qw_clname.getlocalnodeid(?)) AS this_node,
393 COALESCE((? BETWEEN 1 AND extract(epoch from s.st_lag_time)),false) AS lag_exceeded
395 LEFT JOIN $qw_clname.sl_status s ON s.st_received = x.no_id
396 WHERE x.no_conninfo != '<event pending>'
399 ORDER BY origin_sets, @(CASE WHEN (host ~ '^[0-9]{1,3}(.[0-9]{1,3}){3}\$') THEN host::inet ELSE '255.255.255.255'::inet END - ?::inet) ASC";
402 # printLogLn($g_logfile, "DEBUG: " . $query);
405 $sth = $dbh->prepare($query);
407 if ($clsets ne "all") {
409 foreach my $param (split(",", $clsets)) {
410 $sth->bind_param($param_on, $param);
415 # This param is taken 3 times
417 $sth->bind_param($param_on, "_" . $clname);
420 $sth->bind_param($param_on, $g_max_lag);
422 $sth->bind_param($param_on, (isInet($addr) ? $addr : '255.255.255.255'));
425 while (my @node = $sth->fetchrow) {
426 # If some origin sets exist for this node row (we can assume they're the sets we're following since they're filtered in the query)
427 # and the row is flagged as this_node then we have found the best node to read the configuration from.
428 if (defined($node[3]) && $node[10]) {
429 $g_best_config = true;
431 push(@cluster, \@node);
438 printLogLn($g_logfile, "ERROR: Failed to execute query against Postgres server: $@");
450 if (open(CFGFILE, "<", $infile)) {
451 foreach (<CFGFILE>) {
456 s/#(?=(?:(?:[^']|[^"]*+'){2})*+[^']|[^"]*+\z).*//;
458 if (length(trim($_))) {
459 @fields = split('=', $_, 2);
460 $value = qtrim(trim($fields[1]));
461 given(lc($fields[0])) {
463 $g_debug = checkBoolean($value);
465 when(/\bpid_file\b/i) {
468 when(/\blog_file\b/i) {
471 when(/\bslony_user\b/i) {
474 when(/\bslony_pass\b/i) {
477 when(/\bslony_cluster_name\b/i) {
480 when(/\bslony_sets_to_follow\b/i) {
483 when(/\bserver_conninfo\b/i) {
484 push(@g_conninfos, $value);
486 when(/\bfollower_poll_interval\b/i) {
487 $g_poll_interval = checkInteger($value);
489 when(/\bstatus_file\b/i) {
490 $g_status_file = $value;
492 when(/\bpool_conf_template\b/i) {
493 $g_conf_template = $value;
495 when(/\bpool_conf_target\b/i) {
496 $g_conf_target = $value;
498 when(/\bpool_reload_command\b/i) {
499 $g_reload_command = $value;
501 when(/\bpool_mode\b/i) {
502 $g_mode = lc($value);
504 when(/\bpool_all_databases\b/i) {
505 $g_all_databases = checkBoolean($value);
507 when(/\bauth_user\b/i) {
508 $g_auth_user = $value;
510 when(/\bonly_follow_origins\b/i) {
511 $g_origins_only = checkBoolean($value);
513 when(/\bmax_ro_lag\b/i) {
514 $g_max_lag = checkInteger($value);
520 if (defined($g_user) && (scalar(@g_conninfos) > 0)) {
523 # Replace %mode and %clname here for actual value
524 for ($g_pidfile, $g_logfile, $g_status_file, $g_conf_template, $g_conf_target, $g_reload_command) {
526 s/\%clname/$g_clname/g;
532 printLogLn($g_logfile, "ERROR: Could not read configuration from '$infile'");
542 open (PIDFILE, ">", $pidfile);
546 printLogLn($g_logfile, "DEBUG: Created PID file '$pidfile' for process $$");
550 printLogLn($g_logfile, "ERROR: unable to write pidfile at '$pidfile' DETAIL $!");
564 printLogLn($g_logfile, "DEBUG: Removed PID file '$pidfile'");
568 printLogLn($g_logfile, "DEBUG: PID file '$pidfile' never existed to be removed");
572 printLogLn($g_logfile, "ERROR: unable to remove pidfile at '$pidfile' DETAIL $!");
581 if ( grep /^$text$/i, ("y","yes","t","true","on") ) {
584 elsif ( grep /^$text$/i, ("n","no","f","false","off") ) {
594 if (($integer * 1) eq $integer) {
595 $value = int($integer);
600 sub checkProvidesAllSets {
601 my ($originSets, $providerSets) = @_;
604 undef @test_hash{@$originSets}; # add a hash key for each element of @$originSets
605 delete @test_hash{@$providerSets}; # remove all keys for elements of @$providerSets
607 return !%test_hash; # return false if any keys are left in the hash
614 my(@octets) = $address =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
631 $string =~ s/^('|")+//;
632 $string =~ s/('|")+$//;
644 my ($year, $month, $day, $hour, $min, $sec) = (localtime(time))[5,4,3,2,1,0];
645 my $time = sprintf ("%02d:%02d:%02d on %02d/%02d/%04d", $hour, $min, $sec, $day, $month+1, $year+1900);
655 if (open(LOGFILE, ">>", $logfile)) {
656 print LOGFILE getRuntime() . " " . $message;
660 printLn("ERROR: Unable to write to logfile $logfile");
665 printLog ($_[0], $_[1] . $/);
669 print ((@_ ? join($/, @_) : $_), $/);