Class: Bipbip::Plugin
- Inherits:
-
Object
- Object
- Bipbip::Plugin
- Includes:
- InterruptibleSleep
- Defined in:
- lib/bipbip/plugin.rb
Direct Known Subclasses
Apache2, Command, Elasticsearch, FastcgiPhpApc, FastcgiPhpFpm, FastcgiPhpOpcache, Gearman, LogParser, Memcached, Mongodb, Monit, Mysql, Network, Nginx, PhpApc, Postfix, Puppet, Redis, Resque, SocketRedis
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
-
#config ⇒ Object
Returns the value of attribute config.
-
#metric_group ⇒ Object
Returns the value of attribute metric_group.
-
#name ⇒ Object
Returns the value of attribute name.
-
#pid ⇒ Object
Returns the value of attribute pid.
Class Method Summary collapse
Instance Method Summary collapse
- #frequency ⇒ Object
-
#initialize(name, config, frequency, metric_group = nil) ⇒ Plugin
constructor
A new instance of Plugin.
- #interrupt ⇒ Object
- #interrupted? ⇒ Boolean
- #metrics_names ⇒ Object
- #metrics_schema ⇒ Object
- #monitor ⇒ Object
- #run(storages) ⇒ Object
- #source_identifier ⇒ Object
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
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/bipbip/plugin.rb', line 7 def config @config end |
#metric_group ⇒ Object
Returns the value of attribute metric_group.
8 9 10 |
# File 'lib/bipbip/plugin.rb', line 8 def metric_group @metric_group end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/bipbip/plugin.rb', line 6 def name @name end |
#pid ⇒ Object
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
#frequency ⇒ Object
66 67 68 |
# File 'lib/bipbip/plugin.rb', line 66 def frequency @frequency end |
#interrupt ⇒ Object
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
62 63 64 |
# File 'lib/bipbip/plugin.rb', line 62 def interrupted? @interrupted || Process.getpgid(Process.ppid) != Process.getpgrp end |
#metrics_names ⇒ Object
78 79 80 |
# File 'lib/bipbip/plugin.rb', line 78 def metrics_names metrics_schema.map { |metric| metric[:name] } end |
#metrics_schema ⇒ Object
82 83 84 |
# File 'lib/bipbip/plugin.rb', line 82 def metrics_schema raise 'Missing method metrics_schema' end |
#monitor ⇒ Object
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 |