MediaWiki REL1_35
getLagTimes.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
27use Wikimedia\IPUtils;
28
34class GetLagTimes extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Dump replication lag times' );
38 $this->addOption( 'report', "Report the lag values to StatsD" );
39 }
40
41 public function execute() {
42 $services = MediaWikiServices::getInstance();
43 $lbFactory = $services->getDBLoadBalancerFactory();
44 $stats = $services->getStatsdDataFactory();
45 $lbsByType = [
46 'main' => $lbFactory->getAllMainLBs(),
47 'external' => $lbFactory->getAllExternalLBs()
48 ];
49
50 foreach ( $lbsByType as $type => $lbs ) {
51 foreach ( $lbs as $cluster => $lb ) {
52 if ( $lb->getServerCount() <= 1 ) {
53 continue;
54 }
55 $lags = $lb->getLagTimes();
56 foreach ( $lags as $serverIndex => $lag ) {
57 $host = $lb->getServerName( $serverIndex );
58 if ( IPUtils::isValid( $host ) ) {
59 $ip = $host;
60 $host = gethostbyaddr( $host );
61 } else {
62 $ip = gethostbyname( $host );
63 }
64
65 $starLen = min( intval( $lag ), 40 );
66 $stars = str_repeat( '*', $starLen );
67 $this->output( sprintf( "%10s %20s %3d %s\n", $ip, $host, $lag, $stars ) );
68
69 if ( $this->hasOption( 'report' ) ) {
70 $group = ( $type === 'external' ) ? 'external' : $cluster;
71 $stats->gauge( "loadbalancer.lag.$group.$host", intval( $lag * 1e3 ) );
72 }
73 }
74 }
75 }
76 }
77}
78
79$maintClass = GetLagTimes::class;
80require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
Maintenance script that displays replication lag times.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
$maintClass