Class: Fluent::Plugin::NodeExporter::NetdevMetricsCollector

Inherits:
MetricsCollector show all
Defined in:
lib/fluent/plugin/node_exporter/netdev_collector.rb

Constant Summary collapse

RECEIVE_FIELDS =
%w(bytes packets errs drop fifo frame compressed multicast)
TRANSMIT_FIELDS =
%w(bytes packets errs drop fifo colls carrier compressed)

Instance Method Summary collapse

Methods inherited from MetricsCollector

#scan_sysfs_path

Constructor Details

#initialize(config = {}) ⇒ NetdevMetricsCollector

Returns a new instance of NetdevMetricsCollector.



25
26
27
28
29
# File 'lib/fluent/plugin/node_exporter/netdev_collector.rb', line 25

def initialize(config={})
  super(config)

  @metrics = {}
end

Instance Method Details

#cmetricsObject



86
87
88
# File 'lib/fluent/plugin/node_exporter/netdev_collector.rb', line 86

def cmetrics
  @metrics
end

#netdev_updateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fluent/plugin/node_exporter/netdev_collector.rb', line 50

def netdev_update
  netdev_path = File.join(@procfs_path, "net/dev")
  RECEIVE_FIELDS.each_with_index do |field, index|
    metric_name = "receive_#{field}_total"
    @counter = CMetrics::Counter.new
    @counter.create("node", "network", metric_name, "Network device statistic #{metric_name}.", ["device"])
    @metrics[metric_name.intern] = @counter
  end
  TRANSMIT_FIELDS.each_with_index do |field, index|
    metric_name = "transmit_#{field}_total"
    @counter = CMetrics::Counter.new
    @counter.create("node", "network", metric_name, "Network device statistic #{metric_name}.", ["device"])
    @metrics[metric_name.intern] = @counter
  end
  File.readlines(netdev_path).each_with_index do |line, index|
    # net/dev must be 3 columns
    if index == 0 and line.split("|").size != 3
      break
    end
    # first 2 line are header (Inter-face/Receive/Transmit)
    next if index < 2

    interface, *values = line.split
    interface.delete!(":")

    RECEIVE_FIELDS.each_with_index do |field, index|
      metric_name = "receive_#{field}_total"
      @metrics[metric_name.intern].set(values[index].to_f, [interface])
    end
    TRANSMIT_FIELDS.each_with_index do |field, index|
      metric_name = "transmit_#{field}_total"
      @metrics[metric_name.intern].set(values[index + RECEIVE_FIELDS.size].to_f, [interface])
    end
  end
end

#runObject



31
32
33
# File 'lib/fluent/plugin/node_exporter/netdev_collector.rb', line 31

def run
  netdev_update
end

#target_devicesObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/plugin/node_exporter/netdev_collector.rb', line 38

def target_devices
  devices = []
  netdev_path = File.join(@procfs_path, "net/dev")
  File.readlines(netdev_path).each_with_index do |line, index|
    next if index < 2
    interface, _ = line.split
    interface.delete!(":")
    devices << interface
  end
  devices
end