-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial version of analyze logs script
- Loading branch information
1 parent
ac77967
commit cf8a636
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#! /usr/bin/ruby | ||
|
||
require "tmpdir" | ||
require "cheetah" | ||
require "yaml" | ||
|
||
result = {} | ||
|
||
def unpack_file(file) | ||
case file | ||
when /\.gz$/ | ||
Cheetah.run("gzip", "-d", file) | ||
file[/(.*).gz$/, 1] | ||
when /\.bz2?$/ | ||
Cheetah.run("bzip2", "-d", file) | ||
file[/(.*).bz2?$/, 1] | ||
when /\.xz$/ | ||
Cheetah.run("xz", "-d", file) | ||
file[/(.*).xz$/, 1] | ||
when /\.7z$/ | ||
Cheetah.run("7z", "e", file) | ||
file[/(.*).7z$/, 1] | ||
else | ||
file | ||
end | ||
end | ||
|
||
def set_mod_ui(result, mod, ui) | ||
result[:modules] ||= {} | ||
result[:modules][mod] ||= {} | ||
result[:modules][mod][:count] ||= 0 | ||
result[:modules][mod][:count] += 1 | ||
result[:modules][mod][:ui] ||= {} | ||
result[:modules][mod][:ui][ui] ||= 0 | ||
result[:modules][mod][:ui][ui] += 1 | ||
end | ||
|
||
def find_modules(result, content) | ||
# y2start kind of log line. example: | ||
# 2020-01-06 11:31:14 <1> localhost(3282) [Ruby] bin/y2start:22 y2base called with ["installation", "--arg", "continue", "qt", "--noborder", "--auto-fonts", "--fullscreen"] | ||
content.lines.grep(/y2base called with /).each do |line| | ||
mod = line[/\["([^"]+)"/, 1] | ||
raise "invalid module for #{line}." unless mod | ||
interface = case line | ||
when /"qt"/ then :qt | ||
when /"ncurses"/ then :ncurses | ||
when /"UI"/ then :no | ||
else raise "Unknown UI in #{line}." | ||
end | ||
# TODO CLI detection | ||
set_mod_ui(result, mod, interface) | ||
end | ||
# y2base kind of log line. example: | ||
# 2017-11-29 06:25:29 <1> install(3162) [liby2] genericfrontend.cc(main):617 Launched YaST2 component 'y2base' 'installation' '("initial")' 'qt' '--noborder' '--auto-fonts' '--fullscreen' | ||
content.lines.grep(/Launched YaST2 component 'y2base' /).each do |line| | ||
mod = line[/'y2base' '([^']+)'/, 1] | ||
raise "invalid module for #{line}." unless mod | ||
interface = case line | ||
when /'qt'/ then :qt | ||
when /'ncurses'/ then :ncurses | ||
when /'UI'/ then :no | ||
else raise "Unknown UI in #{line}." | ||
end | ||
# TODO CLI detection | ||
set_mod_ui(result, mod, interface) | ||
end | ||
end | ||
|
||
def analyze_log(result, file) | ||
file = unpack_file(file) | ||
# TODO: can be memory consuming | ||
content = File.read(file) | ||
find_modules(result, content) | ||
rescue ArgumentError => e | ||
raise "Failed in #{file} with #{e.inspect}" | ||
end | ||
|
||
# Tarballs | ||
TARBALLS_REGEXPS = [/\.tar\./, /\.tbz2$/, /\.tgz$/, /\.txz$/, /\.t7z$/] | ||
ZIP_REGEXPS = [/\.zip$/] | ||
RAR_REGEXPS = [/\.rar$/] | ||
def single_file(result, file) | ||
if (TARBALLS_REGEXPS + ZIP_REGEXPS + RAR_REGEXPS).any?{ |r| file =~ r } | ||
temp_dir = true | ||
archive = file | ||
file = Dir.mktmpdir("logs-analyzer") | ||
if TARBALLS_REGEXPS.any? { |r| archive =~ r } | ||
Cheetah.run("tar", "xvf", archive, "-C", file) | ||
elsif ZIP_REGEXPS.any? { |r| archive =~ r } | ||
Cheetah.run("unzip", archive, "-d", file) | ||
elsif RAR_REGEXPS.any? { |r| archive =~ r } | ||
Cheetah.run("unrar", "x", archive, file) | ||
else | ||
raise "Should not happen :)" | ||
end | ||
puts "archive #{archive} destination #{file}" | ||
end | ||
|
||
if File.directory?(file) | ||
Dir["#{file}/**/y2log*"].each do |f| | ||
puts "directory #{file} file #{f}" | ||
analyze_log(result, f) | ||
end | ||
else | ||
analyze_log(result, file) | ||
end | ||
ensure | ||
if temp_dir | ||
Cheetah.run("rm", "-rf", file) | ||
end | ||
end | ||
|
||
ARGV.each { |f| single_file(result, f) } | ||
|
||
puts result.to_yaml |