From 997459cfdff1e87593be755357241eb951b5a07b Mon Sep 17 00:00:00 2001 From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch> Date: Wed, 13 Dec 2023 17:13:52 +0100 Subject: [PATCH] more abstraction for shellcmd plugins --- .../deployment/classes/plugins.class.php | 7 ++ .../plugins_shellcmd_request.class.php} | 98 ++++++++++--------- .../deployment/plugins/shellcmd/getdata.php | 4 +- .../plugins/shellcmd/load/plugin.php | 30 +----- .../plugins/shellcmd/processes/plugin.php | 22 +---- .../plugins/shellcmd/top/plugin.php | 22 +---- 6 files changed, 66 insertions(+), 117 deletions(-) rename public_html/deployment/{plugins/shellcmd/plugins_shellcmd.class.php => classes/plugins_shellcmd_request.class.php} (68%) diff --git a/public_html/deployment/classes/plugins.class.php b/public_html/deployment/classes/plugins.class.php index be934225..187813dc 100644 --- a/public_html/deployment/classes/plugins.class.php +++ b/public_html/deployment/classes/plugins.class.php @@ -256,6 +256,13 @@ class ciplugins { // --------------------------------------------------------------- // getter for plugin // --------------------------------------------------------------- + /** + * get config entry + * @param string $sKey name of config value + */ + public function getConfigitem($sKey){ + return isset($this->_aConfig[$sKey]) ? $this->_aConfig[$sKey] : false; + } /** * get plugin config from its config.json * works with diff --git a/public_html/deployment/plugins/shellcmd/plugins_shellcmd.class.php b/public_html/deployment/classes/plugins_shellcmd_request.class.php similarity index 68% rename from public_html/deployment/plugins/shellcmd/plugins_shellcmd.class.php rename to public_html/deployment/classes/plugins_shellcmd_request.class.php index 19516e80..a99ddd34 100644 --- a/public_html/deployment/plugins/shellcmd/plugins_shellcmd.class.php +++ b/public_html/deployment/classes/plugins_shellcmd_request.class.php @@ -3,20 +3,33 @@ * GENERAL CLASS TO FETCH DATA FROM A SHELL COMMAND * FOR THE WEB UI * - * Used in ./getdata.php + * Used in ../plugins/shellcmd/getdata.php * - * TODO: replace this class with classes/plugins.class.php */ -class shellcmd { +require_once('plugins.class.php'); + +class req_shellcmd { protected $_sPlugin=false; protected $_oPlugin=false; protected $_aReturn=false; + protected $_aPluginConfig=[]; + protected $_aResult=[]; protected $_debug=false; + /** + * plugins class instance + * @var object + */ + protected $CI_plugins=false; + + // --------------------------------------------------------------- + // CONSTRUCTOR + // --------------------------------------------------------------- + /** * constructor * @return bool @@ -25,21 +38,23 @@ class shellcmd { return $this->detectPlugin(); } + // --------------------------------------------------------------- + // PRIVATE METHODS + // --------------------------------------------------------------- /** - * write debug output ... if enabled - */ - protected function _wd($s){ - echo $this->_debug ? 'DEBUG '.__CLASS__ . ' '.$s."<br>\n" : ''; - } - - /** - * detect plugin name to load from GET param "plugin" + * helper execute a given command and return array with executed + * command, returncode, output + * @return */ - public function detectPlugin(){ - $this->_sPlugin=isset($_GET['plugin']) && $_GET['plugin'] ? preg_replace('/^a-z0-9/', '', $_GET['plugin']) : false; - $this->_wd("detected plugin: ".$this->_sPlugin); - return true; + protected function _execCommand($sCmd){ + exec("$sCmd", $aOut, $iResult); + return [ + 'command'=>$sCmd, + 'exitcode'=>$iResult, + 'time'=>date("H:i:s"), + 'output'=>$aOut, + ]; } @@ -48,39 +63,35 @@ class shellcmd { * @returm boolean */ protected function _loadPlugin(){ - $this->_oPlugin=false; - if (!$this->_sPlugin){ - $this->_wd("Missing param for a plugin"); - $this->_aReturn=[ 'error' => 'Missing param for a plugin.' ]; - return false; - } - $sPluginfile=$this->_sPlugin.'/plugin.php'; - $sPluginclass='shellcmd_'.$this->_sPlugin; + $this->CI_plugins=new ciplugins(); + $this->CI_plugins->setPlugin($this->_sPlugin, 'shellcmd'); - if (!file_exists($sPluginfile)){ - $this->_wd("Plugin seems to be corrupt. File not found: '. $sPluginfile."); - $this->_aReturn=[ 'error' => 'Plugin seems to be corrupt. File not found: '. $sPluginfile ]; - return false; - } - include($sPluginfile); + $sPluginclass=$this->CI_plugins->getPluginClassname(); $this->_oPlugin=new $sPluginclass(); - return true; + } + /** - * helper execute a given command and return array with executed - * command, returncode, output - * @return + * write debug output ... if enabled */ - protected function _execCommand($sCmd){ - exec("$sCmd", $aOut, $iResult); - return [ - 'command'=>$sCmd, - 'exitcode'=>$iResult, - 'time'=>date("H:i:s"), - 'output'=>$aOut, - ]; + protected function _wd($s){ + echo $this->_debug ? 'DEBUG '.__CLASS__ . ' '.$s."<br>\n" : ''; + } + + // --------------------------------------------------------------- + // PUBLIC METHODS + // --------------------------------------------------------------- + + /** + * detect plugin name to load from GET param "plugin" + */ + public function detectPlugin(){ + $this->_sPlugin=isset($_GET['plugin']) && $_GET['plugin'] ? preg_replace('/^a-z0-9/', '', $_GET['plugin']) : false; + $this->_wd("detected plugin: ".$this->_sPlugin); + return true; } + /** * get data from plugin command and return array with executed * command, returncode, output, parsed data @@ -91,9 +102,8 @@ class shellcmd { if (!$this->_oPlugin){ return $this->_aReturn; } - $sInfofile=$this->_sPlugin.'/config.json'; - $aMeta=json_decode(file_get_contents($sInfofile), 1); - $sCmd=isset($aMeta['command']) ? $aMeta['command'] : $this->_oPlugin->getCommand(); + $this->CI_plugins->getPluginConfig(); + $sCmd=$this->CI_plugins->getConfigitem('command'); $this->_wd("sCmd=$sCmd"); $this->_aResult=$this->_execCommand($sCmd); diff --git a/public_html/deployment/plugins/shellcmd/getdata.php b/public_html/deployment/plugins/shellcmd/getdata.php index fcfd2ff0..2c16324e 100644 --- a/public_html/deployment/plugins/shellcmd/getdata.php +++ b/public_html/deployment/plugins/shellcmd/getdata.php @@ -9,7 +9,7 @@ */ header('Content-Type: application/json'); -require_once('plugins_shellcmd.class.php'); +require_once('../../classes/plugins_shellcmd_request.class.php'); -$oShell=new shellcmd(); +$oShell=new req_shellcmd(); $oShell->sendResponse(); diff --git a/public_html/deployment/plugins/shellcmd/load/plugin.php b/public_html/deployment/plugins/shellcmd/load/plugin.php index 1c6655be..1eb0a12c 100644 --- a/public_html/deployment/plugins/shellcmd/load/plugin.php +++ b/public_html/deployment/plugins/shellcmd/load/plugin.php @@ -5,43 +5,15 @@ * * ---------------------------------------------------------------------- * 2022-08-05 axel.hahn@iml.unibe.ch + * 2023-12-13 axel.hahn@unibe.ch minified */ class shellcmd_load { - /** - * @var fallback command line to exectute; command in config.josn has priority - */ - protected $_command='uptime'; - - /** - * constructor ... returns command - * @return string - */ - public function __constructor(){ - return $this->getCommand(); - } - /** - * get column width - * @return string - */ - public function getColumns(){ - return false; - } - /** - * get command - * @return string - */ - public function getCommand(){ - return $this->_command; - } - - /** * parse output and extract wanted values in section "data" * @return array */ public function parsedata($aResult){ $aTmp1=array_reverse(explode(',', $aResult['output'][0])); - // print_r($aTmp1); $aResult['data']=[ 'uptime'=>(isset($aTmp1[5]) ? trim(substr($aTmp1[5], 10) . $aTmp1[4]) diff --git a/public_html/deployment/plugins/shellcmd/processes/plugin.php b/public_html/deployment/plugins/shellcmd/processes/plugin.php index 327065c9..ef858fea 100644 --- a/public_html/deployment/plugins/shellcmd/processes/plugin.php +++ b/public_html/deployment/plugins/shellcmd/processes/plugin.php @@ -5,29 +5,9 @@ * * ---------------------------------------------------------------------- * 2022-09-19 axel.hahn@iml.unibe.ch + * 2023-12-13 axel.hahn@unibe.ch minified */ class shellcmd_processes { - /** - * @var fallback command line to exectute; command in config.josn has priority - */ - protected $_command="ps -ef --forest "; - - /** - * constructor ... returns command - * @return string - */ - public function __constructor(){ - return $this->getCommand(); - } - - /** - * get command - * @return string - */ - public function getCommand(){ - return $this->_command; - } - /** * parse output and extract wanted values in section "data" * @return array diff --git a/public_html/deployment/plugins/shellcmd/top/plugin.php b/public_html/deployment/plugins/shellcmd/top/plugin.php index 2b64044c..b0a5fa56 100644 --- a/public_html/deployment/plugins/shellcmd/top/plugin.php +++ b/public_html/deployment/plugins/shellcmd/top/plugin.php @@ -5,29 +5,9 @@ * * ---------------------------------------------------------------------- * 2023-11-23 axel.hahn@unibe.ch + * 2023-12-13 axel.hahn@unibe.ch minified */ class shellcmd_top { - /** - * @var fallback command line to exectute; command in config.josn has priority - */ - protected $_command="top -b -n 1 | head -20"; - - /** - * constructor ... returns command - * @return string - */ - public function __constructor(){ - return $this->getCommand(); - } - - /** - * get command - * @return string - */ - public function getCommand(){ - return $this->_command; - } - /** * parse output and extract wanted values in section "data" * @return array -- GitLab