Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'net-snmp' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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()
})
})
}
service.error('OID ' + varbinds[i].oid + ' gave error: ' + net_snmp.varbindError(varbinds[i]));
value = null;
failed++;
}
else {
// test fom Counter64
// varbinds[i].type = net_snmp.ObjectType.Counter64;
// varbinds[i].value = new Buffer([0x34, 0x49, 0x2e, 0xdc, 0xd1]);
switch(varbinds[i].type) {
case net_snmp.ObjectType.OctetString:
if (service.snmp_oids_index[varbinds[i].oid].type !== 'title' && service.snmp_oids_index[varbinds[i].oid].type !== 'name') {
// parse floating point values, exposed as strings
value = parseFloat(varbinds[i].value) * 1000;
if (__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as float in string)");
}
else {
// just use the string
value = varbinds[i].value;
if (__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as string)");
}
break;
case net_snmp.ObjectType.Counter64:
// copy the buffer
value = '0x' + varbinds[i].value.toString('hex');
if(__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as buffer)");
break;
case net_snmp.ObjectType.Integer:
case net_snmp.ObjectType.Counter:
if (__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as string)");
}
break;
case net_snmp.ObjectType.Counter64:
// copy the buffer
value = '0x' + varbinds[i].value.toString('hex');
if(__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as buffer)");
break;
case net_snmp.ObjectType.Integer:
case net_snmp.ObjectType.Counter:
case net_snmp.ObjectType.Gauge:
default:
value = varbinds[i].value;
if(__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as number)");
break;
}
ok++;
}
if(value !== null) {
switch(service.snmp_oids_index[varbinds[i].oid].type) {
case 'title': service.snmp_oids_index[varbinds[i].oid].link.title += ' ' + value; break;
case 'name' : service.snmp_oids_index[varbinds[i].oid].link.name = value.toString().replace(/\W/g, '_'); break;
case 'value': service.snmp_oids_index[varbinds[i].oid].link.value = value; break;
}
}
}
if(__DEBUG === true)
getSession(host, community, node.version, node.timeout).set(varbinds, function (error, varbinds) {
if (error) {
node.error(error.toString(), msg);
}
else {
for (var i = 0; i < varbinds.length; i++) {
// for version 2c we must check each OID for an error condition
if (snmp.isVarbindError(varbinds[i])) {
node.error(snmp.varbindError(varbinds[i]), msg);
}
}
}
});
}
service.error('Received error = ' + netdata.stringify(error) + ' varbinds = ' + netdata.stringify(varbinds));
// make all values null
var len = slice.length;
while(len--)
service.snmp_oids_index[slice[len]].value = null;
}
else {
if(__DEBUG === true)
netdata.debug(service.module.name + ': ' + service.name + ': got valid ' + service.module.name + ' response: ' + netdata.stringify(varbinds));
var varbinds_len = varbinds.length;
for(var i = 0; i < varbinds_len ; i++) {
var value = null;
if(net_snmp.isVarbindError(varbinds[i])) {
if(__DEBUG === true)
netdata.debug(service.module.name + ': ' + service.name + ': failed ' + service.module.name + ' get for OIDs ' + varbinds[i].oid);
service.error('OID ' + varbinds[i].oid + ' gave error: ' + net_snmp.varbindError(varbinds[i]));
value = null;
failed++;
}
else {
// test fom Counter64
// varbinds[i].type = net_snmp.ObjectType.Counter64;
// varbinds[i].value = new Buffer([0x34, 0x49, 0x2e, 0xdc, 0xd1]);
switch(varbinds[i].type) {
case net_snmp.ObjectType.OctetString:
if (service.snmp_oids_index[varbinds[i].oid].type !== 'title' && service.snmp_oids_index[varbinds[i].oid].type !== 'name') {
// parse floating point values, exposed as strings
while(len--)
service.snmp_oids_index[slice[len]].value = null;
}
else {
if(__DEBUG === true)
netdata.debug(service.module.name + ': ' + service.name + ': got valid ' + service.module.name + ' response: ' + netdata.stringify(varbinds));
var varbinds_len = varbinds.length;
for(var i = 0; i < varbinds_len ; i++) {
var value = null;
if(net_snmp.isVarbindError(varbinds[i])) {
if(__DEBUG === true)
netdata.debug(service.module.name + ': ' + service.name + ': failed ' + service.module.name + ' get for OIDs ' + varbinds[i].oid);
service.error('OID ' + varbinds[i].oid + ' gave error: ' + net_snmp.varbindError(varbinds[i]));
value = null;
failed++;
}
else {
// test fom Counter64
// varbinds[i].type = net_snmp.ObjectType.Counter64;
// varbinds[i].value = new Buffer([0x34, 0x49, 0x2e, 0xdc, 0xd1]);
switch(varbinds[i].type) {
case net_snmp.ObjectType.OctetString:
if (service.snmp_oids_index[varbinds[i].oid].type !== 'title' && service.snmp_oids_index[varbinds[i].oid].type !== 'name') {
// parse floating point values, exposed as strings
value = parseFloat(varbinds[i].value) * 1000;
if (__DEBUG === true) netdata.debug(service.module.name + ': ' + service.name + ': found ' + service.module.name + ' value of OIDs ' + varbinds[i].oid + ", ObjectType " + net_snmp.ObjectType[varbinds[i].type] + " (" + netdata.stringify(varbinds[i].type) + "), typeof(" + typeof(varbinds[i].value) + "), in JSON: " + netdata.stringify(varbinds[i].value) + ", value = " + value.toString() + " (parsed as float in string)");
}
else {
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);
}
}
}
getSession(host, community, node.version, node.timeout).get(oids.split(","), function (error, varbinds) {
if (error) {
node.error(error.toString(), msg);
}
else {
for (var i = 0; i < varbinds.length; i++) {
if (snmp.isVarbindError(varbinds[i])) {
node.error(snmp.varbindError(varbinds[i]), msg);
}
else {
if (varbinds[i].type == 4) { varbinds[i].value = varbinds[i].value.toString(); }
varbinds[i].tstr = snmp.ObjectType[varbinds[i].type];
//node.log(varbinds[i].oid + "|" + varbinds[i].tstr + "|" + varbinds[i].value);
}
}
msg.oid = oids;
msg.payload = varbinds;
node.send(msg);
}
});
}