MediaWiki  master
getLagTimes.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
27 use Wikimedia\IPUtils;
28 
34 class 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;
80 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
GetLagTimes\__construct
__construct()
Default constructor.
Definition: getLagTimes.php:35
GetLagTimes\execute
execute()
Do the actual work.
Definition: getLagTimes.php:41
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:146
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:53
$maintClass
$maintClass
Definition: getLagTimes.php:79
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:241
GetLagTimes
Maintenance script that displays replication lag times.
Definition: getLagTimes.php:34
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:429
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:262
$type
$type
Definition: testCompression.php:52