Jump to content

Portal:Toolforge/Admin/replagstats

From Wikitech
This page may be outdated or contain incorrect details. Please update it if you can.
#!/usr/bin/perl -w

use encoding 'utf8';
use strict;
use warnings;

use DBI;
use Socket;

# Database servers with most active databases.
my %Servers = ('s1' => 'enwiki_p',
               's2' => 'itwiki_p',
               's3' => 'mlwiki_p',
               's4' => 'commonswiki_p',
               's5' => 'dewiki_p',
               's6' => 'ruwiki_p',
               's7' => 'eswiki_p');

# Loop forever.
while (1) {

    # Iterate over all servers.
    foreach my $Server (keys (%Servers)) {
        # Connect to server.
        my $DB = DBI->connect ("DBI:mysql:database=$Servers{$Server};host=$Server.labsdb;mysql_read_default_file=" . (getpwuid ($<)) [7] . "/replica.my.cnf",
                               undef,
                               undef);
        if (!defined ($DB)) {
            warn ("$0: Could not connect to $Server: " . DBI->errstr ());
            next;
        }

        # Retrieve replication lag.
        my $r = $DB->selectall_arrayref ("SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(rc_timestamp) FROM recentchanges ORDER BY rc_timestamp DESC LIMIT 1;");
        if (!defined ($r)) {
            warn ("$0: Could not retrieve replication lag for $Server: " . $DB->errstr ());
            $DB->disconnect ();
            next;
        }

        # Disconnect from server.
        $DB->disconnect ();
    }

    # Wait a minute.
    sleep (60);
}