Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if(__DEBUG === true)
service.error('no OIDs to process.');
callback(null);
return;
}
if(typeof service.snmp_session === 'undefined' || service.snmp_session === null) {
// no SNMP session has been created for this service
// the SNMP session is just the initialization of NET-SNMP
if(__DEBUG === true)
netdata.debug(service.module.name + ': ' + service.name + ': opening ' + this.name + ' session on ' + service.request.hostname + ' community ' + service.request.community + ' options ' + netdata.stringify(service.request.options));
// create the SNMP session
service.snmp_session = net_snmp.createSession (service.request.hostname, service.request.community, service.request.options);
if(__DEBUG === true)
netdata.debug(service.module.name + ': ' + service.name + ': got ' + this.name + ' session: ' + netdata.stringify(service.snmp_session));
// if we later need traps, this is how to do it:
//service.snmp_session.trap(net_snmp.TrapType.LinkDown, function(error) {
// if(error) console.error('trap error: ' + netdata.stringify(error));
//});
}
// do it, get the SNMP values for the sessions we need
this.getdata(service, 0, 0, 0, callback);
}
};
return new Promise((resolve, reject) => {
var options = { ...defaultOptions, ...opt }
var session = snmp.createSession(options.ip || options.host, options.community, options)
session.get(oids, function (error, varbinds) {
if (error) {
return reject(error)
} else {
return resolve(varbinds)
}
// If done, close the session
session.close()
})
})
}
function getSession(host, community, version, timeout) {
var sessionKey = host + ":" + community + ":" + version;
var port = 161;
if (host.indexOf(":") !== -1) {
port = host.split(":")[1];
host = host.split(":")[0];
}
if (!(sessionKey in sessions)) {
sessions[sessionKey] = snmp.createSession(host, community, { port:port, version:version, timeout:(timeout || 5000) });
}
return sessions[sessionKey];
}
function SNMP(data) {
this.host = data.host;
this.traps = data.traps;
this.session = snmp.createSession ("127.0.0.1", "public");
var oids = ["1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.1.6.0"];
this.session.get (oids, function (error, varbinds) {
if (error) {
console.error (error);
} else {
for (var i = 0; i < varbinds.length; i++) {
if (snmp.isVarbindError(varbinds[i])) {
console.error (snmp.varbindError (varbinds[i]));
} else {
console.log (varbinds[i].oid + " = " + varbinds[i].value);
}
}
}
return new Promise((resolve, reject) => {
var options = { ...defaultOptions, ...opt }
var aVarbinds = []
var session = snmp.createSession(options.ip || options.host, options.community, options)
session.subtree(oid, options.maxRepetitions, function feedCb(varbinds) {
aVarbinds.push(...varbinds)
}, function doneCb(error) {
if (error)
return reject(error.toString())
else
return resolve(aVarbinds)
})
})
}