Skip to content

Commit

Permalink
Fixed net settings
Browse files Browse the repository at this point in the history
  • Loading branch information
meklis committed Dec 24, 2024
1 parent d01d627 commit 4fb6f7f
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 39 deletions.
7 changes: 6 additions & 1 deletion configs/models/Dell-switches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ models:
multi_console_command: \SwitcherCore\Modules\DellSwitch\MultiRawConsoleCommand
lldp_info: \SwitcherCore\Modules\DellSwitch\LldpInfo
net_settings: \SwitcherCore\Modules\General\NetSettings
arps: \SwitcherCore\Modules\DellSwitch\ArpInfo

- name: Dell Networking OS
key: dell_networking_os
Expand Down Expand Up @@ -61,10 +62,11 @@ models:
multi_console_command: \SwitcherCore\Modules\DellSwitch\MultiRawConsoleCommand
lldp_info: \SwitcherCore\Modules\DellSwitch\LldpInfo
net_settings: \SwitcherCore\Modules\General\NetSettings
arps: \SwitcherCore\Modules\DellSwitch\ArpInfo

- name: Dell EMC Networking OS10
key: dell_emc_networking_os
device_type: SWITCH
device_type: L3
inputs:
- snmp
- console
Expand Down Expand Up @@ -92,3 +94,6 @@ models:
multi_console_command: \SwitcherCore\Modules\DellSwitch\MultiRawConsoleCommand
lldp_info: \SwitcherCore\Modules\DellSwitch\LldpInfo
net_settings: \SwitcherCore\Modules\General\NetSettings
arps: \SwitcherCore\Modules\DellSwitch\ArpInfo
bgp_info: \SwitcherCore\Modules\DellSwitch\BgpInfo
bgp_peers: \SwitcherCore\Modules\DellSwitch\BgpPeers
9 changes: 7 additions & 2 deletions configs/models/Juniper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ models:
link_info: \SwitcherCore\Modules\Juniper\LinkInfo
interface_counters: \SwitcherCore\Modules\Juniper\Counters
system: \SwitcherCore\Modules\Juniper\System
vlan_list: \SwitcherCore\Modules\Juniper\VlanList
vlans: \SwitcherCore\Modules\Juniper\VlanList
errors: \SwitcherCore\Modules\Juniper\Errors
rmon: \SwitcherCore\Modules\Juniper\Rmon
pvid: \SwitcherCore\Modules\Juniper\PvidDot1q
Expand All @@ -29,6 +29,8 @@ models:
lldp_info: \SwitcherCore\Modules\Juniper\LldpInfo
net_settings: \SwitcherCore\Modules\General\NetSettings
arps: \SwitcherCore\Modules\Juniper\ArpInfo
bgp_info: \SwitcherCore\Modules\Juniper\BgpInfo
bgp_peers: \SwitcherCore\Modules\Juniper\BgpPeers

- name: Juniper JNP204 series
key: juniper_jnp204_series
Expand All @@ -46,7 +48,7 @@ models:
link_info: \SwitcherCore\Modules\Juniper\LinkInfo
interface_counters: \SwitcherCore\Modules\Juniper\Counters
system: \SwitcherCore\Modules\Juniper\System
vlan_list: \SwitcherCore\Modules\Juniper\VlanList
vlans: \SwitcherCore\Modules\Juniper\VlanList
errors: \SwitcherCore\Modules\Juniper\Errors
rmon: \SwitcherCore\Modules\Juniper\Rmon
pvid: \SwitcherCore\Modules\Juniper\PvidDot1q
Expand All @@ -59,5 +61,8 @@ models:
lldp_info: \SwitcherCore\Modules\Juniper\LldpInfo
net_settings: \SwitcherCore\Modules\General\NetSettings
arps: \SwitcherCore\Modules\Juniper\ArpInfo
bgp_info: \SwitcherCore\Modules\Juniper\BgpInfo
bgp_peers: \SwitcherCore\Modules\Juniper\BgpPeers



7 changes: 7 additions & 0 deletions configs/modules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,16 @@

- name: bgp_info
descr: BGP information
- name: bgp_peers
descr: BGP information
- name: bgp_peer_routes
descr: BGP information

- name: arps
descr: ARP information
arguments:
- {name: ip, pattern: '^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$', required: no}
- {name: vlan_id, pattern: '^[0-9]{1,4}$', required: no}


