1
2
3
4
5
6
7
8
9
10
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#!/usr/bin/env ruby
#
# lognotify.rb
# http://github.com/omp/lognotify
#
# Copyright 2010 David Vazgenovich Shakaryan <dvshakaryan@gmail.com>
# Distributed under the terms of the GNU General Public License v3.
# See http://www.gnu.org/licenses/gpl.txt for the full license text.
require 'ftools'
CACHE_DIR="~/.cache/lognotify"
CONFIG_DIR="~/.config/lognotify"
# Configuration file parser.
def parse identifier
conf = Hash.new
path = File.expand_path(CONFIG_DIR + '/' + identifier + '.conf')
File.open(path) do |file|
file.each_line do |line|
# Remove whitespace from beginning of line, allowing for indentation.
line.lstrip!
# Ignore empty lines and comments.
unless line.empty? or line[0,1] == '#'
key, value = line.split('=', 2)
# Raise an error if line does not contain a key/value pair.
raise 'Error reading line ' + file.lineno.to_s + '.' if value.nil?
conf[key.strip.to_sym] = value.strip
end
end
end
return conf
end
# Count lines in cached log.
def count_lines identifier
path = File.expand_path(CACHE_DIR + '/' + identifier + '.log')
return File.open(path).readlines.length
end
# Retrieve new lines via SSH.
def retrieve_lines path, lines, hostname
command = "cat #{path}"
command << " | sed '1,#{lines}d'" if lines > 0
return %x[ssh #{hostname} "#{command}"]
end
# Append new lines to cached log.
def append_lines identifier, lines
path = File.expand_path(CACHE_DIR + '/' + identifier + '.log')
file = File.open(path, 'a')
file.print lines
file.close
end
# Output all messages immediately, as opposed to buffering.
STDOUT.sync = true
# Create cache directory, if nonexistent.
path = File.expand_path(CACHE_DIR)
File.makedirs(path) unless File.directory?(path)
# Treat each argument as a log identifier.
ARGV.each do |identifier|
conf = parse(identifier)
# Create cache file, if nonexistent.
path = File.expand_path(CACHE_DIR + '/' + identifier + '.log')
File.open(path, 'w').close unless File.exist?(path)
print '* Counting lines in cached log... '
lines = count_lines(identifier)
puts lines
print '* Retrieving new lines via SSH... '
newlines = retrieve_lines(conf[:log_path], lines, conf[:ssh_hostname])
puts 'Done'
puts '* Number of new lines: ' + newlines.lines.count.to_s
unless newlines.lines.count.zero?
print '* Appending new lines to cached log... '
append_lines(identifier, newlines)
puts 'Done'
puts newlines
end
end
|