11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/log/parser.rb', line 11
def self.fetch_log(server, remote_path, user_name, password)
dir = File.join(Dir.home, '.log-parser')
log = File.join(dir, 'tmp.log')
Dir.mkdir(dir) unless Dir.exist?(dir)
puts 'fetching log...'
Net::SCP::download!(server, user_name, remote_path, log)
puts 'reading log...'
file_line_path = File.join(dir, '.line')
if File.exists? file_line_path
line_nb = IO.read(file_line_path).to_i
else
line_nb = nil
end
file = File.open(log, 'r')
count = 0
log_file = File.open(File.join(dir, 'logstash.log'), 'a+')
while !file.eof?
count+=1
line = file.readline
if line_nb.nil? or line_nb < count
log_file.write(line)
end
end
log_file.close
file_line = File.new(file_line_path, 'w+')
file_line.write(count.to_s+'\n')
file_line.close
file.close
end
|