Class: Riemann::Tools::Portcheck

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

Constant Summary

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

#initializePortcheck

Returns a new instance of Portcheck.



16
17
18
19
20
21
# File 'lib/riemann/tools/portcheck.rb', line 16

def initialize
  super

  @hostname = opts.fetch(:hostname)
  @ports = opts.fetch(:ports)
end

Instance Method Details

#tickObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/riemann/tools/portcheck.rb', line 23

def tick
  @ports.each do |thisport|
    # try opening tcp connection with 5s timeout;
    # if this fails, the port is considered closed
    portopen = begin
      Socket.tcp(@hostname, thisport, connect_timeout: 5) { true }
    rescue StandardError
      false
    end
    state = if portopen
              'ok'
            else
              'critical'
            end
    report(
      host: @hostname.to_s,
      service: "port #{thisport}",
      state: state.to_s,
      tags: ['portcheck'],
    )
  end
end