Upgrade to Pro — share decks privately, control downloads, hide ads and more …

analyze_logs_by_rake

namu
February 06, 2014

 analyze_logs_by_rake

ログ解析にRakeを使うことをオススメします
M3 Tech Talk #18 2014/01/31 #m3dev

namu

February 06, 2014
Tweet

More Decks by namu

Other Decks in Technology

Transcript

  1. rakeͷ֓ཁ • ʮRakefileʯͱ͍͏ϑΝΠϧ໊ͰॲཧΛهड़ • هࡌ͸׬શͳRuby • taskϝιουͰλεΫΛఆٛ • ґଘؔ܎Λఆٛͯ͠Ұ࿈ͷॲཧͷྲྀΕΛ࡞ΕΔ •

    fileϝιουͰϑΝΠϧΛ࡞੒͢ΔλεΫΛఆٛ • ϑΝΠϧ͕͋ͬͨΓɺґଘ͢ΔϑΝΠϧΑΓ৽͚͠Ε͹εΩοϓ ͢Δ • shϝιουͰγΣϧίϚϯυΛ࣮ߦͰ͖Δ
  2. $ rake -P #λεΫҰཡ rake hello rake world hello !

    $ rake hello # hello Λ࣮ߦ Hello ! $ rake world # worldΛ࣮ߦ Hello World ! $ rake file1.txt ˌfile1.txtΛ࡞੒ $ ls file1.txt ! $ cat file1.txt 11111 # Rakefile ! task :hello do puts "Hello" end ! task :world => [:hello] do puts "World" end ! file "file1.txt" do |t| sh "echo 1111 > #{t.name}" end
  3. $ rake all.txt << create file1.txt >> complete file1.txt <<

    create file2.txt echo 2222 > file2.txt >> complete file2.txt << create all.txt cat file1.txt file2.txt > all.txt >> complete all.txt ! $ cat file1.txt 1111 ! $ cat file2.txt 2222 ! $ cat all.txt 1111 2222 file "file1.txt" do |t| puts "<< create file1.txt" File.open(t.name, "w") do |file| file.puts("1111") end puts ">> complete file1.txt" end ! file "file2.txt" do |t| puts "<< create file2.txt" sh "echo 2222 > #{t.name}" puts ">> complete file2.txt" end ! file "all.txt" => ["file1.txt", "file2.txt"] do |t| ! puts "<< create all.txt" sh "cat #{t.prerequisites.join(' ')} 
 > #{t.name}" puts ">> complete all.txt" end
  4. $ pwrake -j 2 all.txt << create file1.txt << create

    file2.txt echo 2222 > file2.txt >> complete file1.txt >> complete file2.txt << create all.txt cat file1.txt file2.txt > all.txt >> complete all.txt file "file1.txt" do |t| puts "<< create file1.txt" File.open(t.name, "w") do |file| file.puts("1111") end puts ">> complete file1.txt" end ! file "file2.txt" do |t| puts "<< create file2.txt" sh "echo 2222 > #{t.name}" puts ">> complete file2.txt" end ! file "all.txt" => ["file1.txt", "file2.txt"] do |t| ! puts "<< create all.txt" sh "cat #{t.prerequisites.join(' ')} 
 > #{t.name}" puts ">> complete all.txt" end
  5. $ rake -P rake data/result.1.txt rake data/result.2.txt rake data/result.3.txt rake

    data/result_all.log data/result.1.txt data/result.2.txt data/result.3.txt ! $ pwrake -j 2 run # # log/access.1.log => data/result.1.log # log/access.2.log => data/result.2.log # log/access.3.log => data/result.3.log # results = [] (1..3).each do |num| logfile = "data/result.#{num}.txt" results << logfile ! file logfile do |t| sh "grep 'iPad' log/access.#{num}.log 
 > #{t.name}" end end ! # # data/result.1.log # data/result.2.log => data/result_all.log # data/result.3.log # file “data/result_all.log" => results do |t| sh "cat #{t.prerequisites.join(' ‘)} > 
 #{t.name}" end ! task :run => [“data/result_all.log"]
  6. Tips • fileλεΫͷλʔήοτ ͱ͢ΔϑΝΠϧ͸ɺॲཧ ͷ࠷ޙʹmvͰ࡞Δ • ్தͰఀࢭͨ͠ͱ͖ʹɺ ࡞Γ͔͚ϑΝΠϧ͕͋Δ ͨΊʹɺεΩοϓ͞Εͳ ͍Α͏ʹ͢ΔͨΊ

    file “target.txt” do |t| ! sh “grep HOGE src.log > “ +
 “ #{t.name}.tmp “ ! sh “mv #{t.name}.tmp #{t.name}” end