Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / maintenance / getLagTimes.php
blob677bfa223fa84f25d5706911152b9134428e2146
1 <?php
2 /**
3 * Display replication lag times.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 use MediaWiki\MediaWikiServices;
28 /**
29 * Maintenance script that displays replication lag times.
31 * @ingroup Maintenance
33 class 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" );
40 public function execute() {
41 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
42 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
44 $lbs = $lbFactory->getAllMainLBs() + $lbFactory->getAllExternalLBs();
45 foreach ( $lbs as $cluster => $lb ) {
46 if ( $lb->getServerCount() <= 1 ) {
47 continue;
49 $lags = $lb->getLagTimes();
50 foreach ( $lags as $serverIndex => $lag ) {
51 $host = $lb->getServerName( $serverIndex );
52 if ( IP::isValid( $host ) ) {
53 $ip = $host;
54 $host = gethostbyaddr( $host );
55 } else {
56 $ip = gethostbyname( $host );
59 $starLen = min( intval( $lag ), 40 );
60 $stars = str_repeat( '*', $starLen );
61 $this->output( sprintf( "%10s %20s %3d %s\n", $ip, $host, $lag, $stars ) );
63 if ( $this->hasOption( 'report' ) ) {
64 $stats->gauge( "loadbalancer.lag.$cluster.$host", $lag );
71 $maintClass = "GetLagTimes";
72 require_once RUN_MAINTENANCE_IF_MAIN;