Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from sdx23/master
Add riemann-portcheck
- Loading branch information
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |