Class: Probe
- Inherits:
-
Object
- Object
- Probe
- Defined in:
- lib/probe.rb
Instance Method Summary collapse
- #command_probe ⇒ Object
- #error_wrapper(&block) ⇒ Object
- #http_probe ⇒ Object
-
#initialize(opts) ⇒ Probe
constructor
A new instance of Probe.
- #run ⇒ Object
- #tcp_probe ⇒ Object
- #udp_probe ⇒ Object
Constructor Details
#initialize(opts) ⇒ Probe
Returns a new instance of Probe.
9 10 11 12 13 |
# File 'lib/probe.rb', line 9 def initialize(opts) @type = opts[:type].downcase @timeout = opts[:timeout] || 5 @opts = opts end |
Instance Method Details
#command_probe ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/probe.rb', line 33 def command_probe cmd = @opts[:command] run_as = @opts[:run_as] if run_as.nil? run_as = 'nobody' if Process.uid.zero? else warn "WARNNG: Option 'run_as' requires root, ignoring it." unless Process.uid.zero? run_as = nil unless Process.uid.zero? end Timeout::timeout(@timeout) do user = Etc.getpwnam(run_as) if run_as pid = Process.fork do STDOUT.reopen('/dev/null') STDERR.reopen('/dev/null') unless run_as.nil? Process.egid = Process.gid = user.gid Process.euid = Process.uid = user.uid end exec cmd end status = Process.wait pid exit_status = $?.exitstatus raise "Command [#{cmd}] exited with #{exit_status}" if exit_status > 0 end end |
#error_wrapper(&block) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/probe.rb', line 103 def error_wrapper(&block) begin instance_eval(&block) if block_given? raise 'block should be passed to error_wrapper' unless block_given? rescue => e return "#{e.class} => #{e.}" end "SUCCESS" end |
#http_probe ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/probe.rb', line 60 def http_probe url = @opts[:url] method = @opts[:method] || 'get' payload = @opts[:payload] if @opts.key? :payload insecure = @opts[:insecure] || false headers = @opts[:headers] || {} Timeout::timeout(@timeout) do uri = URI(url) is_https = uri.scheme == 'https' uri.path = '/' if uri.path.empty? req = Object.const_get("Net::HTTP::#{method.capitalize}").new(uri.path) req.body = payload headers.each {|k,v| req[k] = v } opts = {} if is_https if @opts[:ca_file] opts[:ca_file] = @opts[:ca_file] end if @opts[:cert_file] opts[:cert] = OpenSSL::X509::Certificate.new( File.read @opts[:cert_file] ) end if @opts[:key_file] opts[:key] = OpenSSL::PKey::RSA.new( File.read @opts[:key_file] ) end end opts[:use_ssl] = is_https opts[:verify_mode] = OpenSSL::SSL::VERIFY_NONE if is_https && insecure Net::HTTP.start( uri.host, uri.port, opts ) do |http| res = http.request(req) raise "Request to #{url} returned code #{res.code}" unless res.code == '200' end end end |
#run ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/probe.rb', line 113 def run case @type when 'tcp' error_wrapper { tcp_probe } when 'udp' error_wrapper { udp_probe } when 'command' error_wrapper { command_probe } when 'http' error_wrapper { http_probe } else raise "Type '#{@type}' not supported" end end |
#tcp_probe ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/probe.rb', line 15 def tcp_probe host = @opts[:host] port = @opts[:port] Timeout::timeout(@timeout) do TCPSocket.new(host, port).close end end |
#udp_probe ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/probe.rb', line 23 def udp_probe host = @opts[:host] port = @opts[:port] Timeout::timeout(@timeout) do u = UDPSocket.new u.connect(host,port) u.puts "check" end end |