Class: PerfmonProcGetter
- Inherits:
-
Object
- Object
- PerfmonProcGetter
- Defined in:
- lib/logstash/inputs/perfmon_proc_getter.rb
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Instance Method Summary collapse
-
#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”.
-
#get_all_counters_command ⇒ Object
Gets the command line that lists all available perf counters on the system.
-
#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.
-
#initialize ⇒ PerfmonProcGetter
constructor
Initializes the PerfmonProcGetter class.
-
#proc_is_running? ⇒ Boolean
Gets a value indicating whether the typeperf process is currently running.
-
#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.
-
#stop_process ⇒ Object
Kills the typeperf process.
-
#wait_for_process_to_start ⇒ Object
Waits until the typeperf process is running.
Constructor Details
#initialize ⇒ PerfmonProcGetter
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
#pid ⇒ Object (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”
51 52 53 54 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 51 def counter_exists?(counter_name) counter_name = counter_name.gsub(/\(.+\)/, '(*)') return @all_counters.downcase.include?(counter_name.downcase) end |
#get_all_counters_command ⇒ Object
Gets the command line that lists all available perf counters on the system
68 69 70 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 68 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
59 60 61 62 63 64 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 59 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
40 41 42 43 44 45 46 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 40 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 30 |
# 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| while line = r.gets if @pid.nil? @pid = thr.pid end 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 ⇒ Object
Kills the typeperf process
33 34 35 36 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 33 def stop_process Process.kill(9, @pid) @pid = nil end |
#wait_for_process_to_start ⇒ Object
Waits until the typeperf process is running
73 74 75 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 73 def wait_for_process_to_start sleep 0.5 until proc_is_running? end |