#Action Get arp info with filters
Expand Down
2 changes: 1 addition & 1 deletion configs/oids/global.oids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

- {name: ip.netSettings, oid: .1.3.6.1.2.1.4.20.1}
- {name: ip.arp.macAddr, oid: .1.3.6.1.2.1.4.22.1.2}
- {name: ip.arp.iface, oid: .1.3.6.1.2.1.4.22.1}
- {name: ip.arp.iface, oid: .1.3.6.1.2.1.4.22.1.3}

- {name: bgp.version, oid: .1.3.6.1.2.1.15.1.0}
- {name: bgp.localAs, oid: .1.3.6.1.2.1.15.2.0}
Expand Down
52 changes: 52 additions & 0 deletions src/Modules/DellSwitch/ArpInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace SwitcherCore\Modules\DellSwitch;

use SnmpWrapper\Oid;
use SwitcherCore\Modules\AbstractModule;
use SwitcherCore\Modules\Helper;

class ArpInfo extends AbstractModule
{
use InterfacesTrait;

public function run($params = [])
{
$oid = $this->oids->getOidByName('ip.arp.macAddr')->getOid();

$this->response = $this->formatResponse($this->snmp->walk([Oid::init($oid)]));
return $this;
}

public function getPretty()
{
if ($this->response['ip.arp.macAddr']->error()) {
throw new \SNMPException($this->response['ip.arp.macAddr']->error());
}
$vlanIds = $this->getVlanIdsMap();
$response = [];
foreach ($this->response['ip.arp.macAddr']->fetchAll() as $mac) {
$ip = Helper::getIndexByOid($mac->getOid(), 3) . "." .
Helper::getIndexByOid($mac->getOid(), 2) . "." .
Helper::getIndexByOid($mac->getOid(), 1) . "." .
Helper::getIndexByOid($mac->getOid())
;
$vlanIdIndex = Helper::getIndexByOid($mac->getOid(), 4);

$response[] = [
'mac' => $mac->getHexValue(),
'ip' => $ip,
'vlan_id' => isset($vlanIds[$vlanIdIndex]) ? $vlanIds[$vlanIdIndex] : null,
'interface' => null,
];
}

return $response;
}

public function getPrettyFiltered($filter = [])
{
return $this->getPretty();
}

}
44 changes: 44 additions & 0 deletions src/Modules/DellSwitch/BgpInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace SwitcherCore\Modules\DellSwitch;

use SnmpWrapper\Oid;
use SwitcherCore\Modules\AbstractModule;

class BgpInfo extends AbstractModule
{
public function run($params = [])
{
$oid = [
$this->oids->getOidByName('bgp.version'),
$this->oids->getOidByName('bgp.localAs'),
$this->oids->getOidByName('bgp.identifier'),
];

$this->response = $this->formatResponse($this->snmp->get($oid));

foreach ($this->response as $data) {
if($data->error()) {
throw new \SNMPException($data->error());
}
}

return $this;
}

public function getPretty()
{

return [
'version' => trim($this->getResponseByName('bgp.version')->fetchOne()->getValue()),
'local_as' => $this->getResponseByName('bgp.localAs')->fetchOne()->getValue(),
'identifier' => $this->getResponseByName('bgp.identifier')->fetchOne()->getValue(),
];
}

public function getPrettyFiltered($filter = [])
{
return $this->getPretty();
}

}
45 changes: 45 additions & 0 deletions src/Modules/DellSwitch/BgpPeers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace SwitcherCore\Modules\DellSwitch;

use SwitcherCore\Modules\AbstractModule;
use SwitcherCore\Modules\Helper;

class BgpPeers extends AbstractModule
{
public function run($params = [])
{
$oids = $this->oids->getOidsByRegex('^bgp\.peer\..*');
$this->response = $this->formatResponse($this->snmp->walk($oids));

foreach ($this->response as $data) {
if($data->error()) {
throw new \SNMPException($data->error());
}
}
return $this;
}

public function getPretty()
{
$bgpPeers = [];
foreach ($this->response as $key => $data) {
$name = str_replace("bgp.peer.", "", $key);
foreach ($data->fetchAll() as $d) {
$idx =
Helper::getIndexByOid($d->getOid(), 3) . "." .
Helper::getIndexByOid($d->getOid(), 2) . "." .
Helper::getIndexByOid($d->getOid(), 1) . "." .
Helper::getIndexByOid($d->getOid()) ;
$bgpPeers[$idx][$name] = $d->getParsedValue();
}
}
return array_values($bgpPeers);
}

public function getPrettyFiltered($filter = [])
{
return $this->getPretty();
}

}
32 changes: 31 additions & 1 deletion src/Modules/DellSwitch/InterfacesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ function parseInterface($iface, $parseBy = 'id')
}

