Adding most active mac and runner up stats

This commit is contained in:
Will Bradley 2013-09-27 00:56:52 -07:00
parent 3ddfb3442b
commit e61ac5d32e
2 changed files with 65 additions and 1 deletions

View File

@ -19,6 +19,56 @@ require "optparse"
#require "rubygems" #require "rubygems"
def index def index
recent_mac_logs_ungrouped = MacLog.last(1000)
@mac_time_start_date = recent_mac_logs_ungrouped.first.created_at
recent_mac_logs = recent_mac_logs_ungrouped.group_by(&:mac)
@mac_times = {}
# Go thru each mac
recent_mac_logs.each do |mac_log|
last_active = nil
# And the entries for each mac (mac_log.first is the string, mac_log.last is the array)
mac_log.last.each do |entry|
# Find an activate followed immediately by a deactivate
if entry.action == "activate"
last_active = entry
else
if last_active && entry.action == "deactivate"
# Calculate the time difference between the two and append to this mac's total time
this_entry = @mac_times[entry.mac]
if this_entry
this_time = this_entry[:time]
else
this_time = 0
end
@mac_times[entry.mac] = {:mac => entry, :time => (entry.created_at - last_active.created_at) + this_time}
else
# No pair found; discard.
last_active = nil
end
end
end
end
@mac_times_sorted = @mac_times.sort{|a,b| b.last[:time] <=> a.last[:time] }
@most_active_mac = nil
@runner_up_mac = nil
@mac_times_sorted.each do |mac_time|
unless @most_active_mac
this_mac = Mac.find_by_mac(mac_time.first)
unless this_mac.hidden
@most_active_mac = this_mac
@most_active = mac_time
end
else
unless @runner_up_mac
this_mac = Mac.find_by_mac(mac_time.first)
unless this_mac.hidden
@runner_up_mac = this_mac
@runner_up = mac_time
end
end
end
end
#@active_macs = Mac.where(:active => true, :hidden => false) #@active_macs = Mac.where(:active => true, :hidden => false)
#@active_macs += Mac.where(:active => true, :hidden => nil) #@active_macs += Mac.where(:active => true, :hidden => nil)
@ -211,7 +261,7 @@ Rails.logger.info "starting scan..."
Rails.logger.info "Reading stdin: "+stdin.inspect Rails.logger.info "Reading stdin: "+stdin.inspect
stdin.each { |line| stdin.each { |line|
next if line !~ /^([\d\.]+)\s+([[:xdigit:]:]+)\s/; next if line !~ /^([\d\.]+)\s+([[:xdigit:]:]+)\s/;
macs[$2] = $1; macs[($2).downcase] = ($1).downcase;
} }
} }

View File

@ -1,3 +1,17 @@
<b>Most Active Machine Last <%= distance_of_time_in_words DateTime.now, @mac_time_start_date %>:</b>
<span title="<%= @most_active_mac.mac %><%= " - "+@most_active_mac.ip.to_s if can? :read_details, @most_active_mac %>">
<%= @most_active_mac.user.name unless @most_active_mac.user.blank? %>
<%= "("+@most_active_mac.note+")" unless @most_active_mac.note.blank? %>
- <%= (@most_active.last[:time] / 1.hour).round %> hrs
</span>
<br/>
<b>Runner Up:</b>
<span title="<%= @runner_up_mac.mac %><%= " - "+@runner_up_mac.ip.to_s if can? :read_details, @runner_up_mac %>">
<%= @runner_up_mac.user.name unless @runner_up_mac.user.blank? %>
<%= "("+@runner_up_mac.note+")" unless @runner_up_mac.note.blank? %>
- <%= (@runner_up.last[:time] / 1.hour).round %> hrs
</span>
<h2>What machines are on our network?</h2> <h2>What machines are on our network?</h2>
<%= link_to "New MAC registration", new_mac_path if can? :create, Mac %> <%= link_to "New MAC registration", new_mac_path if can? :create, Mac %>