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