From a6abecc7eed07424b2ed14f83c2d9abf15d0fd72 Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Thu, 19 Oct 2017 09:49:17 +0200 Subject: [PATCH] sysutils/monit: merge version 1.4 from master --- sysutils/monit/Makefile | 2 +- .../OPNsense/Monit/Api/StatusController.php | 8 +++ .../OPNsense/Monit/forms/general.xml | 72 +++++++++++++++++++ .../mvc/app/models/OPNsense/Monit/Monit.xml | 53 +++++++++++++- .../mvc/app/views/OPNsense/Monit/index.volt | 26 ++++++- .../scripts/OPNsense/Monit/post-install.php | 22 ++++-- .../service/templates/OPNsense/Monit/monitrc | 44 +++++++++++- 7 files changed, 217 insertions(+), 10 deletions(-) diff --git a/sysutils/monit/Makefile b/sysutils/monit/Makefile index a05e812cd..ef727c4a7 100644 --- a/sysutils/monit/Makefile +++ b/sysutils/monit/Makefile @@ -1,5 +1,5 @@ PLUGIN_NAME= monit -PLUGIN_VERSION= 1.3 +PLUGIN_VERSION= 1.4 PLUGIN_COMMENT= Proactive system monitoring PLUGIN_MAINTAINER= frank.brendel@eurolog.com PLUGIN_DEPENDS= monit diff --git a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/StatusController.php b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/StatusController.php index e124b870d..a9bf4c1ee 100644 --- a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/StatusController.php +++ b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/StatusController.php @@ -31,6 +31,7 @@ namespace OPNsense\Monit\Api; use \OPNsense\Base\ApiControllerBase; +use \OPNsense\Monit\Monit; /** * Class StatusController @@ -54,6 +55,13 @@ class StatusController extends ApiControllerBase // get monit status page $request = "GET /_status?format=text HTTP/1.0\r\n"; + + // get credentials if configured + $mdlMonit = new Monit(); + if ($mdlMonit->general->httpdUsername->__toString() != null && trim($mdlMonit->general->httpdUsername->__toString()) !== "" && + $mdlMonit->general->httpdPassword->__toString() != null && trim($mdlMonit->general->httpdPassword->__toString()) !== "") { + $request .= "Authorization: Basic " . base64_encode($mdlMonit->general->httpdUsername->__toString() . ":" . $mdlMonit->general->httpdPassword->__toString()) . "\r\n"; + } $request .= "\r\n"; $count = fwrite($socket, $request); $result['count'] = $count; diff --git a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/forms/general.xml b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/forms/general.xml index 36d6c5be6..1f5e476f4 100644 --- a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/forms/general.xml +++ b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/forms/general.xml @@ -49,4 +49,76 @@ checkbox + + monit.general.logfile + + text + syslog or the path to a file.]]> + true + + + monit.general.statefile + + text + + true + + + monit.general.eventqueuePath + + text + + true + + + monit.general.eventqueueSlots + + text + + true + + + monit.general.httpdEnabled + + checkbox + + true + + + monit.general.httpdPort + + text + + true + + + monit.general.httpdAllow + + select_multiple + + true + See Monit Authentication]]> + true + user:password, @group... Finish with TAB. + + + monit.general.mmonitUrl + + text + https://user:pass@192.168.1.10:8443/collector
If you want to control Monit services from your M/Monit instance you have to configure the Monit Port too and add corresponding firewall rules as well.]]>
+ true +
+ + monit.general.mmonitTimeout + + text + true + + + monit.general.mmonitRegisterCredentials + + checkbox + + true + diff --git a/sysutils/monit/src/opnsense/mvc/app/models/OPNsense/Monit/Monit.xml b/sysutils/monit/src/opnsense/mvc/app/models/OPNsense/Monit/Monit.xml index c0f8dd7d2..18eb7b10f 100644 --- a/sysutils/monit/src/opnsense/mvc/app/models/OPNsense/Monit/Monit.xml +++ b/sysutils/monit/src/opnsense/mvc/app/models/OPNsense/Monit/Monit.xml @@ -1,6 +1,6 @@ //OPNsense/monit - 1.0.0 + 1.0.1 Monit settings @@ -46,6 +46,57 @@ 0 Y + + N + syslog facility log_daemon + /^(\/[^\/ ]*)+\/?|syslog.*$/ + Should be a valid absolute path to the log file or the keyword "syslog". + + + N + /^(\/[^\/ ]*)+\/?$/ + Should be a valid absolute path to the state file. + + + N + /^(\/[^\/ ]*)+\/?$/ + Should be a valid absolute path to the eventqueue directory. + + + N + Eventqueue Slots must be a numeric value. + + + 0 + Y + + + + + 2812 + Y + 1 + 65535 + Local Monit Port needs to be an integer value between 0 and 65535 + + + N + Y + + + N + + + 5 + Y + 0 + 86400 + M/Monit Timeout needs to be an integer value between 0 and 86400 + + + 1 + Y + diff --git a/sysutils/monit/src/opnsense/mvc/app/views/OPNsense/Monit/index.volt b/sysutils/monit/src/opnsense/mvc/app/views/OPNsense/Monit/index.volt index df1107c90..338c3d849 100644 --- a/sysutils/monit/src/opnsense/mvc/app/views/OPNsense/Monit/index.volt +++ b/sysutils/monit/src/opnsense/mvc/app/views/OPNsense/Monit/index.volt @@ -68,6 +68,30 @@ POSSIBILITY OF SUCH DAMAGE. updateServiceStatusUI(data['status']); }); }); + + // show/hide httpd/mmonit options + function ShowHideGeneralFields(){ + if ($('#monit\\.general\\.httpdEnabled')[0].checked) { + $('tr[for="monit.general.httpdPort"]').removeClass('hidden'); + $('tr[for="monit.general.httpdAllow"]').removeClass('hidden'); + $('tr[for="monit.general.mmonitUrl"]').removeClass('hidden'); + $('tr[for="monit.general.mmonitTimeout"]').removeClass('hidden'); + $('tr[for="monit.general.mmonitRegisterCredentials"]').removeClass('hidden'); + } else { + $('tr[for="monit.general.httpdPort"]').addClass('hidden'); + $('tr[for="monit.general.httpdAllow"]').addClass('hidden'); + $('tr[for="monit.general.mmonitUrl"]').addClass('hidden'); + $('tr[for="monit.general.mmonitTimeout"]').addClass('hidden'); + $('tr[for="monit.general.mmonitRegisterCredentials"]').addClass('hidden'); + } + }; + $('#monit\\.general\\.httpdEnabled').unbind('click').click(function(){ + ShowHideGeneralFields(); + }); + $('#show_advanced_frm_GeneralSettings').click(function(){ + ShowHideGeneralFields(); + }); + $('#btn_ApplyGeneralSettings').unbind('click').click(function(){ $("#frm_GeneralSettings_progress").addClass("fa fa-spinner fa-pulse"); var frm_id = 'frm_GeneralSettings'; @@ -257,7 +281,7 @@ POSSIBILITY OF SUCH DAMAGE.
- + {{ partial("layout_partials/base_form",['fields':formGeneralSettings,'id':'frm_GeneralSettings','apply_btn_id':'btn_ApplyGeneralSettings'])}}
diff --git a/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/post-install.php b/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/post-install.php index f0d11aa2a..25d7450fa 100755 --- a/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/post-install.php +++ b/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/post-install.php @@ -37,16 +37,29 @@ use OPNsense\Monit\Monit; $mdlMonit = new OPNsense\Monit\Monit; +$cfg = Config::getInstance(); +$cfgObj = $cfg->object(); +$shellObj = new OPNsense\Core\Shell; +$generalNode = $mdlMonit->getNodeByReference('general'); + +// generate password for local Monit plugin user +if (empty($cfgObj->general->httpdUsername) && empty($cfgObj->general->httpdPassword)) { + srand(); + $generalNode->setNodes(array( + "httpdUsername" => "root", + "httpdPassword" => substr(str_shuffle(str_repeat('0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz', 32)), rand(0, 16), rand(17, 32)) + ) + ); + $mdlMonit->serializeToConfig(false, true); + $cfg->save(); +} + $nodes = $mdlMonit->getNodes(); // test if Monit is already configured if (count($nodes['service']) != 0 || count($nodes['test']) != 0) { exit; } -$cfg = Config::getInstance(); -$cfgObj = $cfg->object(); -$shellObj = new OPNsense\Core\Shell; - // get number of cpus and calculate load average limits $nCPU = array(); $shellObj->exec('/sbin/sysctl -n kern.smp.cpus', false, $nCPU); @@ -130,7 +143,6 @@ $systemService['tests'] = substr($systemService['tests'], 0, -1); $rootFsService['tests'] = substr($rootFsService['tests'], 0, -1); // set general properties -$generalNode = $mdlMonit->getNodeByReference('general'); $generalNode->setNodes($generalSettings); // add an alert with (almost) default settings diff --git a/sysutils/monit/src/opnsense/service/templates/OPNsense/Monit/monitrc b/sysutils/monit/src/opnsense/service/templates/OPNsense/Monit/monitrc index f98a69a18..1fa8bc96e 100644 --- a/sysutils/monit/src/opnsense/service/templates/OPNsense/Monit/monitrc +++ b/sysutils/monit/src/opnsense/service/templates/OPNsense/Monit/monitrc @@ -2,12 +2,52 @@ {% from 'OPNsense/Macros/interface.macro' import physical_interface %} {% if helpers.exists('OPNsense.monit.general') %} +{% if helpers.exists('OPNsense.monit.general.httpdEnabled') and OPNsense.monit.general.httpdEnabled|default('0') == '1' %} +{% set httpdCredentials = OPNsense.monit.general.httpdUsername ~ ':"' ~ OPNsense.monit.general.httpdPassword ~ '"' %} +{% set httpdport = "port " ~ OPNsense.monit.general.httpdPort %} +set httpd unixsocket /var/run/monit.sock {{ httpdport }} + allow localhost + allow {{ httpdCredentials }} +{% if helpers.exists('OPNsense.monit.general.httpdAllow') %} +{% for allow in OPNsense.monit.general.httpdAllow.split(",") %} +{% set cred = allow.split(":") %} +{% if cred[1] %} +{% set pass = cred[1].split() %} +{% set allow = cred[0] ~ ':"' ~ pass[0] ~ '" ' ~ pass[1] %} +{% endif %} + allow {{ allow }} +{% endfor %} +{% endif %} +{% if helpers.exists('OPNsense.monit.general.mmonitUrl') and OPNsense.monit.general.mmonitUrl|default('') != '' %} +{% set mregister = 'register without credentials' if OPNsense.monit.general.mmonitRegisterCredentials|default('1') == '0' %} + +set mmonit {{ OPNsense.monit.general.mmonitUrl }} timeout {{ OPNsense.monit.general.mmonitTimeout }} seconds {{ mregister }} +{% endif %} +{% else %} set httpd unixsocket /var/run/monit.sock - allow localhost + allow localhost +{% endif %} set daemon {{ OPNsense.monit.general.interval }} with start delay {{ OPNsense.monit.general.startdelay }} +{% if helpers.exists('OPNsense.monit.general.logfile') and OPNsense.monit.general.logfile|default('') != '' %} +set logfile {{ OPNsense.monit.general.logfile }} +{% else %} set logfile syslog facility log_daemon +{% endif %} + +{% if helpers.exists('OPNsense.monit.general.statefile') and OPNsense.monit.general.statefile|default('') != '' %} +set statefile {{ OPNsense.monit.general.statefile }} +{% endif %} + +{% if helpers.exists('OPNsense.monit.general.eventqueuePath') and OPNsense.monit.general.eventqueuePath|default('') != '' %} +{% set slots = '' %} +{% if helpers.exists('OPNsense.monit.general.eventqueueSlots') %} +{% set slots = "slots " ~ OPNsense.monit.general.eventqueueSlots %} +{% endif %} + +set eventqueue basedir {{ OPNsense.monit.general.eventqueuePath }} {{ slots }} +{% endif %} {% if helpers.exists('OPNsense.monit.general.mailserver') %} {% set port = "port " ~ OPNsense.monit.general.port if OPNsense.monit.general.port|default('') != '' %} @@ -15,7 +55,7 @@ set logfile syslog facility log_daemon {% set password = '' %} {% if helpers.exists('OPNsense.monit.general.username') and helpers.exists('OPNsense.monit.general.password') %} {% set username = "username " ~ OPNsense.monit.general.username %} -{% set password = "password " ~ OPNsense.monit.general.password %} +{% set password = "password " ~ '"' ~ OPNsense.monit.general.password ~ '"' %} {% endif %} {% set ssl = 'using ssl' if OPNsense.monit.general.ssl|default('0') == '1' %} set mailserver {{ OPNsense.monit.general.mailserver }} {{ port }} {{ username }} {{ password }} {{ ssl }}