我发送陷阱消息到特定主机OID之前我可以确定会话是否成功创建,以便我可以发送陷阱消息。
main.js
var snmp = require("net-snmp");
var msg = require('./event.js');
var dns = require('dns');
var os = require('os');
function process (msg) {
var host = msg.event.body.snmp.trapHost;
//Create snmp Session
var session = snmp.createSession(host,"public",sessionOptions);
var options = {upTime: 1000};
try {
dns.lookup (os.hostname (), function (error, host) {
if (error) {
console.error(error);
} else {
session.trap(trapOid, varbinds, options, function (error) {
if (error)
console.log(error);
else
console.log('SNMP successfully delivered');
});
}
});
} catch (e) {
console.log("SNMP processing error: " + e);
}
};
process(msg);