Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs')
- var http = require('http')
- var mime = require('mime')
- var snmp = require('snmpjs')
- var Logger = require('bunyan')
- var util = require('util')
- var config = JSON.parse(fs.readFileSync(process.argv[2] || 'microcontrold.json'))
- config.log = config.log || {}
- config.snmp = config.snmp || {}
- config.snmp.manager = config.snmp.manager || {}
- config.snmp.manager.trap_community = config.snmp.manager.trap_community || null
- config.http = config.http || {}
- config.http.port = config.http.port || 80
- config.http.static_directory = config.http.static_directory || null
- var log_options = { name: 'microcontrold' }
- switch (typeof config.log)
- {
- case 'array':
- log_options.streams = config.log
- break
- case 'object':
- if ('type' in config.log || 'path' in config.log || 'stream' in config.log)
- log_options.streams = [ config.log ]
- else if ('level' in config.log)
- log_options.level = config.log.level
- break
- }
- var log = new Logger(log_options)
- var output_oids = {
- '1.3.6.1.4.1.17095.3.1.0': 'OUT0',
- '1.3.6.1.4.1.17095.3.2.0': 'OUT1',
- '1.3.6.1.4.1.17095.3.3.0': 'OUT2',
- '1.3.6.1.4.1.17095.3.4.0': 'OUT3',
- '1.3.6.1.4.1.17095.3.5.0': 'OUT4',
- '1.3.6.1.4.1.17095.3.6.0': 'OUT5',
- }
- var input_oids = {
- '1.3.6.1.4.1.17095.4.1.0': 'TEMP',
- '1.3.6.1.4.1.17095.4.2.0': 'VCC',
- '1.3.6.1.4.1.17095.4.3.0': 'INP1',
- '1.3.6.1.4.1.17095.4.4.0': 'INP2',
- '1.3.6.1.4.1.17095.4.5.0': 'INP3',
- '1.3.6.1.4.1.17095.4.6.0': 'INP4',
- '1.3.6.1.4.1.17095.4.7.0': 'INP5',
- '1.3.6.1.4.1.17095.5.1.0': 'INP6',
- '1.3.6.1.4.1.17095.5.2.0': 'INP7',
- '1.3.6.1.4.1.17095.5.3.0': 'INP8',
- '1.3.6.1.4.1.17095.5.4.0': 'INP9',
- '1.3.6.1.4.1.17095.5.5.0': 'INP10',
- '1.3.6.1.4.1.17095.5.6.0': 'INP11',
- '1.3.6.1.4.1.17095.6.1.0': 'DTH22t',
- '1.3.6.1.4.1.17095.6.2.0': 'DTH22h',
- // '1.3.6.1.4.1.17095.7.1.0': 'I3XI5',
- // '1.3.6.1.4.1.17095.7.2.0': 'PXT',
- '1.3.6.1.4.1.17095.10.1.0': 'INP1D',
- '1.3.6.1.4.1.17095.10.2.0': 'INP2D',
- '1.3.6.1.4.1.17095.10.3.0': 'INP3D',
- '1.3.6.1.4.1.17095.10.4.0': 'INP4D',
- }
- /*
- var inputs = {}
- for (oid in input_oids)
- inputs[input_oids[oid]] = { name: input_oids[oid], oid: oid, value: null }
- */
- var inputs = Object.keys(input_oids).reduce(function(s, oid)
- {
- s[input_oids[oid]] = { name: input_oids[oid], oid: oid, value: null }; return s
- }, {}) //Object.create(null)
- var httpd = http.createServer()
- httpd.log = log.child({ component: 'httpd', serializers: { request: Logger.stdSerializers.req } })
- httpd.on('request', function(request, response)
- {
- if (request.method == 'GET')
- {
- if (status = /^\/status(?:\/(.+))?/.exec(request.url))
- {
- var input = status[1]
- var result = typeof input !== 'undefined' ? result = inputs[input] : inputs
- response.writeHead(200, { 'Content-Type': 'application/json'})
- response.write(JSON.stringify(result))
- response.end()
- }
- else
- {
- if (request.url == '/') request.url = '/index.html'
- var filename = config.http.static_directory + request.url
- fs.readFile(filename, function(error, contents)
- {
- if (error)
- if (error.code == 'ENOENT')
- response.writeHead(404, 'File Not Found')
- else
- {
- response.writeHead(500, 'Internal Server Error')
- response.write(JSON.stringify(error))
- }
- if (contents)
- {
- response.writeHead(200, { 'Content-Length': contents.length, 'Content-Type': mime.lookup(filename) })
- response.write(contents)
- }
- response.end()
- })
- }
- }
- })
- httpd.listen(config.http.port, function() { httpd.log.info('Listening on port ' + httpd.address().port) })
- var socket_io_server = require('socket.io')(httpd)
- socket_io_server.on('connection', function(socket)
- {
- httpd.log.info({ client_id: socket.client.id, request: socket.client.request }, 'Connect')
- socket.emit('status', inputs)
- socket.join('microcontrold')
- })
- var snmpd = snmp.createTrapListener({ name: 'snmpd', log: log })
- snmpd._log.fields.component = 'snmpd'
- snmpd.on('trap', function(snmpmsg)
- {
- if (
- // Access Checks
- (snmpmsg.community == config.snmp.manager.trap_community) &&
- (snmpmsg.pdu.agent_addr == '10.98.184.246') &&
- // Trap Checks
- (snmpmsg.pdu.generic_trap == 6) &&
- (snmpmsg.pdu.specific_trap == 768) &&
- (snmpmsg.pdu.enterprise == '1.3.6.1.4.1.17095')
- )
- for (var vb = 0; vb < snmpmsg.pdu.varbinds.length; vb++)
- {
- var oid = snmpmsg.pdu.varbinds[vb].oid
- var name = input_oids[oid]
- var value = snmpmsg.pdu.varbinds[vb].data.value
- var input = inputs[name]
- if (input.value != value)
- {
- snmpd._log.info({ input: { oid: oid, name: name, 'old value': input.value, 'new value': value } }, 'Update')
- input.value = value
- socket_io_server.to('microcontrold').emit('update', input)
- }
- }
- })
- snmpd.bind({family: 'udp4', port: 162})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement