/** * 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; }
/** * Execute command * * @param InputInterface $input Input instance * @param OutputInterface $output Output instance * * @return int|null|void */ public function execute(InputInterface $input, OutputInterface $output) { $versionList = array(); // ############################ // System (LSB Version) // ############################ $versionRow = array('system' => 'System', 'version' => UnixUtility::lsbSystemDescription()); $versionList[] = array_values($versionRow); // ############################ // PHP // ############################ $versionList[] = array('PHP', phpversion()); // ############################ // MySQL // ############################ $query = 'SHOW VARIABLES LIKE \'version\''; $versionRow = DatabaseConnection::getList($query); $versionList[] = array('MySQL', $versionRow['version']); // ############################ // Apache // ############################ $versionRow = array('system' => 'Apache', 'version' => 'Unknown'); $command = new CommandBuilder('apache2ctl', '-v'); $command->setOutputRedirect(CommandBuilder::OUTPUT_REDIRECT_NO_STDERR); $execOutput = $command->execute()->getOutput(); foreach ($execOutput as $execOutputLine) { if (strpos($execOutputLine, ':') !== false) { list($tmpKey, $tmpVersion) = explode(':', trim($execOutputLine), 2); switch (strtolower($tmpKey)) { case 'server version': $versionRow['version'] = trim($tmpVersion); break; } } } $versionList[] = array_values($versionRow); // ############################ // Docker // ############################ $versionRow = array('system' => 'Docker', 'version' => \CliTools\Utility\UnixUtility::dockerVersion()); $versionList[] = array_values($versionRow); // ############################ // CliTools // ############################ $versionList[] = array('CliTools', CLITOOLS_COMMAND_VERSION); // ######################## // Output // ######################## /** @var \Symfony\Component\Console\Helper\Table $table */ $table = new Table($output); $table->setHeaders(array('System', 'Version')); foreach ($versionList as $versionRow) { $table->addRow(array_values($versionRow)); } $table->render(); return 0; }
/** * Reload tty */ public static function reloadTtyBanner($ttyName) { // Check if we can reload tty try { $who = new CommandBuilder('who'); $who->addPipeCommand(new CommandBuilder('grep', '%s', array($ttyName))); $who->execute(); // if there is no exception -> there is a logged in user } catch (\Exception $e) { // if there is an exception -> there is NO logged in user try { $ps = new CommandBuilder('ps', 'h -o pid,comm,args -C getty'); $ps->addPipeCommand(new CommandBuilder('grep', '%s', array($ttyName))); $output = $ps->execute()->getOutput(); if (!empty($output)) { $outputLine = trim(reset($output)); $outputLineParts = preg_split('/[\\s]+/', $outputLine); list($pid) = $outputLineParts; posix_kill($pid, SIGHUP); } } catch (\Exception $e) { } } }
/** * Build list of running processes * * @return array */ protected function buildProcessList() { $currentPid = posix_getpid(); $processList = array('all' => 'all processes'); $command = new CommandBuilder('ps'); $command->addArgumentRaw('h -o pid,comm,args')->addArgumentTemplate('-C %s', implode(',', $this->traceProcessNameList)); $cmdOutput = $command->execute()->getOutput(); $pidList = array(); foreach ($cmdOutput as $outputLine) { $outputLine = trim($outputLine); $outputLineParts = preg_split('/[\\s]+/', $outputLine); list($pid, $cmd) = $outputLineParts; $pid = (int) $pid; unset($outputLineParts[0], $outputLineParts[1]); $args = implode(' ', $outputLineParts); $cmd = $pid . ' [' . $cmd . '] ' . $args; // don't show current pid if ($pid === $currentPid) { continue; } $pidList[] = (int) $pid; $processList[(int) $pid] = $cmd; } return array($pidList, $processList); }