diff --git a/lib/watchmonkey_cli.rb b/lib/watchmonkey_cli.rb index 31764dd..9fec3bb 100644 --- a/lib/watchmonkey_cli.rb +++ b/lib/watchmonkey_cli.rb @@ -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" diff --git a/lib/watchmonkey_cli/application/configuration.tpl b/lib/watchmonkey_cli/application/configuration.tpl index d04304a..dc23ce3 100644 --- a/lib/watchmonkey_cli/application/configuration.tpl +++ b/lib/watchmonkey_cli/application/configuration.tpl @@ -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 # ----- diff --git a/lib/watchmonkey_cli/checkers/tcp_port.rb b/lib/watchmonkey_cli/checkers/tcp_port.rb new file mode 100644 index 0000000..b20337b --- /dev/null +++ b/lib/watchmonkey_cli/checkers/tcp_port.rb @@ -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 diff --git a/lib/watchmonkey_cli/ssh_connection.rb b/lib/watchmonkey_cli/ssh_connection.rb index bd60d87..aaeaad1 100644 --- a/lib/watchmonkey_cli/ssh_connection.rb +++ b/lib/watchmonkey_cli/ssh_connection.rb @@ -1,5 +1,7 @@ module WatchmonkeyCli class SshConnection + attr_reader :opts + def initialize(id, opts = {}, &initializer) @id = id