private $_interfaces;
private $_vlans;

function getVlanIdsMap()
{
if ($this->_vlans) {
return $this->_vlans;
}
if ($info = $this->getCache('INTERFACES_VLANS', true)) {
$this->_vlans = $info;
return $info;
}
$this->fetchInfoFromDevice();
return $this->_vlans;
}


function getInterfacesIds()
{
Expand All @@ -84,6 +99,14 @@ function getInterfacesIds()
$this->_interfaces = $info;
return $info;
}
$this->fetchInfoFromDevice();
return $this->_interfaces;
}
function fetchInfoFromDevice()
{
$this->_interfaces = null;
$this->_vlans = null;

$response = $this->snmp->walk([
Oid::init($this->oids->getOidByName('if.Name')->getOid()),
Oid::init($this->oids->getOidByName('dot1q.PortIfIndex')->getOid()),
Expand Down Expand Up @@ -152,10 +175,17 @@ function ($i) {
}
}

$vlans = [];
foreach ($responses['if.Name'] as $r) {
if (preg_match('/^Vlan ([0-9]{1,5})$/', trim($r->getValue()), $m)) {
$vlans[Helper::getIndexByOid($r->getOid())] = (int)$m[1];
}
}

$this->_interfaces = $ifaces;
$this->setCache("INTERFACES", $ifaces, 600, true);
return $ifaces;
$this->_vlans = $vlans;
$this->setCache("INTERFACES_VLANS", $vlans, 600, true);
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/Modules/General/NetSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ protected function formate() {
}
$response['_arps'] = array_values($arps);
if(count($arps) > 0) {
$response['gateway_ip'] = $response['_arps'][0]['ip'];
$response['gateway_mac_address'] = $response['_arps'][0]['mac_address'];
foreach ($arps as $ip => $arp) {
if($arp['mac_address'] === "FF:FF:FF:FF:FF:FF") {
continue;
}
if($arp['mac_address'] === $response['self_mac_address']) {
continue;
}
$response['gateway_ip'] = $arp['ip'];
$response['gateway_mac_address'] = $arp['mac_address'];
}
}
return $response;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Modules/Juniper/ArpInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SwitcherCore\Modules\Juniper;

use SnmpWrapper\Oid;
use SwitcherCore\Modules\AbstractModule;
use SwitcherCore\Modules\Helper;

Expand All @@ -11,8 +12,9 @@ class ArpInfo extends AbstractModule

public function run($params = [])
{
$oid = $this->oids->getOidByName('ip.arp.macAddr');
$this->response = $this->formatResponse($this->snmp->walk([$oid]));
$oid = $this->oids->getOidByName('ip.arp.macAddr')->getOid();

$this->response = $this->formatResponse($this->snmp->walk([Oid::init($oid)]));
return $this;
}

Expand Down
26 changes: 23 additions & 3 deletions src/Modules/Juniper/BgpInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,43 @@

namespace SwitcherCore\Modules\Juniper;

use SnmpWrapper\Oid;
use SwitcherCore\Modules\AbstractModule;

class BgpInfo extends AbstractModule
{
public function run($params = [])
{
// TODO: Implement run() method.
$oid = [
$this->oids->getOidByName('bgp.version'),
$this->oids->getOidByName('bgp.localAs'),
$this->oids->getOidByName('bgp.identifier'),
];

$this->response = $this->formatResponse($this->snmp->get($oid));

foreach ($this->response as $data) {
if($data->error()) {
throw new \SNMPException($data->error());
}
}

return $this;
}

public function getPretty()
{
// TODO: Implement getPretty() method.

return [
'version' => trim($this->getResponseByName('bgp.version')->fetchOne()->getValue()),
'local_as' => $this->getResponseByName('bgp.localAs')->fetchOne()->getValue(),
'identifier' => $this->getResponseByName('bgp.identifier')->fetchOne()->getValue(),
];
}

public function getPrettyFiltered($filter = [])
{
// TODO: Implement getPrettyFiltered() method.
return $this->getPretty();
}

}
Loading

0 comments on commit 4fb6f7f

Please sign in to comment.