Class: Probe

Inherits:
Object
  • Object
show all
Defined in:
lib/probe.rb

Instance Method Summary collapse

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_probeObject



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)
    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

#http_probeObject



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
102
103
# 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 
 
    p [opts]   
 
    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

#runObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/probe.rb', line 105

def run 
  case @type 
    when 'tcp'
      tcp_probe 
    when 'udp'
      udp_probe
    when 'command'
      command_probe
    when 'http'
      http_probe
    else 
      raise "Type '#{@type}' not supported"
  end
end

#tcp_probeObject



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_probeObject



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