Class: HQ::CheckSite::Script

Inherits:
Tools::CheckScript
  • Object
show all
Defined in:
lib/hq/check-site/script.rb

Defined Under Namespace

Classes: CustomHTTP

Instance Method Summary collapse

Constructor Details

#initializeScript

Returns a new instance of Script.



44
45
46
47
# File 'lib/hq/check-site/script.rb', line 44

def initialize
	super
	@name = "Site"
end

Instance Method Details

#check_address(address) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/hq/check-site/script.rb', line 158

def check_address address

	cookies = {}

	begin

		# open http connection

		http =
			CustomHTTP.new \
				@base_url.host,
				@base_url.port

		http.override_address = address

		http.open_timeout = @timeout_time
		http.read_timeout = @timeout_time
		http.use_ssl = @base_url.scheme == "https"
		http.start

		success = true

		@config_elem.find("step").each do
			|step_elem|

			success =
				check_step http, cookies, step_elem

			break unless success

		end

		@successes += 1 if success

	rescue Errno::ECONNREFUSED

		@failures += 1

	rescue Timeout::Error

		@failures += 1

	end

end

#check_step(http, cookies, step_elem) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/hq/check-site/script.rb', line 204

def check_step http, cookies, step_elem

	@postscript << "performing step #{step_elem["name"]}"

	request_elem = step_elem.find_first "request"
	response_elem = step_elem.find_first "response"

	# create request

	path = @base_url.path + (request_elem["path"] || "")

	req =
		case request_elem["method"] || "get"
		when "get"
			Net::HTTP::Get.new path
		when "post"
			Net::HTTP::Post.new path
		else
			raise "error"
		end

	# set standard headers

	req["host"] = @base_url.host
	req["user-agent"] = "hq check-site"

	unless cookies.empty?
		req["cookie"] =
			cookies.map {
				|name, value|
				"#{name}=#{value}"
			}.join ", "
	end

	# set custom headers

	request_elem.find("header").each do
		|header_elem|
		req[header_elem["name"]] = header_elem["value"]
	end

	# set http auth

	if request_elem["username"]
		req.basic_auth \
			request_elem["username"],
			request_elem["password"]
	end

	# set form data

	form_data = {}

	request_elem.find("param").each do |param_elem|
		form_data[param_elem["name"]] = param_elem["value"]
	end

	req.set_form_data form_data

	# make request

	start_time = Time.now

	res = http.request req

	end_time = Time.now
	duration = end_time - start_time

	# save cookies

	if res["set-cookie"]
		WEBrick::Cookie.parse_set_cookies(res["set-cookie"]).each do
			|cookie|
			cookies[cookie.name] = cookie.value
		end
	end

	# process results

	@worst = duration if @worst == nil
	@worst = duration if duration > @worst

	debug "REQUEST #{req.path}"
	req.each { |k,v| debug "  #{k}: #{v}" }
	debug "RESPONSE #{res.code} #{res.message}"
	res.each { |k,v| debug "  #{k}: #{v}" }

	if res.code != "200"

		debug "EXPECTED response code 200"

		if @report_error == nil &&
			res.body =~ /#{response_elem["report-error"]}/

			@report_error = $1

		end

		@error_codes << res.code
		return false

	elsif response_elem["body-regex"] &&
		res.body !~ /#{response_elem["body-regex"]}/

		debug "EXPECTED body to match #{response_elem["body-regex"]}"

		if @opts[:debug]
			debug "BODY"
			debug res.body.gsub(/^/, "  ")
		end

		@mismatches += 1
		return false

	end

	return true

end

#debug(message) ⇒ Object



324
325
326
# File 'lib/hq/check-site/script.rb', line 324

def debug message
	@postscript << message
end

#perform_checksObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/hq/check-site/script.rb', line 90

def perform_checks

	addresses = Resolv.getaddresses @base_url.host

	@worst = nil
	@successes = 0
	@failures = 0
	@mismatches = 0
	@error_codes = []
	@report_error = nil

	addresses.each do
		|address|

		check_address address

	end

	errors = @error_codes.size
	total = @successes + errors + @failures + @mismatches

	if total == 0
		critical "unable to resolve #{@base_url.host}"
	else
		message "#{total} hosts found"

		critical "#{@failures} uncontactable" \
			if @failures > 0

		critical "#{errors} errors (#{@error_codes.uniq.sort.join ","})" \
			if errors > 0

		critical "#{@mismatches} mismatches" \
			if @mismatches > 0

		if @worst != nil

			if @worst >= @critical_time
				critical "#{@worst}s time (critical is #{@critical_time})"
			elsif @worst >= @warning_time
				warning "#{@worst}s time (warning is #{@warning_time})"
			else
				message "#{@worst}s time"
			end

		end

		if @report_error != nil
			message "response '#{@report_error}'"
		end

	end

	if @worst

		performance \
			"time",
			@worst,
			:units => "s",
			:warning => @warning_time,
			:critical => @critical_time,
			:minimum => 0.0,
			:maximum => @timeout_time

	end

end

#prepareObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hq/check-site/script.rb', line 66

def prepare

	config_doc =
		XML::Document.file @opts[:config]

	@config_elem =
		config_doc.root

	@timings_elem =
		@config_elem.find_first "timings"

	@critical_time =
		@timings_elem["critical"].to_f

	@warning_time =
		@timings_elem["warning"].to_f

	@timeout_time =
		@timings_elem["timeout"].to_f

	@base_url = URI.parse @config_elem["base-url"]

end

#process_argsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hq/check-site/script.rb', line 49

def process_args

	@opts, @args =
		Tools::Getopt.process @args, [

			{ :name => :config,
				:required => true },

			{ :name => :debug,
				:boolean => true },

		]

	@args.empty? or raise "Extra args on command line"

end