Class: Railswatch::SystemMonitor::ResourcesMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/railswatch/system_monitor/resources_monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 6

def context
  @context
end

#roleObject (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

#monitorsObject



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

#payloadObject



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

#runObject



48
49
50
# File 'lib/railswatch/system_monitor/resources_monitor.rb', line 48

def run
  store_data(payload)
end

#server_idObject



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_monitoringObject



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_monitoringObject



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(resource_log_message(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