Skip to content

Instantly share code, notes, and snippets.

@yaauie
Created December 30, 2019 19:40
Show Gist options
  • Save yaauie/f7c70216cb8931a772a1fa5d748e09bf to your computer and use it in GitHub Desktop.
Save yaauie/f7c70216cb8931a772a1fa5d748e09bf to your computer and use it in GitHub Desktop.
##
# Converts one or more fields containing CPU measurements to millis,
# respecting the `m` suffix. By default the measurements will be modified
# _in place_, replacing the given value with the converted value.
#
# USAGE:
# ~~~ logstash-pipeline
# filter {
# ruby {
# path => "/path/to/this/convert-cpu-to-millis.logstash-filter-ruby.rb"
# script_params => {
# source => "[limits][cpu]"
# }
# }
# }
# ~~~
def register(params)
params = params.dup
source = params.delete('source') { report_configuration_error("missing required param `source`") }
@source = ::Kernel::Array(source)
target = params.include?('target') ? params.delete('target') : @source
@target = ::Kernel::Array(source)
if @source.size != @target.size
report_configuration_error("mismatching `target` and `source`; when `target` is supplied, it must contain the same number of fields as `source`.")
end
params.empty? || report_configuration_error("unknown script parameter(s): #{params.keys}.")
end
def report_configuration_error(message)
raise LogStash::ConfigurationError, message
end
def filter(event)
@source.zip(@target).each do |source, target|
if event.include?(source)
event.set(target, convert_cpu_to_millis(event.get(source)))
end
end
rescue => e
logger.error('failed to convert cpu to millis', exception: e.message)
event.tag('_convertcpuerror')
ensure
return [event]
end
def convert_cpu_to_millis(cpuVar)
result = 0
if cpuVar.end_with?'m'
result = cpuVar.chomp('m').to_i;
else
result = (cpuVar.to_f*1000).to_i;
end;
rescue
result
end
##
# Converts one or more fields containing memory measurements to Megabytes,
# respecting the given suffix. By default the measurements will be modified
# _in place_, replacing the given value with the converted value.
#
# USAGE:
# ~~~ logstash-pipeline
# filter {
# ruby {
# path => "/path/to/this/convert-memory-to-megas.logstash-filter-ruby.rb"
# script_params => {
# source => "[limits][memory]"
# }
# }
# }
# ~~~
def register(params)
params = params.dup
source = params.delete('source') { report_configuration_error("missing required param `source`") }
@source = ::Kernel::Array(source)
target = params.include?('target') ? params.delete('target') : @source
@target = ::Kernel::Array(source)
if @source.size != @target.size
report_configuration_error("mismatching `target` and `source`; when `target` is supplied, it must contain the same number of fields as `source`.")
end
params.empty? || report_configuration_error("unknown script parameter(s): #{params.keys}.")
end
def report_configuration_error(message)
raise LogStash::ConfigurationError, message
end
def filter(event)
@source.zip(@target).each do |source, target|
if event.include?(source)
event.set(target, convert_memory_to_megas(event.get(source)))
end
end
rescue => e
logger.error('failed to convert memory to megas', exception: e.message)
event.tag('_convertmemoryerror')
ensure
return [event]
end
SCALE_FACTOR = 1024
MB_PER_GB = SCALE_FACTOR ** 1
MB_PER_TB = SCALE_FACTOR ** 2
MB_PER_PB = SCALE_FACTOR ** 3
MB_PER_EB = SCALE_FACTOR ** 4
MB_PER_ZB = SCALE_FACTOR ** 5
MB_PER_YB = SCALE_FACTOR ** 6
def convert_memory_to_megas(memVar)
result = 0
if memVar.eql?'0'
result = memVar.to_i;
elsif memVar.end_with?('Ki')
result = (memVar.chomp('Ki').to_f/1024).round.to_i;
elsif memVar.end_with?('K')
result = (memVar.chomp('K').to_f/1024).round.to_i;
elsif memVar.end_with?('k')
result = (memVar.chomp('k').to_f/1024).round.to_i;
elsif memVar.end_with?('M')
result = memVar.chomp('M').to_i;
elsif memVar.end_with?('Mi')
result = memVar.chomp('Mi').to_i;
elsif memVar.end_with?('Gi')
result = memVar.chomp('Gi').to_i*MB_PER_GB;
elsif memVar.end_with?('G')
result = memVar.chomp('G').to_i*MB_PER_GB;
elsif memVar.end_with?('T')
result = memVar.chomp('T').to_i*(MB_PER_TB);
elsif memVar.end_with?('Ti')
result = memVar.chomp('Ti').to_i*(MB_PER_TB);
elsif memVar.end_with?('P')
result = memVar.chomp('P').to_i*(MB_PER_PB);
elsif memVar.end_with?('Pi')
result = memVar.chomp('Pi').to_i*(MB_PER_PB);
elsif memVar.end_with?('E')
result = memVar.chomp('E').to_i*(MB_PER_EB);
elsif memVar.end_with?('Ei')
result = memVar.chomp('Ei').to_i*(MB_PER_EB);
elsif memVar.end_with?('Z')
result = memVar.chomp('Z').to_i*(MB_PER_ZB);
elsif memVar.end_with?('Zi')
result = memVar.chomp('Zi').to_i*(MB_PER_ZB);
elsif memVar.end_with?('Y')
result = memVar.chomp('Y').to_i*(MB_PER_YB);
elsif memVar.end_with?('Yi')
result = memVar.chomp('Yi').to_i*(MB_PER_YB);
else
result = (memVar.to_f/(1024*1024)).round.to_i;
end
rescue
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment