diff --git a/doc/Developing/Dynamic-Config.md b/doc/Developing/Dynamic-Config.md new file mode 100644 index 0000000000..73e808f5cc --- /dev/null +++ b/doc/Developing/Dynamic-Config.md @@ -0,0 +1,50 @@ +# Adding new config options to WebUI + +Adding support for users to update a new config option via the WebUI is now a lot easier for general options. This +document shows you how to add a new config option and even section to the WebUI. + +#### Update DB + +Firstly you will need to add the config option to the database. Here's an example: + +```sql +insert into config (config_name,config_value,config_default,config_descr,config_group,config_group_order,config_sub_group,config_sub_group_order,config_hidden,config_disabled) values ('alert.tolerance_window','','','Tolerance window in seconds','alerting',0,'general',0,'0','0'); +``` + +This will determine the default config option for `$config['alert']['tolerance_window']`. + +#### Update WebUI + +If the sub-section you want to add the new config option already exists then update the relevant file within +`html/pages/settings/` otherwise you will need to create the new sub-section page. Here's an example of this: + +[Commit example](https://github.com/librenm/librenms/commit/c5998f9ee27acdac0c0f7d3092fc830c51ff684c) + +```php + 'alert.tolerance_window', + 'descr' => 'Tolerance window for cron', + 'type' => 'text', + ), +); + +echo ' +
+
+'; + +echo generate_dynamic_config_panel('Email transport',true,$config_groups,$mail_conf,'mail'); + +echo ' +
+
+'; +``` + +And that should be it! diff --git a/html/includes/forms/update-config-item.inc.php b/html/includes/forms/update-config-item.inc.php index b15444d9e9..4b9ffe75d7 100644 --- a/html/includes/forms/update-config-item.inc.php +++ b/html/includes/forms/update-config-item.inc.php @@ -23,7 +23,7 @@ $config_type = mres($_POST['config_type']); $status = 'error'; if (!is_numeric($config_id)) { - $message = 'ERROR: No alert selected'; + $message = 'ERROR: No config item'; } else if ($action == 'update-textarea') { $extras = explode(PHP_EOL, $_POST['config_value']); diff --git a/html/includes/functions.inc.php b/html/includes/functions.inc.php index e29c1d8632..21121d0cef 100644 --- a/html/includes/functions.inc.php +++ b/html/includes/functions.inc.php @@ -1179,3 +1179,73 @@ function dynamic_override_config($type, $name, $device) { return ''; } }//end dynamic_override_config() + +function generate_dynamic_config_panel($title,$end_panel=true,$config_groups,$items=array(),$transport='') { + $anchor = md5($title); + $output = ' +
+
+

+ '.$title.' + '; + if (!empty($transport)) { + $output .= ''; + } + $output .= ' +

