class Network::Monitor::Interface

Constants

Counter32Max
QueryColumns

Public Class Methods

hosts() click to toggle source
# File lib/network/monitor/interface.rb, line 33
def self.hosts
        Interface.all.group("host").collect { |r| r.host }
end
print_statistics(out, min = 0.01) click to toggle source
query(host) click to toggle source
# 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

Public Instance Methods

previous() click to toggle source
# 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_string() click to toggle source
# File lib/network/monitor/interface.rb, line 78
def speed_string
        bits_to_string(ifSpeed.to_i)
end
statistics(prev) click to toggle source
# 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

Private Instance Methods

bits_to_string(size) click to toggle source
# File lib/network/monitor/interface.rb, line 147
def bits_to_string(size)
        human_size = size
        levels = 0
  
        while human_size >= 1000
                human_size /= 1000.0
                levels += 1
        end
  
        #maybe localize this?
        sprintf("%0.1f%s", human_size, ['', 'K', 'M', 'G', 'T', 'X'].fetch(levels)) + 'b'
end