Anton Smirnov 5 lat temu
rodzic
commit
46b33adbd1
10 zmienionych plików z 203 dodań i 0 usunięć
  1. +6
    -0
      .gitignore
  2. +2
    -0
      Gemfile
  3. +35
    -0
      app/app.rb
  4. +63
    -0
      app/config.rb
  5. +9
    -0
      app/include.rb
  6. +21
    -0
      app/listener.rb
  7. +30
    -0
      app/mailer.rb
  8. +26
    -0
      config/config.yml.example
  9. +6
    -0
      journald-watcher-run.rb
  10. +5
    -0
      journald-watcher.rb

+ 6
- 0
.gitignore Wyświetl plik

@@ -1,4 +1,10 @@
# config
config/config.yml

# ide
*.iml
*.ipr
*.iws

# daemons
*.pid

+ 2
- 0
Gemfile Wyświetl plik

@@ -4,3 +4,5 @@ gem 'daemons', '~> 1.1.9'
gem 'mail', '~> 2.6.3'
gem 'systemd-journal', '~> 1.2.1'

# debug
# gem 'pry'

+ 35
- 0
app/app.rb Wyświetl plik

@@ -0,0 +1,35 @@
module JournaldWatcher
class App
attr_reader :config

def run
load_config
create_listener
create_mailer
listen
end

def load_config
@config = Config.new
end

def create_listener
@listener = Listener.new(
Systemd::Journal.new(flags: @config.flags),
@config.filter
)
end

def create_mailer
@mailer = Mailer.new(@config.mailer_from, @config.mailer_to, @config.mailer_subject)
end

def listen
min_priority = @config.min_priority

@listener.listen do |entry|
@mailer.mail_entry(entry) if entry.priority.to_i <= min_priority # lower priorities have higher values
end
end
end
end

+ 63
- 0
app/config.rb Wyświetl plik

@@ -0,0 +1,63 @@
module JournaldWatcher
class Config
def initialize
@data = YAML.load_file(File.dirname(__FILE__) + '/../config/config.yml')
end

def flags
return nil unless @data['flags']

@data['flags'] = [@data['flags']] unless @data['flags'].is_a? Array

flags_arr = @data['flags']

flags = 0
flags |= Systemd::Journal::Flags::LOCAL_ONLY if flags_arr.include? 'local'
flags |= Systemd::Journal::Flags::RUNTIME_ONLY if flags_arr.include? 'runtime'
flags |= Systemd::Journal::Flags::SYSTEM_ONLY if flags_arr.include? 'system'

flags
end

def filter
@data['filter'] or raise 'No filter given'
end

def mailer_from
@data['mail'] && @data['mail']['from'] or raise 'No from address given'
end

def mailer_to
@data['mail'] && @data['mail']['to'] or raise 'No to address(es) given'
end

def mailer_subject
@data['mail'] && @data['mail']['subject'] || ''
end

def min_priority
priority_str = @data['min_priority']

case priority_str
when 'emerg'
Systemd::Journal::LOG_EMERG
when 'alert'
Systemd::Journal::LOG_ALERT
when 'crit'
Systemd::Journal::LOG_CRIT
when 'err'
Systemd::Journal::LOG_ERR
when 'warning'
Systemd::Journal::LOG_WARNING
when 'notice'
Systemd::Journal::LOG_NOTICE
when 'info'
Systemd::Journal::LOG_INFO
when 'debug'
Systemd::Journal::LOG_DEBUG
else
Systemd::Journal::LOG_DEBUG
end
end
end
end

+ 9
- 0
app/include.rb Wyświetl plik

@@ -0,0 +1,9 @@
require 'yaml'
require 'json'
require 'mail'
require 'systemd/journal'

require_relative 'app'
require_relative 'config'
require_relative 'listener'
require_relative 'mailer'

+ 21
- 0
app/listener.rb Wyświetl plik

@@ -0,0 +1,21 @@
module JournaldWatcher
class Listener
def initialize(journal, filter)
@journal = journal
@filter = filter
end

def listen(&block)
@journal.filter @filter
@journal.seek :tail
watch { |entry| block.call(entry) }
end

private

# like Systemd::Journal.watch but don't wait forever
def watch
loop { (yield @journal.current_entry while @journal.move_next) if @journal.wait(100_000) }
end
end
end

+ 30
- 0
app/mailer.rb Wyświetl plik

@@ -0,0 +1,30 @@
require 'pry'

module JournaldWatcher
class Mailer
def initialize(from, to, subject)
@from = from
@to = to
@subject = subject
end

def mail_entry(entry)
subject = entry.message

if subject.length > 50
subject = subject[0..50] + '...'
end

msg = Mail.new
msg.to = @to
msg.from = @from
msg.subject = "#{@subject} #{subject}"
msg.body = entry.message
msg.add_file filename: 'data.json', content: entry.to_h.to_json

binding.pry

# msg.deliver
end
end
end

+ 26
- 0
config/config.yml.example Wyświetl plik

@@ -0,0 +1,26 @@
# [optional]
# *flags* is systemd-journal flags
# a combination of local, runtime, system
# local: Only open journal files generated on the local machine.
# runtime: Only open non-persistent journal files.
# system: Only open kernel and system service journal files.
flags: [local]

# [required]
# *filter* is systemd filter
# _exe: "/usr/bin/ruby-mri" will be converted to _EXE=/usr/bin/ruby-mri
#
# @see `man 7 systemd.journal-fields`
filter:
_exe: "/usr/bin/ruby-mri"

# [required]
mail:
from: "[email protected]" # from address
to: "[email protected]" # to addresses
subject: "[Journald Watcher]" # subject prefix

# [optional]
# one of emerg, alert, crit, err, warning, notice, info, debug
# debug if nothing specified or incorrect
min_priority: warning

+ 6
- 0
journald-watcher-run.rb Wyświetl plik

@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

require_relative 'app/include'

jw = JournaldWatcher::App.new
jw.run

+ 5
- 0
journald-watcher.rb Wyświetl plik

@@ -0,0 +1,5 @@
#!/usr/bin/env ruby

require 'daemons'

Daemons.run('journald-watcher-run.rb')

Ładowanie…
Anuluj
Zapisz