Class: Railswatch::SystemMonitor::ResourcesMonitor
- Inherits:
-
Object
- Object
- Railswatch::SystemMonitor::ResourcesMonitor
- Defined in:
- lib/railswatch/system_monitor/resources_monitor.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
Instance Method Summary collapse
-
#initialize(context, role) ⇒ ResourcesMonitor
constructor
A new instance of ResourcesMonitor.
- #monitors ⇒ Object
- #payload ⇒ Object
- #run ⇒ Object
- #server_id ⇒ Object
- #start_monitoring ⇒ Object
- #stop_monitoring ⇒ Object
-
#store_data(data) ⇒ Object
rubocop:disable Metrics/MethodLength.
Constructor Details
#initialize(context, role) ⇒ ResourcesMonitor
Returns a new instance of ResourcesMonitor.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 8 def initialize(context, role) @context = context @role = role @mutex = Mutex.new @thread = nil return unless Railswatch._resource_monitor_enabled start_monitoring end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 6 def context @context end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
6 7 8 |
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 6 def role @role end |
Instance Method Details
#monitors ⇒ Object
42 43 44 45 46 |
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 42 def monitors @monitors ||= Railswatch.system_monitors.map do |class_name| Railswatch::Widgets.const_get(class_name).new(nil) end end |
#payload ⇒ Object
36 37 38 39 40 |
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 36 def payload monitors.reduce({}) do |data, monitor| data.merge(monitor.key => monitor.measure) end end |
#run ⇒ Object
48 49 50 |
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 48 def run store_data(payload) end |
#server_id ⇒ Object
67 68 69 |
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 67 def server_id @server_id ||= ENV['RAILSWATCH_SERVER_ID'] || `hostname`.strip end |
#start_monitoring ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 19 def start_monitoring @mutex.synchronize do return if @thread @thread = Thread.new { monitor_loop } end end |
#stop_monitoring ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 27 def stop_monitoring @mutex.synchronize do return unless @thread @thread.kill @thread = nil end end |
#store_data(data) ⇒ Object
rubocop:disable Metrics/MethodLength
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 52 def store_data(data) # rubocop:disable Metrics/MethodLength now = Railswatch::Utils.kind_of_now now = now.change(sec: 0, usec: 0) Railswatch.log((data)) Railswatch::Models::ResourceRecord.new( server: server_id, context: context, role: role, datetime: now.strftime(Railswatch::FORMAT), datetimei: now.to_i, json: data ).save end |