Class: Bipbip::Agent
- Inherits:
-
Object
- Object
- Bipbip::Agent
- Defined in:
- lib/bipbip/agent.rb
Constant Summary collapse
- PLUGIN_RESPAWN_DELAY =
5
Instance Attribute Summary collapse
-
#plugins ⇒ Object
Returns the value of attribute plugins.
-
#storages ⇒ Object
Returns the value of attribute storages.
Instance Method Summary collapse
-
#initialize(config_file = nil) ⇒ Agent
constructor
A new instance of Agent.
- #interrupt ⇒ Object
- #load_config(config_file) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(config_file = nil) ⇒ Agent
Returns a new instance of Agent.
10 11 12 13 14 15 |
# File 'lib/bipbip/agent.rb', line 10 def initialize(config_file = nil) @plugins = [] @storages = [] load_config(config_file) if config_file end |
Instance Attribute Details
#plugins ⇒ Object
Returns the value of attribute plugins.
7 8 9 |
# File 'lib/bipbip/agent.rb', line 7 def plugins @plugins end |
#storages ⇒ Object
Returns the value of attribute storages.
8 9 10 |
# File 'lib/bipbip/agent.rb', line 8 def storages @storages end |
Instance Method Details
#interrupt ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/bipbip/agent.rb', line 92 def interrupt @interrupted = true Bipbip.logger.info 'Interrupt, killing plugin processes...' @plugins.each do |plugin| Process.kill('TERM', plugin.pid) if Process.exists?(plugin.pid) end Bipbip.logger.info 'Waiting for all plugin processes to exit...' Process.waitall end |
#load_config(config_file) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/bipbip/agent.rb', line 54 def load_config(config_file) config = YAML.load(File.open(config_file)) config = { 'logfile' => STDOUT, 'loglevel' => 'INFO', 'frequency' => 60, 'include' => nil, 'services' => [], 'services' => [], }.merge(config) Bipbip.logger = Logger.new(config['logfile']) Bipbip.logger.level = Logger::const_get(config['loglevel']) services = config['services'].to_a if config['include'] include_path = File.(config['include'].to_s, File.dirname(config_file)) files = Dir[include_path + '/**/*.yaml', include_path + '/**/*.yml'] services += files.map { |file| YAML.load(File.open(file)) } end @plugins = services.map do |service| plugin_name = service['plugin'] metric_group = service['metric_group'] frequency = service['frequency'].nil? ? config['frequency'] : service['frequency'] plugin_config = service.reject { |key, value| ['plugin', 'frequency', 'metric_group'].include?(key) } Bipbip::Plugin.factory(plugin_name, plugin_config, frequency, metric_group) end storages = config['storages'].to_a @storages = storages.map do |storage| storage_name = storage['name'].to_s storage_config = storage.reject { |key, value| ['name'].include?(key) } Bipbip::Storage.factory(storage_name, storage_config) end end |
#run ⇒ Object
17 18 19 20 21 22 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 |
# File 'lib/bipbip/agent.rb', line 17 def run Bipbip.logger.info 'Startup...' Bipbip.logger.warn 'No storages configured' if @storages.empty? if @plugins.empty? raise 'No services configured' end @storages.each do |storage| @plugins.each do |plugin| Bipbip.logger.info "Setting up plugin #{plugin.name} for storage #{storage.name}" storage.setup_plugin(plugin) end end ['INT', 'TERM'].each { |sig| trap(sig) { Thread.new do interrupt exit end } } @plugins.each do |plugin| Bipbip.logger.info "Starting plugin #{plugin.name} with config #{plugin.config}" plugin.run(@storages) end @interrupted = false until @interrupted pid = Process.wait(-1) plugin = plugin_by_pid(pid) Bipbip.logger.error "Plugin #{plugin.name} with config #{plugin.config} died. Respawning..." sleep(PLUGIN_RESPAWN_DELAY) plugin.run(@storages) end end |