Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.89% covered (success)
91.89%
34 / 37
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetLagTimes
91.89% covered (success)
91.89%
34 / 37
50.00% covered (danger)
50.00%
1 / 2
10.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
91.18% covered (success)
91.18%
31 / 34
0.00% covered (danger)
0.00%
0 / 1
9.06
1<?php
2/**
3 * Display replication lag times.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/Maintenance.php';
26// @codeCoverageIgnoreEnd
27
28use MediaWiki\Maintenance\Maintenance;
29use Wikimedia\IPUtils;
30
31/**
32 * Maintenance script that displays replication lag times.
33 *
34 * @ingroup Maintenance
35 */
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