MediaWiki master
getLagTimes.php
Go to the documentation of this file.
1<?php
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/Maintenance.php';
26// @codeCoverageIgnoreEnd
27
28use Wikimedia\IPUtils;
29
35class GetLagTimes extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Dump replication lag times' );
39 $this->addOption( 'report', "Report the lag values to StatsD" );
40 }
41
42 public function execute() {
43 $services = $this->getServiceContainer();
44 $lbFactory = $services->getDBLoadBalancerFactory();
45 $stats = $services->getStatsFactory();
46 $lbsByType = [
47 'main' => $lbFactory->getAllMainLBs(),
48 'external' => $lbFactory->getAllExternalLBs()
49 ];
50
51 foreach ( $lbsByType as $type => $lbs ) {
52 foreach ( $lbs as $cluster => $lb ) {
53 if ( $lb->getServerCount() <= 1 ) {
54 continue;
55 }
56 $lags = $lb->getLagTimes();
57 foreach ( $lags as $serverIndex => $lag ) {
58 $host = $lb->getServerName( $serverIndex );
59 if ( IPUtils::isValid( $host ) ) {
60 $ip = $host;
61 $host = gethostbyaddr( $host );
62 } else {
63 $ip = gethostbyname( $host );
64 }
65
66 if ( $lag === false ) {
67 $stars = 'replication stopped or errored';
68 } else {
69 $starLen = min( intval( $lag ), 40 );
70 $stars = str_repeat( '*', $starLen );
71 }
72 $this->output( sprintf( "%10s %20s %3d %s\n", $ip, $host, $lag, $stars ) );
73
74 if ( $this->hasOption( 'report' ) ) {
75 $group = ( $type === 'external' ) ? 'external' : $cluster;
76
77 // $lag is the lag duration in seconds
78 // emit milliseconds for backwards-compatibility
79 $stats->getGauge( 'loadbalancer_lag_milliseconds' )
80 ->setLabel( 'group', $group )
81 ->setLabel( 'host', $host )
82 ->copyToStatsdAt( "loadbalancer.lag.$group.$host" )
83 ->set( (int)( $lag * 1e3 ) );
84
85 // emit seconds also to align with Prometheus' recommendations
86 $stats->getGauge( 'loadbalancer_lag_seconds' )
87 ->setLabel( 'group', $group )
88 ->setLabel( 'host', $host )
89 ->set( (int)$lag );
90 }
91 }
92 }
93 }
94 }
95}
96
97// @codeCoverageIgnoreStart
98$maintClass = GetLagTimes::class;
99require_once RUN_MAINTENANCE_IF_MAIN;
100// @codeCoverageIgnoreEnd
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.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
$maintClass