No direct script access allowed'); # core use IR\Core\Application as Application; # mvc use IR\Mvc\Controller as Controller; # helpers use IR\App\Helpers\Authentication as Authentication; use IR\App\Helpers\Page as Page; use IR\App\Helpers\Permissions as Permissions; # exceptions use IR\Exceptions\Types\PageException as PageException; use IR\Logs\Logger; class JavaProcesses extends Controller { /** * @app * @readwrite */ protected $app; /** * @app * @readwrite */ protected $authenticatedUser; /** * @name init * @description initializing process before the action method executed * @once * @protected */ public function init() { # set the current application to a local variable $this->app = Application::getCurrent(); # connect to the database $this->app->database('system')->connect(); # check for authentication if(!Authentication::isUserAuthenticated()) { Page::redirect($this->app->http->request->getBaseURL() . RDS . 'auth' . RDS . 'login.' . DEFAULT_EXTENSION); } # check users roles Authentication::checkUserRoles(); # get the authenticated user $this->authenticatedUser = Authentication::getAuthenticatedUser(); } /** * @name main * @description the main action * @before init * @after checkForMessage,closeConnections */ public function main() { # check for permissions $access = Permissions::checkForAuthorization($this->authenticatedUser, __CLASS__, __FUNCTION__); if ($access == false) { throw new PageException('Access Denied!', 403); } if (is_array($_POST) && isset($_POST['processesToTerminate']) && count($_POST['processesToTerminate']) > 0) { $app = Application::getCurrent(); $message = 'Internal server error!'; $flag = 'error'; // Loop through the selected processes foreach ($_POST['processesToTerminate'] as $pidToTerminate) { // Attempt to kill the selected process $res = $app->utils->terminal->cmd("sudo kill -9 {$pidToTerminate}"); if ($res) { $message = "Processes terminated successfully"; $flag = 'success'; } else { $message = "Failed to terminate process with PID {$pidToTerminate}."; break; // Stop on the first error } Logger::getInstance()->error("[JavaProcesses main] Res " . json_encode($res)); } Page::registerMessage($flag, $message); } # Fetch the processes $output = shell_exec("ps -eo pid,etime,%cpu,%mem,args | awk '{split($2, a, \"-\"); if (length(a) > 1) {split(a[2], b, \":\"); elapsed_time = a[1]*24*60 + b[1]*60 + b[2];} else {split(a[1], b, \":\"); elapsed_time = b[1]*60 + b[2];} print $1 \",\" substr($0, index($0, $5)) \",\" elapsed_time \",\" $3 \",\" $4;}' | grep 'java' | sort -k3,3nr | head -n -0"); if (!$output) { $this->masterView->set([ 'application' => 'true', 'javaProcesses' => 'true' ]); $this->pageView->set(['processes' => []]); } else { $lines = explode("\n", trim($output)); $processes = []; foreach ($lines as $line) { list($pid, $command, $time, $cpu, $mem) = explode(",", $line); $processes[] = [ 'process_id' => trim($pid), 'command' => trim($command), 'time' => trim($time) . ' hours', 'cpu' => trim($cpu) . '%', 'mem' => trim($mem) . '%', ]; } foreach ($processes as $key => $process) { $command = $process["command"]; $parts = explode(" ", $command); $base64Part = end($parts); $base64PartDecoded = base64_decode($base64Part); $processes[$key]["command"] = str_replace($base64Part, $base64PartDecoded, $process["command"]); } $this->masterView->set([ 'application' => 'true', 'javaProcesses' => 'true' ]); $this->pageView->set(['processes' => $processes]); } } /** * @name closeConnections * @description close all connections * @once * @protected */ public function closeConnections() { # connect to the database $this->app->database('system')->disconnect(); $this->app->database('clients')->disconnect(); } /** * @name checkForMessage * @description checks for session messages * @once * @protected */ public function checkForMessage() { # check for message Page::checkForMessage($this); } }