Skip to content

Commit

Permalink
Added TCP port checker
Browse files Browse the repository at this point in the history
  • Loading branch information
2called-chaos committed Nov 1, 2016
1 parent 37d3f2c commit 42f470f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/watchmonkey_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
require "watchmonkey_cli/checkers/ftp_availability"
require "watchmonkey_cli/checkers/mysql_replication"
require "watchmonkey_cli/checkers/ssl_expiration"
require "watchmonkey_cli/checkers/tcp_port"
require "watchmonkey_cli/checkers/ts3_license"
require "watchmonkey_cli/checkers/unix_defaults"
require "watchmonkey_cli/checkers/unix_df"
require "watchmonkey_cli/checkers/unix_file_exists"
require "watchmonkey_cli/checkers/unix_load"
require "watchmonkey_cli/checkers/unix_mdadm"
require "watchmonkey_cli/checkers/unix_memory"
require "watchmonkey_cli/checkers/ts3_license"
require "watchmonkey_cli/checkers/www_availability"
14 changes: 14 additions & 0 deletions lib/watchmonkey_cli/application/configuration.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ www_availability "https://example.com", ssl_expiration: false
www_availability "https://example.com", ssl_expiration: { threshold: 4.weeks }


# -----
# TCP port
# -----
# Attempts to establish a TCP connection to a given port.
# Host might be :local/SSH connection/String(IP/DNS)
# Available options:
#
# message Error message when connection cannot be established
# timeout Timeout in seconds to wait for a connection (default = 2 seconds - false/nil = 1 hour)
#
tcp_port "ftp.example.com", 21, message: "FTP offline"
tcp_port :my_server, 21, message: "FTP offline"


# -----
# FTP availability
# -----
Expand Down
29 changes: 29 additions & 0 deletions lib/watchmonkey_cli/checkers/tcp_port.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module WatchmonkeyCli
module Checkers
class TcpPort < Checker
self.checker_name = "tcp_port"

def enqueue host, port, opts = {}
opts = { message: "Port #{port} (TCP) is not reachable!", timeout: 2 }.merge(opts)
host = app.fetch_connection(:loopback, :local) if !host || host == :local
host = app.fetch_connection(:ssh, host) if host.is_a?(Symbol)
app.enqueue(self, host, port, opts)
end

def check! result, host, port, opts = {}
result.result = port_open?(host.is_a?(String) ? host : host.is_a?(WatchmonkeyCli::LoopbackConnection) ? "127.0.0.1" : host.opts[:host_name] || host.opts[:host] || host.opts[:ip], port, opts)
result.error! "#{opts[:message]}" unless result.result
end

def port_open?(ip, port, opts = {})
Timeout::timeout(opts[:timeout] ? opts[:timeout] : 3600) do
s = TCPSocket.new(ip, port)
s.close
end
true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Timeout::Error
return false
end
end
end
end
2 changes: 2 additions & 0 deletions lib/watchmonkey_cli/ssh_connection.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module WatchmonkeyCli
class SshConnection
attr_reader :opts

def initialize(id, opts = {}, &initializer)
@id = id

Expand Down

0 comments on commit 42f470f

Please sign in to comment.