Skip to content

Commit

Permalink
Merge pull request #170 from sdx23/master
Browse files Browse the repository at this point in the history
Add riemann-portcheck
  • Loading branch information
jamtur01 committed Jun 11, 2017
2 parents 0abacc6 + b9073fc commit 00aeb62
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.markdown
Expand Up @@ -38,7 +38,8 @@ ship with the `riemann-tools` gem, including:
* riemann-consul - Monitor Consul.
* riemann-fd - Linux file descriptor use.
* riemann-kvminstance - Monitor KVM instances.
* riemann-ntp - Monitor NTP
* riemann-ntp - Monitor NTP.
* riemann-portcheck - Monitor open TCP ports.

Also contained in the repository are a number of stand-alone monitoring
tools, which are shipped as separate gems.
Expand Down
41 changes: 41 additions & 0 deletions bin/riemann-portcheck
@@ -0,0 +1,41 @@
#!/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

0 comments on commit 00aeb62

Please sign in to comment.