Flow/Scripting/UsefulFunctions/InterfaceErrorsScript

From Observer GigaFlow Support | VIAVI Solutions Inc.
Jump to: navigation, search

log.warn("Policy to check for errors on an interface") var rosutils = Java.type('ros.CROSUtils'); //==================================================================== // // Policy to check for errors on an interface // List of devices to be checked contained in the devices array // List of Interfaces to be ignored in the ignoreList array // // Updated Feb 2015 by Shane Conneely // //===================================================================

var mail = true; var sending = 0; var mailText = ; var filename = './resources/webapps/static/iferrors.html'; var w = new java.io.FileWriter(filename, false); var devices = ["83.245.74.6", "10.100.4.1", "10.100.4.2", "10.100.4.5", "10.1.4.1", "10.2.4.1", "10.3.4.1", "10.4.4.1", "10.6.4.1", "10.7.4.1", "10.8.4.1", "10.9.4.1", "10.10.4.1", "10.11.4.1", "10.12.4.1", "10.13.4.1", "10.14.4.1", "10.15.4.1", "10.16.4.1", "88.151.80.179", "83.70.199.180"]; var ignoreList = ["10.2.4.1:10013", "10.9.4.1:10013", "88.151.80.178:2"]; //Add "Device:ifIndex" to this array and it will ignore that index when checking errors

