Class: Riemann::Tools::Net

Inherits:
Object
  • Object
show all
Includes:
Riemann::Tools
Defined in:
lib/riemann/tools/net.rb

Constant Summary collapse

FREEBSD_MAPPING =
{
  'collisions'       => 'tx colls',
  'dropped-packets'  => 'rx drop',
  'received-bytes'   => 'rx bytes',
  'received-packets' => 'rx packets',
  'received-errors'  => 'rx errs',
  'sent-bytes'       => 'tx bytes',
  'sent-packets'     => 'tx packets',
  'send-errors'      => 'tx errs',
}.freeze

Constants included from Riemann::Tools

VERSION

Instance Attribute Summary

Attributes included from Riemann::Tools

#argv

Instance Method Summary collapse

Methods included from Riemann::Tools

#attributes, #endpoint_name, included, #options, #report, #riemann, #run

Constructor Details

#initializeNet

Returns a new instance of Net.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/riemann/tools/net.rb', line 14

def initialize
  super

  @old_state = nil
  @interfaces = if opts[:interfaces]
                  opts[:interfaces].reject(&:empty?).map(&:dup)
                else
                  []
                end
  @ignore_interfaces = opts[:ignore_interfaces].reject(&:empty?).map(&:dup)

  ostype = `uname -s`.chomp.downcase
  case ostype
  when 'freebsd'
    @state = method :freebsd_state
  else
    puts "WARNING: OS '#{ostype}' not explicitly supported. Falling back to Linux" unless ostype == 'linux'
    @state = method :linux_state
  end
end

Instance Method Details

#freebsd_stateObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/riemann/tools/net.rb', line 54

def freebsd_state
  require 'json'

  state = {}

  all_stats = JSON.parse(`netstat -inb --libxo=json`)
  all_stats.dig('statistics', 'interface').select { |s| s['mtu'] }.each do |interface_stats|
    next unless report_interface?(interface_stats['name'])

    FREEBSD_MAPPING.each do |key, service|
      state["#{interface_stats['name']} #{service}"] = interface_stats[key]
    end
  end

  state
end

#linux_stateObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/riemann/tools/net.rb', line 71

def linux_state
  f = File.read('/proc/net/dev')
  state = {}
  f.split("\n").each do |line|
    next unless line =~ /\A\s*([[:alnum:]-]+?):\s*([\s\d]+)\s*/

    iface = Regexp.last_match(1)

    next unless report_interface?(iface)

    ['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',].map do |service|
      "#{iface} #{service}"
    end.zip( # rubocop:disable Style/MultilineBlockChain
      Regexp.last_match(2).split(/\s+/).map(&:to_i),
    ).each do |service, value|
      state[service] = value
    end
  end

  state
end

#report_interface?(iface) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/riemann/tools/net.rb', line 35

def report_interface?(iface)
  if !@interfaces.empty?
    @interfaces.any? { |pattern| iface.match?(pattern) }
  else
    @ignore_interfaces.none? { |pattern| iface.match?(pattern) }
  end
end

#tickObject



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
# File 'lib/riemann/tools/net.rb', line 108

def tick
  state = @state.call

  if @old_state
    # Report services from `@old_state` that don't exist in `state` as expired
    @old_state.reject { |k| state.key?(k) }.each_key do |service|
      report(service: service.dup, state: 'expired')
    end

    # Report delta for services that have values in both `@old_state` and `state`
    state.each do |service, metric|
      next unless @old_state.key?(service)

      delta = metric - @old_state[service]
      svc_state = case service
                  when /drop$/, /errs$/
                    if delta.positive?
                      'warning'
                    else
                      'ok'
                    end
                  else
                    'ok'
                  end

      report(
        service: service.dup,
        metric: (delta.to_f / opts[:interval]),
        state: svc_state,
      )
    end
  end

  @old_state = state
end