+
+
+
+ '; + + if (!empty($items)) { + foreach ($items as $item) { + $output .= ' +
+ +
+
+ '; + if ($item['type'] == 'checkbox') { + $output .= ''; + } + elseif ($item['type'] == 'text') { + $output .= ' + + + '; + } + elseif ($item['type'] == 'select') { + $output .= ' + + + '; + } + $output .= ' +
+
+ '; + } + } + + if ($end_panel === true) { + $output .= ' +
+
+
+ '; + } + return $output; +}//end generate_dynamic_config_panel() diff --git a/html/js/librenms.js b/html/js/librenms.js index cd9af4fd6d..c205352d23 100644 --- a/html/js/librenms.js +++ b/html/js/librenms.js @@ -1,4 +1,5 @@ $(document).ready(function() { + // Device override ajax calls $("[name='override_config']").bootstrapSwitch('offColor','danger'); $('input[name="override_config"]').on('switchChange.bootstrapSwitch', function(event, state) { event.preventDefault(); @@ -23,4 +24,76 @@ $(document).ready(function() { } }); }); + + // Checkbox config ajax calls + $("[name='global-config-check']").bootstrapSwitch('offColor','danger'); + $('input[name="global-config-check"]').on('switchChange.bootstrapSwitch', function(event, state) { + event.preventDefault(); + var $this = $(this); + var config_id = $this.data("config_id"); + $.ajax({ + type: 'POST', + url: 'ajax_form.php', + data: {type: "update-config-item", config_id: config_id, config_value: state}, + dataType: "json", + success: function (data) { + if (data.status == 'ok') { + toastr.success('Config updated'); + } else { + toastr.error(data.message); + } + }, + error: function () { + toastr.error(data.message); + } + }); + }); + + // Input field config ajax calls + $(document).on('blur', 'input[name="global-config-input"]', function(event) { + event.preventDefault(); + var $this = $(this); + var config_id = $this.data("config_id"); + var config_value = $this.val(); + $.ajax({ + type: 'POST', + url: 'ajax_form.php', + data: {type: "update-config-item", config_id: config_id, config_value: config_value}, + dataType: "json", + success: function (data) { + if (data.status == 'ok') { + toastr.success('Config updated'); + } else { + toastr.error(data.message); + } + }, + error: function () { + toastr.error(data.message); + } + }); + }); + + // Select config ajax calls + $( 'select[name="global-config-select"]').change(function(event) { + event.preventDefault(); + var $this = $(this); + var config_id = $this.data("config_id"); + var config_value = $this.val(); + $.ajax({ + type: 'POST', + url: 'ajax_form.php', + data: {type: "update-config-item", config_id: config_id, config_value: config_value}, + dataType: "json", + success: function (data) { + if (data.status == 'ok') { + toastr.success('Config updated'); + } else { + toastr.error(data.message); + } + }, + error: function () { + toastr.error(data.message); + } + }); + }); }); diff --git a/html/pages/settings.inc.php b/html/pages/settings.inc.php index 2f7a9517e4..f2a85f7409 100644 --- a/html/pages/settings.inc.php +++ b/html/pages/settings.inc.php @@ -23,12 +23,28 @@ * @package LibreNMS * @subpackage Page */ -$pagetitle[] = 'Global Settings'; + +if (empty($vars['sub'])) { + $page_name = 'Global'; +} +else { + $page_name = ucfirst($vars['sub']); +} + +$pagetitle[] = $page_name . ' Settings'; $config['memcached']['enable'] = false; ?> + +
-

Global Settings

+

+ +


@@ -59,7 +75,7 @@ else { foreach (dbFetchRows("SELECT `config_group` FROM `config` GROUP BY `config_group`") as $sub_page) { $sub_page = $sub_page['config_group']; ?> -
+ 'alert.admins', + 'descr' => 'Issue alerts to admins', + 'type' => 'checkbox', + ), + array('name' => 'alert.globals', + 'descr' => 'Issue alerts to read only users', + 'type' => 'checkbox', + ), + array('name' => 'alert.syscontact', + 'descr' => 'Issue alerts to sysContact', + 'type' => 'checkbox', + ), + array('name' => 'alert.default_only', + 'descr' => 'Send alerts to default contact only', + 'type' => 'checkbox', + ), + array('name' => 'alert.default_mail', + 'descr' => 'Default contact', + 'type' => 'text', + ), + array('name' => 'alert.tolerance_window', + 'descr' => 'Tolerance window for cron', + 'type' => 'text', + ), + array('name' => 'alert.fixed-contacts', + 'descr' => 'Updates to contact email addresses not honored', + 'type' => 'checkbox', + ), +); + +$mail_conf = array( + array('name' => 'alert.transports.mail', + 'descr' => 'Enable email alerting', + 'type' => 'checkbox', + ), + array('name' => 'email_backend', + 'descr' => 'How to deliver mail', + 'options' => $dyn_config['email_backend'], + 'type' => 'select', + ), + array('name' => 'email_user', + 'descr' => 'From name', + 'type' => 'text', + ), + array('name' => 'email_sendmail_path', + 'descr' => 'Sendmail path', + 'type' => 'text', + ), + array('name' => 'email_smtp_host', + 'descr' => 'SMTP Host', + 'type' => 'text', + ), + array('name' => 'email_smtp_port', + 'descr' => 'SMTP Port', + 'type' => 'text', + ), + array('name' => 'email_smtp_timeout', + 'descr' => 'SMTP Timeout', + 'type' => 'text', + ), + array('name' => 'email_smtp_secure', + 'descr' => 'SMTP Secure', + 'type' => 'select', + 'options' => $dyn_config['email_smtp_secure'], + ), + array('name' => 'email_smtp_auth', + 'descr' => 'SMTP Authentication', + 'type' => 'checkbox', + ), + array('name' => 'email_smtp_username', + 'descr' => 'SMTP Authentication Username', + 'type' => 'text', + ), + array('name' => 'email_smtp_password', + 'descr' => 'SMTP Authentication Password', + 'type' => 'text', + ), +); + echo '
-
- -
-
-
- -
-
- -
-
-
- -
-
- -
-
-
- -
-
- -
-
-
- -
-
- -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
-
- -
-
- -
-
-
-
-
-
-
-

