Class: Solanum::Source::Network

Inherits:
Solanum::Source show all
Defined in:
lib/solanum/source/network.rb

Overview

bytes - The total number of bytes of data transmitted or received by the interface. packets - The total number of packets of data transmitted or received by the interface. errs - The total number of transmit or receive errors detected by the device driver. drop - The total number of packets dropped by the device driver. fifo - The number of FIFO buffer errors. frame - The number of packet framing errors. colls - The number of collisions detected on the interface. compressed - The number of compressed packets transmitted or received by the device driver. (This appears to be unused in the 2.2.15 kernel.) carrier - The number of carrier losses detected by the device driver. multicast - The number of multicast frames transmitted or received by the device driver.

Constant Summary collapse

STAT_FILE =
'/proc/net/dev'
FIELDS =
%w{
  rx_bytes rx_packets rx_errs rx_drop rx_fifo rx_frame rx_compressed rx_multicast
  tx_bytes tx_packets tx_errs tx_drop tx_fifo tx_colls tx_carrier tx_compressed
}
SIMPLE_FIELDS =
%w{rx_bytes rx_packets tx_bytes tx_packets}

Instance Attribute Summary collapse

Attributes inherited from Solanum::Source

#attributes, #period, #type

Instance Method Summary collapse

Methods inherited from Solanum::Source

#next_run

Constructor Details

#initialize(opts) ⇒ Network

Returns a new instance of Network.



26
27
28
29
30
31
# File 'lib/solanum/source/network.rb', line 26

def initialize(opts)
  super(opts)
  @interfaces = opts['interfaces'] || []
  @detailed = opts['detailed'] || false
  @last = {}
end

Instance Attribute Details

#detailedObject (readonly)

Returns the value of attribute detailed.



14
15
16
# File 'lib/solanum/source/network.rb', line 14

def detailed
  @detailed
end

#interfacesObject (readonly)

Returns the value of attribute interfaces.



14
15
16
# File 'lib/solanum/source/network.rb', line 14

def interfaces
  @interfaces
end

Instance Method Details

#collect!Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/solanum/source/network.rb', line 41

def collect!
  events = []

  File.readlines(STAT_FILE).drop(2).each do |line|
    iface, stats = parse_stats(line)

    if @interfaces.empty? || @interfaces.include?(iface)
      if @last[iface]
        FIELDS.each do |field|
          next unless @detailed || SIMPLE_FIELDS.include?(field)
          diff = stats[field] - @last[iface][field]
          events << {
            service: "net #{iface} #{field.gsub('_', ' ')}",
            metric: diff,
          }
        end
      end
      @last[iface] = stats
    end
  end

  events
end

#parse_stats(line) ⇒ Object



34
35
36
37
38
# File 'lib/solanum/source/network.rb', line 34

def parse_stats(line)
  columns = line.strip.split(/\s+/)
  iface = columns.shift.chomp(':')
  return iface, Hash[FIELDS.zip(columns.map(&:to_i))]
end