var d = new Date(); var n = d.getHours() % 4; var dataStoreInErrors = actions.getDataStore('ifInErrors'); var dataStoreOutErrors = actions.getDataStore('ifOutErrors'); //if (n==0){ if (n != -1 || typeof dataStoreInErrors === 'undefined') {

   for (var i = 0; i < devices.length; i++) {
       var thisDevice = actions.getDevice(devices[i]);
       var dev_id = rosutils.inet_atob(devices[i]);
       log.warn('device # ' + i + ': IP: ' + devices[i] + " ID:" + dev_id);
       if (thisDevice != null && thisDevice != 'undefined' && thisDevice != "" && thisDevice.getSysOID().indexOf('1.3.6.1.4.1.9') == 0 && dev_id != 0) {
           var dev_name = thisDevice.getName();
           log.warn('IN BP1 for device ' + devices[i]);
           var uptime = actions.querySNMPNumber(thisDevice, '.1.3.6.1.2.1.1.3.0', -1);
           log.warn('IN BP1a - uptime = ' + uptime);
           if (uptime == null || uptime == -1) {
               log.warn('IN BP1b for  uptime, ' + devices[i] + ' Cant be contacted
'); //mailText+=new Date()+' '+kommon.utils.inet_itoa(thisDevice)+ ' Cant be contacted
'; } else { if (uptime < 360000) { log.warn('IN BP1b - uptime = ' + devices[i] + ' Has uptime of < 1 hour, up for:' + (uptime / (6000)) + ' Mins.'); //mailText+=new Date()+' '+kommon.utils.inet_itoa(thisDevice)+ ' Has uptime of < 1 hour, up for:'+(uptime/(6000))+' Mins.

'; } //else { // log.warn('IN BP1d - uptime = '+kommon.utils.inet_itoa(thisDevice)+ ' Has uptime of > 1 hour, up for:'+(uptime/(6000))+' Mins.'); // mailText+=new Date()+' '+kommon.utils.inet_itoa(thisDevice)+ ' Has uptime of > 1 hour, up for:'+(uptime/(6000))+' Mins.
'; // } checkInterfaceErrors(thisDevice, dev_name, dev_id); } //mailText+='


'; } else { log.warn('No such device # ' + i + ': IP: ' + devices[i] + " ID:" + dev_id); } } if (mailText.length > 0) { flog(mailText); // write to file if (mail == true) { log.warn("Sending mail" + mailText); actions.sendMail('ronan.ohart@fastcom.ie', 'IFErrorsLive', mailText); actions.sendMail('shane.conneely@fastcom.ie', 'IFErrorsLive', mailText); sending++; } // log.warn('interface errors email : '+mailText); } else { log.warn('NO PROCESSING DONE FOR DEVICE: ' + devices[i]); }

}

w.close();


// Write Interface Error results to file

function flog(message) {

   w.write(new Date() + '    ' + message);

}

log.warn('All in errors' + dataStoreInErrors); log.warn('All out errors ' + dataStoreOutErrors);


function checkInterfaceErrors(thisDevice, dev_name, dev_id) {

   //  OID  1.3.6.1.2.1.2.2.1.14 = interface in errors 
   //  OID 1.3.6.1.2.1.2.2.1.20 = interface out errors
   var snmpdata = actions.querySNMPTable(thisDevice, ['.1.3.6.1.2.1.2.2.1.14', '.1.3.6.1.2.1.2.2.1.20']);
   var _duplex = ;
   var spd_unit = ;
   var spd = ;
   var duplex = ;
   //log.warn(snmpdata);
   //log.warn( actions.getInterface(thisDevice, 1));
   for (var i = 0; i < snmpdata.size(); i++) {
       var anEntry = snmpdata.get(i).getColumns();
       var inErrors = 0;
       var outErrors = 0;
       //log.warn(anEntry);
       //var if_status = actions.getInterfaces(thisDevice);
       //log.warn('All interfaces for device '+thisDevice+'  '+if_status);
       log.warn("index:" + snmpdata.get(i).getIndex());
       var info1 = actions.getInterface(thisDevice, parseInt(snmpdata.get(i).getIndex()));
       //log.warn(info1);
       //var if_info2=actions.getInterfaceByKey(thisDevice,if_status[i]);
       log.warn('info1 index # ' + i + '  ' + info1);
       spd = info1.getSpeed();
       if (spd < 1000000000) {
           spd_unit = "Mbps";
           spd = (spd / 1000000);
       } else {
           spd_unit = 'Gbps';
           spd = (spd / 1000000000);
       }


       /*    switch (info1.get('ifDuplex')) {   // Test port duplex status.   1 = half, 2=full, 3=disagree, 4=auto
        case 1:
        duplex = "Unknown";
        break;
        case 2:
        duplex = "Half Duplex";
        break;
        case 3:
        duplex = "<b> color='green' Full Duplex</b>";
        break;
        case r:
        duplex = "<b>Auto";
        break;
        
        } */


       log.warn("ifinfo duplex:" + info1.getIfDuplexS() + " speed:" + spd + ' ' + spd_unit);
       //log.warn('if_info2 index # ' + i + '  ' + if_info2);



       if (anEntry[0] != null) {
           inErrors = anEntry[0].getVariable().toLong();
       }
       log.warn("1ifinfo duplex:" + info1.getIfDuplexS() + " speed:" + spd + ' ' + spd_unit);
       if (anEntry[1] != null) {
           outErrors = anEntry[1].getVariable().toLong();
       }
       if (inErrors != 0 || outErrors != 0) {
           checkPreviousErrorCount(thisDevice, parseInt(snmpdata.get(i).getIndex()), inErrors, outErrors, dev_name, dev_id);
           //mailText+='Interface: '+info1.get('ifName')+' ' +info1.get('ifAlias') +':  In Errors:'+inErrors+', Out Errors:'+outErrors+' Speed: '+spd+' '+spd_unit+'   '+'Duplex: '+info1.get('ifDuplex')+'
'; } }

}


function checkPreviousErrorCount(thisDevice, thisIndex, inErrors, outErrors, dev_name, dev_id) {

   var key = dev_id + ':' + thisIndex;


   if (inErrors != 0) {
       log.warn('          SNMPR:' + 'checking key:' + key + ' In:' + inErrors + ', Out:' + outErrors + ' Index:' + thisIndex + ' ' + actions.getInterface(thisDevice, thisIndex));
       if (isInArray(ignoreList, String(dev_id + ':' + thisIndex))) {
       } else {
           if ((inErrors - dataStoreInErrors.get(key)) >= 2) {
               if (dataStoreInErrors.containsKey(key)) {
                   log.warn('                      SNMPR E:' + key + ' In was found');                              // Had errors before
                   if (dataStoreInErrors.get(key) !== inErrors) {                       // Error Rate changed
                       mail = true;
                       mailText += 'Interface Alert for Device ' + thisDevice.getIP() + ' -> ' + dev_name + '</b>

';
                       mailText += 'Interface: ' + '<a href="http://77.95.160.35:8080/overview/device/interface/?difSelection=' + dev_id + '_' + thisIndex + '">' + actions.getInterface(thisDevice, thisIndex).getIfDescr() + ' - ' + actions.getInterface(thisDevice, thisIndex).getIfAlias() + '</a>' + '

';
                       mailText += 'Total error count: ' + numberWithCommas(inErrors) + ' : ' + 'Errors since last check: ' + numberWithCommas(inErrors - dataStoreInErrors.get(key)) + '

'; mailText += '

';
                       //mailText+='SNMPR E:'+'In was:'+dataStoreInErrors.get(key)+', now:'+inErrors+' diff:'+(inErrors - dataStoreInErrors.get(key))+' Index:'+thisIndex+' '+actions.getInterface(thisDevice,thisIndex)+'


'; log.warn(' SNMPR E:' + 'In was:' + dataStoreInErrors.get(key) + ', now:' + inErrors + ' diff:' + (inErrors - dataStoreInErrors.get(key)) + ' Index:' + thisIndex + ' ' + actions.getInterface(thisDevice, thisIndex)); } else { log.warn(' SNMPR E:' + 'No change In was:' + dataStoreInErrors.get(key) + ', now:' + inErrors + ' diff:' + (inErrors - dataStoreInErrors.get(key)) + ' Index:' + thisIndex + ' ' + actions.getInterface(thisDevice, thisIndex)); } } } }
       dataStoreInErrors.put(key, inErrors);
   } else if (dataStoreInErrors.containsKey(key)) {
       dataStoreInErrors.remove(key);
   } else {
   }
   if (outErrors !== 0) {
       if (isInArray(ignoreList, String(dev_id + ':' + thisIndex))) {
       } else {
           if ((outErrors - dataStoreInErrors.get(key)) >= 200) {
               if (dataStoreOutErrors.containsKey(key)) {
                   log.warn('                      SNMPR E:' + key + ' Out was found');    //Had errors before
                   if (dataStoreOutErrors.get(key) != outErrors) {                //Error Rate changed
                       mail = true;
                       mailText += '<b>Interface Alert for Device ' + thisDevice.getIP() + ' -> ' + dev_name + '</b>

';
                       mailText += 'Interface: ' + '<a href="http://77.95.160.35:8080/overview/device/interface/?difSelectiondifSelection=' + dev_id + '_' + thisIndex + '">' + actions.getInterface(thisDevice, thisIndex).get("ifDescr") + ' - ' + actions.getInterface(thisDevice, thisIndex).get("ifAlias") + '</a>' + '
'; mailText += 'Total error count: ' + numberWithCommas(outErrors) + ' : ' + 'Errors since last check: ' + numberWithCommas(outErrors - dataStoreOutErrors.get(key)) + '

';
                       //mailText+='SNMPR E:'+'Out was:'+dataStoreOutErrors.get(key)+', now:'+outErrors+' diff:'+(outErrors - dataStoreOutErrors.get(key))+' Index:'+thisIndex+' '+actions.getInterface(thisDevice,thisIndex)+'


'; log.warn(' SNMPR E:' + 'Out was:' + dataStoreOutErrors.get(key) + ', now:' + outErrors + ' diff:' + (outErrors - dataStoreOutErrors.get(key)) + ' Index:' + thisIndex + ' ' + actions.getInterface(thisDevice, thisIndex)); } else { log.warn(' SNMPR E:' + 'No change Out was:' + dataStoreOutErrors.get(key) + ', now:' + outErrors + ' diff:' + (outErrors - dataStoreOutErrors.get(key)) + ' Index:' + thisIndex + ' ' + actions.getInterface(thisDevice, thisIndex)); } } } }
       dataStoreOutErrors.put(key, outErrors);
   } else if (dataStoreOutErrors.containsKey(key)) {
       dataStoreOutErrors.remove(key);
   } else {
   }

}

function numberWithCommas(x) {

   return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

}

function isInArray(array, search) {

   return (array.indexOf(search) >= 0) ? true : false;

} log.warn("Policy to check for errors on an interface Done, send " + sending + " mails")