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