Module: Riemann::Tools

Included in:
ApacheStatus, Cloudant, ConsulHealth, DirFilesCount, DirSpace, Diskstats, Fd, Freeswitch, Haproxy, Health, HttpCheck, Hwmon, Kvm, Md, Memcached, Net, NginxStatus, Ntp, Portcheck, Proc, TLSCheck, Varnish, Zookeeper, Zpool
Defined in:
lib/riemann/tools/version.rb,
lib/riemann/tools.rb,
lib/riemann/tools/fd.rb,
lib/riemann/tools/md.rb,
lib/riemann/tools/kvm.rb,
lib/riemann/tools/net.rb,
lib/riemann/tools/ntp.rb,
lib/riemann/tools/proc.rb,
lib/riemann/tools/bench.rb,
lib/riemann/tools/hwmon.rb,
lib/riemann/tools/utils.rb,
lib/riemann/tools/zpool.rb,
lib/riemann/tools/health.rb,
lib/riemann/tools/haproxy.rb,
lib/riemann/tools/varnish.rb,
lib/riemann/tools/cloudant.rb,
lib/riemann/tools/dir_space.rb,
lib/riemann/tools/diskstats.rb,
lib/riemann/tools/memcached.rb,
lib/riemann/tools/portcheck.rb,
lib/riemann/tools/tls_check.rb,
lib/riemann/tools/zookeeper.rb,
lib/riemann/tools/freeswitch.rb,
lib/riemann/tools/http_check.rb,
lib/riemann/tools/nginx_status.rb,
lib/riemann/tools/apache_status.rb,
lib/riemann/tools/consul_health.rb,
lib/riemann/tools/dir_files_count.rb,
lib/riemann/tools/mdstat_parser.tab.rb,
lib/riemann/tools/uptime_parser.tab.rb,
lib/riemann/tools/riemann_client_wrapper.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Utils Classes: ApacheStatus, Bench, Cloudant, ConsulHealth, DirFilesCount, DirSpace, Diskstats, Fd, Freeswitch, Haproxy, Health, HttpCheck, Hwmon, Kvm, Md, MdstatParser, Memcached, Net, NginxStatus, Ntp, Portcheck, Proc, RiemannClientWrapper, TLSCheck, UptimeParser, Varnish, Zookeeper, Zpool

Constant Summary collapse

VERSION =
'1.11.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



48
49
50
# File 'lib/riemann/tools.rb', line 48

def argv
  @argv
end

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/riemann/tools.rb', line 8

def self.included(base)
  base.instance_eval do
    def run
      new.run
    end

    def opt(*args)
      args.unshift :opt
      @opts ||= []
      @opts << args
    end

    def options
      p = Optimist::Parser.new
      @opts.each do |o|
        p.send(*o)
      end
      Optimist.with_standard_exception_handling(p) do
        p.parse ARGV
      end
    end

    opt :host, 'Riemann host', default: '127.0.0.1'
    opt :port, 'Riemann port', default: 5555
    opt :event_host, 'Event hostname', type: String
    opt :interval, 'Seconds between updates', default: 5
    opt :tag, 'Tag to add to events', type: String, multi: true
    opt :ttl, 'TTL for events (twice the interval when unspecified)', type: Integer
    opt :minimum_ttl, 'Minimum TTL for events', type: Integer, short: :none
    opt :attribute, 'Attribute to add to the event', type: String, multi: true
    opt :timeout, 'Timeout (in seconds) when waiting for acknowledgements', default: 30
    opt :tcp, 'Use TCP transport instead of UDP (improves reliability, slight overhead.', default: true
    opt :tls, 'Use TLS for securing traffic', default: false
    opt :tls_key, 'TLS Key to use when using TLS', type: String
    opt :tls_cert, 'TLS Certificate to use when using TLS', type: String
    opt :tls_ca_cert, 'Trusted CA Certificate when using TLS', type: String
    opt :tls_verify, 'Verify TLS peer when using TLS', default: true
  end
end

Instance Method Details

#attributesObject



65
66
67
68
69
70
# File 'lib/riemann/tools.rb', line 65

def attributes
  @attributes ||= options[:attribute].to_h do |attr|
    k, v = attr.split('=')
    [k, v] if k && v
  end
end

#endpoint_name(address, port) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/riemann/tools.rb', line 105

def endpoint_name(address, port)
  if address.ipv6?
    "[#{address}]:#{port}"
  else
    "#{address}:#{port}"
  end
end

#initialize(allow_arguments: false) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/riemann/tools.rb', line 50

def initialize(allow_arguments: false)
  options
  @argv = ARGV.dup
  abort "Error: stray arguments: #{ARGV.map(&:inspect).join(', ')}" if ARGV.any? && !allow_arguments

  options[:ttl] ||= options[:interval] * 2
  options[:ttl] = [options[:minimum_ttl], options[:ttl]].compact.max
end

#optionsObject Also known as: opts

Returns parsed options (cached) from command line.



60
61
62
# File 'lib/riemann/tools.rb', line 60

def options
  @options ||= self.class.options
end

#report(event) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/riemann/tools.rb', line 72

def report(event)
  event[:tags] = event.fetch(:tags, []) + options[:tag]

  event[:ttl] ||= options[:ttl]

  event[:host] = options[:event_host].dup if options[:event_host]

  event = event.merge(attributes)

  riemann << event
end

#riemannObject Also known as: r



84
85
86
# File 'lib/riemann/tools.rb', line 84

def riemann
  @riemann ||= RiemannClientWrapper.new(options)
end

#runObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/riemann/tools.rb', line 89

def run
  t0 = Time.now
  loop do
    begin
      tick
    rescue StandardError => e
      warn "#{e.class} #{e}\n#{e.backtrace.join "\n"}"
    end

    # Sleep.
    sleep(options[:interval] - ((Time.now - t0) % options[:interval]))
  end
end

#tickObject



103
# File 'lib/riemann/tools.rb', line 103

def tick; end