Class: Bipbip::Plugin

Inherits:
Object
  • Object
show all
Includes:
InterruptibleSleep
Defined in:
lib/bipbip/plugin.rb

Defined Under Namespace

Classes: Apache2, Command, CommandStatus, Coturn, Elasticsearch, FastcgiPhpApc, FastcgiPhpFpm, FastcgiPhpOpcache, Gearman, JanusAudioroom, JanusRtpbroadcast, LogParser, MeasurementTimeout, Memcached, Mongodb, Monit, Mysql, Network, Nginx, PhpApc, Postfix, Puppet, Redis, Resque, SocketRedis, SystemdUnit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InterruptibleSleep

#interrupt_sleep, #interruptible_sleep

Constructor Details

#initialize(name, config, frequency, tags = nil, metric_group = nil) ⇒ Plugin

Returns a new instance of Plugin.



25
26
27
28
29
30
31
# File 'lib/bipbip/plugin.rb', line 25

def initialize(name, config, frequency, tags = nil, metric_group = nil)
  @name = name.to_s
  @config = config.to_hash
  @frequency = frequency.to_f
  @tags = tags.to_a
  @metric_group = (metric_group || name).to_s
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/bipbip/plugin.rb', line 9

def config
  @config
end

#frequencyObject

Returns the value of attribute frequency.



10
11
12
# File 'lib/bipbip/plugin.rb', line 10

def frequency
  @frequency
end

#metric_groupObject

Returns the value of attribute metric_group.



12
13
14
# File 'lib/bipbip/plugin.rb', line 12

def metric_group
  @metric_group
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/bipbip/plugin.rb', line 8

def name
  @name
end

#tagsObject

Returns the value of attribute tags.



11
12
13
# File 'lib/bipbip/plugin.rb', line 11

def tags
  @tags
end

Class Method Details

.factory(name, config, frequency, tags, metric_group = nil) ⇒ Object



14
15
16
17
# File 'lib/bipbip/plugin.rb', line 14

def self.factory(name, config, frequency, tags, metric_group = nil)
  require "bipbip/plugin/#{Bipbip::Helper.name_to_filename(name)}"
  Plugin.const_get(Bipbip::Helper.name_to_classname(name)).new(name, config, frequency, tags, metric_group)
end

.factory_from_plugin(plugin) ⇒ Bipbip::Plugin

Parameters:

Returns:



21
22
23
# File 'lib/bipbip/plugin.rb', line 21

def self.factory_from_plugin(plugin)
  plugin.class.new(plugin.name, plugin.config, plugin.frequency, plugin.tags, plugin.metric_group)
end

Instance Method Details

#metrics_namesObject



65
66
67
# File 'lib/bipbip/plugin.rb', line 65

def metrics_names
  metrics_schema.map { |metric| metric[:name] }
end

#metrics_schemaObject



69
70
71
# File 'lib/bipbip/plugin.rb', line 69

def metrics_schema
  raise 'Missing method metrics_schema'
end

#monitorObject



73
74
75
# File 'lib/bipbip/plugin.rb', line 73

def monitor
  raise 'Missing method monitor'
end

#run(storages) ⇒ Object

Parameters:

  • storages (Array)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bipbip/plugin.rb', line 34

def run(storages)
  timeout = frequency * 2
  loop do
    time = Time.now
    Timeout.timeout(timeout, MeasurementTimeout) do
      run_measurement(time, storages)
    end
    interruptible_sleep (frequency - (Time.now - time))
  end
rescue MeasurementTimeout => e
  log(Logger::ERROR, "Measurement timeout of #{timeout} seconds reached.")
  retry
rescue StandardError => e
  log_exception(Logger::ERROR, e)
  interruptible_sleep frequency
  retry
rescue Exception => e
  log_exception(Logger::FATAL, e)
  raise e
end

#source_identifierObject



57
58
59
60
61
62
63
# File 'lib/bipbip/plugin.rb', line 57

def source_identifier
  identifier = Bipbip.fqdn + '::' + metric_group
  unless config.empty?
    identifier += '::' + config.values.first.to_s.gsub(/[^\w]/, '_')
  end
  identifier
end