Script To SNMP

From Observer GigaFlow Support | VIAVI Solutions Inc.
Jump to: navigation, search
   var now = new Date().getTime();
   function createTables() {
       try {
           //actions.getDatabaseManager().executePrepared("drop table if exists serialnumbers ",[[]])
           actions.getDatabaseManager().executePrepared("create table if not exists serialnumbers (ip text, name text, serialnumber text, snmpoid text,firstseen bigint,lastseen bigint,unique(ip))", [[]])
           actions.getDatabaseManager().executePrepared("create or replace rule \"replace_serialnumber\" as on insert to \"serialnumbers\" where exists(select 1 from serialnumbers where ip=NEW.ip) do instead (update serialnumbers set name=NEW.name,serialnumber=NEW.serialnumber,snmpoid=NEW.snmpoid,lastseen=NEW.lastseen where ip=NEW.ip);", [[]])
       } catch (err) {
           log.warn(err.message);
       }
   }
   function start() {
       createTables()
       for (var deviceid in actions.getDevices()) {
           var device = actions.getDevice(deviceid);
           if (device != null && device.sysOID.length > 0) {
               getSerialNumber(device)
           } else {
               //log.warn("No SNMP "+device.getIP());
           }
       }
   }
   function getSerialNumber(device) {
       try {
           if (device.sysOID.indexOf("1.3.6.1.4.1.9") === 0) {
               getSerialNumberCisco(device)
           } else {
               log.warn('sysOID unknown for device ' + device.getIP() + " " + device.getName() + " oid:" + device.sysOID);
           }
       } catch (err) {
           log.warn(err.message);
       }
   }
   function getSerialNumberCisco(device) {
       var value = actions.querySNMPText(device, '.1.3.6.1.2.1.47.1.1.1.1.11.1', -1); //Query device for OID .1.3.6.1.2.1.1.5.0(sysName) and store
       if (value !== null) {
           insert(device, value)
           log.warn('value for device ' + device.getIP() + " " + device.getName() + " " + value + " oid:" + device.sysOID); //Write information to log
       } else {
           log.warn('null for device ' + device.getIP() + " " + device.getName() + " " + value + " oid:" + device.sysOID); //Write information to log
       }
   }
   function insert(device, serialnumber) {
       actions.getDatabaseManager().executePrepared("insert into serialnumbers (ip, name, serialnumber, snmpoid,firstseen,lastseen) values(?,?,?,?,?,?)", device.getIP(), device.getName(), serialnumber, device.sysOID, now, now)
   }
   start();