You are browsing a read-only backup copy of Wikitech. The live site can be found at wikitech.wikimedia.org

Portal:Toolforge/Admin/replagstats

From Wikitech-static
Jump to navigation Jump to search
#!/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);
}