lag.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Shows database lag
  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. require_once __DIR__ . '/Maintenance.php';
  24. use MediaWiki\MediaWikiServices;
  25. /**
  26. * Maintenance script to show database lag.
  27. *
  28. * @ingroup Maintenance
  29. */
  30. class DatabaseLag extends Maintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription( 'Shows database lag' );
  34. $this->addOption( 'r', "Don't exit immediately, but show the lag every 5 seconds" );
  35. }
  36. public function execute() {
  37. $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
  38. if ( $this->hasOption( 'r' ) ) {
  39. echo 'time ';
  40. $serverCount = $lb->getServerCount();
  41. for ( $i = 1; $i < $serverCount; $i++ ) {
  42. $hostname = $lb->getServerName( $i );
  43. printf( "%-12s ", $hostname );
  44. }
  45. echo "\n";
  46. while ( 1 ) {
  47. $lags = $lb->getLagTimes();
  48. unset( $lags[0] );
  49. echo gmdate( 'H:i:s' ) . ' ';
  50. foreach ( $lags as $lag ) {
  51. printf( "%-12s ", $lag === false ? 'false' : $lag );
  52. }
  53. echo "\n";
  54. sleep( 5 );
  55. }
  56. } else {
  57. $lags = $lb->getLagTimes();
  58. foreach ( $lags as $i => $lag ) {
  59. $name = $lb->getServerName( $i );
  60. $this->output( sprintf( "%-20s %s\n", $name, $lag === false ? 'false' : $lag ) );
  61. }
  62. }
  63. }
  64. }
  65. $maintClass = DatabaseLag::class;
  66. require_once RUN_MAINTENANCE_IF_MAIN;