Skip to content

Commit

Permalink
Fix GetServiceStatus and refactor get module status and info
Browse files Browse the repository at this point in the history
  • Loading branch information
sonertari committed Aug 11, 2021
1 parent 240fc94 commit 9ffa414
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 131 deletions.
18 changes: 6 additions & 12 deletions src/Model/collectd.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,13 @@ function Stop()
return $killed;
}

function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
function _getModuleInfo($start)
{
$status= parent::_getModuleStatus($start, $generate_info, $do_cache);

if ($generate_info) {
// We are interested in the ping times in the last 60 seconds only
$gateway_host= $this->getGatewayPingHost();
$status['info']['gateway_ping_time']= $this->getPingAverage($gateway_host);

$remote_host= $this->getRemotePingHost();
$status['info']['remote_ping_time']= $this->getPingAverage($remote_host);
}
return $status;
// We are interested in the ping times in the last 60 seconds only
return array(
'gateway_ping_time' => $this->getPingAverage($this->getGatewayPingHost()),
'remote_ping_time' => $this->getPingAverage($this->getRemotePingHost()),
);
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/Model/dhcpd.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,12 @@ function SetDhcpdConf($lanip, $lanmask, $lannet, $lanbc, $lanmin, $lanmax)
return $retval;
}

function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
function _getModuleInfo($start)
{
$status= parent::_getModuleStatus($start, $generate_info, $do_cache);

if ($generate_info) {
$status['info']['leases']= $this->_getLeasesLineCount();
$status['info']['hosts']= $this->_getArpTableLineCount();
}
return $status;
return array(
'leases' => $this->_getLeasesLineCount(),
'hosts' => $this->_getArpTableLineCount(),
);
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/Model/e2guardian.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,9 @@ function __construct()
);
}

function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
function _getModuleInfo($start)
{
$status= parent::_getModuleStatus($start, $generate_info, $do_cache);

if ($generate_info) {
$status['info']['requests']= $this->getRrdValue('derive-all.rrd', $start, $result);
}
return $status;
return array('requests' => $this->getRrdValue('derive-all.rrd', $start, $result));
}

function GetConfFile($confname, $group)
Expand Down
96 changes: 42 additions & 54 deletions src/Model/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2706,14 +2706,8 @@ function GetServiceStatus($get_info= 0, $start= '10min', $interval_changed= 0)

$info= array();
$cacheInfo= UTMFWDIR.'/cache/info.json';
$generate_info= $interval_changed;

if ($get_info && !$generate_info && !$this->getCachedContents($cacheInfo, $info)) {
$generate_info= TRUE;
}

