Skip to content

Instantly share code, notes, and snippets.

@ncdc
Created February 28, 2014 18:12
Show Gist options
  • Save ncdc/9276468 to your computer and use it in GitHub Desktop.
Save ncdc/9276468 to your computer and use it in GitHub Desktop.
class FilePlugin < MetricPlugin
def initialize(config)
super
end
def process(app, gear, timestamp, fields)
File.open("/root/gear_metrics.log", 'a') do |f|
fields.each do |key, value|
f.puts "#{app}.#{gear}.#{key} #{value} #{timestamp.to_i}"
end
end
end
end
require 'socket'
class GraphiteClient
def carbon
begin
@carbon ||= TCPSocket.new('localhost', 2003)
rescue => e
@carbon = nil
end
@carbon
end
def send(data)
begin
carbon.puts(data) if carbon
rescue => e
@carbon = nil
end
end
end
class GraphitePlugin < MetricPlugin
def initialize(config)
super
@client = GraphiteClient.new
end
def process(app, gear, timestamp, fields)
fields.each do |key, value|
@client.send "#{app}.#{gear}.#{key} #{value} #{timestamp.to_i}"
end
end
end
#!/usr/bin/oo-ruby
require 'time'
require 'syslog-logger'
require File.join(File.dirname(__FILE__), 'metric_plugin')
class MetricHandler
def initialize
@logger = Logger::Syslog.new('openshift-metrics')
@logger.level = Logger::DEBUG
#TODO take in config
config = {}
plugin_dir = File.join(File.dirname(__FILE__), 'metric_plugins')
plugin_files = Dir.glob(File.join(plugin_dir, '*')).find_all { |e| File.file?(e) }
plugin_files.each do |p|
begin
@logger.info "Attempting to load #{p}"
require p
rescue LoadError => e
@logger.info "Error loading #{p}"
end
end
@plugins = MetricPlugin.repository.collect do |plugin|
begin
plugin.new(config)
rescue => e
@logger.warn "Error loading plugin #{plugin}: #{e.message}"
nil
end
end.to_a.compact
@logger.error "No valid plugins found in #{plugin_dir}" if @plugins.empty?
@logger.info "Loaded plugins: #{@plugins.join(', ')}"
end
def run
while line = gets
begin
@logger.debug("Received line: #{line}")
# 2014-02-27T14:50:22.011665-05:00
timestamp = Time.parse($1) if line =~ /^([^ ]+) /
@logger.debug "Timestamp = #{timestamp}"
app = $1 if line =~ /app=([^ ]+)/
@logger.debug "app = #{app}"
gear = $1 if line =~ /gear=([^ ]+)/
@logger.debug "gear = #{gear}"
# skip if we don't have what we need
next unless timestamp and app and gear
index = line.index('type=metric')
# get all the k=v pairs after type=metric as a hash
fields = Hash[line[index..-1].split(' ')[1..-1].map {|kv| kv.split('=')}]
fields.delete('app')
fields.delete('gear')
fields.delete('ns')
@plugins.each do |plugin|
@logger.debug "Invoking plugin #{plugin}"
plugin.process(app, gear, timestamp, fields)
@logger.debug "DONE Invoking plugin #{plugin}"
end
end
end
end
end
MetricHandler.new.run
module MetricPluginTemplate
module ClassMethods
def repository
@repository ||= []
end
def inherited(klass)
repository << klass
end
end
def self.included(klass)
klass.extend ClassMethods
end
end
class MetricPlugin
include MetricPluginTemplate
attr_reader :config
def initialize(config)
@config = config
end
def process(app, gear, timestamp, fields)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment