Class: Graphiterb::Monitors::PeriodicMonitor

Inherits:
Object
  • Object
show all
Includes:
Utils::SystemInfo
Defined in:
lib/graphiterb/monitors.rb

Overview

Accepts a lightweight call every iteration.

Once either a time or an iteration criterion is met, executes the block and resets the timer until next execution.

Note that the time_interval is measured *excution to execution* and not in multiples of iter_interval. Say I set a time_interval of 300s, and happen to iterate at 297s and 310s after start. Then the monitor will execute at 310s, and the next execution will happen on or after 610s.

Also note that when either criterion is met, both criteria are reset. Say I set a time interval of 300s and an iter_interval of 10_000; and that at 250s I reach iteration 10_000. Then the monitor will execute on or after 20_000 iteration or 550s, whichever happens first.

Stolen from Monkeyshines::Monitor::PeriodicMonitor

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::SystemInfo

#graphite_identifier, #hostname, #node_name

Constructor Details

#initialize(main_scope, options = {}) ⇒ PeriodicMonitor

Create a new PeriodicMonitor



49
50
51
52
53
54
55
56
57
58
# File 'lib/graphiterb/monitors.rb', line 49

def initialize main_scope, options={}
  self.main_scope    = main_scope
  self.started_at    = Time.now.utc.to_f
  self.last_time     = started_at
  self.iter          = 0
  self.current_iter  = 0
  self.options       = options
  self.time_interval = options[:time]  || 30 
  self.iter_interval = options[:iters] || 30
end

Instance Attribute Details

#current_iterObject

Internal metrics stored by the monitor.



42
43
44
# File 'lib/graphiterb/monitors.rb', line 42

def current_iter
  @current_iter
end

#iterObject

Internal metrics stored by the monitor.



42
43
44
# File 'lib/graphiterb/monitors.rb', line 42

def iter
  @iter
end

#iter_intervalObject

Maximum number of internal “iterations” that should elapse between running the monitor



36
37
38
# File 'lib/graphiterb/monitors.rb', line 36

def iter_interval
  @iter_interval
end

#last_timeObject

Internal metrics stored by the monitor.



42
43
44
# File 'lib/graphiterb/monitors.rb', line 42

def last_time
  @last_time
end

#main_scopeObject

The main scope under which the monitor’s metrics will be written.



29
30
31
# File 'lib/graphiterb/monitors.rb', line 29

def main_scope
  @main_scope
end

#optionsObject

The options hash the monitor was created with.



39
40
41
# File 'lib/graphiterb/monitors.rb', line 39

def options
  @options
end

#started_atObject

Internal metrics stored by the monitor.



42
43
44
# File 'lib/graphiterb/monitors.rb', line 42

def started_at
  @started_at
end

#time_intervalObject

Maximum number of seconds that should elapse between running the monitor.



33
34
35
# File 'lib/graphiterb/monitors.rb', line 33

def time_interval
  @time_interval
end

Instance Method Details

#enough_iterations?Boolean

True if more than iter_interval has elapsed since last execution.

Returns:

  • (Boolean)


70
71
72
# File 'lib/graphiterb/monitors.rb', line 70

def enough_iterations?
  iter % iter_interval == 0 if iter_interval
end

#enough_time?(now) ⇒ Boolean

True if more than time_interval has elapsed since last execution.

Returns:

  • (Boolean)


75
76
77
# File 'lib/graphiterb/monitors.rb', line 75

def enough_time? now
  (now - last_time) > time_interval if time_interval
end

#get_metrics(metrics, since) ⇒ Object

Add metrics to the metrics array.

This method is meant to be overridden by a sub-class (indeed, it will raise an error if called directly).

It should take an array of metrics and a time interval since last ran and insert metrics into the array.“)

Parameters:

  • metrics (Array<String>)
  • since (Float)

    the number of seconds since the last time the monitor ran

Raises:



141
142
143
# File 'lib/graphiterb/monitors.rb', line 141

def get_metrics metrics, since
  raise Graphiterb::NotImplementedError.new("Override the get_metrics method of the #{self.class} class")
end

#inst_rate(now) ⇒ Float

“Instantaneous” iterations per second

Returns:

  • (Float)


96
97
98
# File 'lib/graphiterb/monitors.rb', line 96

def inst_rate now
  current_iter.to_f / (now-last_time).to_f
end

#periodically {|Array<String>, Time| ... } ⇒ Object

If the interval conditions are met, executes block; otherwise just does bookkeeping and returns.

Yields:

  • (Array<String>, Time)


118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/graphiterb/monitors.rb', line 118

def periodically &block
  self.iter += 1
  self.current_iter += 1
  now       = Time.now.utc.to_f
  if enough_iterations? || enough_time?(now)
    metrics = []
    block.call(metrics, (now-last_time))
    sender.send(*metrics)
    self.last_time = now
    self.current_iter = 0
  end
end

#rateFloat

Overall iterations per second

Returns:

  • (Float)


89
90
91
# File 'lib/graphiterb/monitors.rb', line 89

def rate
  iter.to_f / since.to_f
end

#run!Object

Run this monitor.

Sleep for 1 second and then wake up and check if either enough time (time_interval) or enough iterations (iter_interval) have passed run get_metrics if so.



150
151
152
153
154
155
156
157
# File 'lib/graphiterb/monitors.rb', line 150

def run!
  loop do
    periodically do |metrics, since|
      get_metrics metrics, since
    end
    sleep 1
  end
end

#scope(*names) ⇒ String

Return the scope built from this monitor’s main_scope and the given names.

monitor.main_scope
#=> 'system.parameters'
monitor.scope 'disk', 'space'
#=> 'system.paramters.disk.space'

Parameters:

  • names (Array<String>)

Returns:

  • (String)


110
111
112
# File 'lib/graphiterb/monitors.rb', line 110

def scope *names
  [main_scope, *names].flatten.reject(&:blank?).join('.')
end

#senderGraphiterb::Sender

The Graphiterb::Sender used to communicate with the Graphite server.

Returns:



64
65
66
# File 'lib/graphiterb/monitors.rb', line 64

def sender
  @sender ||= Graphiterb::Sender.new
end

#sinceTime

Time since monitor was created

Returns:

  • (Time)


82
83
84
# File 'lib/graphiterb/monitors.rb', line 82

def since
  Time.now.utc.to_f - started_at
end