Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.25% covered (warning)
81.25%
26 / 32
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetLagTimes
89.66% covered (warning)
89.66%
26 / 29
50.00% covered (danger)
50.00%
1 / 2
10.11
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
88.46% covered (warning)
88.46%
23 / 26
0.00% covered (danger)
0.00%
0 / 1
9.12
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
24require_once __DIR__ . '/Maintenance.php';
25
26use Wikimedia\IPUtils;
27
28/**
29 * Maintenance script that displays replication lag times.
30 *
31 * @ingroup Maintenance
32 */
33class GetLagTimes extends Maintenance {
34    public function __construct() {
35        parent::__construct();
36        $this->addDescription( 'Dump replication lag times' );
37        $this->addOption( 'report', "Report the lag values to StatsD" );
38    }
39
40    public function execute() {
41        $services = $this->getServiceContainer();
42        $lbFactory = $services->getDBLoadBalancerFactory();
43        $stats = $services->getStatsdDataFactory();
44        $lbsByType = [
45            'main' => $lbFactory->getAllMainLBs(),
46            'external' => $lbFactory->getAllExternalLBs()
47        ];
48
49        foreach ( $lbsByType as $type => $lbs ) {
50            foreach ( $lbs as $cluster => $lb ) {
51                if ( $lb->getServerCount() <= 1 ) {
52                    continue;
53                }
54                $lags = $lb->getLagTimes();
55                foreach ( $lags as $serverIndex => $lag ) {
56                    $host = $lb->getServerName( $serverIndex );
57                    if ( IPUtils::isValid( $host ) ) {
58                        $ip = $host;
59                        $host = gethostbyaddr( $host );
60                    } else {
61                        $ip = gethostbyname( $host );
62                    }
63
64                    if ( $lag === false ) {
65                        $stars = 'replication stopped or errored';
66                    } else {
67                        $starLen = min( intval( $lag ), 40 );
68                        $stars = str_repeat( '*', $starLen );
69                    }
70                    $this->output( sprintf( "%10s %20s %3d %s\n", $ip, $host, $lag, $stars ) );
71
72                    if ( $this->hasOption( 'report' ) ) {
73                        $group = ( $type === 'external' ) ? 'external' : $cluster;
74                        $stats->gauge( "loadbalancer.lag.$group.$host", (int)( $lag * 1e3 ) );
75                    }
76                }
77            }
78        }
79    }
80}
81
82$maintClass = GetLagTimes::class;
83require_once RUN_MAINTENANCE_IF_MAIN;