59 lines
2.0 KiB
PHP
Executable File
59 lines
2.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Symfony\Component\Console\Helper\Table;
|
|
|
|
class DbMonitorCommand extends Command
|
|
{
|
|
protected static $defaultName = 'db:monitor';
|
|
protected static $defaultDescription = 'Monitora o status atual do banco de dados e processos ativos';
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$connection = Capsule::connection();
|
|
$dbName = $connection->getDatabaseName();
|
|
|
|
$output->writeln("<info>Monitorando banco de dados: {$dbName}</info>");
|
|
$output->writeln("===============================================");
|
|
|
|
// Active processes
|
|
$processes = $connection->select('SHOW PROCESSLIST');
|
|
$output->writeln("<comment>Processos Ativos:</comment>");
|
|
$processTable = new Table($output);
|
|
$processTable->setHeaders(['ID', 'User', 'Host', 'Command', 'Time', 'State', 'Info']);
|
|
|
|
foreach ($processes as $p) {
|
|
$processTable->addRow([
|
|
$p->Id,
|
|
$p->User,
|
|
$p->Host,
|
|
$p->Command,
|
|
$p->Time,
|
|
$p->State,
|
|
substr($p->Info ?? '', 0, 50) . (strlen($p->Info ?? '') > 50 ? '...' : '')
|
|
]);
|
|
}
|
|
$processTable->render();
|
|
|
|
$output->writeln("");
|
|
|
|
// Database status variables
|
|
$status = $connection->select("SHOW STATUS WHERE Variable_name IN ('Threads_connected', 'Threads_running', 'Uptime', 'Questions')");
|
|
$output->writeln("<comment>Variáveis de Status:</comment>");
|
|
$statusTable = new Table($output);
|
|
$statusTable->setHeaders(['Variável', 'Valor']);
|
|
|
|
foreach ($status as $s) {
|
|
$statusTable->addRow([$s->Variable_name, $s->Value]);
|
|
}
|
|
$statusTable->render();
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|