opensolutions/OSS_SNMP

encoded keys ?

Closed this issue · 3 comments

if have some issues with encoded keys:

RUCKUS-SZ-WLAN-MIB::ruckusSZAPMac.'.$y*..'.32 = STRING: XX:XX:XX:XX:XX:XX

as you can see, at the end of my key there is an encoded key-part .'.$y*..'., this is also shown with plain cli client, but if i add the output-filter -O b all keys are fine:

snmpwalk -O b -c yyyyyyy XXX.XXX.XXX.XXX .1.3.6.1.4.1.25053.1.4.2.1.1.2.2
...
RUCKUS-SZ-WLAN-MIB::ruckusSZAPMac.6.36.121.42.5.80.32 = STRING: XX:XX:XX:XX:XX:XX

how can i set this output filter with this lib to get the key in right way?

i know, i know... but please, i need help! push

ok, i did it ... just a workaround but it works. just extends the "snmp" class and hack this solution:

    public function setOidOutputFormat($f) {
        return parent::setOidOutputFormat($this->currentOidOutputFormat = $f);
    }

    public function getOidOutputFormat() {
        return $this->currentOidOutputFormat;
    }

    public function readMIB(string $mibFilename) {
        if (snmp_read_mib($mibFilename)) {
            $this->loadedMibs[] = $mibFilename;
        }
        return $this;
    }

    public function realWalk($oid, bool $reparseOids = false): array {

        $oldOidOutputFormat = null;

        if ($reparseOids) {
            $oldOidOutputFormat = $this->getOidOutputFormat();
            $this->setOidOutputFormat(self::OID_OUTPUT_NUMERIC);
        }

        $raw = (array) parent::realWalk($oid);

        if ($reparseOids) {
            $this->setOidOutputFormat($oldOidOutputFormat);

            $oids = array_keys($raw);
            $translatedOids = $this->translateOids($oids);

            $translatedRaw = [];

            foreach ($raw as $rawOid => $value) {
                $translatedRaw[$translatedOids[$rawOid] ?? $rawOid] = $value;
            }

            $raw = $translatedRaw;
        }

        return $raw;
    }

    public function translateOids(array $oids) {
        $oids = array_unique($oids);
        $oidsFilenames = implode(':', $this->loadedMibs);
        $cmd = sprintf('snmptranslate -Obf -m +%s %s', $oidsFilenames, implode(' ', $oids)); // TODO: handly dynamic OUTPUT filter!

        $process = new \Symfony\Component\Process\Process($cmd);
        $process->run();
        if ($process->isSuccessful()) {
            $out = $process->getOutput();
            $translatedOids = explode(PHP_EOL, $out);
            $translatedOids = array_filter($translatedOids);
            $translatedOidsCount = count($translatedOids);
            $oidsCount = count($oids);
            if ($translatedOidsCount === $oidsCount) {
                return array_combine($oids, $translatedOids);
            } else {
                throw new \LengthException(sprintf('Given OIDs count "%d" and returned OIDs count "%d" does not match!', $oidsCount, $translatedOidsCount));
            }
        } else {
            throw new ProcessFailedException($process);
        }
    }

If you want to put together a non-hacky pull request, I'd happily look at that.