Class: CheckTaskr::HttpResultAction

Inherits:
JobsAction show all
Includes:
Socket::Constants
Defined in:
lib/check-taskr/task/http_result.rb

Instance Attribute Summary collapse

Attributes inherited from JobsAction

#name

Instance Method Summary collapse

Methods inherited from JobsAction

setup

Constructor Details

#initialize(options) ⇒ HttpResultAction

Returns a new instance of HttpResultAction.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/check-taskr/task/http_result.rb', line 25

def initialize(options)
  @name ||= options[:name]
  @ip = options[:ip]
  @port = options[:port] || 80
  @path = options[:path] || "/"
  @method = options[:method] || :get
  @post_data = options[:post_data]
  @expect_result = options[:expect_result] || "ok"  #默认期望返回200
  @error_code = options[:error_code] || @@default_error_code
  @error_msg = options[:error_msg] || @@default_error_msg
end

Instance Attribute Details

#error_codeObject

Returns the value of attribute error_code.



21
22
23
# File 'lib/check-taskr/task/http_result.rb', line 21

def error_code
  @error_code
end

#error_msgObject

Returns the value of attribute error_msg.



21
22
23
# File 'lib/check-taskr/task/http_result.rb', line 21

def error_msg
  @error_msg
end

#expect_resultObject

Returns the value of attribute expect_result.



21
22
23
# File 'lib/check-taskr/task/http_result.rb', line 21

def expect_result
  @expect_result
end

#ipObject

Returns the value of attribute ip.



21
22
23
# File 'lib/check-taskr/task/http_result.rb', line 21

def ip
  @ip
end

#methodObject

Returns the value of attribute method.



21
22
23
# File 'lib/check-taskr/task/http_result.rb', line 21

def method
  @method
end

#pathObject

Returns the value of attribute path.



21
22
23
# File 'lib/check-taskr/task/http_result.rb', line 21

def path
  @path
end

#portObject

Returns the value of attribute port.



21
22
23
# File 'lib/check-taskr/task/http_result.rb', line 21

def port
  @port
end

#post_dataObject

Returns the value of attribute post_data.



21
22
23
# File 'lib/check-taskr/task/http_result.rb', line 21

def post_data
  @post_data
end

Instance Method Details

#executeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/check-taskr/task/http_result.rb', line 37

def execute
  log = Log4r::Logger['default']
  log.debug "http action: ip=#{@ip}, port=#{@port}, name=#{@name}"
  hash = {:stat => 0, :ip => @ip, :msg => "OK", :error_id => @error_code }
  begin
    Net::HTTP.start(@ip, @port) do |http|
      http.read_timeout = 5
      if @method == :get
        response = http.get(@path)
      end
      case @method
      when :get
        response = http.get(@path)
      when :post
        response = http.post(@path, @post_data)
      end
      result = response.body
      hash[:timestamp] = Time.now.to_i
      unless result.include?(@expect_result)
        hash[:stat] = 1
        hash[:msg] = "HTTP #{@method.to_s} #{@path}期望返回值包含\"#{@expect_result}\",但返回\"#{result}\""
        log.warn hash.to_json
      end
    end
  rescue Exception => e
    hash[:stat] = 2
    hash[:timestamp] = Time.now.to_i
    hash[:msg] = "HTTP #{@method.to_s} #{@path}出现异常:#{e}"
    log.error hash.to_json
  end
  hash
end