例外:
NoMethodError in CsdashboardController#index
undefined method `[]' for nil:NilClass
Rails.root: /opt/csdashboard
Application Trace | Framework Trace | Full Trace
app/controllers/csdashboard_controller.rb:106:in `index'
Request
Parameters:
None
第106行
@dropdataitem = DropSeries.new(1, @data3[0]["fields"]["@timestamp"])
这是完整的课程,抱歉,如果代码太多了。
class CsdashboardController < ApplicationController
def index
@query_size = 500
@esearch = Elasticsearch::Client.new :log => true
@ts = DateTime.now - 30.days
if params[:query].present?
@ts = DateTime.strptime(params[:ct], '%m-%d-%Y %H:%M') unless params[:ct].nil?
@ts = DateTime.now - params[:min].to_i.minutes unless params[:min].nil?
@ts = DateTime.now - params[:hrs].to_i.hours unless params[:hrs].nil?
@ts = DateTime.now - params[:days].to_i.days unless params[:days].nil?
@ts = DateTime.now - 100.years unless params[:alltime].nil?
@data = @esearch.search :q => (params[:query]), :size => @query_size
@data = @data["hits"]["hits"]
@data = @data.sort_by { |h| h["_source"]["@timestamp"] }.reverse!
@filtered_data = Array.new
if @data.count > 0
@data.each do |d|
break if d["_source"]["@timestamp"].to_time < @ts
@filtered_data.push(d) if d["_source"].has_key?("@message") || d["_source"].has_key?("message")
end
end
render 'csdashboard/search'
else
# will render 'csdashboard/index'
@widgets = Array.new
@dropitems = Array.new
@dropdataitems = Array.new
@ts = DateTime.now - 30.days
@ts = DateTime.strptime(params[:ct], '%m-%d-%Y %H:%M') unless params[:ct].nil?
@ts = DateTime.now - params[:min].to_i.minutes unless params[:min].nil?
@ts = DateTime.now - params[:hrs].to_i.hours unless params[:hrs].nil?
@ts = DateTime.now - params[:days].to_i.days unless params[:days].nil?
@ts = DateTime.now - 100.years unless params[:alltime].nil?
@data3 = @esearch.search :body => {:query => {:term => {"@message" => "drop"}}, :size => @query_size, :fields => ["@message", "@timestamp"] }
@data3 = @data3["hits"]["hits"]
@dropdatahash = Hash.new(0)
@data3.each do |dd|
break if dd["fields"]["@timestamp"].to_time < @ts
unless dd["fields"]["@message"].nil?
ds = dd["fields"]["@timestamp"].to_date.strftime("%m/%Y")
@dropdatahash[ds] += 1
end
end
@ddhkeys = @dropdatahash.keys
#@dropdatahash = @dropdatahash.sort_by { |k,v| k }.reverse!# _by { |k| k["key"] }
@ddhkeys = @ddhkeys.sort_by{|d| m,y=d.split("/");[y,m]}
@dropdataitem = Array.new
@dropdataitem = DropSeries.new(1, @data3[0]["fields"]["@timestamp"])
@dropdataitems = Array.new
@ddhkeys.each do |dh|
#@dropdataitems.push(DropSeries.new(dh[1], dh[0]))
@dropdataitems.push(DropSeries.new(@dropdatahash[dh], dh))
end
@snort_data_hash = Array.new
@ip_addresses = Array.new
@snort_data = @esearch.search :q => ("snort"), :size => @query_size
@snort_data = @snort_data["hits"]["hits"]
@locations = Array.new
if @snort_data.count > 0
a= Array.new
@snort_data.each do |f|
break if f["_source"]["@timestamp"].to_time < @ts
ip_data = nil
msg = f["_source"]["@message"]
ip_data = msg.scan(/(?:[0-9]{1,3}\.){3}[0-9]{1,3}/)
a.push(ip_data[0]) unless ip_data[0].nil?
end
snort_res=Hash[a.group_by {|x| x}.map {|k,v| [k,v.count]}]
snort_res.each do |r|
@snort_data_hash.push(SnortIP.new(r[0],r[1]))
@ip_addresses.push(r[0].to_s)
#@geoloc = Geokit::Geocoders::IpGeocoder.geocode(r[0].to_s)
@geoloc = Geokit::Geocoders::MultiGeocoder.geocode(r[0].to_s)
@ip_addresses.push(@geoloc.lat)
location = Location.new(@geoloc.lat, @geoloc.lng, r[1], r[0])
@locations.push(location)
end
end
@data = @esearch.search :q => ("classification"), :size => @query_size
@data = @data["hits"]["hits"]
@data = @data.sort_by { |h| h["_source"]["@timestamp"] }.reverse!
if @data.count > 0
@filtered_data = Array.new
@data.each do |d|
break if d["_source"]["@timestamp"].to_time < @ts
@filtered_data.push(d)
end
if @filtered_data.count > 0
a= Array.new
@filtered_data.each do |f|
str_to_split = f["_source"]["@message"]
str = str_to_split
msg_data = str.split(%r{\[(Classification):+([\w\s]*)})
msg_data.each_with_index do |d,index|
classification_msg = msg_data[index + 1].to_s.squish if d.to_s == "Classification"
a.push(classification_msg) unless classification_msg.nil?
end
end
res=Hash[a.group_by {|x| x}.map {|k,v| [k,v.count]}]
res.each do |r|
@widgets.push(Widget1.new(r[0],r[1]))
end
end
end
end
end
end
答案 0 :(得分:0)
您的错误只有3个选项:
@data3 == nil
@data3[0] == nil
@data3[0]["fields"] == nil
你应该自己检查哪一个是