Class: Spectacular::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/spectacular/device.rb

Defined Under Namespace

Classes: Klass

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, history = 10) ⇒ Device

Returns a new instance of Device.



9
10
11
12
# File 'lib/spectacular/device.rb', line 9

def initialize host, history = 10
  @host    = host
  @history = new_history history
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



7
8
9
# File 'lib/spectacular/device.rb', line 7

def history
  @history
end

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/spectacular/device.rb', line 7

def host
  @host
end

Instance Method Details

#columnsObject



14
15
16
# File 'lib/spectacular/device.rb', line 14

def columns
  ["ifIndex", "ifDescr", "ifInOctets", "ifOutOctets"]
end

#interfacesObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/spectacular/device.rb', line 18

def interfaces
  manager = snmp_manager host
  interfaces = []
  manager.walk(['ifDescr']) do |row|
    interfaces << row.first.value.to_s
  end
  interfaces
ensure
  manager.close
end

#monitor(interval = 1) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/spectacular/device.rb', line 29

def monitor interval = 1
  manager = snmp_manager host

  loop do
    manager.walk(columns) do |row|
      key = row[1].value.to_s

      result = diff history.last(key), row

      yield result unless result.empty?

      history.add key, row.map(&:dup)
    end
    sleep interval
  end

ensure
  manager.close
end