Class: Riemann::Babbler::Plugin::NetStat

Inherits:
Riemann::Babbler::Plugin show all
Defined in:
lib/riemann/babbler/plugins/net_stat.rb

Constant Summary

Constants included from Riemann::Babbler::Plugins::Helpers

Riemann::Babbler::Plugins::Helpers::RIEMANN_RESERVED_FIELDS

Constants included from Errors

Errors::CONNECTION_PROBLEM, Errors::INIT_CONNECT, Errors::RESOLV_RIEMANN_SERVER, Errors::USER_CALL_SHUTDOWN

Instance Attribute Summary

Attributes inherited from Riemann::Babbler::Plugin

#errors, #plugin, #plugin_name, #riemann

Instance Method Summary collapse

Methods inherited from Riemann::Babbler::Plugin

#get_state, inherited, #initialize, #not_minimize_sent_event, #plugin_error!, #plugin_no_error!, registered_plugins, #report, #report_with_diff, #run!, #run_plugin, #set_default, #set_last_event, #tick

Methods included from Riemann::Babbler::Plugins::Helpers

#event_from_hash, #event_from_json, #rest_get, #shell, #unixnow

Methods included from Options

included, #merge_config, #name_to_underscore, #opts, #opts_reset!, #underscore_to_name

Methods included from Logging

#get_logger_speaker, included, #log, #set_logger_speaker

Constructor Details

This class inherits a constructor from Riemann::Babbler::Plugin

Instance Method Details

#collectObject



25
26
27
28
29
30
31
32
# File 'lib/riemann/babbler/plugins/net_stat.rb', line 25

def collect
  count = get_conn_count()
  {
      :service     => "#{plugin.service} tcp #{plugin.ports.join(', ')}",
      :metric      => count,
      :description => "count established connects: #{count} to ports #{plugin.ports.join(', ')}"
  }
end

#get_conn_countObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/riemann/babbler/plugins/net_stat.rb', line 11

def get_conn_count
  filter = nil
  plugin.ports.each do |port|
    if filter == nil
      filter = "\\( src *:#{port}"
    else
      filter += " or src *:#{port}"
    end
  end
  filter += " \\) and not dst 127.0.0.1:*"
  cmd    = 'ss -t -4 -n state established ' + filter + ' | wc -l'
  shell(cmd).to_i - 1
end

#initObject



3
4
5
6
7
8
9
# File 'lib/riemann/babbler/plugins/net_stat.rb', line 3

def init
  plugin.set_default(:service, 'netstat')
  plugin.set_default(:interval, 5)
  plugin.set_default(:ports, [80, 3994])
  plugin.states.set_default(:warning, nil)
  plugin.states.set_default(:critical, nil)
end