Difference between revisions of "Script to SNMP Serial Numbers"
From Observer GigaFlow Support | VIAVI Solutions Inc.
Kevin Wilkie (Talk | contribs) |
Kevin Wilkie (Talk | contribs) |
||
Line 1: | Line 1: | ||
− | + | var now = new Date().getTime(); | |
− | + | var count = 0; | |
− | + | 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() { | |
− | function start() { | + | createTables() |
− | + | for (var deviceid in actions.getDevices()) { | |
− | + | var device = actions.getDevice(deviceid); | |
− | + | if (device != null && device.sysOID.length > 0) { | |
− | + | getSerialNumber(device) | |
− | + | } | |
− | } | + | } |
} | } | ||
− | + | function getSerialNumber(device) { | |
− | function getSerialNumber(device) { | + | try { |
− | + | if (!getSerialNumberNew(device, '.1.3.6.1.2.1.47.1.1.1.1.11')) { | |
− | + | if (device.sysOID.indexOf("1.3.6.1.4.1.9") === 0) { | |
− | + | getSerialNumberNew(device, '.1.3.6.1.4.1.9.3.6.3') | |
− | + | } | |
} | } | ||
+ | } catch (err) { | ||
+ | log.warn(err.message); | ||
} | } | ||
− | |||
− | |||
} | } | ||
− | + | function getSerialNumberNew(device, oid) { | |
− | function getSerialNumberNew(device, oid) { | + | var values = actions.querySNMPTable(device, [oid]) |
− | + | if (values != null && values.size() > 0) { | |
− | + | for (var v in values) { | |
− | + | var cols = values.get(v).getColumns() | |
− | + | if (cols.length > 0) { | |
− | + | var thisValue = cols[0].getVariable().toString() | |
− | + | if (thisValue.length > 0) { | |
− | + | insert(device, cols[0].getVariable().toString()) | |
− | + | return true; | |
− | + | } | |
} | } | ||
} | } | ||
+ | } else { | ||
+ | log.warn('null for device ' + device.getIP() + " " + device.getName() + " " + values + " oid:" + device.sysOID); //Write information to log | ||
} | } | ||
− | + | return false; | |
− | + | ||
} | } | ||
− | |||
− | |||
− | function insert(device, serialnumber) { | + | 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(); | + | start(); |
Revision as of 13:08, 15 June 2021
var now = new Date().getTime(); var count = 0; 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) } } } function getSerialNumber(device) { try { if (!getSerialNumberNew(device, '.1.3.6.1.2.1.47.1.1.1.1.11')) { if (device.sysOID.indexOf("1.3.6.1.4.1.9") === 0) { getSerialNumberNew(device, '.1.3.6.1.4.1.9.3.6.3') } } } catch (err) { log.warn(err.message); } } function getSerialNumberNew(device, oid) { var values = actions.querySNMPTable(device, [oid]) if (values != null && values.size() > 0) { for (var v in values) { var cols = values.get(v).getColumns() if (cols.length > 0) { var thisValue = cols[0].getVariable().toString() if (thisValue.length > 0) { insert(device, cols[0].getVariable().toString()) return true; } } } } else { log.warn('null for device ' + device.getIP() + " " + device.getName() + " " + values + " oid:" + device.sysOID); //Write information to log } return false; }
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();