Class: Bipbip::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/bipbip/agent.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Agent

Returns a new instance of Agent.



5
6
7
8
9
10
11
12
13
14
# File 'lib/bipbip/agent.rb', line 5

def initialize(config_file)
  @plugin_pids = []
  @logfile = STDOUT
  @loglevel = 'INFO'
  @frequency = 60
  @services = []
  @copperegg_api_key

  load_config(config_file)
end

Instance Method Details

#get_copperegg_dashboardsObject



78
79
80
81
82
83
84
85
86
# File 'lib/bipbip/agent.rb', line 78

def get_copperegg_dashboards
  Bipbip.logger.info 'Loading dashboards'
  dashboards = CopperEgg::CustomDashboard.find
  if dashboards.nil?
    Bipbip.logger.fatal 'Cannot load dashboards'
    exit 1
  end
  dashboards
end

#get_copperegg_metric_groupsObject



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

def get_copperegg_metric_groups
  Bipbip.logger.info 'Loading metric groups'
  metric_groups = CopperEgg::MetricGroup.find
  if metric_groups.nil?
    Bipbip.logger.fatal 'Cannot load metric groups'
    exit 1
  end
  metric_groups
end

#interruptObject



123
124
125
126
127
128
129
130
131
132
# File 'lib/bipbip/agent.rb', line 123

def interrupt
  Bipbip.logger.info 'Interrupt, killing plugin processes...'
  @plugin_pids.each { |pid| Process.kill('TERM', pid) }

  Bipbip.logger.info 'Waiting for all plugin processes to exit...'
  Process.waitall

  Bipbip.logger.info 'Exiting'
  exit
end

#load_config(config_file) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bipbip/agent.rb', line 96

def load_config(config_file)
  config = YAML.load(File.open(config_file))
  if config.has_key?('logfile')
    @logfile = config['logfile'].to_s
  end
  if config.has_key?('loglevel')
    @loglevel = config['loglevel'].to_s
  end
  if config.has_key?('frequency')
    @frequency = config['frequency'].to_i
  end
  if config.has_key?('services')
    @services = config['services'].to_a
  end
  if config.has_key?('include')
    include_path = File.expand_path(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
  if config.has_key?('copperegg')
    config_copperegg = config['copperegg']
    if config_copperegg.has_key?('apikey')
      @copperegg_api_key = config_copperegg['apikey']
    end
  end
end

#plugin_factory(plugin_name) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/bipbip/agent.rb', line 88

def plugin_factory(plugin_name)
  file_name = plugin_name.tr('-', '_')
  require "bipbip/plugin/#{file_name}"

  class_name = plugin_name.split('-').map{|w| w.capitalize}.join
  Plugin::const_get(class_name).new(plugin_name)
end

#runObject



16
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bipbip/agent.rb', line 16

def run
  Bipbip.logger = Logger.new(@logfile)
  Bipbip.logger.level = Logger::const_get(@loglevel)
  Bipbip.logger.info 'Startup...'

  Bipbip.logger.info "Using CopperEgg API key `#{@copperegg_api_key}`"
  CopperEgg::Api.apikey = @copperegg_api_key

  if ![5, 15, 60, 300, 900, 3600, 21600].include?(@frequency)
    Bipbip.logger.fatal "Invalid frequency: #{@frequency}"
    exit 1
  end

  ['INT', 'TERM'].each { |sig| trap(sig) {
    Thread.new { interrupt }
  } }

  metric_groups = get_copperegg_metric_groups
  dashboards = get_copperegg_dashboards

  plugin_names = @services.map { |service| service['plugin'] }
  plugin_names.each do |plugin_name|
    plugin = plugin_factory(plugin_name)

    metric_group = metric_groups.detect { |m| m.name == plugin_name }
    if metric_group.nil? || !metric_group.is_a?(CopperEgg::MetricGroup)
      Bipbip.logger.info "Creating metric group `#{plugin_name}`"
      metric_group = CopperEgg::MetricGroup.new(:name => plugin_name, :label => plugin_name, :frequency => @frequency)
    end
    metric_group.frequency = @frequency
    metric_group.metrics = plugin.metrics_schema
    metric_group.save

    dashboard = dashboards.detect { |d| d.name == plugin_name }
    if dashboard.nil?
      Bipbip.logger.info "Creating dashboard `#{plugin_name}`"
      metrics = metric_group.metrics || []
      CopperEgg::CustomDashboard.create(metric_group, :name => plugin_name, :identifiers => nil, :metrics => metrics)
    end
  end

  @services.each do |service|
    plugin_name = service['plugin']
    plugin = plugin_factory(plugin_name)
    service_config = service.select { |key| !['plugin'].include?(key) }
    Bipbip.logger.info "Starting plugin #{plugin_name}"
    @plugin_pids.push plugin.run(service_config, @frequency)
  end

  p Process.waitall
end