Skip to content

Instantly share code, notes, and snippets.

@ncdc
Last active August 29, 2015 13:58
Show Gist options
  • Save ncdc/9938330 to your computer and use it in GitHub Desktop.
Save ncdc/9938330 to your computer and use it in GitHub Desktop.
#!/usr/bin/env oo-ruby
require 'openshift-origin-node'
require 'strscan'
gears = %w(533c34e04b85e4913e000009
533c38384b85e4913e000031
533c38294b85e4a2d6000015
533c38384b85e49c3c000003
533c38024b85e4913e00001d
533c38024b85e4a2d6000001)
class CggetMetricsParser
def initialize
@gear = nil
@saved = ''
@group = nil
end
# Processes output from cgget and sends each metric to Syslog
#
# The ouput has the following sequences/types of data
#
# HEADER
# /openshift/$gear_uuid:
#
# SINGLE KEY-VALUE PAIR
# cpu.rt_period_us: 1000000
#
# PARENT-CHILD KEY-VALUE PAIRS
# cpu.stat: nr_periods 6266
# nr_throttled 0
# throttled_time 0
#
# SEPARATOR
# <a blank line separates gears>
#
# This method will take each chunk of output from cgget, keep track
# of what it is in the middle of processing, and emit individual
# metrics to Syslog as it sees them.
#
def <<(data)
scanner = StringScanner.new(data)
loop do
# look for a newline
line = scanner.scan_until(/\n/)
if line.nil?
# no newline, save what we got and wait for the next call to <<
@saved += scanner.rest
break
end
# got a full line
line = @saved + line
# clear out anything we might have previously saved
@saved = ''
# strip off any newline
line.chomp!
# HEADER check
# see if we're looking for the gear
if @gear.nil?
# line must be a gear of the form /openshift/$uuid
@gear = line[0..-2].gsub('/openshift/', '')
# there may be more data to process, so move on to the next
# loop iteration
next
end
# SEPARATOR check
# see if we've reached the end of data for the current gear
# i.e. a blank line
if line =~ /^\s*$/
# clear out the gear
@gear = nil
# there may be more data to process, so move on to the next
# loop iteration
next
end
# CHILD check
# currently in a group
if line =~ /^\s/
key, value = line.split
publish(key, value)
else
# no longer in a group if we previously were
@group = nil
key, value = line.split(':')
value.strip!
if key == 'cpuacct.usage_percpu'
# got a line of the form "cpuacct.usage_percpu: 3180064217 3240110361"
value.split.each_with_index do |usage, i|
publish("#{key}.#{i}", usage)
end
elsif value =~ /\s/
# got a line of the form "cpu.stat: nr_periods 6266"
# so we're now in a group
@group = "#{key}."
key, value = value.split
publish(key, value)
else
# not in a group, got a line of the form "cpu.rt_runtime_us: 0"
publish(key, value)
end
end
end
end
def publish(key, value)
Syslog.info("type=metric app=#{@apps[@gear]} gear=#{@gear} #{@group}#{key}=#{value}"
end
end
end
p = CggetMetricsParser.new
OpenShift::Runtime::Utils.oo_spawn("cgget -a #{gears.map {|g| "/openshift/#{g}"}.join(' ')}", out: p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment