Class: SnmpTrapServer

Inherits:
Object
  • Object
show all
Defined in:
lib/monitor/server/snmp/snmptrap_server.rb

Overview

Could not find default MIB directory, tried:

/usr/share/ruby/snmp/mibs
/usr/lib/ruby/data/ruby/snmp/mibs

Instance Method Summary collapse

Constructor Details

#initialize(buff) ⇒ SnmpTrapServer

Returns a new instance of SnmpTrapServer.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/monitor/server/snmp/snmptrap_server.rb', line 19

def initialize(buff)
  @buff=buff
  begin
    if !snmp_traps_supported?()
      $log.error("Ruby SNMP lib version is too old, you must at least upgrade to 1.0.1")
    end
    @server = SNMP::TrapListener.new(:Host=>'', :Port => $config.snmptrap_port, :Community => $config.snmptrap_community) do |manager|
      $log.info("SNMP trap Server: listening on udp port #{$config.snmptrap_port}")
      manager.on_trap_default do |trap|
        @buff.put trap
        $log.debug("Trap received: #{trap.inspect}")
      end
    end
  rescue Errno::EADDRINUSE => msg
    $log.fatal("Syslog Server: #{msg}, socket in use")
    raise Errno::EADDRINUSE
  rescue Errno::EACCES => msg
    $log.fatal("Syslog Server: #{msg}, need root perms")
    raise Errno::EACCES
  rescue Exception => e
    raise e
  end
end

Instance Method Details

#close_socketObject



43
44
45
# File 'lib/monitor/server/snmp/snmptrap_server.rb', line 43

def close_socket()
  @server.exit() unless @server == nil
end

#get_threadObject



47
48
49
# File 'lib/monitor/server/snmp/snmptrap_server.rb', line 47

def get_thread
  @server
end

#snmp_traps_supported?Boolean

check if snmp lib support traps

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
# File 'lib/monitor/server/snmp/snmptrap_server.rb', line 10

def snmp_traps_supported?
  begin
    SNMP::UDPServerTransport.new('localhost', 9999, Socket::AF_INET)
    return true
  rescue ArgumentError
    return false
  end
end