if ($get_info && $generate_info) {
require_once($MODEL_PATH.'/'.$ModelFiles['collectd']);
if ($get_info && ($interval_changed || !$this->getCachedContents($cacheInfo, $info))) { require_once($MODEL_PATH.'/'.$ModelFiles['collectd']);

$model= new $Models['collectd']();
$gateway= $model->getGatewayPingHost();
Expand Down Expand Up @@ -2753,45 +2747,38 @@ function GetServiceStatus($get_info= 0, $start= '10min', $interval_changed= 0)

exec("doas sh $MODEL_PATH/rrdgraph.sh -$start $gateway $remote_target $intif $extif $disk $interval_changed", $output, $retval);
Error(implode("\n", $output));

foreach ($ModelsToStat as $name => $caption) {
require_once($MODEL_PATH.'/'.$ModelFiles[$name]);
$model= new $Models[$name]();

$module_info= $model->_getModuleInfo($DashboardIntervals2Seconds[$start]);
if ($module_info !== FALSE) {
$info[$name]= $module_info;
}
}

file_put_contents($cacheInfo, json_encode($info), LOCK_EX);
}

$status= array();
$cacheStatus= UTMFWDIR.'/cache/status.json';

if ($interval_changed || !$this->getCachedContents($cacheStatus, $status)) {
foreach ($ModelsToStat as $name => $caption) {
if (array_key_exists($name, $ModelFiles)) {
require_once($MODEL_PATH.'/'.$ModelFiles[$name]);

if (class_exists($Models[$name])) {
$model= new $Models[$name]();

// Pass interval down to _getModuleStatus()
// Do not cache module status individually here, we accumulate and cache them all in status.json below
$module_status= $model->_getModuleStatus($DashboardIntervals2Seconds[$start], $get_info && $generate_info, FALSE);
if ($module_status !== FALSE) {
$status[$name]= $module_status['status'];
if ($get_info) {
$info[$name]= $module_status['info'];
}
}
}
else {
ctlr_syslog(LOG_NOTICE, __FILE__, __FUNCTION__, __LINE__, "Not in Models: $name");
}
}
else {
ctlr_syslog(LOG_NOTICE, __FILE__, __FUNCTION__, __LINE__, "Not in ModelFiles: $name");
require_once($MODEL_PATH.'/'.$ModelFiles[$name]);
$model= new $Models[$name]();

// Do not cache module status individually here, we accumulate and cache them all in status.json below
$module_status= $model->_getModuleStatus($DashboardIntervals2Seconds[$start], FALSE);
if ($module_status !== FALSE) {
$status[$name]= $module_status;
}
}

file_put_contents($cacheStatus, json_encode($status), LOCK_EX);
}

if ($generate_info) {
file_put_contents($cacheInfo, json_encode($info), LOCK_EX);
}

$output= array();
$output['status']= $status;
if ($get_info) {
Expand Down Expand Up @@ -2831,11 +2818,10 @@ function GetModuleStatus()
{
global $StatusCheckInterval;

$module_status= $this->_getModuleStatus($StatusCheckInterval);
return Output(json_encode($module_status['status']));
return Output(json_encode($this->_getModuleStatus($StatusCheckInterval)));
}

function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
function _getModuleStatus($start, $do_cache= TRUE)
{
$status= array();
$cache= "{$this->UTMFWDIR}/cache/{$this->CollectdName}_status.json";
Expand All @@ -2844,20 +2830,17 @@ function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
$runStatus= $this->IsRunning() ? 'R' : 'S';

$status= array(
'status' => array(
'Status' => $runStatus,
'ErrorStatus' => 'U',
'Critical' => 0,
'Error' => 0,
'Warning' => 0,
'Logs' => array(),
),
'info' => array()
'Status' => $runStatus,
'ErrorStatus' => 'U',
'Critical' => 0,
'Error' => 0,
'Warning' => 0,
'Logs' => array(),
);

$logs= $this->getFifoLogs($start);
if ($logs !== FALSE) {
$status['status']['Logs']= $logs;
$status['Logs']= $logs;
}

$result= array(
Expand All @@ -2867,20 +2850,20 @@ function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
);

foreach (array('warning', 'error', 'critical') as $prio) {
$status['status'][ucfirst($prio)]= $this->getRrdValue("derive-$prio.rrd", $start, $result[$prio]);
$status[ucfirst($prio)]= $this->getRrdValue("derive-$prio.rrd", $start, $result[$prio]);
}

if ($status['status']['Critical']) {
$status['status']['ErrorStatus']= 'C';
if ($status['Critical']) {
$status['ErrorStatus']= 'C';
}
else if ($status['status']['Error']) {
$status['status']['ErrorStatus']= 'E';
else if ($status['Error']) {
$status['ErrorStatus']= 'E';
}
else if ($status['status']['Warning']) {
$status['status']['ErrorStatus']= 'W';
else if ($status['Warning']) {
$status['ErrorStatus']= 'W';
}
else if ($result['critical'] && $result['error'] && $result['warning']) {
$status['status']['ErrorStatus']= 'N';
$status['ErrorStatus']= 'N';
}

if ($do_cache) {
Expand All @@ -2891,6 +2874,11 @@ function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
return $status;
}

function _getModuleInfo($start)
{
return FALSE;
}

function getFifoLogs($interval= 60)
{
$lastLogs= array();
Expand Down
13 changes: 5 additions & 8 deletions src/Model/openssh.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,12 @@ function ParseLogLine($logline, &$cols)
return FALSE;
}

function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
function _getModuleInfo($start)
{
$status= parent::_getModuleStatus($start, $generate_info, $do_cache);

if ($generate_info) {
$status['info']['accepted']= $this->getRrdValue('derive-accepted.rrd', $start, $result);
$status['info']['failed']= $this->getRrdValue('derive-failed.rrd', $start, $result);
}
return $status;
return array(
'accepted' => $this->getRrdValue('derive-accepted.rrd', $start, $result),
'failed' => $this->getRrdValue('derive-failed.rrd', $start, $result),
);
}

function _getFileLineCount($file, $re= '', $needle= '', $month='', $day='', $hour='', $minute='')
Expand Down
9 changes: 2 additions & 7 deletions src/Model/pf.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,11 @@ function Stop()
}
}

function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
function _getModuleInfo($start)
{
/// @todo Should we get more status info from pfctl -s info output?
// For example, the memory counter seems important: 'memory could not be allocated'
$status= parent::_getModuleStatus($start, $generate_info, $do_cache);

if ($generate_info) {
$status['info']['states']= $this->_getStateCount();
}
return $status;
return array('states' => $this->_getStateCount());
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/Model/snort.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,9 @@ function IsInstanceRunning($if)
return FALSE;
}

function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
function _getModuleInfo($start)
{
$status= parent::_getModuleStatus($start, $generate_info, $do_cache);

if ($generate_info) {
$status['info']['alerts']= $this->getRrdValue('derive-alert.rrd', $start, $result);
}
return $status;
return array('alerts' => $this->getRrdValue('derive-alert.rrd', $start, $result));
}

function SetConfig($confname)
Expand Down
13 changes: 5 additions & 8 deletions src/Model/sslproxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,12 @@ function Stop()
return $this->Kill();
}

function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
function _getModuleInfo($start)
{
$status= parent::_getModuleStatus($start, $generate_info, $do_cache);

if ($generate_info) {
$status['info']['conns']= $this->getRrdValue('derive-mld.rrd', $start, $result);
$status['info']['fds']= $this->getRrdValue('gauge-mfd.rrd', $start, $result, 'gauge');
}
return $status;
return array(
'conns' => $this->getRrdValue('derive-mld.rrd', $start, $result),
'fds' => $this->getRrdValue('gauge-mfd.rrd', $start, $result, 'gauge'),
);
}

/**
Expand Down
39 changes: 19 additions & 20 deletions src/Model/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,31 +714,30 @@ function GetConfig()
return Output(json_encode($config));
}

function _getModuleStatus($start, $generate_info= FALSE, $do_cache= TRUE)
function _getModuleInfo($start)
{
$status= parent::_getModuleStatus($start, $generate_info, $do_cache);
$info= array();

if ($generate_info) {
$uptime= $this->RunShellCommand('/usr/bin/uptime');
if (preg_match('/up (.*), (\d+) user.*/', $uptime, $match)) {
$status['info']['uptime']= $match[1];
$status['info']['users']= $match[2];
} else {
$status['info']['uptime']= '';
$status['info']['users']= '';
}
$uptime= $this->RunShellCommand('/usr/bin/uptime');
if (preg_match('/up (.*), (\d+) user.*/', $uptime, $match)) {
$info['uptime']= $match[1];
$info['users']= $match[2];
} else {
$info['uptime']= '';
$info['users']= '';
}

/// @attention Don't call $this->_getPartitions() instead, it needs an explode()
exec('/bin/df -h', $parts);
$partitions= array();
foreach ($parts as $p) {
if (preg_match('/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/', $p, $match)) {
$partitions[$match[6]]= $match[5];
}
/// @attention Don't call $this->_getPartitions() instead, it needs an explode()
exec('/bin/df -h', $parts);
$partitions= array();
foreach ($parts as $p) {
if (preg_match('/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/', $p, $match)) {
$partitions[$match[6]]= $match[5];
}
$status['info']['partitions']= $partitions;
}
return $status;
$info['partitions']= $partitions;

return $info;
}

/**
Expand Down

0 comments on commit 9ffa414

Please sign in to comment.