Module: NagiosLogger
- Defined in:
- lib/nagios_logger.rb
Constant Summary collapse
- SERVICE_LUT =
{ "CPU on ra" => "Тестовый сервер", "CPU on remaccess" => "Рабочий сервер", "HTTP" => "УРМ", "HTTP-work" => "СОД", "HTTP-lesson" => "Тестовый СОД" }
- LEVEL_LUT =
{ "CRITICAL" => "Ошибка", "WARNING" => "Предупреждение" }
Instance Method Summary collapse
- #load_file(fname) ⇒ Object
- #load_file_list(pref) ⇒ Object
- #load_files(pref, mask) ⇒ Object
- #make_events(sites) ⇒ Object
- #parse_line(line) ⇒ Object
- #place_host(hsh, sites) ⇒ Object
- #sort_sites(sites) ⇒ Object
Instance Method Details
#load_file(fname) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/nagios_logger.rb', line 26 def load_file(fname) data = [] File.open(fname, "r") do |f| f.readlines.each do |line| data << line end end data end |
#load_file_list(pref) ⇒ Object
14 15 16 |
# File 'lib/nagios_logger.rb', line 14 def load_file_list(pref) Dir[File.join(pref, "*")].map{|n| File.basename(n) } end |
#load_files(pref, mask) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/nagios_logger.rb', line 18 def load_files(pref, mask) data = [] Dir[File.(File.join(pref, mask))].each do |fname| data += load_file(fname) end data end |
#make_events(sites) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/nagios_logger.rb', line 36 def make_events(sites) events = [] event = [] sites.each do |k, v| v.each do |key, val| val.each do | it | event << it if it[:level] == "OK" events << event.dup event.clear end end end end events.sort{|a, b| a.first[:stamp] <=> b.first[:stamp] } end |
#parse_line(line) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/nagios_logger.rb', line 53 def parse_line(line) data = {} line.force_encoding("KOI8-R").encode("UTF-8").scan(/^\[(\d+)\]\s+(.+)$/u) do |m| unless m[0] #$stderr << m return nil end if m[1] !~ /^SERVICE ALERT/ return nil end data.merge!(:stamp => Time.at(m[0].to_i)) m[1].scan(/SERVICE ALERT:\s+(.+?);(.+?);(.+?);(.+?);(.+?);(.+)$/) do |n| data[:host] = n[0] data[:service] = n[1] data[:level] = n[2] data[:type] = n[3] data[:count] = n[4] data[:message] = n[5] end return nil unless SERVICE_LUT.has_key?(data[:service]) end data[:service] = SERVICE_LUT[data[:service]] data end |
#place_host(hsh, sites) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/nagios_logger.rb', line 78 def place_host(hsh, sites) unless sites.has_key?(hsh[:host]) sites[hsh[:host]] = {} end unless sites[hsh[:host]].has_key?(hsh[:service]) sites[hsh[:host]][hsh[:service]] = [] end sites[hsh[:host]][hsh[:service]] << hsh end |
#sort_sites(sites) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/nagios_logger.rb', line 88 def sort_sites(sites) sites.each do |k, v| v.each do |key, val| val.sort!{|a, b| a[:stamp] <=> b[:stamp] } end end end |