Closed skydiablo closed 6 years ago
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.
if have some issues with encoded keys:
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:how can i set this output filter with this lib to get the key in right way?