Sha256: e2faa0a532fb6323885ad73ea73a81ca6772a57791de94b1105548400e397cf3

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

require 'pidly'
require 'instrumental_tools/metric_script_executor'
require 'instrumental_tools/system_inspector'

class ServerController < Pidly::Control
  COMMANDS = [:start, :stop, :status, :restart, :clean, :kill, :foreground]

  attr_accessor :run_options, :pid

  before_start do
    extra_info = if run_options[:daemon]
                   "(#{run_options[:pid_location]}), log: #{run_options[:log_location]}"
                 end
    puts "Starting daemon process: #{@pid} #{extra_info}"
  end

  start :foreground

  stop do
    puts "Attempting to kill daemon process: #{@pid}"
  end

  error do
    puts 'Error encountered'
  end

  def self.run(options)
    agent = Instrumental::Agent.new(options[:api_key], :collector => [options[:collector], options[:port]].compact.join(':'))
    puts "instrument_server version #{Instrumental::Tools::VERSION} started at #{Time.now.utc}"
    puts "Collecting stats under the hostname: #{options[:hostname]}"
    report_interval = options[:report_interval]
    custom_metrics  = MetricScriptExecutor.new(options[:script_location])
    loop do
      t = Time.now.to_i
      next_run_at = (t - t % report_interval) + report_interval
      sleep [next_run_at - t, 0].max
      inspector = SystemInspector.new
      inspector.load_all
      count = 0
      inspector.gauges.each do |stat, value|
        metric = "#{options[:hostname]}.#{stat}"
        agent.gauge(metric, value)
        if options[:debug]
          puts [metric, value].join(":")
        end
        count += 1
      end
      custom_metrics.run.each do |(stat, value, time)|
        metric = "#{options[:hostname]}.#{stat}"
        agent.gauge(metric, value, time)
        if options[:debug]
          puts [metric, value].join(":")
        end
        count += 1
      end
      puts "Sent #{count} metrics"
    end
  end

  def initialize(options={})
    @run_options = options.delete(:run_options) || {}
    super(options)
  end

  def foreground
    self.class.run(run_options)
  end

  alias_method :clean, :clean!
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
instrumental_tools-1.0.0.pre.1 lib/instrumental_tools/server_controller.rb