From 9aa5d058c6ce77b9331c2778b0e10640a52c57c8 Mon Sep 17 00:00:00 2001 From: CyberVitexus Date: Sat, 18 Jan 2025 19:08:21 +0100 Subject: [PATCH] prepare for zabbix status --- lib/cli.php | 29 +++++++++---------- src/MultiFlexi/Ui/CredentialTypeForm.php | 31 ++++++++++++++++---- src/credentialtype.php | 36 +++++++++++++++++++++++- zabbix/multiflexi.conf | 2 ++ 4 files changed, 77 insertions(+), 21 deletions(-) diff --git a/lib/cli.php b/lib/cli.php index 05eb80a6..77a903b2 100644 --- a/lib/cli.php +++ b/lib/cli.php @@ -34,31 +34,31 @@ \define('APP_NAME', 'MultiFlexi cli'); Shared::user(new Anonym()); -$command = \array_key_exists(1, $argv) ? $argv[1] : 'help'; -$argument = \array_key_exists(2, $argv) ? $argv[2] : null; -$identifier = \array_key_exists(3, $argv) ? $argv[3] : null; -$property = \array_key_exists(4, $argv) ? $argv[4] : null; -$option = \array_key_exists(5, $argv) ? $argv[5] : null; - -// Collect properties starting with -- -$properties = []; - -$probBegin = 0; - +// Parse command line arguments +$command = $argv[1] ?? null; +$argument = $argv[2] ?? null; +$identifier = $argv[3] ?? null; +$property = $argv[4] ?? null; +$format = 'plain'; // Default format + +// Parse options for ($i = 1; $i < \count($argv); ++$i) { if (strpos($argv[$i], '--') === 0) { $probBegin = $i; - break; } } -if ($probBegin) { +if (isset($probBegin)) { for ($i = $probBegin; $i < \count($argv); ++$i) { if (strpos($argv[$i], '--') === 0) { $key = substr($argv[$i], 2); $value = \array_key_exists($i + 1, $argv) ? $argv[$i + 1] : null; - $properties[$key] = $value; + if ($key === 'format') { + $format = $value ?? 'plain'; + } else { + $properties[$key] = $value; + } ++$i; // Skip the next argument as it is the value } } @@ -90,7 +90,6 @@ break; case 'status': - $format = $argument ?? 'plaintext'; $engine = new \MultiFlexi\Engine(); $pdo = $engine->getPdo(); $database = $pdo->getAttribute(\PDO::ATTR_DRIVER_NAME).' '. diff --git a/src/MultiFlexi/Ui/CredentialTypeForm.php b/src/MultiFlexi/Ui/CredentialTypeForm.php index d970efbe..84f2921a 100644 --- a/src/MultiFlexi/Ui/CredentialTypeForm.php +++ b/src/MultiFlexi/Ui/CredentialTypeForm.php @@ -1,10 +1,8 @@ -use MultiFlexi\CredentialType; - getMyKey()); + $formContents[] = new \Ease\TWB4\FormGroup(_('Credential Name'), new \Ease\Html\InputTextTag('name', $credtype->getRecordName())); + + + $submitRow = new \Ease\TWB4\Row(); + $submitRow->addColumn(10, new \Ease\TWB4\SubmitButton('🍏 '._('Apply'), 'primary btn-lg btn-block')); + + if (null === $credtype->getMyKey()) { + $submitRow->addColumn(2, new \Ease\TWB4\SubmitButton('⚰️ '._('Remove').' !', 'disabled btn-lg btn-block', ['disabled' => 'true'])); + } else { + if (WebPage::getRequestValue('remove') === 'true') { + $submitRow->addColumn(2, new \Ease\TWB4\LinkButton('credentialtype.php?delete='.$credtype->getMyKey(), '⚰️ '._('Remove').' !', 'danger btn-lg btn-block')); + } else { + $submitRow->addColumn(2, new \Ease\TWB4\LinkButton('credentialtype.php?id='.$credtype->getMyKey().'&remove=true', '⚰️ '._('Remove').' ?', 'warning btn-lg btn-block')); + } + } + + $formContents[] = $submitRow; + + parent::__construct(['action' => 'credentialtype.php'], ['method' => 'POST'], $formContents); + + } +} diff --git a/src/credentialtype.php b/src/credentialtype.php index 552417f7..aba46221 100644 --- a/src/credentialtype.php +++ b/src/credentialtype.php @@ -19,9 +19,43 @@ WebPage::singleton()->onlyForLogged(); +$credTypeId = WebPage::getRequestValue('id', 'int'); + +$crtype = new \MultiFlexi\CredentialType($credTypeId); + +$delete = WebPage::getRequestValue('delete', 'int'); + +if (null !== $delete) { + $crtype->loadFromSQL($delete); + $cnf = new \MultiFlexi\Configuration(); + $crtype->addStatusMessage(sprintf(_('%d used configurations removed'), $cnf->deleteFromSQL(['app_id' => $appId, 'name' => $crtype->getDataValue('keyname')]))); + + if ($crtype->deleteFromSQL($delete)) { + $crtype->addStatusMessage(_('Credential removed')); + } + + WebPage::singleton()->redirect('credentialtypes.php'); +} + +if (WebPage::singleton()->isPosted()) { + if ($crtype->takeData($_POST) && null !== $crtype->dbsync()) { + $crtype->addStatusMessage(_('Credential field Saved'), 'success'); + } else { + $crtype->addStatusMessage(_('Error saving Credential field'), 'error'); + } +} else { + if ((null === WebPage::getRequestValue('company_id')) === false) { + $crtype->setDataValue('company_id', WebPage::getRequestValue('company_id')); + } + + if ((null === WebPage::getRequestValue('formType')) === false) { + $crtype->setDataValue('formType', WebPage::getRequestValue('formType')); + } +} + + WebPage::singleton()->addItem(new PageTop(_('Crednetial Type'))); -$crtype = new \MultiFlexi\CredentialType(); WebPage::singleton()->container->addItem(new CredentialTypeForm($crtype)); diff --git a/zabbix/multiflexi.conf b/zabbix/multiflexi.conf index dd83139d..5ae574d6 100644 --- a/zabbix/multiflexi.conf +++ b/zabbix/multiflexi.conf @@ -2,3 +2,5 @@ UserParameter=multiflexi.company.lld,multiflexi-zabbix-lld-company UserParameter=multiflexi.job.lld,multiflexi-zabbix-lld-tasks UserParameter=multiflexi.runtemplate.lld[*],multiflexi-zabbix-lld $1 UserParameter=multiflexi.action.lld,multiflexi-zabbix-lld-actions +UserParameter=multiflexi.status,multiflexi-cli status --format=json +UserParameter=multiflexi.jobs.lld,multiflexi-cli jobstatus --format=json