Class: Opticon::Notifier::SNMPTrap

Inherits:
Object
  • Object
show all
Defined in:
lib/opticon/notifier.rb

Overview

Sends failure notifications as SNMP traps.

The notification is sent using the ‘snmptrap` command-line utility, part of the Net-SNMP perl package. You will have to install Net-SNMP prior to using this notifier. On Linux machines this is generally as easy as `apt-get install net-snmp` or `smart install net-snmp`. On Windows machines, you’re on your own.

In the future the net-snmp requirement may be swapped in favor of using the native Ruby SNMP library, but for now you need net-snmp.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to_host, snmp_community, options = {}) ⇒ SNMPTrap

Returns a new instance of SNMPTrap.



94
95
96
97
98
99
# File 'lib/opticon/notifier.rb', line 94

def initialize(to_host, snmp_community, options = {})
  @to_host = to_host
  @snmp_community = snmp_community
  @enterprise_oid = options[:enterprise_oid] || '1.3.6.1.4.1.3.1.1'
  @snmp_persistent_dir = options[:snmp_persistent_dir] || '/tmp'
end

Instance Attribute Details

#enterprise_oidObject

Returns the value of attribute enterprise_oid.



91
92
93
# File 'lib/opticon/notifier.rb', line 91

def enterprise_oid
  @enterprise_oid
end

#snmp_communityObject

Returns the value of attribute snmp_community.



91
92
93
# File 'lib/opticon/notifier.rb', line 91

def snmp_community
  @snmp_community
end

#snmp_persistent_dirObject

Returns the value of attribute snmp_persistent_dir.



91
92
93
# File 'lib/opticon/notifier.rb', line 91

def snmp_persistent_dir
  @snmp_persistent_dir
end

#to_hostObject

Returns the value of attribute to_host.



91
92
93
# File 'lib/opticon/notifier.rb', line 91

def to_host
  @to_host
end

Instance Method Details

#notify(failures) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/opticon/notifier.rb', line 101

def notify(failures)
  failures = [failures] unless failures.kind_of? Array
  
  if ARGV.include?("-v")
    puts "Sending SNMP trap(s) to #{to_host} regarding #{failures.size} failures:"
    failures.each{|f| puts "  #{f.failure_message}"}
  end
  
  failures.each do |f|
    oid = '1.3.1.2.1.1.0'
    typ = 's'
    
    # TODO: make msg format configurable
    msg = ("Opticon Test Failure on URL #{f.uri} :: #{f.failure_message}").gsub(/"/, '\"')
    
    debug = "-d" if ARGV.include?("-v")
    cmd = %{snmptrap -v 1 #{debug} -c #{snmp_community} #{to_host}  #{enterprise_oid} #{ENV['HOSTNAME']} 6 0 '' #{oid} #{typ} "#{msg}"}
    
    puts ">> #{cmd}" if ARGV.include?("-v")
    
    # Band-aid fix for bug in Net-SNMP.
    # See http://sourceforge.net/tracker/index.php?func=detail&aid=1588455&group_id=12694&atid=112694
    ENV['SNMP_PERSISTENT_DIR'] ||= snmp_persistent_dir
    
    `#{cmd}`
  end
end