Many people may not be familiar with snmpjs, but if you are familiar with SNMP then I need some help sending a trap message I generated using snmpjs. Upon running the following code...
var trap = snmp.message.createMessage({
version: 0, //this means send a SNMP v1 trap
community: "public",
pdu: snmp.pdu.createPDU({
op: 4,//SNMP trap v1
//...etc. etc.
}),
});
console.log(trap);
I get a trap that could nearly diff with a trap generated from net-snmp. The problem is, I don't know how to properly send this trap message. snmpjs seems to have NO SEND FUNCTION, which is really confusing to me. Not sure why they would make this framework to generate messages if they have no method of sending them.
Anyhow, my recent idea was to
var str = JSON.stringify(trap);
var buf = new Buffer(str.toString());
var conn = dgram.createSocket('udp4');
conn.send(buf, 0, buf.length, 162, 'localhost', function(err, bytes) {
console.log(bytes+" bytes written");
conn.close();
});
Which actually prints out
3152 bytes written.
But the server that's supposedly receiving traps says nothing. My method of sending is wrong, but if anyone would like to see the server code listening for traps here it is
var trapd = snmp.createTrapListener();
trapd.on('trap', function(msg){
console.log("Message says...");
console.log(msg);
});
trapd.bind({family: 'udp4', port:162});
Which is confusing in itself but that's the way to create an snmp trap listener in accordance with atlantageek.com: http://atlantageek.com/2014/08/23/snmp-trap-listener-in-node/
This confuses me because we seem to be using both dgram and eventEmitter to listen for traps. I am obviously confused as to which way to send the trap.
The answer, for anyone who ever uses snmpjs was the following...
trap.encode();
var socket = dgram.createSocket('udp4');
socket.send(trap._raw.buf, 0, trap._raw.len, 162, 'localhost', function(err, bytes) {
if(err) console.log(err);
console.log(bytes+" bytes written");
});