如何在发送snmp陷阱之前检查会话是否已创建?

hus*_*ain 5 javascript dns net-snmp

在我这样做之前,我正在向特定的主机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);
Run Code Online (Sandbox Code Playgroud)