• This repository has been archived on 08/Mar/2022
  • Stars
    star
    253
  • Rank 160,776 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 13 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Native Javascript SNMP library for Node.js
                                                             __
                                                            /\ \__  __
  ____    ___     ___ ___   _____              ___      __  \ \ ,_\/\_\   __  __     __
 /',__\ /' _ `\ /' __` __`\/\ '__`\  _______ /' _ `\  /'__`\ \ \ \/\/\ \ /\ \/\ \  /'__`\
/\__, `\/\ \/\ \/\ \/\ \/\ \ \ \L\ \/\______\/\ \/\ \/\ \L\.\_\ \ \_\ \ \\ \ \_/ |/\  __/
\/\____/\ \_\ \_\ \_\ \_\ \_\ \ ,__/\/______/\ \_\ \_\ \__/.\_\\ \__\\ \_\\ \___/ \ \____\
 \/___/  \/_/\/_/\/_/\/_/\/_/\ \ \/           \/_/\/_/\/__/\/_/ \/__/ \/_/ \/__/   \/____/
                              \ \_\
                               \/_/

snmp-native Build Status

This is a native SNMP library for Node.js. The purpose is to provide enough functionality to perform large scale monitoring of network equipment. Current features towards this end are:

  • Full implementation of SNMPv2c, including 64 bit data types.
  • Support for Get, GetNext and Set requests, with optimizations such as GetAll and GetSubtree.
  • No unusual external dependencies, no non-JavaScript code.
  • Very high performance, unlimited parallellism. (There are always limits. However, there are no arbitrary such imposed by this code and you at least won't run out of file descriptors.)
  • De facto standards compliance. Generated packets are compared against Net-SNMP and should be identical in all relevant aspects.
  • Well tested. Test coverage should be at or close to 100% for all important code paths.

It specifically does not include:

  • Compatibility with SNMPv1, SNMPv2u or SNMPv3. These are (in order) deprecated, weird, and too complicated. Yes, it's an opinionated library.
  • MIB parsing. Do this in your client app if it's necessary.

It's optimized for polling tens of thousands of counters on hundreds or thousands of hosts in a parallell manner. This is known to work (although performance might be limited by less than optimal SNMP agent implementations in random network gear).

Documentation

Installation

$ npm install snmp-native

Usage

Import

var snmp = require('snmp-native');

new Session(options)

Create a Session. The Session constructor, like most of the other functions, take an options object. The options passed to the Session will be the defaults for any subsequent function calls on that session, but can be overridden as needed. Useful parameters here are host, port and family.

// Create a Session with default settings.
var session = new snmp.Session();

// Create a Session with explicit default host, port, and community.
var session = new snmp.Session({ host: 'device.example.com', port: 161, community: 'special' });

// Create an IPv6 Session.
var session = new snmp.Session({ host: '2001:db8::42', family: 'udp6', community: 'private' });

The following options are recognized as properties in the options object. All can be specified in the Session constructor and optionally overridden at a later time by setting them in the option object to a method call.

For optimum performance when polling many hosts, create a session without specifying the host. Reuse this session for all hosts and specify the host on each get, getAll, etc.

  • host: The host to send the request to. An resolvable name is allowed in addition to IP addresses. Default: 'localhost'.
  • port: The UDP port number to send the request to. Default: 161.
  • community: The SNMP community name. Default: 'public'.
  • family: Address family to bind to. This is only used by the Session constructor since that is when the bind is done. It cannot be changed or overridden after construction. Default: 'udp4'. Valid values: 'udp4' or 'udp6'.
  • timeouts: An array of timeout values. Values are times in milliseconds, the length of the array is the total number of transmissions that will occur. Default: [5000, 5000, 5000, 5000] (four attempts, with five seconds between each). A backoff can be implemented by timeouts along the lines of [ 1000, 2000, 4000, 8000 ]. Retransmissions can be disabled by using only a single timeout value: [ 5000 ].
  • bindPort: UDP port used to bind the socket locally. Default: 0 (random port)
  • msgReceived: A (message, rinfo) => {} function responsible to handle incoming messages and sending UDP responses back. If nothing is given here, the default implementation is used. This is useful if you want to implement custom logic in your application

VarBind objects

All of the get* functions return arrays of VarBind as the result to the callback. The VarBind objects have the following properties:

  • oid: The OID they represent (in array form).
  • type: The integer type code for the returned value.
  • value: The value, in decoded form. This will be an integer for integer, gauge, counter and timetick types, a string for an octet string value, an array for array or IP number types.
  • valueRaw: For octet string values, this is a raw Buffer representing the string.
  • valueHex: For octet string values, this is a hex string representation of the value.
  • sendStamp: The timestamp (in milliseconds) when the request was transmitted.
  • receiveStamp: The timestamp (in milliseconds) when the response was received.

get(options, callback)

Perform a simple GetRequest. Options (in addition to the ones defined above for Session):

  • oid: The OID to get. Example: [1, 3, 6, 1, 4, 1, 1, 2, 3, 4] or '.1.3.6.1.4.1.1.2.3.4'. Both forms are accepted, but the string form will need to be parsed to an array, slightly increasing CPU usage.

Will call the specified callback with an error object (null on success) and the varbind that was received.

session.get({ oid: [1, 3, 6, 1, 4, 1, 42, 1, 0] }, function (error, varbinds) {
    if (error) {
        console.log('Fail :(');
    } else {
        console.log(varbinds[0].oid + ' = ' + varbinds[0].value + ' (' + varbinds[0].type + ')');
    }
});

You can also specify host, community, etc explicitly.

session.get({ oid: [1, 3, 6, 1, 4, 1, 42, 1, 0], host: 'localhost', community: 'test' }, ...);

getNext(options, callback)

Perform a simple GetNextRequest. Options:

  • oid: The OID to get. Example: [1, 3, 6, 1, 4, 1, 1, 2, 3, 4] or '.1.3.6.1.4.1.1.2.3.4'.

Will call the specified callback with an error object (null on success) and the varbind that was received.

session.getNext({ oid: [1, 3, 6, 1, 4, 1, 42, 1, 0] }, function (error, varbinds) {
    if (error) {
        console.log('Fail :(');
    } else {
        console.log(varbinds[0].oid + ' = ' + varbinds[0].value + ' (' + varbinds[0].type + ')');
    }
});

getAll(options, callback)

Perform repeated GetRequests to fetch all the required values. Multiple OIDs will get packed into as few GetRequest packets as possible to minimize roundtrip delays. Gets will be issued serially (not in parallell) to avoid flooding hosts. Options:

  • oids: An array of OIDs to get. Example: [[1, 3, 6, 1, 4, 1, 1, 2, 3], [1, 3, 6, 1, 4, 1, 1, 2, 4]] or ['.1.3.6.1.4.1.1.2.3.4', '.1.3.6.1.4.1.2.3.4.5'].
  • abortOnError: Whether to stop or continue when an error is encountered. Default: false.
  • combinedTimeout: Timeout in milliseconds that the getAll() may take. Default: no timeout.

The callback will be called with an error object or a list of varbinds. If the options property abortOnError is false (default) any variables that couldn't be fetched will simply be omitted from the results. If it is true, the callback will be called with an error object on any failure. If the combinedTimeout is triggered, the callback is called with an error and the partial results.

var oids = [ [1, 3, 6, 1, 4, 1, 42, 1, 0], [1, 3, 6, 1, 4, 1, 42, 2, 0], ... ];
session.getAll({ oids: oids }, function (error, varbinds) {
    varbinds.forEach(function (vb) {
        console.log(vb.oid + ' = ' + vb.value + ' (' + vb.type + ')');
    });
});

getSubtree(options, callback)

Perform repeated GetNextRequests to fetch all values in the specified tree. Options:

  • oid: The OID to get. Example: [1, 3, 6, 1, 4, 1, 1, 2, 3, 4] or '.1.3.6.1.4.1.1.2.3.4'.
  • combinedTimeout: Timeout in milliseconds that the getSubtree() may take. Default: no timeout.

Will call the specified callback with an error object (null on success) and the list of varbinds that was fetched. If the combinedTimeout is triggered, the callback is called with an error and the partial results.

session.getSubtree({ oid: [1, 3, 6, 1, 4, 1, 42] }, function (error, varbinds) {
    if (error) {
        console.log('Fail :(');
    } else {
        varbinds.forEach(function (vb) {
            console.log(vb.oid + ' = ' + vb.value + ' (' + vb.type + ')');
        });
    }
});

set(options, callback)

Perform a simple SetRequest. Options:

  • oid: The OID to perform the set on. Example: [1, 3, 6, 1, 4, 1, 1, 2, 3, 4] or '.1.3.6.1.4.1.1.2.3.4'.
  • value: The value to set. Example: 42.
  • type: The type of the value. Currently supports asn1ber.T.Integer (2), asn1ber.T.Gauge (66), asn1ber.T.IpAddress (64), asn1ber.T.OctetString (4) and asn1ber.T.Null (5). Example: 2.

Example:

session.set({ oid: [1, 3, 6, 1, 4, 1, 42, 1, 0], value: 42, type: 2 }, function (error, varbind) {
    if (error) {
        console.log('Fail :(');
    } else {
        console.log('The set is done.');
    }
});

If you're not really interested in the outcome of the set (and if you are, why aren't you using scripted telnet or ssh instead to begin with?), you can call it without a callback:

session.set({ oid: [1, 3, 6, 1, 4, 1, 42, 1, 0], value: 42, type: 2 });

close()

Cancels all outstanding requests and frees used OS resources. Outstanding requests will call their callback with the "Cancelled" error set.

Example:

session.close();

License

MIT

More Repositories

1

unifi-api

[UNMAINTAINED] An API towards the Ubiquity Networks UniFi controller
Python
331
star
2

smartos-platform-upgrade

Upgrade your SmartOS in style!
Shell
124
star
3

mole

Like 1Password for ssh tunnels and VPN connections, plus sharing within a team.
Go
102
star
4

ipfix

IPFIX parser package for Go
Go
48
star
5

deprecated_lfucache

Package lfucache implements an O(1) LFU cache structure.
Go
41
star
6

zsnapper.old

[UNMAINTAINED] ZFS Snapshotting Service
JavaScript
35
star
7

zsnapper

ZFS Snapshotting Service
Go
30
star
8

node-sudo

[UNMAINTAINED] Like child_process.spawn but with sudo
JavaScript
26
star
9

yardstick

[UNMAINTAINED] Javascript code metrics
JavaScript
25
star
10

ipfixcat

Convert an IPFIX stream to readable JSON
Go
25
star
11

dst

The Datagram Stream Transfer protocol
Go
23
star
12

deprecated_jsonrpc

JSON-RPC 2.0 client for Go
Go
23
star
13

git-autotag

Tags for the lazy software maintainer
Go
21
star
14

node-zfs

[UNMAINTAINED] Node.js wrapper for ZFS management
JavaScript
21
star
15

randomart

Generates OpenSSH-style randomart.
Go
20
star
16

clonehub

Keep a local mirror of all public GitHub repositories
Python
20
star
17

imapchive

IMAP Archiver
Go
17
star
18

Glint

Intuitive GPS Tracker for the iPhone
Objective-C
13
star
19

smartos-ancestry

Script to human-readably show what image the VM:s are based on
11
star
20

ClusteredPoller

[UNMAINTAINED] Database friendly poller for the RTG traffic statistics system.
C
10
star
21

node-vpnc

[UNMAINTAINED] Cisco VPN connector / vpnc wrapper
JavaScript
10
star
22

solaris-extra-snmp

[UNMAINTAINED] Improved SNMP monitoring for Solaris
Python
10
star
23

xdr

[UNMAINTAINED] Go XDR enc/decoder
Go
8
star
24

Aggregator

[UNMAINTAINED] RTG Data Aggregation Library
Ruby
5
star
25

zspace

Space used on ZFS.
Go
5
star
26

node-hostsfile

[UNMAINTAINED] Manipulate the /etc/hosts file
JavaScript
5
star
27

node-pidof

[UNMAINTAINED] Get PID of running process
JavaScript
4
star
28

relup

Upload an asset to a GitHub release
Go
4
star
29

node-filters

[UNMAINTAINED] 1D noise reduction filters
JavaScript
4
star
30

node-yatf

[UNMAINTAINED] Yet Another Table Formatter
JavaScript
3
star
31

bingloader

Tiny tool to load the picture of the day from Bing
Go
3
star
32

luhn

A Luhn-mod-N implementation in Go
Go
3
star
33

node-inpath

[UNMAINTAINED] Find an executable in the $PATH.
JavaScript
3
star
34

para

Parallellize your stdin
Go
3
star
35

mergebot

A bot to do squash merges on pull requests
Go
3
star
36

iDatacenter

VM Administration for iPad
Objective-C
3
star
37

zpoller

[UNMAINTAINED]
JavaScript
2
star
38

git-review

Reviewthing!
Go
2
star
39

cfdns

Deprecated - do not use - Go API client towards Cloudflare DNS service
Go
2
star
40

dl

Trivial tool to download and untar/unzip files on Windows
Go
2
star
41

ev

Execution visualizer for Go
Go
2
star
42

extractudp

Extract packet contents from a PCAP
Go
2
star
43

nmea-collect

Go
2
star
44

zscrub

Complete, all-in-one, enterprise grade ZFS scrubbing solution
Shell
2
star
45

deprecated_ini

Yet another .INI file parser / writer.
Go
2
star
46

snooze

Periodic To-Do List
JavaScript
2
star
47

freezebot

Simple GitHub API integration to lock and label old, inactive, and closed issues.
Go
2
star
48

FlexGraph

Flexible Ruby grapher for RTG or other time series data
Ruby
2
star
49

cube-eds-grapher

[UNMAINTAINED] Graph environmental data from Cube
JavaScript
1
star
50

cfipsync

Deprecated - do not use - Sync IP address from text file to Cloudflare
Go
1
star
51

vmctl

Something to edit SmartOS VMs?
Go
1
star
52

cfdyndns

Dynamic DNS client for Cloudflare
Go
1
star
53

travel

Go
1
star
54

cube-eds-poller

[UNMAINTAINED] Fetch environmental data from an EDS 1-Wire device and inject it into Cube
JavaScript
1
star
55

incontainer

Go
1
star
56

Register

Martial Arts / Sports Student Registry
Ruby
1
star
57

smtpcat

For when you don't have a local MTA.
Go
1
star
58

zip

Go archive/zip with append functionality
Go
1
star
59

node-ircbridge

[UNMAINTAINED] Bridges IRC to JSON-RPC over UDP (client library).
JavaScript
1
star
60

node-openconnect

[UNMAINTAINED] Handle Cisco SSL VPNs with OpenConnect
JavaScript
1
star
61

zfs.old

ZFS package for Go
Go
1
star
62

node-kext

[UNMAINTAINED] Check and load Mac OS X kernel modules
JavaScript
1
star
63

github_docs_badges

Docs badges in Discourse
Ruby
1
star
64

github_bugs_badges

Grant discourse badges for GitHub issues
Ruby
1
star
65

zsync

rsync for ZFS snapshots
Go
1
star
66

node-ntpd-status

[UNMAINTAINED] Get status information from a local or remote ntpd
JavaScript
1
star
67

BuildTools

Various scripts used for building and tagging releases
Shell
1
star
68

logger

A thin layer of logging levels on top of the standard log package
Go
1
star
69

discourse_translators

Badges for translating
Ruby
1
star