Class: ServerMetrics::Network

Inherits:
MultiCollector show all
Defined in:
lib/server_metrics/collectors/network.rb

Instance Attribute Summary

Attributes inherited from Collector

#collector_id, #data, #error

Instance Method Summary collapse

Methods inherited from MultiCollector

#counter, #memory, #remember, #report

Methods inherited from Collector

#convert_to_mb, #counter, from_hash, #initialize, #linux?, #memory, #normalize_key, #option, #osx?, #remember, #report, #run, #to_hash

Constructor Details

This class inherits a constructor from ServerMetrics::Collector

Instance Method Details

#build_reportObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/server_metrics/collectors/network.rb', line 5

def build_report
  if linux?
    lines = File.read("#{ServerMetrics::SystemInfo.proc_dir}/net/dev").lines.to_a[2..-1]
    interfaces = []
    lines.each do |line|
      iface, rest = line.split(':', 2).collect { |e| e.strip }
      interfaces << iface
      next if iface =~ /lo/
      found = true
      cols = rest.split(/\s+/)

      bytes_in, packets_in, bytes_out, packets_out = cols.values_at(0, 1, 8, 9).collect { |i| i.to_i }

      counter(iface, :bytes_in, bytes_in.to_f / 1024.0, :per => :second, :round => 2)
      counter(iface, :packets_in, packets_in.to_f, :per => :second, :round => 2)
      counter(iface, :bytes_out, bytes_out.to_f / 1024.0, :per => :second, :round => 2)
      counter(iface, :packets_out, packets_out.to_f, :per => :second, :round => 2)
    end
  end
end