I am having issues with discovery item being created. I am using the discovery rule that is looking at a external script to find specific interface OID index numbers. The items are not getting created and I am not sure why. There are not errors in the logs or on the GUI. Please excuse the ignorance as this is my first attempt at creating a script. All help is appreciated.
I have created a file that contains the output of an snmpwalk ifDesc index...
I have then created a php script that outputs the results after filtering based on a regex....
################################
#!/usr/bin/php
<?php
function parse_snmp_data($line) {
$snmp_info = [];
if (preg_match_all('/\.(\d+) "?([^"\r\n]*)/', $line, $matches)) {
foreach ($matches[1] as $key => $match) {
$snmp_info['{#SNMPINDEX}'] = trim($match);
}
if (isset($matches[2]) && $matches[2]) {
$snmp_info['{#SNMPVALUE}'] = trim($matches[2][0]);
}
}
if (count($snmp_info) == 2) {
return $snmp_info;
}
}
$output = ['data' => []];
$handle = fopen("snmpwalk.txt", "r");
if ($handle) {
$i = 0;
while (($line = fgets($handle)) !== false) {
if (strstr(strtolower($line), 'pon') && !strstr(strtolower($line), 'ont')) {
$output['data'][$i] = parse_snmp_data($line);
$i++;
}
}
fclose($handle);
print json_encode($output);
} else {
echo "Error opening file";
}
################################################
When testing the php script from the CLI everything works fine...
[bash#]./parsesnmpwalkfile.discovery
{"data":[{"{#SNMPINDEX}":"1600001","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 16, Pon: 1"},{"{#SNMPINDEX}":"1600002","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 16, Pon: 2"},{"{#SNMPINDEX}":"1600003","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 16, Pon: 3"},{"{#SNMPINDEX}":"1600004","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 16, Pon: 4"},{"{#SNMPINDEX}":"1700001","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 1"},{"{#SNMPINDEX}":"1700002","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 2"},{"{#SNMPINDEX}":"1700003","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 3"},{"{#SNMPINDEX}":"1700004","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 4"},{"{#SNMPINDEX}":"1700005","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 5"},{"{#SNMPINDEX}":"1700006","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 6"},{"{#SNMPINDEX}":"1700007","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 7"},{"{#SNMPINDEX}":"1700008","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 8"},{"{#SNMPINDEX}":"1800001","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 1"},{"{#SNMPINDEX}":"1800002","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 2"},{"{#SNMPINDEX}":"1800003","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 3"},{"{#SNMPINDEX}":"1800004","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 4"},{"{#SNMPINDEX}":"1800005","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 5"},{"{#SNMPINDEX}":"1800006","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 6"},{"{#SNMPINDEX}":"1800007","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 7"},{"{#SNMPINDEX}":"1800008","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 8"},{"{#SNMPINDEX}":"2000001","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 20, Pon: 1"},{"{#SNMPINDEX}":"2000002","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 20, Pon: 2"},{"{#SNMPINDEX}":"2000003","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 20, Pon: 3"},{"{#SNMPINDEX}":"2000004","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 20, Pon: 4"}]}
I then created a Discovery Rule on the GUI with the fields...
NAME: Dis Test php snmp w/ regex
TYPE: External Check
KEY: parsesnmpwalkfile.discovery
UPDATE INTERVAL: 1m
I then created a Discovery Item under the Discovery Rule...
NAME: Interface Operational Status - {#SNMPVALUE}
TYPE: SNMPv2 agent
KEY: test.ifOperationalStats.[{#SNMPINDEX}]
SNMP OID: IF-MIB::ifOperStatus.{#SNMPINDEX}
SNMP COMMUNITY: {$SNMP_COMMUNITY}
PORT:
TYPE OF INFORMATION: Character
UPDATE INTERVAL: 1m
HSP: 90d
I have created a file that contains the output of an snmpwalk ifDesc index...
I have then created a php script that outputs the results after filtering based on a regex....
################################
#!/usr/bin/php
<?php
function parse_snmp_data($line) {
$snmp_info = [];
if (preg_match_all('/\.(\d+) "?([^"\r\n]*)/', $line, $matches)) {
foreach ($matches[1] as $key => $match) {
$snmp_info['{#SNMPINDEX}'] = trim($match);
}
if (isset($matches[2]) && $matches[2]) {
$snmp_info['{#SNMPVALUE}'] = trim($matches[2][0]);
}
}
if (count($snmp_info) == 2) {
return $snmp_info;
}
}
$output = ['data' => []];
$handle = fopen("snmpwalk.txt", "r");
if ($handle) {
$i = 0;
while (($line = fgets($handle)) !== false) {
if (strstr(strtolower($line), 'pon') && !strstr(strtolower($line), 'ont')) {
$output['data'][$i] = parse_snmp_data($line);
$i++;
}
}
fclose($handle);
print json_encode($output);
} else {
echo "Error opening file";
}
################################################
When testing the php script from the CLI everything works fine...
[bash#]./parsesnmpwalkfile.discovery
{"data":[{"{#SNMPINDEX}":"1600001","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 16, Pon: 1"},{"{#SNMPINDEX}":"1600002","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 16, Pon: 2"},{"{#SNMPINDEX}":"1600003","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 16, Pon: 3"},{"{#SNMPINDEX}":"1600004","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 16, Pon: 4"},{"{#SNMPINDEX}":"1700001","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 1"},{"{#SNMPINDEX}":"1700002","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 2"},{"{#SNMPINDEX}":"1700003","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 3"},{"{#SNMPINDEX}":"1700004","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 4"},{"{#SNMPINDEX}":"1700005","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 5"},{"{#SNMPINDEX}":"1700006","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 6"},{"{#SNMPINDEX}":"1700007","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 7"},{"{#SNMPINDEX}":"1700008","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 17, Pon: 8"},{"{#SNMPINDEX}":"1800001","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 1"},{"{#SNMPINDEX}":"1800002","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 2"},{"{#SNMPINDEX}":"1800003","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 3"},{"{#SNMPINDEX}":"1800004","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 4"},{"{#SNMPINDEX}":"1800005","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 5"},{"{#SNMPINDEX}":"1800006","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 6"},{"{#SNMPINDEX}":"1800007","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 7"},{"{#SNMPINDEX}":"1800008","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 18, Pon: 8"},{"{#SNMPINDEX}":"2000001","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 20, Pon: 1"},{"{#SNMPINDEX}":"2000002","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 20, Pon: 2"},{"{#SNMPINDEX}":"2000003","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 20, Pon: 3"},{"{#SNMPINDEX}":"2000004","{#SNMPVALUE}":"= STRING: Shelf: 1, Slot: 20, Pon: 4"}]}
I then created a Discovery Rule on the GUI with the fields...
NAME: Dis Test php snmp w/ regex
TYPE: External Check
KEY: parsesnmpwalkfile.discovery
UPDATE INTERVAL: 1m
I then created a Discovery Item under the Discovery Rule...
NAME: Interface Operational Status - {#SNMPVALUE}
TYPE: SNMPv2 agent
KEY: test.ifOperationalStats.[{#SNMPINDEX}]
SNMP OID: IF-MIB::ifOperStatus.{#SNMPINDEX}
SNMP COMMUNITY: {$SNMP_COMMUNITY}
PORT:
TYPE OF INFORMATION: Character
UPDATE INTERVAL: 1m
HSP: 90d
Comment