Ruby program, læser ikke fil fra variabel?
Hej!Jeg er for nylig begyndt, at lege lidt med ruby. Lige nu er jeg ved at forsøge at lave en logparser, som eksekveres med et cron-job, og herfra reagerer, når definerede log-filer indeholder givne sætninger. So far får jeg ingen problemer, hvis jeg "hard-coder" path til en logfil i koden. Men bruger jeg nedenstående kode, brokker den sig over, at det givne filnavn ikke findes ( f.eks. config.conf ).
Det er sikkert en dum logisk fejl, men jeg kan simpelthen ikke få det til at virke lige nu :-)
#!/usr/bin/ruby
require "net/smtp"
class MMS
######SEND MAIL METHOD START
def sendmail (to_mail, subject, body, from_mail, username, password, smtpaddr)
msgstr = <<END_OF_MESSAGE
From: <#{from_mail}>
To: <#{to_mail}>
Subject: #{subject}
#{body}
END_OF_MESSAGE
Net::SMTP.start(smtpaddr, 587, from_mail.split('@')[1],username, password, :login) do |smtp|
smtp.send_message msgstr, from_mail, to_mail
return "OK"
end
end
######SEND MAIL METHOD END
######TAIL FILE METHOD START
def file_tail(filename, num_lines, offset_size = 512)
begin
lines = []
File.open(filename) do |io|
first = io.gets
break unless first
limit = io.stat.size
offset = offset_size
while lines.size < (num_lines+1) && offset <= limit
io.seek -offset, IO::SEEK_END
lines = io.readlines
offset += offset_size
end
end
end
lines.size > num_lines ? lines[-num_lines..-1].map { |line| line.strip } : nil
end
######TAIL FILE METHOD END
end
### READ SETTINGS ###
ReadSettings = File.new("config.conf")
Settings = Hash.new
while (i = ReadSettings.gets)
x = i.split(' ')
Settings[x[0]] = "#{x[1]}"
end
### READ PATTERNS ###
readpatterns = File.new("config.conf")
arrPatterns = Array.new
patternsstarted = 0
patternsend = 0
while (i = readpatterns.gets)
if patternsstarted == 0 && i.to_s.include?('<patterns>')
patternsstarted = 1
elsif i.to_s.include?('</patterns>')
patternsend = 1
elsif patternsstarted == 1 && patternsend != 1
arrPatterns.push(i.to_s)
end
end
### READ LOG PATHS ###
readpaths = File.new("config.conf")
arrLogPaths = Array.new
pathsstarted = 0
pathsend = 0
while (i = readpaths.gets)
if pathsstarted == 0 && i.to_s.include?('<files>')
pathsstarted = 1
elsif i.to_s.include?('</files>')
pathsend = 1
elsif pathsstarted == 1 && pathsend != 1
arrLogPaths.push(i.to_s)
end
end
### CHECK LOGS AND EXECUTE NOTIFICATIONS ###
mmsobj = MMS.new
arrLogPaths.each{|i|
i = i.gsub(" ", "")
thistail = mmsobj.file_tail(i,30)
arrPatterns.each{|p|
if thistail.to_s.include?(p)
mailbody = <<EOM
"A pattern has been detected in:<br>
File: #{i.to_s}<br>
Pattern: #{p.to_s}<br><br>
Best regards<br>
PatternDetector"
EOM
mmsobj.sendmail(Settings["mail_to"], "Logpattern detected", mail_body, Settings["mail_from"], Settings["username"], Settings["password"])
end
}
}
config.conf ser således ud:
###Logparser configuration###
#Mailsettings
<mail>
smtp_server smtp.server.com
mail_from test@test.com
mail_to test@test.com
username <username>
password <password>
</mail>
#One pattern / line.
<patterns>
pattern2
pattern3
</patterns>
#Files to be scanned. If several, one / line
<files>
config.conf
</files>
Problemet er, altså, at hvis jeg bruger denne linje i koden:
thistail = mmsobj.file_tail("config.conf",30)
klager den ikke, men hvis jeg derimod bruger denne:
thistail = mmsobj.file_tail(i,30)
får jeg denne fejl:
$ sudo ruby logparser.rb
logparser.rb:28:in `initialize': No such file or directory - config.conf (Errno::ENOENT)
from logparser.rb:28:in `open'
from logparser.rb:28:in `file_tail'
from logparser.rb:97
from logparser.rb:95:in `each'
from logparser.rb:95
Er der nogen, som måtte ligge inde med en fænomenal idé til hvad jeg gør galt? :)