Class: Instrumentation::Report
- Inherits:
-
Object
- Object
- Instrumentation::Report
- Defined in:
- lib/instrumentation/report.rb
Overview
Reads data from system and process information and writes it to websocket
Instance Attribute Summary collapse
-
#loadavg ⇒ Object
readonly
Returns the value of attribute loadavg.
-
#memory ⇒ Object
readonly
Returns the value of attribute memory.
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
- #event(name, data) ⇒ Object
-
#initialize(pid) ⇒ Report
constructor
A new instance of Report.
- #join ⇒ Object
- #read_data ⇒ Object
- #read_loadavg ⇒ Object
- #read_memory ⇒ Object
- #send_data ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(pid) ⇒ Report
Returns a new instance of Report.
8 9 10 11 12 13 14 |
# 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 |
Instance Attribute Details
#loadavg ⇒ Object (readonly)
Returns the value of attribute loadavg.
5 6 7 |
# File 'lib/instrumentation/report.rb', line 5 def loadavg @loadavg end |
#memory ⇒ Object (readonly)
Returns the value of attribute memory.
5 6 7 |
# File 'lib/instrumentation/report.rb', line 5 def memory @memory end |
#socket ⇒ Object
Returns the value of attribute socket.
6 7 8 |
# File 'lib/instrumentation/report.rb', line 6 def socket @socket end |
Instance Method Details
#event(name, data) ⇒ Object
41 42 43 |
# File 'lib/instrumentation/report.rb', line 41 def event(name, data) { data_type: name, data: data }.to_json end |
#join ⇒ Object
53 54 55 |
# File 'lib/instrumentation/report.rb', line 53 def join @thread.join end |
#read_data ⇒ Object
26 27 28 29 30 31 32 |
# 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 ⇒ Object
49 50 51 |
# File 'lib/instrumentation/report.rb', line 49 def read_loadavg LoadAverage.new.read[:one] end |
#read_memory ⇒ Object
45 46 47 |
# File 'lib/instrumentation/report.rb', line 45 def read_memory Memory.new(@pid).read end |
#send_data ⇒ Object
34 35 36 37 38 39 |
# 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 ⇒ Object
57 58 59 |
# File 'lib/instrumentation/report.rb', line 57 def shutdown @thread.kill end |
#start ⇒ Object
16 17 18 19 20 21 22 23 24 |
# 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 |