Class: CheckTaskr::HttpJsonAction

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

Instance Attribute Summary collapse

Attributes inherited from JobsAction

#name

Instance Method Summary collapse

Methods inherited from JobsAction

setup

Constructor Details

#initialize(options) ⇒ HttpJsonAction

Returns a new instance of HttpJsonAction.



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

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]
  @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.



22
23
24
# File 'lib/check-taskr/task/http_json.rb', line 22

def error_code
  @error_code
end

#error_msgObject

Returns the value of attribute error_msg.



22
23
24
# File 'lib/check-taskr/task/http_json.rb', line 22

def error_msg
  @error_msg
end

#ipObject

Returns the value of attribute ip.



22
23
24
# File 'lib/check-taskr/task/http_json.rb', line 22

def ip
  @ip
end

#methodObject

Returns the value of attribute method.



22
23
24
# File 'lib/check-taskr/task/http_json.rb', line 22

def method
  @method
end

#pathObject

Returns the value of attribute path.



22
23
24
# File 'lib/check-taskr/task/http_json.rb', line 22

def path
  @path
end

#portObject

Returns the value of attribute port.



22
23
24
# File 'lib/check-taskr/task/http_json.rb', line 22

def port
  @port
end

#post_dataObject

Returns the value of attribute post_data.



22
23
24
# File 'lib/check-taskr/task/http_json.rb', line 22

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
69
70
# File 'lib/check-taskr/task/http_json.rb', line 37

def execute
  log = Log4r::Logger['default']
  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
      body = response.body
      log.debug "body=#{body}"
      hash = JSON.load(body)
      # hash[:timestamp] = Time.now.to_i
      #if hash["stat"] && hash["stat"].to_i > 0
      hash.each do |k, v|
        v[:error_id] = @error_code
        v[:ip] = @ip
      end
      #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