- Email transport -

-
-
-
-
- -
-
- -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
-
- -
-
- -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
-
-
-
+echo '

@@ -1158,58 +1056,6 @@ echo '
}); });// End delete Boxcar config - $("[name='global-config-check']").bootstrapSwitch('offColor','danger'); - $('input[name="global-config-check"]').on('switchChange.bootstrapSwitch', function(event, state) { - event.preventDefault(); - var config_id = $(this).data("config_id"); - $.ajax({ - type: 'POST', - url: 'ajax_form.php', - data: {type: "update-config-item", config_id: config_id, config_value: state}, - dataType: "json", - success: function (data) { - if (data.status == 'ok') { - } else { - $("#message").html('
' + data.message + '
'); - } - }, - error: function () { - $("#message").html('
An error occurred.
'); - } - }); - }); - $(document).on('blur', 'input[name="global-config-input"]', function(event) { - event.preventDefault(); - var $this = $(this); - var config_id = $this.data("config_id"); - var config_value = $this.val(); - $.ajax({ - type: 'POST', - url: 'ajax_form.php', - data: {type: "update-config-item", config_id: config_id, config_value: config_value}, - dataType: "json", - success: function (data) { - if (data.status == 'ok') { - $this.closest('.form-group').addClass('has-success'); - $this.next().addClass('glyphicon-ok'); - setTimeout(function(){ - $this.closest('.form-group').removeClass('has-success'); - $this.next().removeClass('glyphicon-ok'); - }, 2000); - } else { - $(this).closest('.form-group').addClass('has-error'); - $this.next().addClass('glyphicon-remove'); - setTimeout(function(){ - $this.closest('.form-group').removeClass('has-error'); - $this.next().removeClass('glyphicon-remove'); - }, 2000); - } - }, - error: function () { - $("#message").html('
An error occurred.
'); - } - }); - }); $( 'select[name="global-config-select"]').change(function(event) { event.preventDefault(); var $this = $(this); diff --git a/html/pages/settings/external.inc.php b/html/pages/settings/external.inc.php new file mode 100644 index 0000000000..2960b2e259 --- /dev/null +++ b/html/pages/settings/external.inc.php @@ -0,0 +1,32 @@ + 'oxidized.enabled', + 'descr' => 'Enable Oxidized support', + 'type' => 'checkbox', + ), + array('name' => 'oxidized.url', + 'descr' => 'URL to your Oxidized API', + 'type' => 'text', + ), + array('name' => 'oxidized.features.versioning', + 'descr' => 'Enable config versioning access', + 'type' => 'checkbox', + ), +); + +echo ' +
+ +'; + +echo generate_dynamic_config_panel('Oxidized integration',true,$config_groups,$oxidized_conf); + +echo ' + +
+'; diff --git a/sql-schema/074.sql b/sql-schema/074.sql new file mode 100644 index 0000000000..2b086026b5 --- /dev/null +++ b/sql-schema/074.sql @@ -0,0 +1 @@ +INSERT INTO config (config_name,config_value,config_default,config_descr,config_group,config_group_order,config_sub_group,config_sub_group_order,config_hidden,config_disabled) values ('oxidized.enabled','false','false','Enable Oxidized support','external',0,'oxidized',0,'0','0'),('oxidized.url','','','Oxidized API url','external',0,'oxidized',0,'0','0'),('oxidized.features.versioning','false','false','Enable Oxidized config versioning','external',0,'oxidized',0,'0','0');