diff --git a/README.md b/README.md index 12e557503..cfce7e7df 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ net/upnp -- Universal Plug and Play Service net/wol -- Wake on LAN Service net-mgmt/snmp -- SNMP Server via bsnmpd sysutils/boot-delay -- Apply a persistent boot delay +sysutils/monit -- Proactive system monitoring sysutils/smart -- SMART tools sysutils/vmware -- VMware tools sysutils/xen -- Xen guest utilities diff --git a/sysutils/monit/+POST_INSTALL.post b/sysutils/monit/+POST_INSTALL.post new file mode 100644 index 000000000..c6ee6c823 --- /dev/null +++ b/sysutils/monit/+POST_INSTALL.post @@ -0,0 +1 @@ +/usr/local/opnsense/scripts/OPNsense/Monit/post-install.php diff --git a/sysutils/monit/src/etc/inc/plugins.inc.d/monit.inc b/sysutils/monit/src/etc/inc/plugins.inc.d/monit.inc index fd864f5bf..179f4ff6d 100644 --- a/sysutils/monit/src/etc/inc/plugins.inc.d/monit.inc +++ b/sysutils/monit/src/etc/inc/plugins.inc.d/monit.inc @@ -31,18 +31,18 @@ */ function monit_services() { - global $config; - $services = array(); - if (isset($config['OPNsense']['monit']['general']['enabled']) && $config['OPNsense']['monit']['general']['enabled'] == 1) { - $services[] = array( - 'description' => gettext('Monit System Monitoring'), - 'configd' => array( - 'restart' => array('monit restart'), - 'start' => array('monit start'), - 'stop' => array('monit stop'), - ), - 'name' => 'monit', - ); - } - return $services; + global $config; + $services = array(); + if (isset($config['OPNsense']['monit']['general']['enabled']) && $config['OPNsense']['monit']['general']['enabled'] == 1) { + $services[] = array( + 'description' => gettext('Monit System Monitoring'), + 'configd' => array( + 'restart' => array('monit restart'), + 'start' => array('monit start'), + 'stop' => array('monit stop'), + ), + 'name' => 'monit', + ); + } + return $services; } diff --git a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/ServiceController.php b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/ServiceController.php index 6d441feb0..b84511268 100644 --- a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/ServiceController.php +++ b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/ServiceController.php @@ -34,139 +34,136 @@ use \OPNsense\Base\ApiControllerBase; use \OPNsense\Core\Backend; use \OPNsense\Monit\Monit; - /** * Class ServiceController * @package OPNsense\Monit */ class ServiceController extends ApiControllerBase { - /** - * test monit configuration - * @return array - */ - public function configtestAction() - { - if ($this->request->isPost()) { - $this->sessionClose(); - } - $result['function'] = "configtest"; + /** + * test monit configuration + * @return array + */ + public function configtestAction() + { + if ($this->request->isPost()) { + $this->sessionClose(); + } + $result['function'] = "configtest"; - $result['template'] = $this->callBackend('template'); - if ($result['template'] != 'OK') - { - $result['result'] = "Template error: " . $result['template']; - return $result; - } + $result['template'] = $this->callBackend('template'); + if ($result['template'] != 'OK') { + $result['result'] = "Template error: " . $result['template']; + return $result; + } - $result['result'] = $this->callBackend('configtest'); - return $result; - } + $result['result'] = $this->callBackend('configtest'); + return $result; + } - /** - * reload monit with new configuration - * @return array - */ - public function reloadAction() - { - if ($this->request->isPost()) { - $this->sessionClose(); - } - $result['function'] = "reload"; + /** + * reload monit with new configuration + * @return array + */ + public function reloadAction() + { + if ($this->request->isPost()) { + $this->sessionClose(); + } + $result['function'] = "reload"; - $result['template'] = $this->callBackend('template'); - if ($result['template'] != 'OK') - { - $result['result'] = "Template error: " . $result['template']; - return $result; - } + $result['template'] = $this->callBackend('template'); + if ($result['template'] != 'OK') { + $result['result'] = "Template error: " . $result['template']; + return $result; + } - $status = $this->callBackend('status'); - if (substr($status, 0, 16) != 'monit is running') { - $result['result'] = "Monit is not running"; - return $result; - } + $status = $this->callBackend('status'); + if (substr($status, 0, 16) != 'monit is running') { + $result['result'] = "Monit is not running"; + return $result; + } - $result['result'] = $this->callBackend('reload'); - return $result; - } + $result['result'] = $this->callBackend('reload'); + return $result; + } - /** - * get status of monit process - * @return array - */ - public function statusAction() - { - if ($this->request->isPost()) { - $this->sessionClose(); - } - $result['function'] = "status"; - $result['result'] = "failed"; - $result['status'] = 'stopped'; - $status = $this->callBackend('status'); - if (substr($status, 0, 16) == 'monit is running') { - $result['result'] = "ok"; - $result['status'] = 'running'; - } else { - $result['error'] = $status; - } - return $result; - } + /** + * get status of monit process + * @return array + */ + public function statusAction() + { + if ($this->request->isPost()) { + $this->sessionClose(); + } + $result['function'] = "status"; + $result['result'] = "failed"; + $result['status'] = 'stopped'; + $status = $this->callBackend('status'); + if (substr($status, 0, 16) == 'monit is running') { + $result['result'] = "ok"; + $result['status'] = 'running'; + } else { + $result['error'] = $status; + } + return $result; + } - /** - * start monit service - * @return array - */ - public function startAction() - { - $result = array("result" => "failed", "function" => "start"); - if ($this->request->isPost()) { - $this->sessionClose(); - $result['result'] = $this->callBackend('start'); - } - return $result; - } + /** + * start monit service + * @return array + */ + public function startAction() + { + $result = array("result" => "failed", "function" => "start"); + if ($this->request->isPost()) { + $this->sessionClose(); + $result['result'] = $this->callBackend('start'); + } + return $result; + } - /** - * stop monit service - * @return array - */ - public function stopAction() - { - $result = array("result" => "failed", "function" => "stop"); - if ($this->request->isPost()) { - $this->sessionClose(); - $result['result'] = $this->callBackend('stop'); - } - return $result; - } + /** + * stop monit service + * @return array + */ + public function stopAction() + { + $result = array("result" => "failed", "function" => "stop"); + if ($this->request->isPost()) { + $this->sessionClose(); + $result['result'] = $this->callBackend('stop'); + } + return $result; + } - /** - * restart monit service - * @return array - */ - public function restartAction() - { - $result = array("result" => "failed", "function" => "restart"); - if ($this->request->isPost()) { - $this->sessionClose(); - $result['result'] = $this->callBackend('restart'); - } - return $result; - } + /** + * restart monit service + * @return array + */ + public function restartAction() + { + $result = array("result" => "failed", "function" => "restart"); + if ($this->request->isPost()) { + $this->sessionClose(); + $result['result'] = $this->callBackend('restart'); + } + return $result; + } - /** - * call backend functions - * @param action - * @return string - */ - protected function callBackend($action) - { - $backend = new Backend(); - if ($action == 'template') { - return trim($backend->configdRun('template reload OPNsense/Monit')); - } else { - return trim($backend->configdRun('monit ' . $action)); - } - } + /** + * call backend functions + * @param action + * @return string + */ + protected function callBackend($action) + { + $backend = new Backend(); + if ($action == 'template') { + return trim($backend->configdRun('template reload OPNsense/Monit')); + } else { + return trim($backend->configdRun('monit ' . $action)); + } + } } diff --git a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/SettingsController.php b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/SettingsController.php index 78856ae3c..735861554 100644 --- a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/SettingsController.php +++ b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/Api/SettingsController.php @@ -44,420 +44,420 @@ class SettingsController extends ApiControllerBase //// GENRAL SETTINGS //// - /** - * retrieve monit general settings or return defaults - * @return array - */ - public function getGeneralAction() - { - return $this->get('general'); - } + /** + * retrieve monit general settings or return defaults + * @return array + */ + public function getGeneralAction() + { + return $this->get('general'); + } - /** - * update monit general settings with given properties - * @return array - */ - public function setGeneralAction() - { - return $this->set('general'); - } + /** + * update monit general settings with given properties + * @return array + */ + public function setGeneralAction() + { + return $this->set('general'); + } - //// ALERT SETTINGS //// + //// ALERT SETTINGS //// - /** - * search alert - * @return array - */ - public function searchAlertAction() - { - $fields = array("enabled", "recipient", "noton", "events", "description"); - return $this->search('alert', $fields); - } + /** + * search alert + * @return array + */ + public function searchAlertAction() + { + $fields = array("enabled", "recipient", "noton", "events", "description"); + return $this->search('alert', $fields); + } - /** - * retrieve monit alert settings or return defaults - * @param $uuid item unique id - * @return array - */ - public function getAlertAction($uuid = null) - { - return $this->get('alert', $uuid); - } + /** + * retrieve monit alert settings or return defaults + * @param $uuid item unique id + * @return array + */ + public function getAlertAction($uuid = null) + { + return $this->get('alert', $uuid); + } - /** - * set monit alert parameter - * @param $uuid item unique id - * @return array - */ - public function setAlertAction($uuid = null) - { - if ($uuid != null) { - return $this->set('alert', $uuid); - } - return array("result" => "failed"); - } + /** + * set monit alert parameter + * @param $uuid item unique id + * @return array + */ + public function setAlertAction($uuid = null) + { + if ($uuid != null) { + return $this->set('alert', $uuid); + } + return array("result" => "failed"); + } - /** - * add monit alert parameter - * @return array - */ - public function addAlertAction() - { - return $this->set('alert', null, true); - } + /** + * add monit alert parameter + * @return array + */ + public function addAlertAction() + { + return $this->set('alert', null, true); + } - /** - * delete monit alert parameter - * @param $uuid item unique id - * @return array - */ - public function delAlertAction($uuid = null) - { - if ($uuid != null) { - return $this->del('alert', $uuid); - } - return array("result" => "failed"); - } + /** + * delete monit alert parameter + * @param $uuid item unique id + * @return array + */ + public function delAlertAction($uuid = null) + { + if ($uuid != null) { + return $this->del('alert', $uuid); + } + return array("result" => "failed"); + } - /** - * toggle monit alert by uuid (enable/disable) - * @param $uuid item unique id - * @return array - */ - public function toggleAlertAction($uuid) - { - if ($uuid != null) { - return $this->toggle('alert', $uuid); - } - return array("result" => "failed"); - } + /** + * toggle monit alert by uuid (enable/disable) + * @param $uuid item unique id + * @return array + */ + public function toggleAlertAction($uuid) + { + if ($uuid != null) { + return $this->toggle('alert', $uuid); + } + return array("result" => "failed"); + } - //// SERVICE SETTINGS //// + //// SERVICE SETTINGS //// - /** - * search service - * @return array - */ - public function searchServiceAction() - { - $fields = array("enabled", "name", "type", "description"); - return $this->search('service', $fields); - } + /** + * search service + * @return array + */ + public function searchServiceAction() + { + $fields = array("enabled", "name", "type", "description"); + return $this->search('service', $fields); + } - /** - * retrieve monit service settings or return defaults - * @param $uuid item unique id - * @return array - */ - public function getServiceAction($uuid = null) - { - return $this->get('service', $uuid); - } + /** + * retrieve monit service settings or return defaults + * @param $uuid item unique id + * @return array + */ + public function getServiceAction($uuid = null) + { + return $this->get('service', $uuid); + } - /** - * set monit service parameter - * @param $uuid item unique id - * @return array - */ - public function setServiceAction($uuid = null) - { - if ($uuid != null) { - return $this->set('service', $uuid); - } - return array("result" => "failed"); - } + /** + * set monit service parameter + * @param $uuid item unique id + * @return array + */ + public function setServiceAction($uuid = null) + { + if ($uuid != null) { + return $this->set('service', $uuid); + } + return array("result" => "failed"); + } - /** - * add monit service parameter - * @return array - */ - public function addServiceAction() - { - return $this->set('service', null, true); - } + /** + * add monit service parameter + * @return array + */ + public function addServiceAction() + { + return $this->set('service', null, true); + } - /** - * delete monit service parameter - * @param $uuid item unique id - * @return array - */ - public function delServiceAction($uuid = null) - { - if ($uuid != null) { - return $this->del('service', $uuid); - } - return array("result" => "failed"); - } + /** + * delete monit service parameter + * @param $uuid item unique id + * @return array + */ + public function delServiceAction($uuid = null) + { + if ($uuid != null) { + return $this->del('service', $uuid); + } + return array("result" => "failed"); + } - /** - * toggle monit service by uuid (enable/disable) - * @param $uuid item unique id - * @return array - */ - public function toggleServiceAction($uuid) - { - if ($uuid != null) { - return $this->toggle('service', $uuid); - } - return array("result" => "failed"); - } + /** + * toggle monit service by uuid (enable/disable) + * @param $uuid item unique id + * @return array + */ + public function toggleServiceAction($uuid) + { + if ($uuid != null) { + return $this->toggle('service', $uuid); + } + return array("result" => "failed"); + } - //// SERVICE TEST SETTINGS //// + //// SERVICE TEST SETTINGS //// - /** - * search test - * @return array - */ - public function searchTestAction() - { - $fields = array("name", "condition", "action"); - return $this->search('test', $fields); - } + /** + * search test + * @return array + */ + public function searchTestAction() + { + $fields = array("name", "condition", "action"); + return $this->search('test', $fields); + } - /** - * retrieve monit service test settings or return defaults - * @param $uuid item unique id - * @return array - */ - public function getTestAction($uuid = null) - { - return $this->get('test', $uuid); - } + /** + * retrieve monit service test settings or return defaults + * @param $uuid item unique id + * @return array + */ + public function getTestAction($uuid = null) + { + return $this->get('test', $uuid); + } - /** - * set monit service test parameter - * @param $uuid item unique id - * @return array - */ - public function setTestAction($uuid = null) - { - if ($uuid != null) { - return $this->set('test', $uuid); - } - return array("result" => "failed"); - } + /** + * set monit service test parameter + * @param $uuid item unique id + * @return array + */ + public function setTestAction($uuid = null) + { + if ($uuid != null) { + return $this->set('test', $uuid); + } + return array("result" => "failed"); + } - /** - * add monit service test parameter - * @return array - */ - public function addTestAction() - { - return $this->set('test', null, true); - } + /** + * add monit service test parameter + * @return array + */ + public function addTestAction() + { + return $this->set('test', null, true); + } - /** - * delete monit service test parameter - * @param $uuid item unique id - * @return array - */ - public function delTestAction($uuid = null) - { - if ($uuid != null) { - return $this->del('test', $uuid); - } - return array("result" => "failed"); - } + /** + * delete monit service test parameter + * @param $uuid item unique id + * @return array + */ + public function delTestAction($uuid = null) + { + if ($uuid != null) { + return $this->del('test', $uuid); + } + return array("result" => "failed"); + } - //// ABSTRACT FUNCTIONS //// + //// ABSTRACT FUNCTIONS //// - /** - * retrieve monit settings - * @param $nodeType - * @param $uuid - * @return result array - */ - private function get($nodeType = null, $uuid = null) - { - $result = array("result" => "failed"); - if($this->request->isGet() && $nodeType != null) { - $mdlMonit = new Monit(); - if ($uuid != null) { - $node = $mdlMonit->getNodeByReference($nodeType . '.' . $uuid); - } else { - if ($nodeType == 'general') { - $node = $mdlMonit->getNodeByReference($nodeType); - } else { - $node = $mdlMonit->$nodeType->Add(); - } - } - if ($node != null) { - $result[$nodeType] = $node->getNodes(); - $result["result"] = "ok"; - } - } - return $result; - } + /** + * retrieve monit settings + * @param $nodeType + * @param $uuid + * @return result array + */ + private function get($nodeType = null, $uuid = null) + { + $result = array("result" => "failed"); + if ($this->request->isGet() && $nodeType != null) { + $mdlMonit = new Monit(); + if ($uuid != null) { + $node = $mdlMonit->getNodeByReference($nodeType . '.' . $uuid); + } else { + if ($nodeType == 'general') { + $node = $mdlMonit->getNodeByReference($nodeType); + } else { + $node = $mdlMonit->$nodeType->Add(); + } + } + if ($node != null) { + $result[$nodeType] = $node->getNodes(); + $result["result"] = "ok"; + } + } + return $result; + } - /** - * set monit properties - * @param $nodeType - * @param $uuid - * @parm $action set or add node - * @return result array - */ - private function set($nodeType = null, $uuid = null, $add = false) - { - $result = array("result" => "failed"); - if ($this->request->isPost() && $this->request->hasPost("monit") && $nodeType != null) { - $mdlMonit = new Monit(); - if($add == false) { // set node - if ($uuid != null) { - $node = $mdlMonit->getNodeByReference($nodeType . '.' . $uuid); - } else { - if ($nodeType == 'general') { - $node = $mdlMonit->getNodeByReference($nodeType); - } else { - $node = $mdlMonit->$nodeType->Add(); - } - } - } else { - $node = $mdlMonit->$nodeType->Add(); - } - if ($node != null) { - $monitInfo = $this->request->getPost("monit"); + /** + * set monit properties + * @param $nodeType + * @param $uuid + * @parm $action set or add node + * @return result array + */ + private function set($nodeType = null, $uuid = null, $add = false) + { + $result = array("result" => "failed"); + if ($this->request->isPost() && $this->request->hasPost("monit") && $nodeType != null) { + $mdlMonit = new Monit(); + if ($add == false) { // set node + if ($uuid != null) { + $node = $mdlMonit->getNodeByReference($nodeType . '.' . $uuid); + } else { + if ($nodeType == 'general') { + $node = $mdlMonit->getNodeByReference($nodeType); + } else { + $node = $mdlMonit->$nodeType->Add(); + } + } + } else { + $node = $mdlMonit->$nodeType->Add(); + } + if ($node != null) { + $monitInfo = $this->request->getPost("monit"); - // perform plugin specific validations - if ($nodeType == 'service') { - switch ($monitInfo[$nodeType]['type']) { - case 'process': - if (empty($monitInfo[$nodeType]['pidfile']) && empty($monitInfo[$nodeType]['match'])) { - $result["validations"]['monit.service.pidfile'] = "Please set at least one of Pidfile or Match."; - $result["validations"]['monit.service.match'] = $result["validations"]['monit.service.pidfile']; - } - break; - case 'host': - if (empty($monitInfo[$nodeType]['address'])) { - $result["validations"]['monit.service.address'] = "Address is mandatory for 'Remote Host' checks."; - } - break; - case 'network': - if (empty($monitInfo[$nodeType]['address']) && empty($monitInfo[$nodeType]['interface'])) { - $result["validations"]['monit.service.address'] = "Please set at least one of Address or Interface."; - $result["validations"]['monit.service.interface'] = $result["validations"]['monit.service.address']; - } - break; - case 'system': - break; - default: - if (empty($monitInfo[$nodeType]['path'])) { - $result["validations"]['monit.service.path'] = "Path is mandatory."; - } - } - } + // perform plugin specific validations + if ($nodeType == 'service') { + switch ($monitInfo[$nodeType]['type']) { + case 'process': + if (empty($monitInfo[$nodeType]['pidfile']) && empty($monitInfo[$nodeType]['match'])) { + $result["validations"]['monit.service.pidfile'] = "Please set at least one of Pidfile or Match."; + $result["validations"]['monit.service.match'] = $result["validations"]['monit.service.pidfile']; + } + break; + case 'host': + if (empty($monitInfo[$nodeType]['address'])) { + $result["validations"]['monit.service.address'] = "Address is mandatory for 'Remote Host' checks."; + } + break; + case 'network': + if (empty($monitInfo[$nodeType]['address']) && empty($monitInfo[$nodeType]['interface'])) { + $result["validations"]['monit.service.address'] = "Please set at least one of Address or Interface."; + $result["validations"]['monit.service.interface'] = $result["validations"]['monit.service.address']; + } + break; + case 'system': + break; + default: + if (empty($monitInfo[$nodeType]['path'])) { + $result["validations"]['monit.service.path'] = "Path is mandatory."; + } + } + } - $node->setNodes($monitInfo[$nodeType]); - $valMsgs = $mdlMonit->performValidation(); - foreach ($valMsgs as $field => $msg) { - $fieldnm = str_replace($node->__reference, "monit." . $nodeType, $msg->getField()); - $result["validations"][$fieldnm] = $msg->getMessage(); - } - if ($valMsgs->count() == 0) { - $mdlMonit->serializeToConfig(); - Config::getInstance()->save(); - $result["result"] = "saved"; - } - } - } - return $result; - } + $node->setNodes($monitInfo[$nodeType]); + $valMsgs = $mdlMonit->performValidation(); + foreach ($valMsgs as $field => $msg) { + $fieldnm = str_replace($node->__reference, "monit." . $nodeType, $msg->getField()); + $result["validations"][$fieldnm] = $msg->getMessage(); + } + if ($valMsgs->count() == 0) { + $mdlMonit->serializeToConfig(); + Config::getInstance()->save(); + $result["result"] = "saved"; + } + } + } + return $result; + } - /** - * delete monit properties - * @param $nodeType - * @param $uuid - * @return result array - */ - private function del($nodeType = null, $uuid = null) - { - $result = array("result" => "failed"); - if ($this->request->isPost() && $nodeType != null) { - $mdlMonit = new Monit(); - if ($uuid != null) { - $node = $mdlMonit->getNodeByReference($nodeType . '.' . $uuid); - if ($node != null) { - if ($mdlMonit->$nodeType->del($uuid) == true) { - // remove test from services - if ($nodeType == 'test') { - // get a list of all services - $services = $mdlMonit->service->getNodes(); - foreach ($services as $serviceUuid => $service) { - foreach ($service['tests'] as $testUuid => $test) { - // service has a reference to a test - if ($testUuid == $uuid) { - // get service model and remove $uuid from tests - $ref = 'service.' . $serviceUuid . '.tests'; - $tstNode = $mdlMonit->getNodeByReference($ref); - $svcTests = str_replace($uuid, '', $tstNode->__toString()); - $svcTests = str_replace(',,', ',', $svcTests); - $svcTests = rtrim($svcTests, ','); - $svcTests = ltrim($svcTests, ','); - $mdlMonit->setNodeByReference($ref, $svcTests); - } - } - } - } - $mdlMonit->serializeToConfig(); - Config::getInstance()->save(); - $svcMonit = new ServiceController(); - $result = $svcMonit->reloadAction(); - } - } else { - $result['result'] = "not found"; - } - } else { - $result['result'] = "uuid not given"; - } - } - return $result; - } + /** + * delete monit properties + * @param $nodeType + * @param $uuid + * @return result array + */ + private function del($nodeType = null, $uuid = null) + { + $result = array("result" => "failed"); + if ($this->request->isPost() && $nodeType != null) { + $mdlMonit = new Monit(); + if ($uuid != null) { + $node = $mdlMonit->getNodeByReference($nodeType . '.' . $uuid); + if ($node != null) { + if ($mdlMonit->$nodeType->del($uuid) == true) { + // remove test from services + if ($nodeType == 'test') { + // get a list of all services + $services = $mdlMonit->service->getNodes(); + foreach ($services as $serviceUuid => $service) { + foreach ($service['tests'] as $testUuid => $test) { + // service has a reference to a test + if ($testUuid == $uuid) { + // get service model and remove $uuid from tests + $ref = 'service.' . $serviceUuid . '.tests'; + $tstNode = $mdlMonit->getNodeByReference($ref); + $svcTests = str_replace($uuid, '', $tstNode->__toString()); + $svcTests = str_replace(',,', ',', $svcTests); + $svcTests = rtrim($svcTests, ','); + $svcTests = ltrim($svcTests, ','); + $mdlMonit->setNodeByReference($ref, $svcTests); + } + } + } + } + $mdlMonit->serializeToConfig(); + Config::getInstance()->save(); + $svcMonit = new ServiceController(); + $result = $svcMonit->reloadAction(); + } + } else { + $result['result'] = "not found"; + } + } else { + $result['result'] = "uuid not given"; + } + } + return $result; + } - /** - * toggle monit items (enable/disable) - * @param $nodeType - * @param $uuid - * @return result array - */ - private function toggle($nodeType = null, $uuid = null) - { - $result = array("result" => "failed"); - if ($this->request->isPost() && $nodeType != null) { - $mdlMonit = new Monit(); - if ($uuid != null) { - $node = $mdlMonit->getNodeByReference($nodeType . '.' . $uuid); - if ($node != null) { - if ($node->enabled->__toString() == "1") { - $node->enabled = "0"; - } else { - $node->enabled = "1"; - } - $mdlMonit->serializeToConfig(); - Config::getInstance()->save(); - $svcMonit = new ServiceController(); - $result= $svcMonit->reloadAction(); - } else { - $result['result'] = "not found"; - } - } else { - $result['result'] = "uuid not given"; - } - } - return $result; - } + /** + * toggle monit items (enable/disable) + * @param $nodeType + * @param $uuid + * @return result array + */ + private function toggle($nodeType = null, $uuid = null) + { + $result = array("result" => "failed"); + if ($this->request->isPost() && $nodeType != null) { + $mdlMonit = new Monit(); + if ($uuid != null) { + $node = $mdlMonit->getNodeByReference($nodeType . '.' . $uuid); + if ($node != null) { + if ($node->enabled->__toString() == "1") { + $node->enabled = "0"; + } else { + $node->enabled = "1"; + } + $mdlMonit->serializeToConfig(); + Config::getInstance()->save(); + $svcMonit = new ServiceController(); + $result= $svcMonit->reloadAction(); + } else { + $result['result'] = "not found"; + } + } else { + $result['result'] = "uuid not given"; + } + } + return $result; + } - /** - * search monit settings - * @param $nodeType - * @param requested field list - * @return array - */ - private function search($nodeType = null, &$fields = null) - { - $this->sessionClose(); - if($nodeType != null) { - $mdlMonit = new Monit(); - $grid = new UIModelGrid($mdlMonit->$nodeType); - return $grid->fetchBindRequest($this->request, $fields); - } - } + /** + * search monit settings + * @param $nodeType + * @param requested field list + * @return array + */ + private function search($nodeType = null, &$fields = null) + { + $this->sessionClose(); + if ($nodeType != null) { + $mdlMonit = new Monit(); + $grid = new UIModelGrid($mdlMonit->$nodeType); + return $grid->fetchBindRequest($this->request, $fields); + } + } } 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 525950a30..e124b870d 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 @@ -38,97 +38,97 @@ use \OPNsense\Base\ApiControllerBase; */ class StatusController extends ApiControllerBase { - /** - * get monit status page - * see monit(1) - * @return array - */ - public function getAction() - { - $result = array("result" => "failed", "function" => "getStatus"); + /** + * get monit status page + * see monit(1) + * @return array + */ + public function getAction() + { + $result = array("result" => "failed", "function" => "getStatus"); - // connect monit httpd socket defined in monitrc by 'set httpd ...' - if (file_exists("/var/run/monit.sock") && filetype("/var/run/monit.sock") == "socket" ) { - // throws an exception therefore no error handling - $socket = stream_socket_client("unix:///var/run/monit.sock", $errno, $errstr); + // connect monit httpd socket defined in monitrc by 'set httpd ...' + if (file_exists("/var/run/monit.sock") && filetype("/var/run/monit.sock") == "socket") { + // throws an exception therefore no error handling + $socket = stream_socket_client("unix:///var/run/monit.sock", $errno, $errstr); - // get monit status page - $request = "GET /_status?format=text HTTP/1.0\r\n"; - $request .= "\r\n"; - $count = fwrite($socket, $request); - $result['count'] = $count; - $result['status'] = ''; - $result['orig'] = ''; - $result['httpstatus'] = preg_replace( "/\r|\n/", "", fgets($socket)); - $ignorelines = 1; - if ($result['httpstatus'] == 'HTTP/1.0 200 OK') { - while (!feof($socket)) { - $line = fgets($socket); - $result['orig'] .= $line; + // get monit status page + $request = "GET /_status?format=text HTTP/1.0\r\n"; + $request .= "\r\n"; + $count = fwrite($socket, $request); + $result['count'] = $count; + $result['status'] = ''; + $result['orig'] = ''; + $result['httpstatus'] = preg_replace("/\r|\n/", "", fgets($socket)); + $ignorelines = 1; + if ($result['httpstatus'] == 'HTTP/1.0 200 OK') { + while (!feof($socket)) { + $line = fgets($socket); + $result['orig'] .= $line; - // ignore lines (mostly HTTP headers) until a line starts with 'Monit' e.g. 'Monit 5.20.0 uptime: 2d 23h 2m' - if (substr($line, 0, 5) == 'Monit') { - $ignorelines = 0; - } - if ($ignorelines) { - continue; - } - $result['status'] .= $line; - } - $result['result'] = "ok"; + // ignore lines (mostly HTTP headers) until a line starts with 'Monit' e.g. 'Monit 5.20.0 uptime: 2d 23h 2m' + if (substr($line, 0, 5) == 'Monit') { + $ignorelines = 0; + } + if ($ignorelines) { + continue; + } + $result['status'] .= $line; + } + $result['result'] = "ok"; + } + fclose($socket); - } - fclose($socket); - - // response contains shell color escape codes; convert them to CSS - $result['status'] = '
' . $this->bashColorToCSS($result['status']) . ''; - } else { - $result['status'] = '
+ // response contains shell color escape codes; convert them to CSS + $result['status'] = '' . $this->bashColorToCSS($result['status']) . ''; + } else { + $result['status'] = 'Either the file /var/run/monit.sock does not exists or it is not a unix socket. Please check if the Monit service is running. If you have started Monit recently, wait for StartDelay seconds and refresh this page.'; - } - return $result; - } + } + return $result; + } - /** - * convert bash color escape codes to CSS - * @param $string - * @return string - */ - private function bashColorToCSS($string) { - $colors = [ - '/\x1b\[0;30m(.*?)\x1b\[0m/s' => '$1', + /** + * convert bash color escape codes to CSS + * @param $string + * @return string + */ + private function bashColorToCSS($string) + { + $colors = [ + '/\x1b\[0;30m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;30m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;31m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;32m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;33m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;34m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;35m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;36m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;37m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;39m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;30m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;31m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;32m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;33m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;34m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;35m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;36m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;37m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;39m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[1;30m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[1;31m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[1;32m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[1;33m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[1;34m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[1;35m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[1;36m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[1;37m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[1;30m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[1;31m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[1;32m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[1;33m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[1;34m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[1;35m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[1;36m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[1;37m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;90m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;91m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;92m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;93m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;94m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;95m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;96m(.*?)\x1b\[0m/s' => '$1', - '/\x1b\[0;97m(.*?)\x1b\[0m/s' => '$1' + '/\x1b\[0;90m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;91m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;92m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;93m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;94m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;95m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;96m(.*?)\x1b\[0m/s' => '$1', + '/\x1b\[0;97m(.*?)\x1b\[0m/s' => '$1' ]; - return preg_replace(array_keys($colors), $colors, $string); - } + return preg_replace(array_keys($colors), $colors, $string); + } } diff --git a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/StatusController.php b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/StatusController.php index 2e38a2613..e6377875b 100644 --- a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/StatusController.php +++ b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/StatusController.php @@ -35,13 +35,13 @@ namespace OPNsense\Monit; */ class StatusController extends \OPNsense\Base\IndexController { - /** - * monit status page - * @throws \Exception - */ - public function indexAction() - { - $this->view->title = gettext('Monit System Monitoring - Status'); - $this->view->pick('OPNsense/Monit/status'); - } + /** + * monit status page + * @throws \Exception + */ + public function indexAction() + { + $this->view->title = gettext('Monit System Monitoring - Status'); + $this->view->pick('OPNsense/Monit/status'); + } } diff --git a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/forms/services.xml b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/forms/services.xml index 6fa73bbd5..15d0f2c8c 100644 --- a/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/forms/services.xml +++ b/sysutils/monit/src/opnsense/mvc/app/controllers/OPNsense/Monit/forms/services.xml @@ -72,4 +72,3 @@text - diff --git a/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/+POST_INSTALL.post b/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/+POST_INSTALL.post deleted file mode 100755 index 77a81f2b2..000000000 --- a/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/+POST_INSTALL.post +++ /dev/null @@ -1 +0,0 @@ -/usr/local/bin/php /usr/local/opnsense/scripts/OPNsense/Monit/post-install.php diff --git a/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/post-install.php b/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/post-install.php old mode 100644 new mode 100755 index c57a19bdc..b32b2a3d4 --- a/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/post-install.php +++ b/sysutils/monit/src/opnsense/scripts/OPNsense/Monit/post-install.php @@ -1,3 +1,4 @@ +#!/usr/local/bin/php getNodes(); // test if Monit is already configured -if(count($nodes['service']) != 0 || count($nodes['test']) != 0) { - exit; +if (count($nodes['service']) != 0 || count($nodes['test']) != 0) { + exit; } $cfg = Config::getInstance(); @@ -59,46 +60,46 @@ $domainName = $cfgObj->system->domain; // 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") + 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" => $hostName . "." . $domainName, - "type" => "system", - "tests" => "" + "enabled" => 1, + "name" => $hostName . "." . $domainName, + "type" => "system", + "tests" => "" ); // define root filesystem service $rootFsService = array( - "enabled" => 1, - "name" => "RootFs", - "type" => "filesystem", - "path" => "/", - "tests" => "" + "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'] . ","; - } + $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