Net Interfaces methods in Agent/Module updates in NetworkLink

This commit is contained in:
fbsanchez 2020-11-13 16:07:59 +01:00
parent d08aa0b32b
commit 916537ea11
3 changed files with 298 additions and 45 deletions

View File

@ -410,13 +410,81 @@ class Agent extends Entity
} }
/**
* Return a list of interfaces.
*
* @param array $filter Filter interfaces by name in array.
*
* @return array Of interfaces and modules PandoraFMS\Modules.
*/
public function getInterfaces(array $filter=[])
{
$modules = $this->searchModules(
['nombre' => '%ifOperStatus%']
);
$interfaces = [];
foreach ($modules as $module) {
$matches = [];
if (preg_match(
'/^(.*?)_ifOperStatus$/',
$module->name(),
$matches
) > 0
) {
$interface = $matches[1];
}
if (empty($interface) === true) {
continue;
}
if (empty($filter) === false
&& in_array($interface, $filter) !== true
) {
continue;
}
$interfaces[$interface] = [
'ifOperStatus' => array_shift(
$this->searchModules(
['nombre' => $interface.'_ifOperStatus']
)
),
'ifInOctets' => array_shift(
$this->searchModules(
['nombre' => $interface.'_ifInOctets']
)
),
'ifOutOctets' => array_shift(
$this->searchModules(
['nombre' => $interface.'_ifOutOctets']
)
),
'ifHCInOctets' => array_shift(
$this->searchModules(
['nombre' => $interface.'_ifHCInOctets']
)
),
'ifHCOutOctets' => array_shift(
$this->searchModules(
['nombre' => $interface.'_ifHCOutOctets']
)
),
];
}
return $interfaces;
}
/** /**
* Search for modules into this agent. * Search for modules into this agent.
* *
* @param array $filter Filters. * @param array $filter Filters.
* @param integer $limit Limit search results. * @param integer $limit Limit search results.
* *
* @return PandoraFMS\Module Module found. * @return array Of PandoraFMS\Module Modules found.
*/ */
public function searchModules(array $filter, int $limit=0) public function searchModules(array $filter, int $limit=0)
{ {
@ -443,7 +511,12 @@ class Agent extends Entity
return $results; return $results;
} else { } else {
// Search in db. // Search in db.
return Module::search($filter, $limit); $return = Module::search($filter, $limit);
if (is_array($return) === false) {
return [];
}
return $return;
} }
} }

View File

