sysutils/monit: style sweep and +POST_INSTALL tweaks

This commit is contained in:
Franco Fichtner
2017-03-02 09:33:15 +01:00
parent f2ed6f5d87
commit f2a8cb9d9e
10 changed files with 639 additions and 641 deletions
+1
View File
@@ -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
+1
View File
@@ -0,0 +1 @@
/usr/local/opnsense/scripts/OPNsense/Monit/post-install.php
@@ -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;
}
@@ -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));
}
}
}
@@ -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'] = '<pre style="color:WhiteSmoke;background-color:DimGrey">' . $this->bashColorToCSS($result['status']) . '</pre>';
} else {
$result['status'] = '<pre style="color:WhiteSmoke;background-color:DimGrey">
// response contains shell color escape codes; convert them to CSS
$result['status'] = '<pre style="color:WhiteSmoke;background-color:DimGrey">' . $this->bashColorToCSS($result['status']) . '</pre>';
} else {
$result['status'] = '<pre style="color:WhiteSmoke;background-color:DimGrey">
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.</pre>';
}
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' => '<span style="font-weight:bold">$1</span>',
/**
* convert bash color escape codes to CSS
* @param $string
* @return string
*/
private function bashColorToCSS($string)
{
$colors = [
'/\x1b\[0;30m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold">$1</span>',
'/\x1b\[0;30m(.*?)\x1b\[0m/s' => '<span style="color:Black;">$1</span>',
'/\x1b\[0;31m(.*?)\x1b\[0m/s' => '<span style="color:Red;">$1</span>',
'/\x1b\[0;32m(.*?)\x1b\[0m/s' => '<span style="color:Green;">$1</span>',
'/\x1b\[0;33m(.*?)\x1b\[0m/s' => '<span style="color:Yellow;">$1</span>',
'/\x1b\[0;34m(.*?)\x1b\[0m/s' => '<span style="color:Blue;">$1</span>',
'/\x1b\[0;35m(.*?)\x1b\[0m/s' => '<span style="color:Magents;">$1</span>',
'/\x1b\[0;36m(.*?)\x1b\[0m/s' => '<span style="color:Cyan;">$1</span>',
'/\x1b\[0;37m(.*?)\x1b\[0m/s' => '<span style="color:WhiteSmoke;">$1</span>',
'/\x1b\[0;39m(.*?)\x1b\[0m/s' => '<span>$1</span>',
'/\x1b\[0;30m(.*?)\x1b\[0m/s' => '<span style="color:Black;">$1</span>',
'/\x1b\[0;31m(.*?)\x1b\[0m/s' => '<span style="color:Red;">$1</span>',
'/\x1b\[0;32m(.*?)\x1b\[0m/s' => '<span style="color:Green;">$1</span>',
'/\x1b\[0;33m(.*?)\x1b\[0m/s' => '<span style="color:Yellow;">$1</span>',
'/\x1b\[0;34m(.*?)\x1b\[0m/s' => '<span style="color:Blue;">$1</span>',
'/\x1b\[0;35m(.*?)\x1b\[0m/s' => '<span style="color:Magents;">$1</span>',
'/\x1b\[0;36m(.*?)\x1b\[0m/s' => '<span style="color:Cyan;">$1</span>',
'/\x1b\[0;37m(.*?)\x1b\[0m/s' => '<span style="color:WhiteSmoke;">$1</span>',
'/\x1b\[0;39m(.*?)\x1b\[0m/s' => '<span>$1</span>',
'/\x1b\[1;30m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Black;">$1</span>',
'/\x1b\[1;31m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Red;">$1</span>',
'/\x1b\[1;32m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Green;">$1</span>',
'/\x1b\[1;33m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Yellow;">$1</span>',
'/\x1b\[1;34m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Blue;">$1</span>',
'/\x1b\[1;35m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Magenta;">$1</span>',
'/\x1b\[1;36m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Cyan;">$1</span>',
'/\x1b\[1;37m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:White:">$1</span>',
'/\x1b\[1;30m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Black;">$1</span>',
'/\x1b\[1;31m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Red;">$1</span>',
'/\x1b\[1;32m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Green;">$1</span>',
'/\x1b\[1;33m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Yellow;">$1</span>',
'/\x1b\[1;34m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Blue;">$1</span>',
'/\x1b\[1;35m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Magenta;">$1</span>',
'/\x1b\[1;36m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:Cyan;">$1</span>',
'/\x1b\[1;37m(.*?)\x1b\[0m/s' => '<span style="font-weight:bold; color:White:">$1</span>',
'/\x1b\[0;90m(.*?)\x1b\[0m/s' => '<span style="color:DargGrey">$1</span>',
'/\x1b\[0;91m(.*?)\x1b\[0m/s' => '<span style="color:LightCoral">$1</span>',
'/\x1b\[0;92m(.*?)\x1b\[0m/s' => '<span style="color:LightGreen;">$1</span>',
'/\x1b\[0;93m(.*?)\x1b\[0m/s' => '<span style="color:LightYellow;">$1</span>',
'/\x1b\[0;94m(.*?)\x1b\[0m/s' => '<span style="color:LightSkyBlue;">$1</span>',
'/\x1b\[0;95m(.*?)\x1b\[0m/s' => '<span style="color:LightPink;">$1</span>',
'/\x1b\[0;96m(.*?)\x1b\[0m/s' => '<span style="color:LightCyan;">$1</span>',
'/\x1b\[0;97m(.*?)\x1b\[0m/s' => '<span style="color:White;">$1</span>'
'/\x1b\[0;90m(.*?)\x1b\[0m/s' => '<span style="color:DargGrey">$1</span>',
'/\x1b\[0;91m(.*?)\x1b\[0m/s' => '<span style="color:LightCoral">$1</span>',
'/\x1b\[0;92m(.*?)\x1b\[0m/s' => '<span style="color:LightGreen;">$1</span>',
'/\x1b\[0;93m(.*?)\x1b\[0m/s' => '<span style="color:LightYellow;">$1</span>',
'/\x1b\[0;94m(.*?)\x1b\[0m/s' => '<span style="color:LightSkyBlue;">$1</span>',
'/\x1b\[0;95m(.*?)\x1b\[0m/s' => '<span style="color:LightPink;">$1</span>',
'/\x1b\[0;96m(.*?)\x1b\[0m/s' => '<span style="color:LightCyan;">$1</span>',
'/\x1b\[0;97m(.*?)\x1b\[0m/s' => '<span style="color:White;">$1</span>'
];
return preg_replace(array_keys($colors), $colors, $string);
}
return preg_replace(array_keys($colors), $colors, $string);
}
}
@@ -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');
}
}
@@ -72,4 +72,3 @@
<type>text</type>
</field>
</form>
@@ -1 +0,0 @@
/usr/local/bin/php /usr/local/opnsense/scripts/OPNsense/Monit/post-install.php
+32 -31
View File
@@ -1,3 +1,4 @@
#!/usr/local/bin/php
<?php
/**
@@ -38,8 +39,8 @@ $mdlMonit = new OPNsense\Monit\Monit;
$nodes = $mdlMonit->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