Advertisement
Guest User

Untitled

a guest
Apr 17th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fs = require('fs')
  2. var http = require('http')
  3. var mime = require('mime')
  4. var snmp = require('snmpjs')
  5. var Logger = require('bunyan')
  6. var util = require('util')
  7.  
  8. var config = JSON.parse(fs.readFileSync(process.argv[2] || 'microcontrold.json'))
  9. config.log = config.log || {}
  10. config.snmp = config.snmp || {}
  11. config.snmp.manager = config.snmp.manager || {}
  12. config.snmp.manager.trap_community = config.snmp.manager.trap_community || null
  13. config.http = config.http || {}
  14. config.http.port = config.http.port || 80
  15. config.http.static_directory = config.http.static_directory || null
  16.  
  17. var log_options = { name: 'microcontrold' }
  18. switch (typeof config.log)
  19. {
  20.     case 'array':
  21.         log_options.streams = config.log
  22.         break
  23.     case 'object':
  24.         if ('type' in config.log || 'path' in config.log || 'stream' in config.log)
  25.             log_options.streams = [ config.log ]
  26.         else if ('level' in config.log)
  27.             log_options.level = config.log.level
  28.         break
  29. }
  30. var log = new Logger(log_options)
  31.  
  32. var output_oids = {
  33.     '1.3.6.1.4.1.17095.3.1.0':  'OUT0',
  34.     '1.3.6.1.4.1.17095.3.2.0':  'OUT1',
  35.     '1.3.6.1.4.1.17095.3.3.0':  'OUT2',
  36.     '1.3.6.1.4.1.17095.3.4.0':  'OUT3',
  37.     '1.3.6.1.4.1.17095.3.5.0':  'OUT4',
  38.     '1.3.6.1.4.1.17095.3.6.0':  'OUT5',
  39. }
  40.  
  41. var input_oids = {
  42.     '1.3.6.1.4.1.17095.4.1.0':  'TEMP',
  43.     '1.3.6.1.4.1.17095.4.2.0':  'VCC',
  44.     '1.3.6.1.4.1.17095.4.3.0':  'INP1',
  45.     '1.3.6.1.4.1.17095.4.4.0':  'INP2',
  46.     '1.3.6.1.4.1.17095.4.5.0':  'INP3',
  47.     '1.3.6.1.4.1.17095.4.6.0':  'INP4',
  48.     '1.3.6.1.4.1.17095.4.7.0':  'INP5',
  49.     '1.3.6.1.4.1.17095.5.1.0':  'INP6',
  50.     '1.3.6.1.4.1.17095.5.2.0':  'INP7',
  51.     '1.3.6.1.4.1.17095.5.3.0':  'INP8',
  52.     '1.3.6.1.4.1.17095.5.4.0':  'INP9',
  53.     '1.3.6.1.4.1.17095.5.5.0':  'INP10',
  54.     '1.3.6.1.4.1.17095.5.6.0':  'INP11',
  55.     '1.3.6.1.4.1.17095.6.1.0':  'DTH22t',
  56.     '1.3.6.1.4.1.17095.6.2.0':  'DTH22h',
  57. //  '1.3.6.1.4.1.17095.7.1.0':  'I3XI5',
  58. //  '1.3.6.1.4.1.17095.7.2.0':  'PXT',
  59.     '1.3.6.1.4.1.17095.10.1.0': 'INP1D',
  60.     '1.3.6.1.4.1.17095.10.2.0': 'INP2D',
  61.     '1.3.6.1.4.1.17095.10.3.0': 'INP3D',
  62.     '1.3.6.1.4.1.17095.10.4.0': 'INP4D',
  63. }
  64.  
  65. /*
  66. var inputs = {}
  67. for (oid in input_oids)
  68.     inputs[input_oids[oid]] = { name: input_oids[oid], oid: oid, value: null }
  69. */
  70. var inputs = Object.keys(input_oids).reduce(function(s, oid)
  71. {
  72.     s[input_oids[oid]] = { name: input_oids[oid], oid: oid, value: null }; return s
  73. }, {}) //Object.create(null)
  74.  
  75. var httpd = http.createServer()
  76. httpd.log = log.child({ component: 'httpd', serializers: { request: Logger.stdSerializers.req } })
  77. httpd.on('request', function(request, response)
  78. {
  79.     if (request.method == 'GET')
  80.     {
  81.         if (status = /^\/status(?:\/(.+))?/.exec(request.url))
  82.         {
  83.             var input = status[1]
  84.             var result = typeof input !== 'undefined' ? result = inputs[input] : inputs
  85.             response.writeHead(200, { 'Content-Type': 'application/json'})
  86.             response.write(JSON.stringify(result))
  87.             response.end()
  88.         }
  89.         else
  90.         {
  91.             if (request.url == '/') request.url = '/index.html'
  92.             var filename = config.http.static_directory + request.url
  93.             fs.readFile(filename, function(error, contents)
  94.             {
  95.                 if (error)
  96.                     if (error.code == 'ENOENT')
  97.                         response.writeHead(404, 'File Not Found')
  98.                     else
  99.                     {
  100.                         response.writeHead(500, 'Internal Server Error')
  101.                         response.write(JSON.stringify(error))
  102.                     }
  103.                 if (contents)
  104.                 {
  105.                     response.writeHead(200, { 'Content-Length': contents.length, 'Content-Type': mime.lookup(filename) })
  106.                     response.write(contents)
  107.                 }
  108.                 response.end()
  109.             })
  110.         }
  111.     }
  112. })
  113. httpd.listen(config.http.port, function() { httpd.log.info('Listening on port ' + httpd.address().port) })
  114.  
  115. var socket_io_server = require('socket.io')(httpd)
  116. socket_io_server.on('connection', function(socket)
  117. {
  118.     httpd.log.info({ client_id: socket.client.id, request: socket.client.request }, 'Connect')
  119.     socket.emit('status', inputs)
  120.     socket.join('microcontrold')
  121. })
  122.  
  123. var snmpd = snmp.createTrapListener({ name: 'snmpd', log: log })
  124. snmpd._log.fields.component = 'snmpd'
  125. snmpd.on('trap', function(snmpmsg)
  126. {
  127.     if (
  128.         // Access Checks
  129.         (snmpmsg.community == config.snmp.manager.trap_community) &&
  130.         (snmpmsg.pdu.agent_addr == '10.98.184.246') &&
  131.         // Trap Checks
  132.         (snmpmsg.pdu.generic_trap == 6) &&
  133.         (snmpmsg.pdu.specific_trap == 768) &&
  134.         (snmpmsg.pdu.enterprise == '1.3.6.1.4.1.17095')
  135.     )
  136.         for (var vb = 0; vb < snmpmsg.pdu.varbinds.length; vb++)
  137.         {
  138.             var oid = snmpmsg.pdu.varbinds[vb].oid
  139.             var name = input_oids[oid]
  140.             var value = snmpmsg.pdu.varbinds[vb].data.value
  141.             var input = inputs[name]
  142.  
  143.             if (input.value != value)
  144.             {
  145.                 snmpd._log.info({ input: { oid: oid, name: name, 'old value': input.value, 'new value': value } }, 'Update')
  146.                 input.value = value
  147.                 socket_io_server.to('microcontrold').emit('update', input)
  148.             }
  149.         }
  150. })
  151. snmpd.bind({family: 'udp4', port: 162})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement