Skip to content
Browse files

few new bits of code: some basic alarm tracking concepts, etc.

not sure if things will stay like this, but its a good checkpoint
for the moment, and it works (for IF-MIB::link{Down,Up} only).

It may not be as modular or as flexible as required for the future,
however, and its not as succinct as I'd like.
  • Loading branch information...
1 parent 4b0e58e commit 65ba1dec1ea25aee00f37e824731c368464d530d @jontow committed
Showing with 84 additions and 15 deletions.
  1. +9 −0 core/alarm.rb
  2. +62 −9 core/main.rb
  3. +2 −2 handlers/snmp-trap-handler/if-mib-handler.rb
  4. +11 −4 handlers/snmp-trap-handler/snmp-trap-handler.rb
View
9 core/alarm.rb
@@ -0,0 +1,9 @@
+#
+# 2011-10-21 -- jontow@zenbsd.net
+#
+
+class Alarm
+ def initialize
+ # ?
+ end
+end
View
71 core/main.rb
@@ -11,6 +11,7 @@
require 'json'
require File.dirname(__FILE__) + '/config'
+require File.dirname(__FILE__) + '/alarm'
require File.dirname(__FILE__) + '/../handlers/snmp-trap-handler/snmp-trap-handler'
Thread.abort_on_exception = true
@@ -20,6 +21,8 @@ def initialize
@sockets = []
@eventqueue = Queue.new
@cfg = CoreConfig.new
+ @alarm = Alarm.new
+ @active_alarms = []
corelisten
end
@@ -58,24 +61,74 @@ def runloop
end
def parse_event(rawevent)
- begin
- event = JSON.parse(rawevent)
- route_event(event)
- rescue => e
- puts "Malformed event! JSON parse error: #{e.message}"
- p rawevent
- end
+ # XXX
+ # I don't know if this code will stay here or not.. doesn't
+ # seem right to duplicate it, and the use case seems rare.
+ #
+ #if rawevent =~ /^set (.*)/
+ # if alarm_set($1)
+ # puts "\nSET ALARM: #{$1}\n\n"
+ # else
+ # puts "\nSET ALARM (ALREADYSET): #{$1}\n\n"
+ # end
+ #elsif rawevent =~ /^clear (.*)/
+ # if alarm_clear($1)
+ # puts "\nCLEAR ALARM: #{$1}\n\n"
+ # else
+ # puts "\nCLEAR ALARM (NOTSET): #{$1}\n\n"
+ # end
+ #else
+ begin
+ event = JSON.parse(rawevent)
+ route_event(event)
+ rescue => e
+ puts "Malformed event! JSON parse error: #{e.message}"
+ end
+ #end
end
def route_event(event)
case event['type']
when "snmptrap"
- SNMPTrapHandler.new(event)
+ th = SNMPTrapHandler.new(event)
+ case th.state
+ when :set
+ set(th.alarm)
+ when :clear
+ clear(th.alarm)
+ else
+ puts "Trap state unknown: #{th.alarm}"
+ end
else
puts "GOT UNHANDLED EVENT:"
puts event
end
-
+ end
+
+ def alarm_active?(alarm)
+ @active_alarms.include?(alarm)
+ end
+
+ def set(alarm)
+ if !alarm_active?(alarm)
+ @active_alarms << alarm
+ puts "ALARM SET (#{@active_alarms.join(", ")})"
+ return true
+ else
+ puts "ALARM ALREADY SET (#{@active_alarms.join(", ")})"
+ return false
+ end
+ end
+
+ def clear(alarm)
+ if alarm_active?(alarm)
+ @active_alarms.delete(alarm)
+ puts "ALARM CLEAR (#{@active_alarms.join(", ")})"
+ return true
+ else
+ puts "ALARM ALREADY CLEAR (#{@active_alarms.join(", ")})"
+ return false
+ end
end
end
View
4 handlers/snmp-trap-handler/if-mib-handler.rb
@@ -7,13 +7,13 @@
module IFMIB
def self.linkdown(evinfo)
if evinfo =~ /IF-MIB::ifName\.[0-9]+\ ::\ (.*)/
- return "linkDown: #{$1}"
+ return "linkfail: #{$1}"
end
end
def self.linkup(evinfo)
if evinfo =~ /IF-MIB::ifName\.[0-9]+\ ::\ (.*)/
- return "linkUp: #{$1}"
+ return "linkfail: #{$1}"
end
end
end
View
15 handlers/snmp-trap-handler/snmp-trap-handler.rb
@@ -9,17 +9,24 @@
end
class SNMPTrapHandler
+ attr_reader :state, :alarm
+
def initialize(event)
+ @state = nil
+ @alarm = nil
+
puts "TRAP -- #{event['name']}:"
puts event['info']
case event['name']
when "IF-MIB::linkDown"
- alarm = IFMIB.linkdown(event['info'])
- p alarm
+ @state = :set
+ @alarm = IFMIB.linkdown(event['info'])
+ p @alarm
when "IF-MIB::linkUp"
- alarm = IFMIB.linkup(event['info'])
- p alarm
+ @state = :clear
+ @alarm = IFMIB.linkup(event['info'])
+ p @alarm
else
puts "Unhandled IF-MIB trap: #{event['name']}"
end

0 comments on commit 65ba1de

Please sign in to comment.
Something went wrong with that request. Please try again.