Skip to content

Commit

Permalink
Make installer allow default template as an option (#5583)
Browse files Browse the repository at this point in the history
Closes #5583
  • Loading branch information
netniV committed Nov 27, 2023
1 parent d35224a commit 2c1df01
Showing 1 changed file with 94 additions and 54 deletions.
148 changes: 94 additions & 54 deletions lib/installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,36 @@ public function __construct($install_params = array()) {
DB_STATUS_SKIPPED => 'fa fa-check-circle'
);

$this->defaultAutomation = array(
array(
'name' => 'Net-SNMP Device',
'hash' => '07d3fe6a52915f99e642d22e27d967a4',
'sysDescrMatch' => 'Linux',
'sysNameMatch' => '',
'sysOidMatch' => '',
'availMethod' => 2,
'sequence' => 1
),
array(
'name' => 'Windows Device',
'hash' => '5b8300be607dce4f030b026a381b91cd',
'sysDescrMatch' => 'Windows',
'sysNameMatch' => '',
'sysOidMatch' => '',
'availMethod' => 2,
'sequence' => 2
),
array(
'name' => 'Cisco Router',
'hash' => 'cae6a879f86edacb2471055783bec6d0',
'sysDescrMatch' => '(Cisco Internetwork Operating System Software|IOS)',
'sysNameMatch' => '',
'sysOidMatch' => '',
'availMethod' => 2,
'sequence' => 3
)
);

$this->errors = array();
$this->templates = array();
$this->eula = read_config_option('install_eula', true);
Expand Down Expand Up @@ -338,6 +368,10 @@ protected function processParameters($params_install = array()) {
case 'CronInterval':
$this->setCronInterval($value);

break;
case 'DefaultTemplate':
$this->setDefaultTemplate($value);

break;
case 'AutomationMode':
$this->setAutomationMode($value);
Expand Down Expand Up @@ -388,36 +422,6 @@ protected function processParameters($params_install = array()) {
* @arg install_params - optional key/value array where key matches
* XXX from setXXX/getXXX functions */
private function setDefaults($install_params = array()) {
$this->defaultAutomation = array(
array(
'name' => 'Net-SNMP Device',
'hash' => '07d3fe6a52915f99e642d22e27d967a4',
'sysDescrMatch' => 'Linux',
'sysNameMatch' => '',
'sysOidMatch' => '',
'availMethod' => 2,
'sequence' => 1
),
array(
'name' => 'Windows Device',
'hash' => '5b8300be607dce4f030b026a381b91cd',
'sysDescrMatch' => 'Windows',
'sysNameMatch' => '',
'sysOidMatch' => '',
'availMethod' => 2,
'sequence' => 2
),
array(
'name' => 'Cisco Router',
'hash' => 'cae6a879f86edacb2471055783bec6d0',
'sysDescrMatch' => '(Cisco Internetwork Operating System Software|IOS)',
'sysNameMatch' => '',
'sysOidMatch' => '',
'availMethod' => 2,
'sequence' => 3
)
);

$this->tables = $this->getTables();

// You have to set the paths before you start executing commands
Expand All @@ -427,6 +431,7 @@ private function setDefaults($install_params = array()) {
$this->permissions = $this->getPermissions();
$this->modules = $this->getModules();

$this->setDefaultTemplate($this->getDefaultTemplate());
$this->setTemplates($this->getTemplates());
$this->setProfile($this->getProfile());
$this->setAutomationMode($this->getAutomationMode());
Expand Down Expand Up @@ -1207,6 +1212,65 @@ private function getModules() {
return $this->extensions;
}


private function getDefaultTemplate($force = false) {
global $config;

$default_template = read_config_option('default_template', true);
log_install_always('templates', 'getDefaultTemplate(): Default Device Template is \'' . $default_template . '\'');

$sysDescrMatch = $config['cacti_server_os'] == 'win32' ? 'Windows' : 'Linux';
foreach ($this->defaultAutomation as $item) {
if ($item['sysDescrMatch'] == $sysDescrMatch || !empty($default_template)) {
$host_template_id = db_fetch_cell_prepared(
'SELECT id
FROM host_template
WHERE hash = ?',
array($item['hash'])
);

if (!empty($host_template_id)) {
if ($host_template_id != $default_template) {
log_install_always('templates', 'getDefaultTemplate(): Changing Device Template to \'' . $item['name'] . '\'');

set_config_option('default_template', $host_template_id);
}
break;
} else {
log_install_debug('templates', 'getDefaultTemplate(): Unable to find Device Template for \'' . $item['name'] . '\'');
}
}
}

if (empty($default_template) && !$force) {
set_default_action('default_template', '');
$default_template = $this->getDefaultTemplate(true);
}

return $default_template;
}

private function setDefaultTemplate($param_default_template = '') {
$default_template = null;
if (!empty($param_default_template)) {
foreach ($this->defaultAutomation as $item) {
if ($item[''] == $param_default_template) {
$default_template = $param_default_template;
break;
}
}
}

if (empty($default_template)) {
$default_template = $this->getDefaultTemplate();
}

set_config_option('default_template', $default_template);

$default_template = $this->getDefaultTemplate();
log_install_always('template', 'setDefaultTemplate(): Device default template is \'' . $default_template . '\'');
}

/* getTemplates() - returns a list of expected templates and whether
* they have been selected for installation */
private function getTemplates() {
Expand Down Expand Up @@ -3201,30 +3265,6 @@ private function install() {
}
}

private function setDefaultTemplate() {
$default_template = read_config_option('default_template');
log_install_always('', __('Current Default Device Template is \'%s\'', $default_template));

if (read_config_option('default_template', true) == '') {
foreach($this->defaultAutomation as $item) {
$host_template_id = db_fetch_cell_prepared('SELECT id
FROM host_template
WHERE hash = ?',
array($item['hash']));

if (!empty($host_template_id)) {
log_install_always('', __('Setting the Default Device Template to \'%s\'', $item['name']));

set_config_option('default_template', $host_template_id);

break;
} else {
log_install_always('', __('Unable to find Device Template to \'%s\'', $item['name']));
}
}
}
}

private function installTemplate() {
global $config;

Expand Down

0 comments on commit 2c1df01

Please sign in to comment.