Class: Bipbip::Plugin

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

Defined Under Namespace

Classes: Apache2, Command, Elasticsearch, FastcgiPhpApc, FastcgiPhpFpm, FastcgiPhpOpcache, Gearman, LogParser, Memcached, Mongodb, Monit, Mysql, Network, Nginx, PhpApc, Postfix, Puppet, Redis, Resque, SocketRedis

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, metric_group = nil) ⇒ Plugin

Returns a new instance of Plugin.



16
17
18
19
20
21
# File 'lib/bipbip/plugin.rb', line 16

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

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#metric_groupObject

Returns the value of attribute metric_group.



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

def metric_group
  @metric_group
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/bipbip/plugin.rb', line 6

def name
  @name
end

#pidObject

Returns the value of attribute pid.



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

def pid
  @pid
end

Class Method Details

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



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

def self.factory(name, config, frequency, 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, metric_group)
end

Instance Method Details

#frequencyObject



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

def frequency
  @frequency
end

#interruptObject



56
57
58
59
60
# File 'lib/bipbip/plugin.rb', line 56

def interrupt
  log(Logger::INFO, "Interrupting plugin process #{Process.pid}")
  @interrupted = true
  interrupt_sleep
end

#interrupted?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/bipbip/plugin.rb', line 62

def interrupted?
  @interrupted || Process.getpgid(Process.ppid) != Process.getpgrp
end

#metrics_namesObject



78
79
80
# File 'lib/bipbip/plugin.rb', line 78

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

#metrics_schemaObject



82
83
84
# File 'lib/bipbip/plugin.rb', line 82

def metrics_schema
  raise 'Missing method metrics_schema'
end

#monitorObject



86
87
88
# File 'lib/bipbip/plugin.rb', line 86

def monitor
  raise 'Missing method monitor'
end

#run(storages) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bipbip/plugin.rb', line 23

def run(storages)
  @pid = fork do
    ['INT', 'TERM'].each { |sig| trap(sig) {
      Thread.new { interrupt } if !@interrupted
    } }

    retry_delay = frequency
    begin
      until interrupted? do
        time = Time.now
        data = monitor
        if data.empty?
          raise "#{name} #{source_identifier}: Empty data"
        end
        log(Logger::DEBUG, "Data: #{data}")
        storages.each do |storage|
          storage.store_sample(self, time, data)
        end
        retry_delay = frequency
        interruptible_sleep (frequency - (Time.now - time))
      end
    rescue => e
      log_exception(Logger::ERROR, e)
      interruptible_sleep retry_delay
      retry_delay += frequency if retry_delay < frequency * 10
      retry
    rescue Exception => e
      log_exception(Logger::FATAL, e)
      raise e
    end
  end
end

#source_identifierObject



70
71
72
73
74
75
76
# File 'lib/bipbip/plugin.rb', line 70

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