/openbus.logstash/logstash/linux/inputs/snmptrap.rb

https://gitlab.com/marcelosabino/OpenbusBR · Ruby · 89 lines · 64 code · 10 blank · 15 comment · 3 complexity · fc6b002b6efc641c2761ec268e9ed9d5 MD5 · raw file

  1. # encoding: utf-8
  2. require "logstash/inputs/base"
  3. require "logstash/namespace"
  4. # Read snmp trap messages as events
  5. #
  6. # Resulting @message looks like :
  7. # #<SNMP::SNMPv1_Trap:0x6f1a7a4 @varbind_list=[#<SNMP::VarBind:0x2d7bcd8f @value="teststring",
  8. # @name=[1.11.12.13.14.15]>], @timestamp=#<SNMP::TimeTicks:0x1af47e9d @value=55>, @generic_trap=6,
  9. # @enterprise=[1.2.3.4.5.6], @source_ip="127.0.0.1", @agent_addr=#<SNMP::IpAddress:0x29a4833e @value="\xC0\xC1\xC2\xC3">,
  10. # @specific_trap=99>
  11. #
  12. class LogStash::Inputs::Snmptrap < LogStash::Inputs::Base
  13. config_name "snmptrap"
  14. milestone 1
  15. # The address to listen on
  16. config :host, :validate => :string, :default => "0.0.0.0"
  17. # The port to listen on. Remember that ports less than 1024 (privileged
  18. # ports) may require root to use. hence the default of 1062.
  19. config :port, :validate => :number, :default => 1062
  20. # SNMP Community String to listen for.
  21. config :community, :validate => :string, :default => "public"
  22. # directory of YAML MIB maps (same format ruby-snmp uses)
  23. config :yamlmibdir, :validate => :string
  24. def initialize(*args)
  25. super(*args)
  26. end # def initialize
  27. public
  28. def register
  29. require "snmp"
  30. @snmptrap = nil
  31. if @yamlmibdir
  32. @logger.info("checking #{@yamlmibdir} for MIBs")
  33. Dir["#{@yamlmibdir}/*.yaml"].each do |yamlfile|
  34. mib_name = File.basename(yamlfile, ".*")
  35. @yaml_mibs ||= []
  36. @yaml_mibs << mib_name
  37. end
  38. @logger.info("found MIBs: #{@yaml_mibs.join(',')}") if @yaml_mibs
  39. end
  40. end # def register
  41. public
  42. def run(output_queue)
  43. begin
  44. # snmp trap server
  45. snmptrap_listener(output_queue)
  46. rescue => e
  47. @logger.warn("SNMP Trap listener died", :exception => e, :backtrace => e.backtrace)
  48. sleep(5)
  49. retry
  50. end # begin
  51. end # def run
  52. private
  53. def snmptrap_listener(output_queue)
  54. traplistener_opts = {:Port => @port, :Community => @community, :Host => @host}
  55. if @yaml_mibs && !@yaml_mibs.empty?
  56. traplistener_opts.merge!({:MibDir => @yamlmibdir, :MibModules => @yaml_mibs})
  57. end
  58. @logger.info("It's a Trap!", traplistener_opts.dup)
  59. @snmptrap = SNMP::TrapListener.new(traplistener_opts)
  60. @snmptrap.on_trap_default do |trap|
  61. begin
  62. event = LogStash::Event.new("message" => trap.inspect, "host" => trap.source_ip)
  63. decorate(event)
  64. event['varbind_string'] = ""
  65. trap.each_varbind do |vb|
  66. event[vb.name.to_s] = vb.value.to_s
  67. event['varbind_string'] << vb.name.to_s + ":\t\t" + vb.value.to_s + "\n"
  68. end
  69. @logger.debug("SNMP Trap received: ", :trap_object => trap.inspect)
  70. output_queue << event
  71. rescue => event
  72. @logger.error("Failed to create event", :trap_object => trap.inspect)
  73. end
  74. end
  75. @snmptrap.join
  76. end # def snmptrap_listener
  77. end # class LogStash::Inputs::Snmptrap