Capture all

Today, Aaron L. shares the tale of an innocent little network mapping program that killed itself with its own thoroughness:

I was hired to take over development on a network topology mapper that came from an acquisition. The product did not work except in small test environments. Every customer demo was a failure.

The code below was used to determine if two ports on two different switches are connected. This process was repeated for every switch in the network. As the number of switches, ports, and MAC addresses increased the run time of the product went up exponentially and typically crashed with an array index out of bounds exception. The code below is neatly presented, the actual code took me over a day of repeatedly saying "WTF?" before I realized the original programmer had no idea what a Map or Set or List was. But after eliminating the arrays the flawed matching algorithm was still there and so shortly all of the acquired code was thrown away and the mapper was re-written from scratch with more efficient ways of connecting switches.


public class Switch {
    Array[] allMACs = new Array[numMACs];
    Array[] portIndexes = new Array[numPorts];
    Array[] ports = new Array[numPorts];

    public void load() {
        // load allMACs by reading switch via SNMP
        // psuedo code to avoid lots of irrelevant SNMP code
        int portCounter = 0;
        int macCounter = 0;
        for each port {
            ports[portCounter] = port;
            portIndexes[portCounter] = macCounter;
            for each MAC on port {
                 allMACs[macCounter++] = MAC;
            }
        }
    }

    public Array[] getMACsForPort(int port) {
        int startIndex;
        int endIndex;
        for (int ictr = 0; ictr < ports.length;="" ictr++)="" {="" if="" (ports[ictr]="=" port)="" {="" startindex="portIndexes[ictr];" endindex="portIndexes[ictr" +="" 1];="" }="" }="" array[]="" portmacs="new" array[endindex="" -="" startindex];="" int="" pctr="0;" for="" (int="" ictr="startIndex;" ictr="">< endindex="" -="" 1;="" ictr++)="" {="" portmacs[pctr++]="allMACs[ictr];" }="" return(portmacs);="" }="" }="" ...="" for="" every="" switch="" in="" the="" network="" {="" for="" every="" other="" switch="" in="" the="" network="" {="" for="" every="" port="" on="" switch="" {="" array[]="" switchportmacs="Switch.getMACsForPort(port);" for="" every="" port="" on="" other="" switch="" {="" array[]="" otherswitchportmacs="OtherSwitch.getMACsForPort(other" port);="" if="" (intersect="" switchportmacs="" with="" otherswitchportmacs="=" true)="" {="" connect="" switch.port="" with="" otherswitch.port;="" }="" }="" }="" }="" }="">
[Advertisement] Onsite, remote, bare-metal or cloud – create, configure and orchestrate 1,000s of servers, all from the same dashboard while continually monitoring for drift and allowing for instantaneous remediation. Download Otter today!