Class: HQ::LogMonitorClient::Script

Inherits:
Object
  • Object
show all
Defined in:
lib/hq/log-monitor-client/script.rb

Defined Under Namespace

Classes: ContextReader

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject

Returns the value of attribute args.



10
11
12
# File 'lib/hq/log-monitor-client/script.rb', line 10

def args
  @args
end

#statusObject

Returns the value of attribute status.



11
12
13
# File 'lib/hq/log-monitor-client/script.rb', line 11

def status
  @status
end

#stderrObject

Returns the value of attribute stderr.



14
15
16
# File 'lib/hq/log-monitor-client/script.rb', line 14

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



13
14
15
# File 'lib/hq/log-monitor-client/script.rb', line 13

def stdout
  @stdout
end

Instance Method Details

#mainObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hq/log-monitor-client/script.rb', line 16

def main

	process_args
	read_config
	read_cache
	perform_checks
	write_cache

	@status = 0

end

#perform_checksObject



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
157
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
203
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
# File 'lib/hq/log-monitor-client/script.rb', line 101

def perform_checks

	@service_elems.each do
		|service_elem|

		fileset_elems = service_elem.find("fileset").to_a

		fileset_elems.each do
			|fileset_elem|

			scan_elems = fileset_elem.find("scan").to_a
			match_elems = fileset_elem.find("match").to_a

			max_before =
				match_elems.map {
					|match_elem|
					(match_elem["before"] || 0).to_i
				}.max

			max_after =
				match_elems.map {
					|match_elem|
					(match_elem["after"] || 0).to_i
				}.max

			# find files

			file_names =
				scan_elems.map {
					|scan_elem|
					Dir[scan_elem["glob"]]
				}.flatten

			# scan files

			file_names.each do
				|file_name|

				file_mtime = File.mtime file_name
				file_size = File.size file_name

				# fast check for modified files

				cache_file = @cache[:files][file_name]

				if cache_file &&
					file_mtime == cache_file[:mtime] &&
					file_size == cache_file[:size]
					next
				end

				# scan the file for matching lines

				mode = cache_file ? :scan : :report

				File.open file_name, "r" do
					|file_io|

					file_reader =
						ContextReader.new \
							file_io,
							max_before + max_after + 1

					file_hash = 0

					# check if the file has changed

					if cache_file

						if file_size < cache_file[:size]

							changed = true

						else

							changed = false

							cache_file[:lines].times do

								line = file_reader.gets

								unless line
									changed = true
									break
								end

								file_hash = [ file_hash, line.hash ].hash

							end

							if file_hash != cache_file[:hash]
								changed = true
							end

						end

					end

					# go back to start if it changed

					if changed
						file_io.seek 0
						file_reader.reset
						file_hash = 0
					end

					# scan the new part of the file

					while line = file_reader.gets

						file_hash = [ file_hash, line.hash ].hash

						# check for a match

						match_elem =
							match_elems.find {
								|match_elem|
								line =~ /#{match_elem["regex"]}/
							}

						# report the match

						if match_elem

							# get context

							lines_before =
								file_reader.lines_before \
									(match_elem["before"] || 0).to_i + 1

							lines_before.pop

							lines_after =
								file_reader.lines_after \
									(match_elem["after"] || 0).to_i

							# send event

							submit_event({
								type: match_elem["type"],
								source: {
									class: @client_elem["class"],
									host: @client_elem["host"],
									service: service_elem["name"],
								},
								location: {
									file: file_name,
									line: file_reader.last_line_number,
								},
								lines: {
									before: lines_before,
									matching: line,
									after: lines_after,
								},
							})

						end

					end

					# save the file's current info in the cache

					@cache[:files][file_name] = {
						mtime: file_mtime,
						size: file_size,
						lines: file_reader.next_line_number,
						hash: file_hash,
					}

				end

			end

		end

	end

end

#process_argsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hq/log-monitor-client/script.rb', line 28

def process_args

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

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

	]

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

end

#read_cacheObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hq/log-monitor-client/script.rb', line 65

def read_cache

	cache_path = @cache_elem["path"]

	if File.exist? cache_path

		@cache =
			YAML.load File.read cache_path

	else

		@cache = {
			files: {},
		}

	end

end

#read_configObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hq/log-monitor-client/script.rb', line 43

def read_config

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

	@config_elem =
		config_doc.root

	@cache_elem =
		@config_elem.find_first("cache")

	@client_elem =
		@config_elem.find_first("client")

	@server_elem =
		@config_elem.find_first("server")

	@service_elems =
		@config_elem.find("service").to_a

end

#submit_event(event) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/hq/log-monitor-client/script.rb', line 280

def submit_event event

	url =
		URI.parse @server_elem["url"]

	http =
		Net::HTTP.new url.host, url.port

	request =
		Net::HTTP::Post.new url.path

	request.body =
		MultiJson.dump event

	response =
		http.request request

end

#write_cacheObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hq/log-monitor-client/script.rb', line 84

def write_cache

	cache_path = @cache_elem["path"]
	cache_temp_path = "#{cache_path}.new"

	File.open cache_temp_path, "w" do
		|cache_temp_io|

		cache_temp_io.write YAML.dump @cache
		cache_temp_io.fsync

	end

	File.rename cache_temp_path, cache_path

end