An SNMP for Node.js

Overview

snmpjs is a pure JavaScript, from-scratch framework for implementing SNMP agents, trap generators, and management applications in Node.js. It is intended primarily to enable the construction of a simple, general-purpose agent as an alternative to Net-SNMP.

var os = require('os');
var snmp = require('snmpjs');
var logger = require('bunyan');

var log = new logger({
    name: 'snmpd',
    level: 'info'
});

var agent = snmp.createAgent({
    log: log
});

agent.request({ oid: '.1.3.6.1.2.1.1.5', handler: function (prq) {
    var nodename = os.hostname();
    var val = snmp.data.createData({ type: 'OctetString',
        value: nodename });

    snmp.provider.readOnlyScalar(prq, val);
} });

agent.bind({ family: 'udp4', port: 161 });

Try hitting that with your favourite SNMP get utility, such as:

snmpget -v 2c -c any localhost .1.3.6.1.2.1.1.5

Features

snmpjs allows you to implement agents, trap generators, and management applications that support the full range of SNMPv1 and SNMPv2c functionality. It is mostly compatible with all relevant standards -- see notes below -- and can also be extended to handle (and, optionally, to generate) SNMP messages that incorporate nonstandard data types and other deviations. Several pieces of standard MIB functionality are included along with a simple agent, but you can easily implement your own MIB subtrees or alternative agent. The library can also be used to incorporate SNMP functionality into other daemons; for example, to allow them to generate traps if a fault condition is detected.

Getting Started

npm install snmpjs

If you're new to SNMP, check out the gentle introduction. Otherwise, the API documentation is:

agentReference for implementing SNMP agents.
mibAPI reference for the Management Information Base (MIB).
protocolAPI reference for low-level message encoding and decoding.
providerAPI reference for MIB providers.

What's Not Included

Many pieces of functionality that would be useful are missing. The most noteworthy, some of which are really needed in order to create a complete suite of standard MIB providers, include:

(*) These items are really bugs that have practical solutions; they will be addressed in subsequent revisions of the software.

Conformance

In general, deviations from the relevant specifications are considered bugs and should have issues opened to track them. However, there is one notable exception, in which the design of snmpjs precludes an effective implementation of required behaviour. Specifically, SetRequests, clearly intended by the standard to be treated as an atomic transaction when multiple varbinds are present, are not handled that way. Instead, they are treated as multiple independent requests that can succeed or fail without affecting the others. If a failure occurs, the first varbind to cause a failure will be highlighted by the error_index field in the response, and the nature of the error will be specified by error_status. Subsequent varbinds may or may not have been assigned successfully. The commitFailed and undoFailed status codes are not used. This approach dramatically simplifies the implementation (the standard disdainfully yet correctly calls this "taking the easy way out"), and practical use of SNMP for control operations where such transactionality is required is exceedingly rare. If you are unfortunate enough to need it, however, you will need to select an agent that is not based on snmpjs.

More Information

LicenseMIT
Codewesolows/node-snmpjs
node.js version0.6.x