6 class HttpResponseCode < PollCondition
7 attr_accessor :code_is, # e.g. 500 or '500' or [404, 500] or %w{404 500}
8 :code_is_not, # e.g. 200 or '200' or [200, 302] or %w{200 302}
9 :times, # e.g. 3 or [3, 5]
10 :host, # e.g. www.example.com
12 :timeout, # e.g. 60.seconds
21 self.code_is = Array(self.code_is).map { |x| x.to_i } if self.code_is
22 self.code_is_not = Array(self.code_is_not).map { |x| x.to_i } if self.code_is_not
24 if self.times.kind_of?(Integer)
25 self.times = [self.times, self.times]
28 @timeline = Timeline.new(self.times[1])
29 @history = Timeline.new(self.times[1])
39 valid &= complain("Attribute 'host' must be specified", self) if self.host.nil?
40 valid &= complain("Attribute 'port' must be specified", self) if self.port.nil?
41 valid &= complain("Attribute 'path' must be specified", self) if self.path.nil?
42 valid &= complain("One (and only one) of attributes 'code_is' and 'code_is_not' must be specified", self) if
43 (self.code_is.nil? && self.code_is_not.nil?) || (self.code_is && self.code_is_not)
44 valid &= complain("Attribute 'timeout' must be specified", self) if self.timeout.nil?
51 Net::HTTP.start(self.host, self.port) do |http|
52 http.read_timeout = self.timeout
53 response = http.head(self.path)
56 actual_response_code = response.code.to_i
57 if self.code_is && self.code_is.include?(actual_response_code)
58 pass(actual_response_code)
59 elsif self.code_is_not && !self.code_is_not.include?(actual_response_code)
60 pass(actual_response_code)
62 fail(actual_response_code)
65 self.code_is ? fail('Timeout') : pass('Timeout')
72 if @timeline.select { |x| x }.size >= self.times.first
73 self.info = "http response abnormal #{history(code, true)}"
76 self.info = "http response nominal #{history(code, true)}"
83 self.info = "http response nominal #{history(code, false)}"
87 def history(code, passed)
89 entry = '*' + entry if passed
91 '[' + @history.join(", ") + ']'