@ -39,6 +39,12 @@ use PandoraFMS\ModuleType;
class Module extends Entity class Module extends Entity
{ {
const INTERFACE_STATUS = 1;
const INTERFACE_INOCTETS = 2;
const INTERFACE_OUTOCTETS = 3;
const INTERFACE_HC_INOCTETS = 4;
const INTERFACE_HC_OUTOCTETS = 5;
/** /**
* Module status (From tagente_estado). * Module status (From tagente_estado).
* *
@ -751,6 +757,84 @@ class Module extends Entity
} }
/**
* Return true if module represents an interface (operStatus, in/outOctets)
*
* @return integer > 0 if interface module, 0 if not.
*/
public function isInterfaceModule():int
{
if (strstr($this->name(), '_ifOperStatus') !== false) {
return self::INTERFACE_STATUS;
}
if (strstr($this->name(), '_ifInOctets') !== false) {
return self::INTERFACE_INOCTETS;
}
if (strstr($this->name(), '_ifOutOctets') !== false) {
return self::INTERFACE_OUTOCTETS;
}
if (strstr($this->name(), '_ifHCInOctets') !== false) {
return self::INTERFACE_HC_INOCTETS;
}
if (strstr($this->name(), '_ifHCOutOctets') !== false) {
return self::INTERFACE_HC_OUTOCTETS;
}
return 0;
}
/**
* Return interface name if module represents an interface module.
*
* @return string|null Interface name or null.
*/
public function getInterfaceName():?string
{
$label = null;
switch ($this->isInterfaceModule()) {
case self::INTERFACE_STATUS:
$label = '_ifOperStatus';
break;
case self::INTERFACE_INOCTETS:
$label = '_ifInOctets';
break;
case self::INTERFACE_OUTOCTETS:
$label = '_ifOutOctets';
break;
case self::INTERFACE_HC_INOCTETS:
$label = '_ifHCInOctets';
break;
case self::INTERFACE_HC_OUTOCTETS:
$label = '_ifHCOutOctets';
break;
default:
// Not an interface module.
return null;
}
if (preg_match(
'/^(.*?)'.$label.'$/',
$this->name(),
$matches
) > 0
) {
return $matches[1];
}
return null;
}
/** /**
* Transforms configuration data into an array. * Transforms configuration data into an array.
* *

View File

@ -396,7 +396,33 @@ final class NetworkLink extends Model
$endX = $data['endX']; $endX = $data['endX'];
$endY = $data['endY']; $endY = $data['endY'];
if (isset($data['width'])) { $linked_start = static::extractlinkedStart($data);
$linked_end = static::extractlinkedEnd($data);
$start = false;
$end = false;
if ($linked_start !== null) {
$start = \db_get_row_filter(
'tlayout_data',
[
'id_layout' => $data['id_layout'],
'id' => $linked_start,
]
);
}
if ($linked_end !== null) {
$end = \db_get_row_filter(
'tlayout_data',
[
'id_layout' => $data['id_layout'],
'id' => $linked_end,
]
);
}
if (isset($data['width']) === true) {
// Row format. // Row format.
$startX = $data['pos_x']; $startX = $data['pos_x'];
$startY = $data['pos_y']; $startY = $data['pos_y'];
@ -404,47 +430,51 @@ final class NetworkLink extends Model
$endY = $data['height']; $endY = $data['height'];
} }
$start = \db_get_row_filter( if ($start === false) {
'tlayout_data', $start = \db_get_row_filter(
[ 'tlayout_data',
'id_layout' => $data['id_layout'], [
'pos_x' => '<'.$startX, 'id_layout' => $data['id_layout'],
'pos_y' => '<'.$startY, 'pos_x' => '<'.$startX,
'pos_x`+`width' => '>'.$startX, 'pos_y' => '<'.$startY,
'pos_y`+`height' => '>'.$startY, 'pos_x`+`width' => '>'.$startX,
'order' => [ 'pos_y`+`height' => '>'.$startY,
[ 'order' => [
'field' => 'show_on_top', [
'order' => 'desc', 'field' => 'show_on_top',
'order' => 'desc',
],
[
'field' => 'id',
'order' => 'desc',
],
], ],
[ ]
'field' => 'id', );
'order' => 'desc', }
],
],
]
);
$end = \db_get_row_filter( if ($end === false) {
'tlayout_data', $end = \db_get_row_filter(
[ 'tlayout_data',
'id_layout' => $data['id_layout'], [
'pos_x' => '<'.$endX, 'id_layout' => $data['id_layout'],
'pos_y' => '<'.$endY, 'pos_x' => '<'.$endX,
'pos_x`+`width' => '>'.$endX, 'pos_y' => '<'.$endY,
'pos_y`+`height' => '>'.$endY, 'pos_x`+`width' => '>'.$endX,
'order' => [ 'pos_y`+`height' => '>'.$endY,
[ 'order' => [
'field' => 'show_on_top', [
'order' => 'desc', 'field' => 'show_on_top',
'order' => 'desc',
],
[
'field' => 'id',
'order' => 'desc',
],
], ],
[ ]
'field' => 'id', );
'order' => 'desc', }
],
],
]
);
return [ return [
'start' => $start, 'start' => $start,
@ -469,6 +499,18 @@ final class NetworkLink extends Model
$linkedStart = null; $linkedStart = null;
$linkedEnd = null; $linkedEnd = null;
/*
* If start['id_agente_modulo'] its a network module (in/out/status)
* then:
*
* start => outOctets
*
* If end['id_agente_modulo'] its a network module (in/out/status)
* then:
* end => inOctets
*
*/
if (isset($links['start']) === true) { if (isset($links['start']) === true) {
$linkedStart = $links['start']['id']; $linkedStart = $links['start']['id'];
if (is_numeric($links['start']['id_agente_modulo']) === true if (is_numeric($links['start']['id_agente_modulo']) === true
@ -478,8 +520,35 @@ final class NetworkLink extends Model
(int) $links['start']['id_agente_modulo'] (int) $links['start']['id_agente_modulo']
); );
$labelStart = $module->nombre(); if ((bool) $module->isInterfaceModule() === true) {
$labelStart .= ': '.$module->lastValue(); $interface_name = $module->getInterfaceName();
$interface = array_shift(
$module->agent()->getInterfaces(
[$interface_name]
)
);
$outOctets = 0;
$inOctets = 0;
if (isset($interface['ifOutOctets']) === true) {
$outOctets = $interface['ifOutOctets']->lastValue();
} else if (isset($interface['ifHCOutOctets']) === true) {
$outOctets = $interface['ifHCOutOctets']->lastValue();
}
if (isset($interface['ifInOctets']) === true) {
$inOctets = $interface['ifInOctets']->lastValue();
} else if (isset($interface['ifHCInOctets']) === true) {
$inOctets = $interface['ifHCInOctets']->lastValue();
}
$labelStart = $interface_name;
$labelStart .= ': '.$outOctets;
$labelEnd = $interface_name;
$labelEnd .= ': '.$inOctets;
}
} }
} }
@ -492,8 +561,35 @@ final class NetworkLink extends Model
(int) $links['end']['id_agente_modulo'] (int) $links['end']['id_agente_modulo']
); );
$labelEnd = $module->nombre(); if ((bool) $module->isInterfaceModule() === true) {
$labelEnd .= ': '.$module->lastValue(); $interface_name = $module->getInterfaceName();
$interface = array_shift(
$module->agent()->getInterfaces(
[$interface_name]
)
);
$outOctets = 0;
$inOctets = 0;
if (isset($interface['ifOutOctets']) === true) {
$outOctets = $interface['ifOutOctets']->lastValue();
} else if (isset($interface['ifHCOutOctets']) === true) {
$outOctets = $interface['ifHCOutOctets']->lastValue();
}
if (isset($interface['ifInOctets']) === true) {
$inOctets = $interface['ifInOctets']->lastValue();
} else if (isset($interface['ifHCInOctets']) === true) {
$inOctets = $interface['ifHCInOctets']->lastValue();
}
$labelStart .= '<br>'.$interface_name;
$labelStart .= ': '.$outOctets;
$labelEnd .= '<br>'.$interface_name;
$labelEnd .= ': '.$inOctets;
}
} }
} }