Class: NagiosWebbedLog

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/nagios_webbed_log.rb

Instance Method Summary collapse

Instance Method Details

#get_paramsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nagios_webbed_log.rb', line 65

def get_params
 if params[:year] && (2000..2050).map(&:to_s).include?(params[:year])
  @year = params[:year]
 else
  @year = "0"
 end
 if params[:month] && (1..12).map(&:to_s).map{|s| s.rjust(2, "0") }.include?(params[:month])
  @month = params[:month]
 else
  @month = "0"
 end
 if params[:day] && (1..31).map(&:to_s).map{|s| s.rjust(2, "0") }.include?(params[:day])
  @day = params[:day]
 else
  @day = "0"
 end
end

#load_datesObject



83
84
85
86
87
88
# File 'lib/nagios_webbed_log.rb', line 83

def load_dates
 @names_list = load_file_list(ENV["NAGIOS_LOG"])
 @years = @names_list.map{|n| n.split("-") }.map{|n| n[3] }.uniq
 @days = @names_list.map{|n| n.split("-") }.map{|n| n[2] }.uniq
 @months = @names_list.map{|n| n.split("-") }.map{|n| n[1] }.uniq
end

#load_eventsObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/nagios_webbed_log.rb', line 54

def load_events
 lines = load_files(ENV["NAGIOS_LOG"],"nagios-#{@month.to_i == 0 ? '*' : @month}-#{@day.to_i == 0 ? '*' : @day}-#{@year.to_i == 0 ? '*' : @year}-*.log")
 sites = {}
 lines.compact.map{|l| parse_line(l) }.compact.each do |hsh|
  place_host(hsh, sites)
 end
 sort_sites(sites)
 events = make_events(sites)
 events
end

#to_csv(data) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nagios_webbed_log.rb', line 89

def to_csv(data)
 levs = %w(OK WARNING CRITICAL UNKNOWN)
 out = CSV.generate( {:col_sep => ";"}) do |csv|
  #csv << hdr
  data.each do |evt|
   lvl = evt.map{|it| levs.index(it[:level]) || 3 }.max
   ll = evt.detect{|it| not levs.include?(it[:level]) }
   csv << [evt.first[:stamp].strftime("%Y-%m-%d"), evt.first[:stamp].strftime("%H:%M:%S"), (evt.last[:stamp] - evt.first[:stamp]).to_i, evt.first[:service], NagiosLogger::LEVEL_LUT[levs[lvl]]]
  end
 end
 out.force_encoding('UTF-8').encode('Windows-1251')
end