Skip to content

Commit

Permalink
[ADD] Added compatibility for Zabbix 5.2 and 5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
intellitrend-team committed Mar 14, 2022
1 parent 57c7fac commit e666d53
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 20 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ This software is licensed under the GNU Lesser General Public License v3.0.

## Changelog

### Version 5.4.2

* Added support for Zabbix 5.0 and 5.2
* Non-functional menu entry for non-superadmins is now hidden

### Version 5.0.2

* Added support for templates
Expand All @@ -24,7 +29,7 @@ This software is licensed under the GNU Lesser General Public License v3.0.

## Requirements

- Zabbix 5.4
- Zabbix 5.0 to 5.4
- File write access to the Zabbix frontend server
- Super admin permissions for the Zabbix users that want to use the frontend module

Expand Down Expand Up @@ -63,5 +68,5 @@ Additional hints:
* The columns must be in the first line of the CSV file.
* The separator character must be ";".

The CSV file can then imported in the same menu entry. You get a chance to preview the host list before the actual import.
The CSV file can then be imported in the same menu entry. You get a chance to preview the host list before the actual import.

7 changes: 6 additions & 1 deletion modules/csv-host-importer/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Zabbix CSV Import Frontend Module
*
* @version 5.0.2
* @version 5.0.0
* @author Wolfgang Alper <[email protected]>
* @copyright IntelliTrend GmbH, https://www.intellitrend.de
* @license GNU Lesser General Public License v3.0
Expand All @@ -19,6 +19,7 @@

use APP;
use CController as CAction;
use CWebUser;

/**
* Please see Core\CModule class for additional reference.
Expand All @@ -29,6 +30,10 @@ class Module extends \Core\CModule {
* Initialize module.
*/
public function init(): void {
// Only super admins should see the menu entry
if (CWebUser::getType() != USER_TYPE_SUPER_ADMIN) {
return;
}
// Initialize main menu (CMenu class instance).
APP::Component()->get('menu.main')->findOrAdd(_('Configuration'))->getSubmenu()->add((new \CMenuItem(_('Host CSV Importer')))->setAction('ichi.import'));
}
Expand Down
52 changes: 46 additions & 6 deletions modules/csv-host-importer/actions/CsvHostImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Zabbix CSV Import Frontend Module
*
* @version 5.0.2
* @version 5.0.0
* @author Wolfgang Alper <[email protected]>
* @copyright IntelliTrend GmbH, https://www.intellitrend.de
* @license GNU Lesser General Public License v3.0
Expand All @@ -23,6 +23,8 @@
use CRoleHelper;
use CUploadFile;
use API;
use CSession; // 5.0.0
use CSessionHelper; // 5.2.0+

/**
* Host CSV importer module action.
Expand Down Expand Up @@ -61,6 +63,8 @@ class CsvHostImport extends CAction {
8 => 'A PHP extension stopped the file upload.',
];

const HOSTLIST_KEY = 'ichi.hostlist';

private $hostlist = [];
private $step = 0;

Expand Down Expand Up @@ -101,7 +105,11 @@ protected function checkInput(): bool {
* @return bool
*/
protected function checkPermissions(): bool {
return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_GENERAL);
if (version_compare(ZABBIX_VERSION, '5.4.0', '>=')) {
return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_GENERAL);
} else {
return ($this->getUserType() == USER_TYPE_SUPER_ADMIN);
}
}

