Class: PerfmonProcGetter

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/inputs/perfmon_proc_getter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePerfmonProcGetter

Initializes the PerfmonProcGetter class



7
8
9
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 7

def initialize
  @all_counters = `#{get_all_counters_command}`
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



4
5
6
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 4

def pid
  @pid
end

Instance Method Details

#counter_exists?(counter_name) ⇒ Boolean

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”

Returns:

  • (Boolean)


50
51
52
53
# 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_commandObject

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



67
68
69
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 67

def get_all_counters_command
  "typeperf -q"
end

#get_typeperf_command(counters, interval) ⇒ Object

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



58
59
60
61
62
63
# 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?Boolean

Gets a value indicating whether the typeperf process is currently running

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# 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) ⇒ Object

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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_processObject

Kills the typeperf process



32
33
34
35
# 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) ⇒ Object

Waits until the PID is set

thr

The object containing process info like the PID



78
79
80
81
82
83
# 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_startObject

Waits until the typeperf process is running



72
73
74
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 72

def wait_for_process_to_start
  sleep 0.5 until proc_is_running?
end