JunOS discovery error line87 - foreach invalid argument


I was tracking something down on the JunOS discovery module and noticed the following on v1.55:

SQL[INSERT IGNORE INTO `vlans` (`device_id`,`vlan_domain`,`vlan_vlan`,`vlan_name`,`vlan_type`)  VALUES (:device_id,:vlan_domain,:vlan_vlan,:vlan_name,NULL) {"device_id":12,"vlan_domain":"1","vlan_vlan":"0","vlan_name":"____juniper_private1____"} 1ms]

Warning: Invalid argument supplied for foreach() in /opt/librenms/includes/discovery/vlans/junos.inc.php on line 87

>> Runtime for discovery module 'vlans': 0.4520 seconds with 4456 bytes

Seems like a quick fix for this is to wrap that little section with a check, and maybe some extra logging for debugging

        if( isset($tagness_by_vlan_index[$vlan_index]) )
            echo "JunOS: Vlan ID: $vlan_id - index: $vlan_index\n";
            foreach ($tagness_by_vlan_index[$vlan_index] as $ifIndex => $tag)
                //$tagArray = print_r($tag, true);
                //echo "JunOS: port-ifIndex $ifIndex has tags [" .
                //     str_replace("\n", "", $tagArray) . "] on it\n";

                if( $tag['tag'] == 0 )
                        echo "JunOS: port-ifIndex $ifIndex is a trunk\n";
                } else {
                        echo "JunOS: port-ifIndex $ifIndex is an access port\n";
                $per_vlan_data[$vlan_id][$ifIndex]['untagged'] = $tag['tag'];
        } else {
            echo "JunOS: Did not detect any ports associated with" .
                 " vlanID: $vlan_id - Index $vlan_index\n";

We noticed this issue when polling an MX10k, seems ok on the QFX 5k platform. Not sure if the above is really the right solution but seems to work alright in our testing.

How to reproduce:
./discovery.php -h router.domain -d -m vlans
(then look for error)

Is this something worth submitting as a pull request?

I would say make indeed a PR out of it.

I’ve created https://github.com/librenms/librenms/pull/10599. Thanks Elias