Created
April 13, 2017 23:47
-
-
Save TENsaga/d7b2ab6da92d6ae434a212293c32227d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/Users/aspen/.rubies/ruby-2.4.1/bin/ruby | |
# Statistics logging system - Aspen Swanson | |
require 'pry' | |
# Class for gathering current up/down bandwidth on en0 in KB | |
class BandwidthMonitor | |
attr_reader :initial | |
attr_accessor :current_in, :current_out, :in_kb, :out_kb | |
def initialize | |
@current_in, @current_out = current_in_out_bandwidth | |
@in_kb = [] | |
@out_kb = [] | |
end | |
# System call utilizing netstat, converted from bytes to kb | |
def current_in_out_bandwidth | |
`netstat -b -I en0 -n | tail -1 | awk '{print $(NF-4),$(NF-1)}' | awk '{ foo = $1 ; out = $2 ; print foo, out }'`.split.map(&:to_i).map { |x| (x /1024) } | |
end | |
def update_current | |
@current_in, @current_out = current_in_out_bandwidth | |
end | |
# Add difference of KB increase to both arrays, update new current | |
def increase | |
in_kb, out_kb = current_in_out_bandwidth | |
@in_kb << in_kb -= @current_in | |
@out_kb << out_kb -= @current_out | |
update_current | |
end | |
# Returns sum total of each array | |
def total | |
return @in_kb.inject(:+), @out_kb.inject(:+) | |
end | |
# For use with Engine | |
def run | |
increase | |
total | |
end | |
end | |
# Class which gathers data for processes and current cpu load | |
class ProcessLoad | |
attr_accessor :process, :load, :time, :total_time, | |
:average_load, :current_load, | |
:average_proc, :current_proc | |
def initialize | |
@time = Time.now | |
@process = [] | |
@load = [] | |
@total_time | |
@average_load | |
@current_load | |
@average_proc | |
@current_proc | |
end | |
# Statistic snapshot | |
def current_stats | |
return @process << `ps -a | wc -l`.strip.to_i, | |
@load << `ps -A -o %cpu | awk '{sum+=$1} END {print sum "%"}'`.strip.to_f | |
end | |
# Average of array values | |
def average(input) | |
(input.inject(:+) / input.length).round | |
end | |
# Format seconds since | |
def run_time(time) | |
(@time - Time.now).round(2).abs | |
end | |
# For use with Engine | |
def run(i) | |
@process, @load = current_stats | |
@total_time = run_time(@time) | |
@average_load = average(@load) | |
@current_load = @load[i] | |
@average_proc = average(@process) | |
@current_proc = @process[i] | |
end | |
end | |
class Engine | |
def initialize | |
@iteration = 0 | |
@bw = BandwidthMonitor.new | |
@pl = ProcessLoad.new | |
end | |
def execute | |
while true | |
@pl.run(@iteration) | |
@bw.run | |
output | |
sleep 1 | |
@iteration += 1 | |
end | |
end | |
def output | |
print `clear` | |
puts "Runtime: #{@pl.total_time} seconds" | |
puts "Stats/ Average/ Current" | |
puts "Load: \t #{@pl.average_load}% / #{@pl.current_load}%" | |
puts "Procs: #{@pl.average_proc} / #{@pl.current_proc}" | |
puts "------------" | |
puts "Init In/Out: | |
#{@bw.total[0]}KB | #{@bw.total[1]}KB " | |
puts "------------" | |
puts "Total In/Out: | |
#{@bw.current_in_out_bandwidth[0]}KB | #{@bw.current_in_out_bandwidth[1]}KB" | |
end | |
end | |
eng = Engine.new | |
p eng.execute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment