Class: CheckTaskr::HttpResultAction
- Inherits:
-
JobsAction
- Object
- JobsAction
- CheckTaskr::HttpResultAction
- Includes:
- Socket::Constants
- Defined in:
- lib/check-taskr/task/http_result.rb
Instance Attribute Summary collapse
-
#error_code ⇒ Object
Returns the value of attribute error_code.
-
#error_msg ⇒ Object
Returns the value of attribute error_msg.
-
#expect_result ⇒ Object
Returns the value of attribute expect_result.
-
#ip ⇒ Object
Returns the value of attribute ip.
-
#method ⇒ Object
Returns the value of attribute method.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
-
#post_data ⇒ Object
Returns the value of attribute post_data.
Attributes inherited from JobsAction
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(options) ⇒ HttpResultAction
constructor
A new instance of HttpResultAction.
Methods inherited from JobsAction
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() @name ||= [:name] @ip = [:ip] @port = [:port] || 80 @path = [:path] || "/" @method = [:method] || :get @post_data = [:post_data] @expect_result = [:expect_result] || "ok" #默认期望返回200 @error_code = [:error_code] || @@default_error_code @error_msg = [:error_msg] || @@default_error_msg end |
Instance Attribute Details
#error_code ⇒ Object
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_msg ⇒ Object
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_result ⇒ Object
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 |
#ip ⇒ Object
Returns the value of attribute ip.
21 22 23 |
# File 'lib/check-taskr/task/http_result.rb', line 21 def ip @ip end |
#method ⇒ Object
Returns the value of attribute method.
21 22 23 |
# File 'lib/check-taskr/task/http_result.rb', line 21 def method @method end |
#path ⇒ Object
Returns the value of attribute path.
21 22 23 |
# File 'lib/check-taskr/task/http_result.rb', line 21 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
21 22 23 |
# File 'lib/check-taskr/task/http_result.rb', line 21 def port @port end |
#post_data ⇒ Object
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
#execute ⇒ Object
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 |