service: almost entirely migrate monit model 1.0.0 et al.

This commit is contained in:
Franco Fichtner 2018-03-10 12:01:14 +01:00
parent 79500101c6
commit 1a22b8822a
7 changed files with 185 additions and 203 deletions

View File

@ -11,6 +11,7 @@ Copyright (c) 2012 Darren Embry <dse@webonastick.com>
Copyright (c) 2005-2012 David Zeller
Copyright (c) 2014-2018 Deciso B.V.
Copyright (c) 2006-2015 Devin Teske <dteske@FreeBSD.org>
Copyright (c) 2016-2018 EURO-LOG AG
Copyright (c) 2014 Electric Sheep Fencing, LLC
Copyright (c) 2010 Erik Fonnesbeck
Copyright (c) 2009 Erik Kristensen <erik@erikkristensen.com>

2
plist
View File

@ -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

View File

@ -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;

View File

@ -32,7 +32,7 @@ namespace OPNsense\Monit;
/**
* Class IndexController
* @package OPNsense\FtpProxy
* @package OPNsense\Monit
*/
class IndexController extends \OPNsense\Base\IndexController
{

View File

@ -0,0 +1,145 @@
<?php
/*
* Copyright (C) 2017 EURO-LOG AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace OPNsense\Monit\Migrations;
use OPNsense\Base\BaseModelMigration;
use OPNsense\Core\Config;
use OPNsense\Core\Shell;
class M1_0_0 extends BaseModelMigration
{
public function run($model)
{
$cfg = Config::getInstance();
$cfgObj = $cfg->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);
}
}

View File

@ -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.
</ul>
<div class="tab-content content-box tab-content">
<div id="general" class="tab-pane fade in active">
<!-- monit general settings -->
{{ partial("layout_partials/base_form",['fields':formGeneralSettings,'id':'frm_GeneralSettings','apply_btn_id':'btn_ApplyGeneralSettings'])}}
</div>
<div id="alerts" class="tab-pane fade in">

View File

@ -1,161 +0,0 @@
#!/usr/local/bin/php
<?php
/**
* Copyright (C) 2017 EURO-LOG AG
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
require_once("config.inc");
use OPNsense\Core\Config;
use OPNsense\Core\Shell;
use OPNsense\Monit\Monit;
$mdlMonit = new Monit();
$cfg = Config::getInstance();
$cfgObj = $cfg->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();