diff --git a/sysutils/monit/+POST_INSTALL.pre b/sysutils/monit/+POST_INSTALL.pre deleted file mode 100644 index c6ee6c823..000000000 --- a/sysutils/monit/+POST_INSTALL.pre +++ /dev/null @@ -1 +0,0 @@ -/usr/local/opnsense/scripts/OPNsense/Monit/post-install.php diff --git a/sysutils/monit/Makefile b/sysutils/monit/Makefile index f276a74c4..fcf7f8619 100644 --- a/sysutils/monit/Makefile +++ b/sysutils/monit/Makefile @@ -1,5 +1,6 @@ PLUGIN_NAME= monit PLUGIN_VERSION= 1.7 +PLUGIN_VERSION= 1 PLUGIN_COMMENT= Proactive system monitoring PLUGIN_MAINTAINER= frank.brendel@eurolog.com PLUGIN_DEPENDS= monit diff --git a/sysutils/monit/src/opnsense/mvc/app/models/OPNsense/Monit/Migrations/M1_0_0.php b/sysutils/monit/src/opnsense/mvc/app/models/OPNsense/Monit/Migrations/M1_0_0.php new file mode 100644 index 000000000..8b0c1adac --- /dev/null +++ b/sysutils/monit/src/opnsense/mvc/app/models/OPNsense/Monit/Migrations/M1_0_0.php @@ -0,0 +1,142 @@ +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); + + /* add system service */ + $serviceNode = $model->service->add(); + $serviceNode->setNodes($systemService); + + /* add root filesystem service */ + $rootFsNode = $model->service->add(); + $rootFsNode->setNodes($rootFsService); + } +} diff --git a/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/post-install.php b/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/post-install.php deleted file mode 100755 index b46f04aba..000000000 --- a/sysutils/monit/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();