class PerfmonProcGetter

Attributes

pid[R]

Public Class Methods

new() click to toggle source

Initializes the PerfmonProcGetter class

# File lib/logstash/inputs/perfmon_proc_getter.rb, line 7
def initialize
  @all_counters = `#{get_all_counters_command}`
end

Public Instance Methods

counter_exists?(counter_name) click to toggle source

Gets a value indicating whether the given counter exists on the system

counter_name

The name of the counter, such as “\Processor(_Total)\% Processor Time”

# File lib/logstash/inputs/perfmon_proc_getter.rb, line 50
def counter_exists?(counter_name)
  counter_name = counter_name.gsub(/\(.+\)/, '(*)')
  return @all_counters.downcase.include?(counter_name.downcase)
end
get_all_counters_command() click to toggle source

Gets the command line that lists all available perf counters on the system

# File lib/logstash/inputs/perfmon_proc_getter.rb, line 67
def get_all_counters_command
  "typeperf -q"
end
get_typeperf_command(counters, interval) click to toggle source

Gets the typeperf command line

counters

Array of counter names, such as [“\Processor(_Total)\% Processor Time”]

interval

The number, in seconds, to wait between each round of collecting metrics

# File lib/logstash/inputs/perfmon_proc_getter.rb, line 58
def get_typeperf_command(counters, interval)
  cmd = "typeperf "
  counters.each { |counter| cmd << "\"#{counter}\" " }
  cmd << "-si #{interval.to_s} "
  return cmd.strip!
end
proc_is_running?() click to toggle source

Gets a value indicating whether the typeperf process is currently running

# File lib/logstash/inputs/perfmon_proc_getter.rb, line 39
def proc_is_running?
  if @pid.nil?
    return false
  else
    return true
  end
end
start_process(counters, interval, output_queue) click to toggle source

Creates a new process that runs typeperf to collect perfmon metrics

counters

Array of counter names, such as [“\Processor(_Total)\% Processor Time”]

interval

The number, in seconds, to wait between each round of collecting metrics

output_queue

The queue to add each new message to

# File lib/logstash/inputs/perfmon_proc_getter.rb, line 15
def start_process(counters, interval, output_queue)
  cmd = get_typeperf_command(counters, interval)
  
  Open3.popen3(cmd) do |w, r, e, thr|
  
    wait_for_process_id_to_be_set(thr)
  
    while line = r.gets
      next if counters.any? { |counter| line.include? counter } # don't show lines that contain headers
      line.gsub!('"', '') # remove quotes
      line.strip!
      output_queue << line
    end
  end
end
stop_process() click to toggle source

Kills the typeperf process

# File lib/logstash/inputs/perfmon_proc_getter.rb, line 32
def stop_process
  Process.kill(9, @pid) 
  @pid = nil
end
wait_for_process_id_to_be_set(thr) click to toggle source

Waits until the PID is set

thr

The object containing process info like the PID

# File lib/logstash/inputs/perfmon_proc_getter.rb, line 78
def wait_for_process_id_to_be_set(thr)
  while @pid.nil?
    @pid = thr.pid
    sleep 0.5
  end
end
wait_for_process_to_start() click to toggle source

Waits until the typeperf process is running

# File lib/logstash/inputs/perfmon_proc_getter.rb, line 72
def wait_for_process_to_start
  sleep 0.5 until proc_is_running?
end