Exemplo n.º 1
0
 /**
  * Execute command
  *
  * @param  InputInterface  $input  Input instance
  * @param  OutputInterface $output Output instance
  *
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $this->elevateProcess($input, $output);
     $procList = array();
     $openFilesTotal = 0;
     $command = new CommandBuilder('lsof', '-n');
     $command->addPipeCommand(new CommandBuilder('grep', '-oE \'^[a-z]+\''))->addPipeCommand(new CommandBuilder('sort'))->addPipeCommand(new CommandBuilder('uniq', '-c'))->addPipeCommand(new CommandBuilder('sort', '-n'))->setOutputRedirect(CommandBuilder::OUTPUT_REDIRECT_NO_STDERR);
     $execOutput = $command->execute()->getOutput();
     foreach ($execOutput as $execOutputLine) {
         // get open files and proc name from output
         list($procOpenFiles, $procName) = explode(' ', trim($execOutputLine), 2);
         // add to total stats
         $openFilesTotal += $procOpenFiles;
         $procList[] = array('name' => $procName, 'open_files' => $procOpenFiles);
     }
     // ########################
     // Output
     // ########################
     /** @var \Symfony\Component\Console\Helper\Table $table */
     $table = new Table($output);
     $table->setHeaders(array('Process', 'Open Files'));
     foreach ($procList as $procRow) {
         $procRow['open_files'] = FormatUtility::number($procRow['open_files']);
         $table->addRow(array_values($procRow));
     }
     // Stats: average
     $table->addRow(new TableSeparator());
     $statsRow = array();
     $statsRow['name'] = 'Total';
     $statsRow['open_files'] = FormatUtility::number($openFilesTotal);
     $table->addRow(array_values($statsRow));
     $table->render();
     return 0;
 }
Exemplo n.º 2
0
 /**
  * Execute command
  *
  * @param  InputInterface  $input  Input instance
  * @param  OutputInterface $output Output instance
  *
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     // Get list of databases
     $databaseList = DatabaseConnection::databaseList();
     if (!empty($databaseList)) {
         // ########################
         // Fetch statistics
         // ########################
         $databaseRowList = array();
         foreach ($databaseList as $database) {
             // Get all tables
             $query = 'SELECT COUNT(*) AS count
                         FROM information_schema.tables
                        WHERE TABLE_SCHEMA = ' . DatabaseConnection::quote($database) . '
                          AND TABLE_TYPE = \'BASE TABLE\'';
             $tableCount = DatabaseConnection::getOne($query);
             // Get all views
             $query = 'SELECT COUNT(*) AS count
                         FROM information_schema.tables
                        WHERE TABLE_SCHEMA = ' . DatabaseConnection::quote($database) . '
                          AND TABLE_TYPE LIKE \'%VIEW\'';
             $viewCount = DatabaseConnection::getOne($query);
             // Get size of database
             $query = 'SELECT SUM(data_length) AS data_size,
                              SUM(index_length) AS index_size,
                              SUM(data_length + index_length) AS total_size
                         FROM information_schema.tables
                        WHERE TABLE_SCHEMA = ' . DatabaseConnection::quote($database);
             $statsRow = DatabaseConnection::getRow($query);
             $databaseRowList[$database] = array('name' => $database, 'table_count' => $tableCount, 'view_count' => $viewCount, 'data_size' => $statsRow['data_size'], 'index_size' => $statsRow['index_size'], 'total_size' => $statsRow['total_size']);
         }
         // ########################
         // Sorting
         // ########################
         // Sort: default by name (natural sort)
         uasort($databaseRowList, function ($a, $b) {
             return strnatcmp($a['name'], $b['name']);
         });
         // Sort: by table names
         if ($input->getOption('sort-name')) {
             uasort($databaseRowList, function ($a, $b) {
                 return $a['table_count'] < $b['table_count'];
             });
         }
         // Sort: by data size
         if ($input->getOption('sort-data')) {
             uasort($databaseRowList, function ($a, $b) {
                 return $a['data_size'] < $b['data_size'];
             });
         }
         // Sort: by index size
         if ($input->getOption('sort-index')) {
             uasort($databaseRowList, function ($a, $b) {
                 return $a['index_size'] < $b['index_size'];
             });
         }
         // Sort: by total size
         if ($input->getOption('sort-total')) {
             uasort($databaseRowList, function ($a, $b) {
                 return $a['total_size'] < $b['total_size'];
             });
         }
         // ########################
         // Stats
         // ########################
         $statsRow = array('name' => '', 'table_count' => 0, 'view_count' => 0, 'data_size' => 0, 'index_size' => 0, 'total_size' => 0);
         $databaseCount = count($databaseRowList);
         foreach ($databaseRowList as $databaseRow) {
             $statsRow['table_count'] += $databaseRow['table_count'];
             $statsRow['view_count'] += $databaseRow['view_count'];
             $statsRow['data_size'] += $databaseRow['data_size'];
             $statsRow['index_size'] += $databaseRow['index_size'];
             $statsRow['total_size'] += $databaseRow['total_size'];
         }
         // ########################
         // Output
         // ########################
         /** @var \Symfony\Component\Console\Helper\Table $table */
         $table = new Table($output);
         $table->setHeaders(array('Database', 'Tables', 'Views', 'Data', 'Index', 'Total'));
         foreach ($databaseRowList as $databaseRow) {
             $databaseRow['table_count'] = FormatUtility::number($databaseRow['table_count']);
             $databaseRow['view_count'] = FormatUtility::number($databaseRow['view_count']);
             $databaseRow['data_size'] = FormatUtility::bytes($databaseRow['data_size']);
             $databaseRow['index_size'] = FormatUtility::bytes($databaseRow['index_size']);
             $databaseRow['total_size'] = FormatUtility::bytes($databaseRow['total_size']);
             $table->addRow(array_values($databaseRow));
         }
         // Stats: average
         if ($databaseCount >= 1) {
             $table->addRow(new TableSeparator());
             $statsAvgRow = array();
             $statsAvgRow['name'] = 'Average';
             $statsAvgRow['table_count'] = FormatUtility::number($statsRow['table_count'] / $databaseCount);
             $statsAvgRow['view_count'] = FormatUtility::number($statsRow['view_count'] / $databaseCount);
             $statsAvgRow['data_size'] = FormatUtility::bytes($statsRow['data_size'] / $databaseCount);
             $statsAvgRow['index_size'] = FormatUtility::bytes($statsRow['index_size'] / $databaseCount);
             $statsAvgRow['total_size'] = FormatUtility::bytes($statsRow['total_size'] / $databaseCount);
             $table->addRow(array_values($statsAvgRow));
         }
         // Stats: total
         $statsTotalRow['name'] = 'Total';
         $statsTotalRow['table_count'] = FormatUtility::number($statsRow['table_count']);
         $statsTotalRow['view_count'] = FormatUtility::number($statsRow['view_count']);
         $statsTotalRow['data_size'] = FormatUtility::bytes($statsRow['data_size']);
         $statsTotalRow['index_size'] = FormatUtility::bytes($statsRow['index_size']);
         $statsTotalRow['total_size'] = FormatUtility::bytes($statsRow['total_size']);
         $table->addRow(array_values($statsTotalRow));
         $table->render();
     } else {
         $output->writeln('<p-error>No databases found</p-error>');
     }
     return 0;
 }