Sha256: 6e0e11ed717a3b18db9f7f0e0cf5d790bbed974a1ef13bfe206f10579a1337d1

Contents?: true

Size: 998 Bytes

Versions: 2

Compression:

Stored size: 998 Bytes

Contents

#!/usr/bin/env ruby

# Checks for open tcp ports.
# (c) Max Voit 2017

require File.expand_path('../../lib/riemann/tools', __FILE__)

class Riemann::Tools::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
    for thisport in @ports
	  # try opening tcp connection with 5s timeout;
      # if this fails, the port is considered closed
      portopen = Socket.tcp(@hostname, thisport, connect_timeout: 5) { true } rescue false
        if portopen
          state = "ok"
        else
          state = "critical"
        end
      report(
        :host    => "#{@hostname}",
        :service => "port #{thisport}",
        :state   => "#{state}",
        :tags    => ["portcheck"]
      )
    end
  end

end

Riemann::Tools::Portcheck.run

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
riemann-tools-0.2.14 bin/riemann-portcheck
riemann-tools-0.2.13 bin/riemann-portcheck