Created
January 8, 2010 21:45
-
-
Save mpasternacki/272449 to your computer and use it in GitHub Desktop.
Periodically send new lines in log file to specified email address.
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
# Script to periodically send new lines in log file to specified mail address. | |
# | |
# Similar to http://bucardo.org/wiki/Tail_n_mail - but I needed | |
# substitution to handle newlines in Python-generated messages, and it | |
# was faster to homebrew my own script than to add features to an | |
# already overfeatured Perl script. And I want to practice Ruby - seems | |
# to be fine for small, one-off scripts. | |
# | |
# License: WTFPL or MIT, whichever you prefer. | |
require 'net/smtp' | |
require 'yaml' | |
DEFAULT_CONFIG = { | |
'file' => '/var/log/FIXME', | |
'offset' => 0, | |
'mailto' => 'FIXME@example.com', | |
'mailfrom' => 'FIXME@example.com', | |
'subject' => 'Logged messages', | |
'FIXME' => "Remove this line from config to notify script you've actually configured anything.", | |
} | |
unless File.exists? ARGV[0] | |
File.open ARGV[0], 'w' do |f| | |
f.puts YAML::dump(DEFAULT_CONFIG) | |
end | |
exit | |
end | |
$conf = YAML::load_file ARGV[0] | |
abort "Please configure #{ARGV[0]} and remove the FIXME line." if $conf['FIXME'] | |
content = '' | |
File.open $conf['file'] do |f| | |
f.seek $conf['offset'] | |
content = f.read | |
$conf['offset'] = f.tell | |
end | |
$conf['substitute'].each do |key, value| | |
content.gsub! key, value | |
end | |
unless content.empty? | |
Net::SMTP.start('localhost') do |smtp| | |
smtp.send_message <<MESSAGE, $conf['mailfrom'], $conf['mailto'] | |
From: <#{$conf['mailfrom']}> | |
To: <#{$conf['mailto']}> | |
Subject: #{$conf['subject']} | |
#{content} | |
MESSAGE | |
end | |
end | |
File.open ARGV[0]+'+', 'w' do |f| | |
f.puts YAML::dump($conf) | |
end | |
File.rename ARGV[0], ARGV[0]+'~' | |
File.rename ARGV[0]+'+', ARGV[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment