Skip to content
Snippets Groups Projects
Commit 997459cf authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

more abstraction for shellcmd plugins

parent 7f71a948
No related branches found
No related tags found
1 merge request!62V2.0
......@@ -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
......
......@@ -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);
......
......@@ -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();
......@@ -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])
......
......@@ -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
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment