Class: CheckTaskr::HttpReturnCodeAction
- Inherits:
-
JobsAction
- Object
- JobsAction
- CheckTaskr::HttpReturnCodeAction
- Includes:
- Socket::Constants
- Defined in:
- lib/check-taskr/task/http_code.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_code ⇒ Object
Returns the value of attribute expect_code.
-
#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) ⇒ HttpReturnCodeAction
constructor
A new instance of HttpReturnCodeAction.
Methods inherited from JobsAction
Constructor Details
#initialize(options) ⇒ HttpReturnCodeAction
Returns a new instance of HttpReturnCodeAction.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/check-taskr/task/http_code.rb', line 25 def initialize() @name ||= [:name] @ip = [:ip] @port = [:port] || 80 @path = [:path] || "/" @method = [:method] || :get @post_data = [:post_data] @expect_code = [:expect_code] || "200" #默认期望返回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_code.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_code.rb', line 21 def error_msg @error_msg end |
#expect_code ⇒ Object
Returns the value of attribute expect_code.
21 22 23 |
# File 'lib/check-taskr/task/http_code.rb', line 21 def expect_code @expect_code end |
#ip ⇒ Object
Returns the value of attribute ip.
21 22 23 |
# File 'lib/check-taskr/task/http_code.rb', line 21 def ip @ip end |
#method ⇒ Object
Returns the value of attribute method.
21 22 23 |
# File 'lib/check-taskr/task/http_code.rb', line 21 def method @method end |
#path ⇒ Object
Returns the value of attribute path.
21 22 23 |
# File 'lib/check-taskr/task/http_code.rb', line 21 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
21 22 23 |
# File 'lib/check-taskr/task/http_code.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_code.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_code.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 code = response.code hash[:timestamp] = Time.now.to_i unless @expect_code.eql?(code) hash[:stat] = 1 hash[:msg] = "HTTP #{@method.to_s} #{@path}期望返回#{@expect_code},但返回#{code}" 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 |