SNMP Access Using Scripts

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


Querying a number (uptime in this case) from an infrastructure device

var ip="172.21.40.254" //Set Ip address
var thisDevice = actions.getDevice(ip); //Get managed device
if (thisDevice != null) { //Check if it exists in the system
    var uptime = actions.querySNMPNumber(thisDevice, '.1.3.6.1.2.1.1.3.0', -1); //Query device for uptime and store
    log.warn('Uptime for device ' + ip+" "+thisDevice.getName()+" "+uptime); //Write information to log
}else{
    log.warn("No such managed device "+ip); //Alert that device isn't a known infrastructure device
}

Querying a String (sysName in this case) from an infrastructure device

var ip="172.21.40.254" //Set Ip address
var thisDevice = actions.getDevice(ip); //Get managed device
if (thisDevice != null) { //Check if it exists in the system
    var uptime = actions.querySNMPText(thisDevice, '.1.3.6.1.2.1.1.5.0', -1); //Query device for OID .1.3.6.1.2.1.1.5.0(sysName) and store
    log.warn('sysName for device ' + ip+" "+thisDevice.getName()+" "+uptime); //Write information to log
}else{
    log.warn("No such managed device "+ip); //Alert that device isn't a known infrastructure device
}

Querying a String (sysName in this case) from an infrastructure device

var ip="172.21.40.254" //Set Ip address
var thisDevice = actions.getDevice(ip); //Get managed device
if (thisDevice != null) { //Check if it exists in the system
    var table = actions.querySNMPTable(thisDevice, ['.1.3.6.1.2.1.1']); //Query device for table(s) and store
    for (var i=0;i<table.size();i++){
          var entry = table.get(i);
          log.warn("This Row:"+i+" :"+entry.getIndex()+" "+showSNMPVariables(entry.getColumns()));
     }
}else{
     log.warn("No such managed device "+ip); //Alert that device isn't a known infrastructure device
}
//A function to return the variables from each returned column
function showSNMPVariables(entry){
    var retValue="";
    for (var i=0;i<entry.length;i++){
        retValue+='\t'+i+':'+entry[i].getVariable().toString()
    }
    return retValue;
}

Getting the SysOID of a device and determining if its a Cisco device

var ip="172.21.40.254" //Set Ip address
var thisDevice = actions.getDevice(ip); //Get managed device
if (thisDevice != null) { //Check if it exists in the system
    log.warn('sysOID for device ' + ip+" "+thisDevice.getName()+" "+thisDevice.getSysOID()); //Write details to log
    if (thisDevice.getSysOID().indexOf('1.3.6.1.4.1.9')==0){ //Look for Ciscos OID and see if it exists ==0
        log.warn("Is Cisco");//Write to log
    }
}else{
    log.warn("No such managed device "+ip); //Alert that device isn't a known infrastructure device
}

If you know the snmp information for a device which isn't an infrastructure device, you can also query that direcly by providing the required params

Querying a number

//querySNMPNumber(String ip, int port, int version, int retries, int timeout, String community, String oid, long adefault)
var uptime = actions.querySNMPNumber("172.21.40.254",161,2,1,1000,"th1s1s1t", '.1.3.6.1.2.1.1.3.0', -1); //Query device for uptime and store

Querying a String

//querySNMPText(String ip, int port, int version, int retries, int timeout, String community, String oid, String adefault)
var sysname = actions.querySNMPText("172.21.40.254",161,2,1,1000,"th1s1s1t", '.1.3.6.1.2.1.1.5.0', -1); //Query device for sysname  and store

Querying a table

//querySNMPTable(String ip, int port, int version, int retries, int timeout, String community, String[] oids)
var table = actions.querySNMPTable("172.21.40.254",161,2,2,1000,"th1s1s1t", ['.1.3.6.1.2.1.1']); //Query device for OID .1.3.6.1.2.1.1(system) and store

Processing table results

var table = actions.querySNMPTable("1.2.3.4",161,2,2,1000,"acommunity", ['.1.3.6.1.4.1.9.9.315.1.2.3.1.3.10012']); //Query device for OID .1.3.6.1.2.1.1(system) and store
log.warn(table);
for (var i=0;i<table.length;i++){
 var cols=table[i].getColumns();
 log.warn("cols:"+cols.length);
 log.warn(cols);
 for (var c=0;c<cols.length;c++){
     log.warn(cols[c]);
   log.warn("c:"+c);

log.warn(cols[c].getOid()); //1.3.6.1.4.1.9.9.315.1.2.3.1.3.10012.212.129.215.245.100.43.10 = 3

   log.warn(cols[c].getVariable().toString()); //3
   log.warn(cols[c].getVariable().toInt());  //3
   log.warn(cols[c].getVariable().toLong());  //3
   
 }
}

Search for devices interface by name

 var descriptionToSearchFor="Vlan501";
 var found=ifindexForIfName("172.21.40.254",descriptionToSearchFor);
 if (found!=-1){
 log.warn("Found ifindex:"+found+" for "+descriptionToSearchFor);
 }else{
log.warn("Didnt find ifindex:"+found+" for "+descriptionToSearchFor);
}
function ifindexForIfName(deviceip,tofind){
var device  = actions.getDevice(deviceip);
if (typeof device!='undefined'){
var interfaces = device.interfaces;
for (var i in interfaces){
//log.warn(i);
//log.warn(interfaces.get(i));
 if (tofind===interfaces.get(i).ifDescr||tofind===interfaces.get(i).ifAlias||tofind===interfaces.get(i).ifName){
 return i;
 }
}
  log.warn("Didn't find:"+tofind);
  }else{
  log.warn("No such device:"+deviceip);
  }
  return -1;
}


Processing devices

var BigInteger = Java.type("java.math.BigInteger"); var rosutils = Java.type('ros.CROSUtils'); var PrintWriter = Java.type('java.io.PrintWriter');
var devices= actions.getDatabaseManager().getVectorFromDBprepared("select  ip from devices;",[]);
var writer = new PrintWriter("./resources/webapps/static/oids.html", "UTF-8");
writer.println("<html>"); writer.println("<head>"); writer.println("<link href='/static/css/jquery.dataTables.css' rel='stylesheet'>");
writer.println("<link href='/static/css/dashboard.css' rel='stylesheet'>"); writer.println("</head>");writer.println("<body><tbody><thead></thead>");
for (var i = 0; i < devices.size(); i++) {
var thisDevice = actions.getDevice(devices.get(i)[0]);
try{
if (thisDevice==null){
log.warn("unknown device:"+devices.get(i)[0])
}else{
var sysDescr =actions.querySNMPText(thisDevice, '.1.3.6.1.2.1.1.1.0', 'Unknown');
var sysObjectID =actions.querySNMPText(thisDevice, '.1.3.6.1.2.1.1.2.0', 'Unknown');
var sysName =actions.querySNMPText(thisDevice, '.1.3.6.1.2.1.1.5.0', 'Unknown');
var sysLocation =actions.querySNMPText(thisDevice, '.1.3.6.1.2.1.1.6.0', 'Unknown');
log.warn(devices.get(i)[0]+"thisDevice:"+sysObjectID+" "+sysName+" "+sysLocation+" "+sysDescr)
writer.println("");
}
}catch(err) {
log.warn("unknown device:"+devices.get(i)[0])
}
}
writer.println("</tbody>
IPsysObjectIDsysNamesysLocationsysDescr
"+devices.get(i)[0]+""+sysObjectID+""+sysName+""+sysLocation+""+sysDescr+"
");
writer.close();