class Instrumentation::Report
Reads data from system and process information and writes it to websocket
Attributes
loadavg[R]
memory[R]
socket[RW]
Public Class Methods
new(pid)
click to toggle source
# File lib/instrumentation/report.rb, line 8 def initialize(pid) @pid = pid @sleep = 1 @memory = BoundedArray.new(300) @loadavg = BoundedArray.new(300) @socket = nil end
Public Instance Methods
event(name, data)
click to toggle source
# File lib/instrumentation/report.rb, line 41 def event(name, data) { data_type: name, data: data }.to_json end
join()
click to toggle source
# File lib/instrumentation/report.rb, line 53 def join @thread.join end
read_data()
click to toggle source
# File lib/instrumentation/report.rb, line 26 def read_data now = Time.now.strftime('%FT%T') @memory = @memory << [now, read_memory] @loadavg = @loadavg << [now, read_loadavg] rescue => error puts "Error when reading data: #{error.inspect}" end
read_loadavg()
click to toggle source
# File lib/instrumentation/report.rb, line 49 def read_loadavg LoadAverage.new.read[:one] end
read_memory()
click to toggle source
# File lib/instrumentation/report.rb, line 45 def read_memory Memory.new(@pid).read end
send_data()
click to toggle source
# File lib/instrumentation/report.rb, line 34 def send_data socket.send_data(event(:memory, @memory.items)) socket.send_data(event(:loadavg, @loadavg.items)) rescue => error puts "Error when sending data: #{error.inspect}" end
shutdown()
click to toggle source
# File lib/instrumentation/report.rb, line 57 def shutdown @thread.kill end
start()
click to toggle source
# File lib/instrumentation/report.rb, line 16 def start @thread = Thread.new do loop do read_data send_data if socket sleep @sleep end end end