Class: Network::Monitor::Interface

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/network/monitor/interface.rb

Constant Summary collapse

Counter32Max =
(2**32) - 1
QueryColumns =
["ifIndex", "ifSpeed", "ifInOctets", "ifInErrors", "ifOutOctets", "ifOutErrors"]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.hostsObject



33
34
35
# File 'lib/network/monitor/interface.rb', line 33

def self.hosts
	Interface.all.group("host").collect { |r| r.host }
end


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/network/monitor/interface.rb', line 86

def self.print_statistics(out, min = 0.01)
	unused_count = 0

	error_interfaces = []

	out.puts "Utilization Report @ #{Time.now.to_s}".center(84)
	Interface.all.group("host, ifIndex").each do |iface|
		r2, r1 = Interface.where("host = ? and ifIndex = ?", iface.host, iface.ifIndex).limit(2).order("created_at desc").to_a

		next if r1 == nil or r2 == nil

		unused = true
		name = iface.host

		s = r2.statistics(r1)
   
		if s[:ifInErrors] > 0 or s[:ifOutErrors] > 0
			error_interfaces << [iface, name, s]
			unused = false
		end
   
		if s[:ifInUsage] > min or s[:ifOutUsage] > min
			out.print "#{name.rjust(20)}: #{iface.ifIndex.to_s.rjust(4)}    "

			[[:ifInUsage, "IN%"], [:ifOutUsage, "OUT%"]].each do |v|
				key, name = *v
       
				out.print name.rjust(8)
				out.print sprintf("%0.2f\%", s[key] * 100.0).rjust(8)
			end
			out.puts r1.speed_string.rjust(10)
     
			unused = false
		end
   
		unused_count += 1 if unused
	end
 
	if error_interfaces.size > 0
		out.puts "Errors Detected".center(84)
		error_interfaces.each do |r|
			iface, name, s = r
     
			out.print "#{name.rjust(20)}: #{iface.ifIndex.to_s.rjust(4)}    "
     
			[[:ifInErrors, "IN\#"], [:ifOutErrors, "OUT\#"]].each do |v|
				key, name = v
       
				out.print name.rjust(5)
				out.print s[key].to_s.rjust(8)
			end
     
			out.puts
		end
	end
 
	out.puts "#{unused_count} ports not displayed.".center(84)
end

.query(host) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/network/monitor/interface.rb', line 37

def self.query(host)
	SNMP::Manager.open(:Host => host, :Version => :SNMPv1) do |manager|
		manager.walk(QueryColumns) do |row|
			next if row[1].value.to_i == 0 # Speed of interface is reported as 0 by some faulty equipment, ignore these ports.

			record = Interface.new(:host => host)

			QueryColumns.each_with_index do |c,i|
				record.send("#{c}=", row[i].value.to_i)
			end

			record.save!
		end
	end
end

Instance Method Details

#previousObject



82
83
84
# File 'lib/network/monitor/interface.rb', line 82

def previous
	Interface.where("host = ? and ifIndex = ? and id < ?", host, ifIndex, id).limit(1).order("created_at desc").first
end

#speed_stringObject



78
79
80
# File 'lib/network/monitor/interface.rb', line 78

def speed_string
	bits_to_string(ifSpeed.to_i)
end

#statistics(prev) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/network/monitor/interface.rb', line 53

def statistics(prev)
	dt = (created_at - prev.created_at).to_f
	s = {
		:startTime => prev.created_at,
		:endTime => created_at,
		:timePeriod => created_at - prev.created_at,
		:ifInOctets => ifInOctets - prev.ifInOctets,
		:ifOutOctets => ifOutOctets - prev.ifOutOctets,
		:ifInErrors => ifInErrors - prev.ifInErrors,
		:ifOutErrors => ifOutErrors - prev.ifOutErrors
	}

	# Counter32 may wrap and produce strange results unless we deal with it..
	[:ifInOctets, :ifOutOctets, :ifInErrors, :ifOutErrors].each do |c|
		if s[c] < 0
			s[c] = Counter32Max + s[c]
		end
	end

	s[:ifInUsage] = (s[:ifInOctets].to_f * 8.0) / (ifSpeed.to_f * dt)
	s[:ifOutUsage] = (s[:ifOutOctets].to_f * 8.0) / (ifSpeed.to_f * dt)

	return s
end