From 4fb6f7fb4157ddfc022f520817c80af74745c209 Mon Sep 17 00:00:00 2001 From: meklis Date: Tue, 24 Dec 2024 11:15:21 +0200 Subject: [PATCH] Fixed net settings --- configs/models/Dell-switches.yml | 7 ++- configs/models/Juniper.yml | 9 +++- configs/modules.yml | 7 +++ configs/oids/global.oids.yml | 2 +- src/Modules/DellSwitch/ArpInfo.php | 52 ++++++++++++++++++++++ src/Modules/DellSwitch/BgpInfo.php | 44 ++++++++++++++++++ src/Modules/DellSwitch/BgpPeers.php | 45 +++++++++++++++++++ src/Modules/DellSwitch/InterfacesTrait.php | 32 ++++++++++++- src/Modules/General/NetSettings.php | 12 ++++- src/Modules/Juniper/ArpInfo.php | 6 ++- src/Modules/Juniper/BgpInfo.php | 26 +++++++++-- src/Modules/Juniper/BgpPeerRoutes.php | 24 ---------- src/Modules/Juniper/BgpPeers.php | 28 ++++++++++-- 13 files changed, 255 insertions(+), 39 deletions(-) create mode 100644 src/Modules/DellSwitch/ArpInfo.php create mode 100644 src/Modules/DellSwitch/BgpInfo.php create mode 100644 src/Modules/DellSwitch/BgpPeers.php delete mode 100644 src/Modules/Juniper/BgpPeerRoutes.php diff --git a/configs/models/Dell-switches.yml b/configs/models/Dell-switches.yml index 81c25949..b2be5dae 100644 --- a/configs/models/Dell-switches.yml +++ b/configs/models/Dell-switches.yml @@ -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 @@ -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 @@ -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 diff --git a/configs/models/Juniper.yml b/configs/models/Juniper.yml index ad2ff8b0..9157dad6 100644 --- a/configs/models/Juniper.yml +++ b/configs/models/Juniper.yml @@ -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 @@ -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 @@ -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 @@ -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 + diff --git a/configs/modules.yml b/configs/modules.yml index 492aa91b..44488855 100755 --- a/configs/modules.yml +++ b/configs/modules.yml @@ -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 diff --git a/configs/oids/global.oids.yml b/configs/oids/global.oids.yml index 8e51bcdc..d23f82b4 100755 --- a/configs/oids/global.oids.yml +++ b/configs/oids/global.oids.yml @@ -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} diff --git a/src/Modules/DellSwitch/ArpInfo.php b/src/Modules/DellSwitch/ArpInfo.php new file mode 100644 index 00000000..ed163230 --- /dev/null +++ b/src/Modules/DellSwitch/ArpInfo.php @@ -0,0 +1,52 @@ +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(); + } + +} \ No newline at end of file diff --git a/src/Modules/DellSwitch/BgpInfo.php b/src/Modules/DellSwitch/BgpInfo.php new file mode 100644 index 00000000..3309b6e6 --- /dev/null +++ b/src/Modules/DellSwitch/BgpInfo.php @@ -0,0 +1,44 @@ +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(); + } + +} \ No newline at end of file diff --git a/src/Modules/DellSwitch/BgpPeers.php b/src/Modules/DellSwitch/BgpPeers.php new file mode 100644 index 00000000..cbf4f507 --- /dev/null +++ b/src/Modules/DellSwitch/BgpPeers.php @@ -0,0 +1,45 @@ +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(); + } + +} \ No newline at end of file diff --git a/src/Modules/DellSwitch/InterfacesTrait.php b/src/Modules/DellSwitch/InterfacesTrait.php index 96b3ec80..f6ce3385 100644 --- a/src/Modules/DellSwitch/InterfacesTrait.php +++ b/src/Modules/DellSwitch/InterfacesTrait.php @@ -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() { @@ -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()), @@ -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); } /** diff --git a/src/Modules/General/NetSettings.php b/src/Modules/General/NetSettings.php index 67bbe511..78a9ce22 100644 --- a/src/Modules/General/NetSettings.php +++ b/src/Modules/General/NetSettings.php @@ -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; } diff --git a/src/Modules/Juniper/ArpInfo.php b/src/Modules/Juniper/ArpInfo.php index d8c2010b..037951ec 100644 --- a/src/Modules/Juniper/ArpInfo.php +++ b/src/Modules/Juniper/ArpInfo.php @@ -2,6 +2,7 @@ namespace SwitcherCore\Modules\Juniper; +use SnmpWrapper\Oid; use SwitcherCore\Modules\AbstractModule; use SwitcherCore\Modules\Helper; @@ -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; } diff --git a/src/Modules/Juniper/BgpInfo.php b/src/Modules/Juniper/BgpInfo.php index 21973da6..ab4dcebb 100644 --- a/src/Modules/Juniper/BgpInfo.php +++ b/src/Modules/Juniper/BgpInfo.php @@ -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(); } } \ No newline at end of file diff --git a/src/Modules/Juniper/BgpPeerRoutes.php b/src/Modules/Juniper/BgpPeerRoutes.php deleted file mode 100644 index c814b696..00000000 --- a/src/Modules/Juniper/BgpPeerRoutes.php +++ /dev/null @@ -1,24 +0,0 @@ -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() { - // TODO: Implement getPretty() method. + $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 = []) { - // TODO: Implement getPrettyFiltered() method. + return $this->getPretty(); } } \ No newline at end of file