Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/development'
Browse files Browse the repository at this point in the history
  • Loading branch information
thost96 committed Nov 16, 2016
2 parents 541beec + c2b9537 commit fa51a9b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release History

* 20161116, v0.2.0
* Added auto discovery for snmp devices on local network
* Update README.md
* Added net-ping to dependencies
* Added License to package.json

* 20161114, v0.1.3
* Changed getNextAsync to getAsync
* Added option to modify snmp port
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
[![npm version](https://badge.fury.io/js/pimatic-snmp.svg)](http://badge.fury.io/js/pimatic-snmp)
[![dependencies status](https://david-dm.org/thost96/pimatic-snmp/status.svg)](https://david-dm.org/thost96/pimatic-snmp)

A pimatic plugin to make snmp get request. The Oid can be found in the device mib file.
A pimatic plugin to make snmp get request. The Oid can be found in the device mib file provided by the manufacture or using [Oidview](http://www.oidview.com/).

## Plugin Configuration

{
"plugin": "snmp",
"debug": false
}

The plugin has the following configuration properties:

| Property | Default | Type | Description |
Expand All @@ -18,7 +20,12 @@ The plugin has the following configuration properties:


## Device Configuration
The following device can be used:
The plugin supports auto discover of snmp devices on your connected networks.
As default the community `"public"` and the oid `'.1.3.6.1.2.1.1.5.0'` for sysname property are used.

Please make sure, that these device(s) also allow ICMP requests for discovery!

The following device can also be created manually:

#### SnmpSensor
The SnmpSensor displays the output of your specified command to the gui.
Expand All @@ -28,7 +35,7 @@ The SnmpSensor displays the output of your specified command to the gui.
"class": "SnmpSensor",
"name": "Snmp Sensor",
"host": "",
"oid": ""
"oid": ".1.3.6.1.2.1.1.5.0"
}

| Property | Default | Type | Description |
Expand All @@ -49,7 +56,8 @@ The oid must be provied using this format: .x.xx.x.x.x.xxx - for example: .1.2.8
* Add support for oid names as label
* Add automatic clearing of attributes if command was changed
* Add set funtion for rule usage
* Add autodiscover functionality
* Add support for multiple oids at a single device
* Implement other devices for Presense, Temperature etc.

## History

Expand All @@ -59,4 +67,4 @@ See [Release History](https://github.com/thost96/pimatic-snmp/blob/master/Histor

Copyright (c) 2016, Thorsten Reichelt and contributors. All rights reserved.

License: [GPL-2.0](https://github.com/thost96/pimatic-snmp/blob/master/LICENSE).
License: [GPL-2.0](https://github.com/thost96/pimatic-snmp/blob/master/LICENSE).
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
"device-config-schema.coffee",
"LICENSE"
],
"version": "0.1.3",
"license": "GPL-2.0",
"version": "0.2.0",
"homepage": "http://github.com/thost96/",
"repository": {
"type": "git",
"url": "git://github.com/thost96/pimatic-snmp.git"
},
"configSchema": "snmp-config-schema.coffee",
"dependencies": {
"snmp-native": "1.0.21"
"snmp-native": "1.0.21",
"net-ping": "^1.2.0"
},
"peerDependencies": {
"pimatic": ">=0.8.0 <1.0.0"
Expand Down
80 changes: 77 additions & 3 deletions snmp.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,92 @@ module.exports = (env) ->
Promise = env.require 'bluebird'
snmp = require 'snmp-native'
_ = env.require 'lodash'
os = require 'os'
ping = env.ping or require("net-ping")


class SNMP extends env.plugins.Plugin

init: (app, @framework, @config) =>

@debug = @config.debug

deviceConfigDef = require("./device-config-schema.coffee")

deviceConfigDef = require("./device-config-schema.coffee")
@framework.deviceManager.registerDeviceClass("SnmpSensor", {
configDef: deviceConfigDef.SnmpSensor,
createCallback: (config) => new SnmpSensor(config, @, @framework)
})


@framework.deviceManager.on 'discover', (eventData) =>
@framework.deviceManager.discoverMessage 'pimatic-snmp', "scanning network for snmp devices"

interfaces = @listInterfaces()
maxPings = 513 #only /24 netmask
pingCount = 0
interfaces.forEach( (iface, ifNum) =>
#/24 netmask only
base = iface.address.match(/([0-9]+\.[0-9]+\.[0-9]+\.)[0-9]+/)[1]
@framework.deviceManager.discoverMessage 'pimatic-snmp', "Scanning #{base}0/#{iface.netmask}"

#console.log base
i = 1 #only /24 netmask

while i < 256 #only netmask /24
do (i) =>
if pingCount > maxPings then return

address = "#{base}#{i}" #increment ip base + i = x.x.x.i = 192.168.1.1/2/3/4
sessionId = ((process.pid + i) % 65535)

session = ping.createSession(
networkProtocol: ping.NetworkProtocol.IPv4 #ipv4 only ?
packetSize: 16
retries: 1
sessionId: sessionId
timeout: eventData.time
ttl: 128
)
session.pingHost(address, (error, target) =>
session.close()
unless error
snmpsession = Promise.promisifyAll( new snmp.Session({host: target, port: 161, community: "public"}) )
snmpsession.getAsync({ oid: '.1.3.6.1.2.1.1.5.0' }).then( (result) =>
if not _.isEmpty(result)

deviceConfig =
id: "snmp-" + target.replace(/\./g,'') #or using sysname result[0].value?
name: result[0].value
class: 'SnmpSensor'
oid: '.1.3.6.1.2.1.1.5.0'
host: target

@framework.deviceManager.discoveredDevice 'pimatic-snmp', "#{deviceConfig.name}", deviceConfig
).catch ( (err) ->
return
)
)

i++
pingCount++

if pingCount > maxPings
@framework.deviceManager.discoverMessage 'pimatic-snmp', "Could not ping all networks, max ping cound reached."
)


listInterfaces: () ->
interfaces = []
ifaces = os.networkInterfaces()
Object.keys(ifaces).forEach( (ifname) =>
ifaces[ifname].forEach (iface) =>
# skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
if 'IPv4' isnt iface.family or iface.internal isnt false then return
if @debug
env.logger.debug iface
interfaces.push {name: ifname, address: iface.address, netmask: iface.netmask}
)
return interfaces


class SnmpSensor extends env.devices.Sensor

Expand Down

0 comments on commit fa51a9b

Please sign in to comment.