Debian Codegraph @

maintenance/getLagTimes.php

raw symbols SCIP
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
25 require_once __DIR__ . '/Maintenance.php';
26 // @codeCoverageIgnoreEnd
27
28 use Wikimedia\IPUtils;
29
30 /**
31 * Maintenance script that displays replication lag times.
32 *
33 * @ingroup Maintenance
34 */
35 class 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;
99 require_once RUN_MAINTENANCE_IF_MAIN;
100 // @codeCoverageIgnoreEnd