forked from phokz/ruby-snmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink_down.rb
49 lines (39 loc) · 1.2 KB
/
link_down.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require 'rubygems'
require 'snmp'
require 'logger'
log = Logger.new($stdout)
mib = SNMP::MIB.new
mib.load_module("IF-MIB")
linkDown_OID = mib.oid("linkDown")
# 1 is "up" and 2 is "down" in the MIB
ADMIN_UP = 1
ADMIN_DOWN = 2
listener = SNMP::TrapListener.new(:Port => 1062, :Community => 'public') do |listener|
log.info "Listening for linkDown traps"
listener.on_trap(linkDown_OID) do |trap|
vb_list = trap.vb_list
up_time = vb_list[0].value # standard varbind - always in v2c trap
trap_oid = vb_list[1].value # standard varbind - always in v2c trap
# extract trap info - order determined by IF-MIB
ifIndex = vb_list[2].value
adminStatus = vb_list[3].value
operStatus = vb_list[4].value
# log stuff based on interface status
if adminStatus == ADMIN_DOWN
log.info "Interface #{ifIndex} turned down"
else
log.error "Problem on interface #{ifIndex}! Link unintentionally down"
end
end
listener.on_trap_v1 do |trap|
log.warn "Unexpected v1 trap: #{trap.generic_trap}"
end
listener.on_trap_v2c do |trap|
log.warn "Unexpected v2c trap: #{trap.trap_oid}"
end
end
trap("INT") do
puts "\nShutting down"
listener.kill
end
listener.join