/** * Update switch's details using SNMP polling * * @throws \OSS_SNMP\Exception * @see self::$OSS_SNMP_SWITCH_ELEMENTS * * @param \OSS_SNMP\SNMP $host An instance of \OSS_SNMP\SNMP for this switch * @param \OSS_Logger $logger An instance of the logger or false * @return \Entities\Switcher For fluent interfaces */ public function snmpPoll($host, $logger = false) { // utility to format dates $formatDate = function ($d) { return $d instanceof \DateTime ? $d->format('Y-m-d H:i:s') : 'Unknown'; }; foreach (self::$OSS_SNMP_SWITCH_ELEMENTS as $p) { $fn = "get{$p}"; $n = $host->getPlatform()->{$fn}(); if ($logger) { switch ($p) { case 'OsDate': if ($formatDate($this->{$fn}()) != $formatDate($n)) { $logger->info(" [{$this->getName()}] Platform: Updating {$p} from " . $formatDate($this->{$fn}()) . " to " . $formatDate($n)); } else { $logger->info(" [{$this->getName()}] Platform: Found {$p}: " . $formatDate($n)); } break; default: if ($logger && $this->{$fn}() != $n) { $logger->info(" [{$this->getName()}] Platform: Updating {$p} from {$this->{$fn}()} to {$n}"); } else { $logger->info(" [{$this->getName()}] Platform: Found {$p}: {$n}"); } break; } } $fn = "set{$p}"; $this->{$fn}($n); } // does this switch support the IANA MAU MIB? try { $host->useMAU()->types(); $this->setMauSupported(true); } catch (\OSS_SNMP\Exception $e) { $this->setMauSupported(false); } $this->setLastPolled(new \DateTime()); return $this; }
/** * Update switch port details from a SNMP poll of the device. * * Pass an instance of OSS_Logger if you want logging enabled. * * @link https://github.com/opensolutions/OSS_SNMP * * @throws \OSS_SNMP\Exception * * @param \OSS_SNMP\SNMP $host An instance of the SNMP host object * @param \OSS_Logger $logger An instance of the logger or false * @return \Entities\SwitchPort For fluent interfaces */ public function snmpUpdate($host, $logger = false) { foreach (self::$OSS_SNMP_MAP as $snmp => $entity) { $fn = "get{$entity}"; switch ($snmp) { case 'lastChanges': $n = $host->useIface()->{$snmp}(true)[$this->getIfIndex()]; // need to allow for small changes due to rounding errors if ($logger && $this->{$fn}() != $n && abs($this->{$fn}() - $n) > 60) { $logger->info("[{$this->getSwitcher()->getName()}]:{$this->getName()} [Index: {$this->getIfIndex()}] Updating {$entity} from [{$this->{$fn}()}] to [{$n}]"); } break; default: $n = $host->useIface()->{$snmp}()[$this->getIfIndex()]; if ($logger && $this->{$fn}() != $n) { $logger->info("[{$this->getSwitcher()->getName()}]:{$this->getName()} [Index: {$this->getIfIndex()}] Updating {$entity} from [{$this->{$fn}()}] to [{$n}]"); } break; } $fn = "set{$entity}"; $this->{$fn}($n); } if ($this->getSwitcher()->getMauSupported()) { foreach (self::$OSS_SNMP_MAU_MAP as $snmp => $entity) { $getfn = "get{$entity['fn']}"; $setfn = "set{$entity['fn']}"; try { if (isset($entity['xlate'])) { $n = $host->useMAU()->{$snmp}($entity['xlate'])[$this->getIfIndex()]; } else { $n = $host->useMAU()->{$snmp}()[$this->getIfIndex()]; } } catch (\OSS_SNMP\Exception $e) { // looks like the switch supports MAU but not all of the MIBs $logger->debug("[{$this->getSwitcher()->getName()}]:{$this->getName()} [Index: {$this->getIfIndex()}] MAU MIB for {$fn} not supported"); $n = null; } if ($n == '*** UNKNOWN ***' && $snmp == 'types') { $n = '(empty)'; } if ($logger && $this->{$getfn}() != $n) { $logger->info("[{$this->getSwitcher()->getName()}]:{$this->getName()} [Index: {$this->getIfIndex()}] Updating {$entity['fn']} from [{$this->{$getfn}()}] to [{$n}]"); } $this->{$setfn}($n); } } try { // not all switches support this // FIXME is there a vendor agnostic way of doing this? // are we a LAG port? $isAggregatePorts = $host->useLAG()->isAggregatePorts(); if (isset($isAggregatePorts[$this->getIfIndex()]) && $isAggregatePorts[$this->getIfIndex()]) { $this->setLagIfIndex($host->useLAG()->portAttachedIds()[$this->getIfIndex()]); } else { $this->setLagIfIndex(null); } } catch (\OSS_SNMP\Exception $e) { } $this->setLastSnmpPoll(new \DateTime()); return $this; }