| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
use Wikimedia\IPUtils; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 78 |
|
| 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 |
|
| 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 |
|
| 98 |
$maintClass = GetLagTimes::class; |
| 99 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 100 |
|