Sha256: 14f2904b583e4d8b642eba06eb14e3535bf4cef8ee3c35a8679324720c507d3a

Contents?: true

Size: 1.11 KB

Versions: 13

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require 'riemann/tools'

# Checks for open tcp ports.
# (c) Max Voit 2017
module Riemann
  module Tools
    class Portcheck
      include Riemann::Tools
      require 'socket'

      opt :hostname, 'Host, defaults to localhost', default: `hostname`.chomp
      opt :ports, "List of ports to check, e.g. '-r 80 443'", type: :ints

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

      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
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
riemann-tools-1.10.0 lib/riemann/tools/portcheck.rb
riemann-tools-1.9.1 lib/riemann/tools/portcheck.rb
riemann-tools-1.9.0 lib/riemann/tools/portcheck.rb
riemann-tools-1.8.2 lib/riemann/tools/portcheck.rb
riemann-tools-1.8.1 lib/riemann/tools/portcheck.rb
riemann-tools-1.8.0 lib/riemann/tools/portcheck.rb
riemann-tools-1.7.1 lib/riemann/tools/portcheck.rb
riemann-tools-1.7.0 lib/riemann/tools/portcheck.rb
riemann-tools-1.6.0 lib/riemann/tools/portcheck.rb
riemann-tools-1.5.0 lib/riemann/tools/portcheck.rb
riemann-tools-1.4.0 lib/riemann/tools/portcheck.rb
riemann-tools-1.3.0 lib/riemann/tools/portcheck.rb
riemann-tools-1.2.0 lib/riemann/tools/portcheck.rb