Class: HQ::LogMonitorClient::Script
- Inherits:
-
Object
- Object
- HQ::LogMonitorClient::Script
- Defined in:
- lib/hq/log-monitor-client/script.rb
Defined Under Namespace
Classes: ContextReader
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#status ⇒ Object
Returns the value of attribute status.
-
#stderr ⇒ Object
Returns the value of attribute stderr.
-
#stdout ⇒ Object
Returns the value of attribute stdout.
Instance Method Summary collapse
- #debug(message) ⇒ Object
- #main ⇒ Object
- #perform_checks ⇒ Object
- #process_args ⇒ Object
- #read_cache ⇒ Object
- #read_config ⇒ Object
- #submit_event(event) ⇒ Object
- #write_cache ⇒ Object
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
11 12 13 |
# File 'lib/hq/log-monitor-client/script.rb', line 11 def args @args end |
#status ⇒ Object
Returns the value of attribute status.
12 13 14 |
# File 'lib/hq/log-monitor-client/script.rb', line 12 def status @status end |
#stderr ⇒ Object
Returns the value of attribute stderr.
15 16 17 |
# File 'lib/hq/log-monitor-client/script.rb', line 15 def stderr @stderr end |
#stdout ⇒ Object
Returns the value of attribute stdout.
14 15 16 |
# File 'lib/hq/log-monitor-client/script.rb', line 14 def stdout @stdout end |
Instance Method Details
#debug(message) ⇒ Object
461 462 463 464 |
# File 'lib/hq/log-monitor-client/script.rb', line 461 def debug return unless @debug $stderr.puts end |
#main ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hq/log-monitor-client/script.rb', line 17 def main process_args read_config read_cache perform_checks write_cache @status = 0 end |
#perform_checks ⇒ Object
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 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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/hq/log-monitor-client/script.rb', line 108 def perform_checks @service_elems.each do |service_elem| debug "==== service #{service_elem["name"]}" 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| debug "-- file #{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] debug "mtime %s and size %s match, skip" % [ file_mtime, 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_digest = Digest::MD5.new # check if the file has changed if cache_file if file_size < cache_file[:size] debug \ "size %s is less than cache size %s" % [ file_size, cache_file[:size], ] changed = true else debug "scanning start of file" changed = false cache_file[:lines].times do |line_num| line = file_reader.gets unless line debug \ "only read %s lines, previously " \ "read %s" % [ line_num, cache_file[:lines], ] changed = true break end file_digest.update line end file_hash = file_digest.to_s if file_hash != cache_file[:hash] debug \ "hash %s does not match previous " \ "value %s" % [ file_hash, cache_file[:hash] ] changed = true end end # go back to start if it changed if changed debug "seeking to start" file_io.seek 0 file_reader.reset file_digest = Digest::MD5.new else debug "start of file not changed" end end # scan the new part of the file debug "searching for events" while line = file_reader.gets file_digest.update line # 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 file_hash = file_digest.to_s file_lines = file_reader.next_line_number debug \ "updating cache mtime %s, size %s, lines %s, " \ "hash %s" % [ file_mtime, file_size, file_lines, file_hash, ] @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_args ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/hq/log-monitor-client/script.rb', line 29 def process_args @opts, @args = Tools::Getopt.process @args, [ { :name => :config, :required => true }, { :name => :debug, :boolean => true, :required => false }, ] @args.empty? \ or raise "Extra args on command line" @debug = @opts[:debug] end |
#read_cache ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/hq/log-monitor-client/script.rb', line 72 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_config ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/hq/log-monitor-client/script.rb', line 50 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
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/hq/log-monitor-client/script.rb', line 345 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_cache ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/hq/log-monitor-client/script.rb', line 91 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 |