net-mgmt/zabbix-agent: migrate to ApiMutableServiceController

This commit is contained in:
Frank Wall
2019-03-17 22:17:11 +01:00
parent 13332d3a8d
commit 8b59b3e9aa
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2017 Frank Wall
* Copyright (C) 2017-2019 Frank Wall
* Copyright (C) 2015 Deciso B.V.
*
* All rights reserved.
@@ -29,7 +29,7 @@
*/
namespace OPNsense\ZabbixAgent\Api;
use \OPNsense\Base\ApiControllerBase;
use \OPNsense\Base\ApiMutableServiceControllerBase;
use \OPNsense\Core\Backend;
use \OPNsense\ZabbixAgent\ZabbixAgent;
@@ -37,123 +37,10 @@ use \OPNsense\ZabbixAgent\ZabbixAgent;
* Class ServiceController
* @package OPNsense\ZabbixAgent
*/
class ServiceController extends ApiControllerBase
class ServiceController extends ApiMutableServiceControllerBase
{
/**
* start zabbix agent service (in background)
* @return array
*/
public function startAction()
{
if ($this->request->isPost()) {
// close session for long running action
$this->sessionClose();
$backend = new Backend();
$response = $backend->configdRun("zabbixagent start");
return array("response" => $response);
} else {
return array("response" => array());
}
}
/**
* stop zabbix agent service
* @return array
*/
public function stopAction()
{
if ($this->request->isPost()) {
// close session for long running action
$this->sessionClose();
$backend = new Backend();
$response = $backend->configdRun("zabbixagent stop");
return array("response" => $response);
} else {
return array("response" => array());
}
}
/**
* restart zabbix agent service
* @return array
*/
public function restartAction()
{
if ($this->request->isPost()) {
// close session for long running action
$this->sessionClose();
$backend = new Backend();
$response = $backend->configdRun("zabbixagent restart");
return array("response" => $response);
} else {
return array("response" => array());
}
}
/**
* retrieve status of zabbix agent service
* @return array
* @throws \Exception
*/
public function statusAction()
{
$backend = new Backend();
$mdlAgent = new ZabbixAgent();
$response = $backend->configdRun("zabbixagent status");
if (strpos($response, "not running") > 0) {
if ($mdlAgent->settings->main->enabled->__toString() == "1") {
$status = "stopped";
} else {
$status = "disabled";
}
} elseif (strpos($response, "is running") > 0) {
$status = "running";
} elseif ($mdlAgent->settings->main->enabled->__toString() == "0") {
$status = "disabled";
} else {
$status = "unkown";
}
return array("status" => $status);
}
/**
* reconfigure zabbix agent, generate config and reload
*/
public function reconfigureAction()
{
if ($this->request->isPost()) {
$force_restart = false;
// close session for long running action
$this->sessionClose();
$mdlAgent = new ZabbixAgent();
$backend = new Backend();
$runStatus = $this->statusAction();
// stop zabbix agent when disabled
if ($runStatus['status'] == "running" &&
($mdlAgent->settings->main->enabled->__toString() == "0" || $force_restart)) {
$this->stopAction();
}
// generate template
$backend->configdRun('template reload OPNsense/ZabbixAgent');
// (res)start daemon
if ($mdlAgent->settings->main->enabled->__toString() == "1") {
if ($runStatus['status'] == "running" && !$force_restart) {
$backend->configdRun("zabbixagent reconfigure");
} else {
$this->startAction();
}
}
return array("status" => "ok");
} else {
return array("status" => "failed");
}
}
protected static $internalServiceClass = '\OPNsense\ZabbixAgent\ZabbixAgent';
protected static $internalServiceTemplate = 'OPNsense/ZabbixAgent';
protected static $internalServiceEnabled = 'settings.main.enabled';
protected static $internalServiceName = 'zabbixagent';
}