private function csvParse(): bool {
Expand Down Expand Up @@ -261,6 +269,39 @@ private function importHosts(): bool {
return true;
}

private function loadSession() {
if (version_compare(ZABBIX_VERSION, '5.2.0', '>=')) {
if (!CSessionHelper::has(self::HOSTLIST_KEY)) {
return false;
}
$this->hostlist = CSessionHelper::get(self::HOSTLIST_KEY);
return true;
} else {
if (!CSession::keyExists(self::HOSTLIST_KEY)) {
return false;
}
$this->hostlist = CSession::getValue(self::HOSTLIST_KEY);
return true;
}
}

private function saveSession() {
if (version_compare(ZABBIX_VERSION, '5.2.0', '>=')) {
CSessionHelper::set(self::HOSTLIST_KEY, $this->hostlist);
} else {
CSession::setValue(self::HOSTLIST_KEY, $this->hostlist);
}
}

private function clearSession() {
if (version_compare(ZABBIX_VERSION, '5.2.0', '>=')) {
CSessionHelper::unset([self::HOSTLIST_KEY]);
} else {
CSession::unsetValue([self::HOSTLIST_KEY]);
}
}


/**
* Prepare the response object for the view. Method called by Zabbix core.
*
Expand All @@ -287,17 +328,16 @@ protected function doAction() {
if (!$this->csvParse()) {
$this->step = 0;
}
$_SESSION['ichi.hostlist'] = $this->hostlist;
$this->saveSession();
break;
case 2:
// import
if (!isset($_SESSION['ichi.hostlist'])) {
if (!$this->loadSession()) {
error(_('Missing host list in session.'));
break;
}
$this->hostlist = $_SESSION['ichi.hostlist'];
if ($this->importHosts()) {
unset($_SESSION['ichi.hostlist']);
$this->clearSession();
}
break;
}
Expand Down
8 changes: 6 additions & 2 deletions modules/csv-host-importer/actions/CsvHostImportExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Zabbix CSV Import Frontend Module
*
* @version 5.0.2
* @version 5.0.0
* @author Wolfgang Alper <[email protected]>
* @copyright IntelliTrend GmbH, https://www.intellitrend.de
* @license GNU Lesser General Public License v3.0
Expand Down Expand Up @@ -56,7 +56,11 @@ protected function checkInput(): bool {
* @return bool
*/
protected function checkPermissions(): bool {
return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_GENERAL);
if (version_compare(ZABBIX_VERSION, '5.4.0', '>=')) {
return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_GENERAL);
} else {
return ($this->getUserType() == USER_TYPE_SUPER_ADMIN);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/csv-host-importer/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 1.0,
"id": "intellitrend_csv_host_importer",
"name": "CSV Host Importer",
"version": "5.0.2",
"version": "5.4.2",
"namespace": "ichi",
"author": "IntelliTrend GmbH",
"url": "https://www.intellitrend.de/",
Expand Down
2 changes: 1 addition & 1 deletion modules/csv-host-importer/views/layout.ichi.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Zabbix CSV Import Frontend Module
*
* @version 5.0.2
* @version 5.0.0
* @author Wolfgang Alper <[email protected]>
* @copyright IntelliTrend GmbH, https://www.intellitrend.de
* @license GNU Lesser General Public License v3.0
Expand Down
16 changes: 9 additions & 7 deletions modules/csv-host-importer/views/module.ichi.import.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Zabbix CSV Import Frontend Module
*
* @version 5.0.2
* @version 5.0.0
* @author Wolfgang Alper <[email protected]>
* @copyright IntelliTrend GmbH, https://www.intellitrend.de
* @license GNU Lesser General Public License v3.0
Expand Down Expand Up @@ -66,9 +66,10 @@
);
$hostlist = $data['hostlist'];

$table = (new CTable())
->setId('hostlist-table')
->addClass(ZBX_STYLE_VALUEMAP_LIST_TABLE);
$table = (new CTable())->setId('hostlist-table');
if (defined('ZBX_STYLE_VALUEMAP_LIST_TABLE')) {
$table->addClass(ZBX_STYLE_VALUEMAP_LIST_TABLE);
}

$cols = [];

Expand Down Expand Up @@ -99,9 +100,10 @@
// import
$hostlist = $data['hostlist'];

$table = (new CTable())
->setId('hostlist-table')
->addClass(ZBX_STYLE_VALUEMAP_LIST_TABLE);
$table = (new CTable())->setId('hostlist-table');
if (defined('ZBX_STYLE_VALUEMAP_LIST_TABLE')) {
$table->addClass(ZBX_STYLE_VALUEMAP_LIST_TABLE);
}

$table->setColumns([
(new CTableColumn(_('Name')))
Expand Down

0 comments on commit e666d53

Please sign in to comment.