diff --git a/LICENSE b/LICENSE index 5be9e4d4e..52295cc9c 100644 --- a/LICENSE +++ b/LICENSE @@ -11,6 +11,7 @@ Copyright (c) 2012 Darren Embry Copyright (c) 2005-2012 David Zeller Copyright (c) 2014-2018 Deciso B.V. Copyright (c) 2006-2015 Devin Teske +Copyright (c) 2016-2018 EURO-LOG AG Copyright (c) 2014 Electric Sheep Fencing, LLC Copyright (c) 2010 Erik Fonnesbeck Copyright (c) 2009 Erik Kristensen diff --git a/plist b/plist index e803092c7..401f19b93 100644 --- a/plist +++ b/plist @@ -493,6 +493,7 @@ /usr/local/opnsense/mvc/app/models/OPNsense/IDS/Migrations/M1_0_0.php /usr/local/opnsense/mvc/app/models/OPNsense/Monit/ACL/ACL.xml /usr/local/opnsense/mvc/app/models/OPNsense/Monit/Menu/Menu.xml +/usr/local/opnsense/mvc/app/models/OPNsense/Monit/Migrations/M1_0_0.php /usr/local/opnsense/mvc/app/models/OPNsense/Monit/Monit.php /usr/local/opnsense/mvc/app/models/OPNsense/Monit/Monit.xml /usr/local/opnsense/mvc/app/models/OPNsense/Ntpd/ACL/ACL.xml @@ -588,7 +589,6 @@ /usr/local/opnsense/scripts/OPNsense/CaptivePortal/set_session_restrictions.py /usr/local/opnsense/scripts/OPNsense/CaptivePortal/sql/init.sql /usr/local/opnsense/scripts/OPNsense/CaptivePortal/strip_template.py -/usr/local/opnsense/scripts/OPNsense/Monit/post-install.php /usr/local/opnsense/scripts/OPNsense/Monit/setup.sh /usr/local/opnsense/scripts/dhcp/get_leases.py /usr/local/opnsense/scripts/dhcp/prefixes.php diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/SettingsController.php b/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/SettingsController.php index 621aaaf45..51b0f7529 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/SettingsController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/SettingsController.php @@ -69,8 +69,8 @@ class SettingsController extends ApiControllerBase } } if ($node != null) { - $result[$nodeType] = $node->getNodes(); - $result["result"] = "ok"; + $result['monit'] = array($nodeType => $node->getNodes()); + $result['result'] = 'ok'; } } return $result; diff --git a/src/opnsense/mvc/app/controllers/OPNsense/Monit/IndexController.php b/src/opnsense/mvc/app/controllers/OPNsense/Monit/IndexController.php index 86c38d3d3..07493b51b 100644 --- a/src/opnsense/mvc/app/controllers/OPNsense/Monit/IndexController.php +++ b/src/opnsense/mvc/app/controllers/OPNsense/Monit/IndexController.php @@ -32,7 +32,7 @@ namespace OPNsense\Monit; /** * Class IndexController - * @package OPNsense\FtpProxy + * @package OPNsense\Monit */ class IndexController extends \OPNsense\Base\IndexController { diff --git a/src/opnsense/mvc/app/models/OPNsense/Monit/Migrations/M1_0_0.php b/src/opnsense/mvc/app/models/OPNsense/Monit/Migrations/M1_0_0.php new file mode 100644 index 000000000..8083367d5 --- /dev/null +++ b/src/opnsense/mvc/app/models/OPNsense/Monit/Migrations/M1_0_0.php @@ -0,0 +1,145 @@ +object(); + $shellObj = new Shell(); + + srand(); + $model->general->httpdUsername = 'root'; + $model->general->httpdPassword = substr( + str_shuffle(str_repeat('0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz', 32)), + rand(0, 16), + rand(17, 32) + ); + + /* get number of cpus and calculate load average limits */ + $nCPU = array(); + $shellObj->exec('/sbin/sysctl -n kern.smp.cpus', false, $nCPU); + $LoadAvg1 = $nCPU[0] * 2; + $LoadAvg5 = $nCPU[0] + ($nCPU[0] / 2); + $LoadAvg15 = $nCPU[0]; + + /* inherit SMTP settings from System->Settings->Notifications */ + if (!empty($cfgObj->notifications->smtp->ipaddress)) { + $model->general->mailserver = $cfgObj->notifications->smtp->ipaddress; + } + if (!empty($cfgObj->notifications->smtp->port)) { + $model->general->port = $cfgObj->notifications->smtp->port; + } + if (!empty($cfgObj->notifications->smtp->username)) { + $model->general->username = $cfgObj->notifications->smtp->username; + } + if (!empty($cfgObj->notifications->smtp->password)) { + $model->general->password = $cfgObj->notifications->smtp->password; + } + if ((!empty($cfgObj->notifications->smtp->tls) && $cfgObj->notifications->smtp->tls == 1) || + (!empty($cfgObj->notifications->smtp->ssl) && $cfgObj->notifications->smtp->ssl == 1)) { + $model->general->ssl = 1; + } + + $alertSettings = array(); + if (!empty($cfgObj->notifications->smtp->notifyemailaddress)) { + $alertSettings['recipient'] = $cfgObj->notifications->smtp->notifyemailaddress; + } + if (!empty($cfgObj->notifications->smtp->fromaddress)) { + $alertSettings['format'] = 'from: ' . $cfgObj->notifications->smtp->fromaddress; + } + $alertNode = $model->alert->Add(); + $alertNode->setNodes($alertSettings); + + /* define some tests */ + $defaultTests = array( + array("name" => "Ping", "condition" => "failed ping", "action" => "alert"), + array("name" => "NetworkLink", "condition" => "failed link", "action" => "alert"), + array("name" => "NetworkSaturation", "condition" => "saturation is greater than 75%", "action" => "alert"), + array("name" => "MemoryUsage", "condition" => "memory usage is greater than 75%", "action" => "alert"), + array("name" => "CPUUsage", "condition" => "cpu usage is greater than 75%", "action" => "alert"), + array("name" => "LoadAvg1", "condition" => "loadavg (1min) is greater than $LoadAvg1", "action" => "alert"), + array("name" => "LoadAvg5", "condition" => "loadavg (5min) is greater than $LoadAvg5", "action" => "alert"), + array("name" => "LoadAvg15", "condition" => "loadavg (15min) is greater than $LoadAvg15", "action" => "alert"), + array("name" => "SpaceUsage", "condition" => "space usage is greater than 75%", "action" => "alert") + ); + + /* define system service */ + $systemService = array( + 'enabled' => 1, + 'name' => '$HOST', + 'type' => 'system', + 'tests' => '' + ); + + /* define root filesystem service */ + $rootFsService = array( + 'enabled' => 1, + 'name' => 'RootFs', + 'type' => 'filesystem', + 'path' => '/', + 'tests' => '' + ); + + foreach ($defaultTests as $defaultTest) { + $testNode = $model->test->add(); + $testNode->setNodes($defaultTest); + if ($defaultTest['name'] == 'MemoryUsage' || + $defaultTest['name'] == 'CPUUsage' || + $defaultTest['name'] == 'LoadAvg1' || + $defaultTest['name'] == 'LoadAvg5' ) { + $systemService['tests'] .= $testNode->getAttributes()['uuid'] . ','; + } + if ($defaultTest['name'] == 'SpaceUsage') { + $rootFsService['tests'] .= $testNode->getAttributes()['uuid'] . ','; + } + } + + /* remove last comma from tests csv */ + $systemService['tests'] = substr($systemService['tests'], 0, -1); + $rootFsService['tests'] = substr($rootFsService['tests'], 0, -1); + + /* XXX storing fails if we reference UUIDs created earlier... */ + $model->serializeToConfig(); + + /* add system service */ + $serviceNode = $model->service->add(); + $serviceNode->setNodes($systemService); + + /* add root filesystem service */ + $rootFsNode = $model->service->add(); + $rootFsNode->setNodes($rootFsService); + } +} diff --git a/src/opnsense/mvc/app/views/OPNsense/Monit/index.volt b/src/opnsense/mvc/app/views/OPNsense/Monit/index.volt index 5649844b3..e4fd2fada 100644 --- a/src/opnsense/mvc/app/views/OPNsense/Monit/index.volt +++ b/src/opnsense/mvc/app/views/OPNsense/Monit/index.volt @@ -83,7 +83,6 @@ POSSIBILITY OF SUCH DAMAGE. /** * general settings */ - // load data mapDataToFormUI({'frm_GeneralSettings':"/api/monit/settings/get/general/"}).done(function(){ formatTokenizersUI(); $('.selectpicker').selectpicker('refresh'); @@ -95,17 +94,17 @@ POSSIBILITY OF SUCH DAMAGE. // 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'); + $('tr[id="row_monit.general.httpdPort"]').removeClass('hidden'); + $('tr[id="row_monit.general.httpdAllow"]').removeClass('hidden'); + $('tr[id="row_monit.general.mmonitUrl"]').removeClass('hidden'); + $('tr[id="row_monit.general.mmonitTimeout"]').removeClass('hidden'); + $('tr[id="row_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'); + $('tr[id="row_monit.general.httpdPort"]').addClass('hidden'); + $('tr[id="row_monit.general.httpdAllow"]').addClass('hidden'); + $('tr[id="row_monit.general.mmonitUrl"]').addClass('hidden'); + $('tr[id="row_monit.general.mmonitTimeout"]').addClass('hidden'); + $('tr[id="row_monit.general.mmonitRegisterCredentials"]').addClass('hidden'); } }; $('#monit\\.general\\.httpdEnabled').unbind('click').click(function(){ @@ -162,52 +161,51 @@ POSSIBILITY OF SUCH DAMAGE. // show hide fields according to selected service type function ShowHideFields(){ var servicetype = $('#monit\\.service\\.type').val(); - $('tr[for="monit.service.pidfile"]').addClass('hidden'); - $('tr[for="monit.service.match"]').addClass('hidden'); - $('tr[for="monit.service.path"]').addClass('hidden'); - $('tr[for="monit.service.address"]').addClass('hidden'); - $('tr[for="monit.service.interface"]').addClass('hidden'); - $('tr[for="monit.service.start"]').removeClass('hidden'); - $('tr[for="monit.service.stop"]').removeClass('hidden'); + $('tr[id="row_monit.service.pidfile"]').addClass('hidden'); + $('tr[id="row_monit.service.match"]').addClass('hidden'); + $('tr[id="row_monit.service.path"]').addClass('hidden'); + $('tr[id="row_monit.service.address"]').addClass('hidden'); + $('tr[id="row_monit.service.interface"]').addClass('hidden'); + $('tr[id="row_monit.service.start"]').removeClass('hidden'); + $('tr[id="row_monit.service.stop"]').removeClass('hidden'); switch (servicetype) { case 'process': var pidfile = $('#monit\\.service\\.pidfile').val(); var match = $('#monit\\.service\\.match').val(); if (pidfile !== '') { - $('tr[for="monit.service.pidfile"]').removeClass('hidden'); - $('tr[for="monit.service.match"]').addClass('hidden'); + $('tr[id="row_monit.service.pidfile"]').removeClass('hidden'); + $('tr[id="row_monit.service.match"]').addClass('hidden'); } else if (match !== '') { - $('tr[for="monit.service.pidfile"]').addClass('hidden'); - $('tr[for="monit.service.match"]').removeClass('hidden'); + $('tr[id="row_monit.service.pidfile"]').addClass('hidden'); + $('tr[id="row_monit.service.match"]').removeClass('hidden'); } else { - $('tr[for="monit.service.pidfile"]').removeClass('hidden'); - $('tr[for="monit.service.match"]').removeClass('hidden'); + $('tr[id="row_monit.service.pidfile"]').removeClass('hidden'); + $('tr[id="row_monit.service.match"]').removeClass('hidden'); } break; case 'host': - $('tr[for="monit.service.address"]').removeClass('hidden'); + $('tr[id="row_monit.service.address"]').removeClass('hidden'); break; case 'network': var address = $('#monit\\.service\\.address').val(); var interface = $('#monit\\.service\\.interface').val(); - console.log('-' + address + '-' + interface + '-'); if (address !== '') { - $('tr[for="monit.service.address"]').removeClass('hidden'); - $('tr[for="monit.service.interface"]').addClass('hidden'); + $('tr[id="row_monit.service.address"]').removeClass('hidden'); + $('tr[id="row_monit.service.interface"]').addClass('hidden'); } else if (interface !== '') { - $('tr[for="monit.service.address"]').addClass('hidden'); - $('tr[for="monit.service.interface"]').removeClass('hidden'); + $('tr[id="row_monit.service.address"]').addClass('hidden'); + $('tr[id="row_monit.service.interface"]').removeClass('hidden'); } else { - $('tr[for="monit.service.address"]').removeClass('hidden'); - $('tr[for="monit.service.interface"]').removeClass('hidden'); + $('tr[id="row_monit.service.address"]').removeClass('hidden'); + $('tr[id="row_monit.service.interface"]').removeClass('hidden'); } break; case 'system': - $('tr[for="monit.service.start"]').addClass('hidden'); - $('tr[for="monit.service.stop"]').addClass('hidden'); + $('tr[id="row_monit.service.start"]').addClass('hidden'); + $('tr[id="row_monit.service.stop"]').addClass('hidden'); break; default: - $('tr[for="monit.service.path"]').removeClass('hidden'); + $('tr[id="row_monit.service.path"]').removeClass('hidden'); } }; $('#DialogEditService').on('shown.bs.modal', function() {ShowHideFields();}); @@ -235,9 +233,9 @@ POSSIBILITY OF SUCH DAMAGE. // show hide execute field function ShowHideExecField(){ var actiontype = $('#monit\\.test\\.action').val(); - $('tr[for="monit.test.path"]').addClass('hidden'); + $('tr[id="row_monit.test.path"]').addClass('hidden'); if (actiontype === 'exec') { - $('tr[for="monit.test.path"]').removeClass('hidden'); + $('tr[id="row_monit.test.path"]').removeClass('hidden'); } }; $('#DialogEditTest').on('shown.bs.modal', function() {ShowHideExecField();}); @@ -282,7 +280,6 @@ POSSIBILITY OF SUCH DAMAGE.
- {{ partial("layout_partials/base_form",['fields':formGeneralSettings,'id':'frm_GeneralSettings','apply_btn_id':'btn_ApplyGeneralSettings'])}}
diff --git a/src/opnsense/scripts/OPNsense/Monit/post-install.php b/src/opnsense/scripts/OPNsense/Monit/post-install.php deleted file mode 100755 index b46f04aba..000000000 --- a/src/opnsense/scripts/OPNsense/Monit/post-install.php +++ /dev/null @@ -1,161 +0,0 @@ -#!/usr/local/bin/php -object(); -$shellObj = new OPNsense\Core\Shell; -$generalNode = $mdlMonit->getNodeByReference('general'); - -if (empty($cfgObj->OPNsense->monit->general->httpdUsername) && empty($cfgObj->OPNsense->monit->general->httpdPassword)) { - print "Generate Monit httpd username and password\n"; - 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; -} - -// get number of cpus and calculate load average limits -$nCPU = array(); -$shellObj->exec('/sbin/sysctl -n kern.smp.cpus', false, $nCPU); -$LoadAvg1 = $nCPU[0] * 2; -$LoadAvg5 = $nCPU[0] + ($nCPU[0] / 2); -$LoadAvg15 = $nCPU[0]; - -// inherit SMTP settings from System->Settings->Notifications -$generalSettings = array(); -if (!empty($cfgObj->notifications->smtp->ipaddress)) { - $generalSettings['mailserver'] = $cfgObj->notifications->smtp->ipaddress; -} -if (!empty($cfgObj->notifications->smtp->port)) { - $generalSettings['port'] = $cfgObj->notifications->smtp->port; -} -if (!empty($cfgObj->notifications->smtp->username)) { - $generalSettings['username'] = $cfgObj->notifications->smtp->username; -} -if (!empty($cfgObj->notifications->smtp->password)) { - $generalSettings['password'] = $cfgObj->notifications->smtp->password; -} -if ((!empty($cfgObj->notifications->smtp->tls) && $cfgObj->notifications->smtp->tls == 1) || - (!empty($cfgObj->notifications->smtp->ssl) && $cfgObj->notifications->smtp->ssl == 1)) { - $generalSettings['ssl'] = 1; -} - -$alertSettings = array(); -if (!empty($cfgObj->notifications->smtp->notifyemailaddress)) { - $alertSettings['recipient'] = $cfgObj->notifications->smtp->notifyemailaddress; -} -if (!empty($cfgObj->notifications->smtp->fromaddress)) { - $alertSettings['format'] = 'from: ' . $cfgObj->notifications->smtp->fromaddress; -} - -// define some tests -$defaultTests = array( - array("name" => "Ping", "condition" => "failed ping", "action" => "alert"), - array("name" => "NetworkLink", "condition" => "failed link", "action" => "alert"), - array("name" => "NetworkSaturation", "condition" => "saturation is greater than 75%", "action" => "alert"), - array("name" => "MemoryUsage", "condition" => "memory usage is greater than 75%", "action" => "alert"), - array("name" => "CPUUsage", "condition" => "cpu usage is greater than 75%", "action" => "alert"), - array("name" => "LoadAvg1", "condition" => "loadavg (1min) is greater than $LoadAvg1", "action" => "alert"), - array("name" => "LoadAvg5", "condition" => "loadavg (5min) is greater than $LoadAvg5", "action" => "alert"), - array("name" => "LoadAvg15", "condition" => "loadavg (15min) is greater than $LoadAvg15", "action" => "alert"), - array("name" => "SpaceUsage", "condition" => "space usage is greater than 75%", "action" => "alert") -); - -// define system service -$systemService = array( - "enabled" => 1, - "name" => '$HOST', - "type" => "system", - "tests" => "" -); - -// define root filesystem service -$rootFsService = array( - "enabled" => 1, - "name" => "RootFs", - "type" => "filesystem", - "path" => "/", - "tests" => "" -); - -foreach ($defaultTests as $defaultTest) { - $testNode = $mdlMonit->test->Add(); - $testNode->setNodes($defaultTest); - if ($defaultTest['name'] == "MemoryUsage" || - $defaultTest['name'] == "CPUUsage" || - $defaultTest['name'] == "LoadAvg1" || - $defaultTest['name'] == "LoadAvg5" ) { - $systemService['tests'] .= $testNode->getAttributes()['uuid'] . ","; - } - if ($defaultTest['name'] == "SpaceUsage") { - $rootFsService['tests'] .= $testNode->getAttributes()['uuid'] . ","; - } -} - -// remove last comma from tests csv -$systemService['tests'] = substr($systemService['tests'], 0, -1); -$rootFsService['tests'] = substr($rootFsService['tests'], 0, -1); - -// set general properties -$generalNode->setNodes($generalSettings); - -// add an alert with (almost) default settings -$alertNode = $mdlMonit->alert->Add(); -$alertNode->setNodes($alertSettings); - -// add system service -$serviceNode = $mdlMonit->service->Add(); -$serviceNode->setNodes($systemService); - -// add root filesystem service -$rootFsNode = $mdlMonit->service->Add(); -$rootFsNode->setNodes($rootFsService); - -// ignore validations because ModelRelationField does not work -$mdlMonit->serializeToConfig(false, true); -$cfg->save();