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
5 6 7 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 5 def initialize @all_counters = `#{get_all_counters_command}` end |
Instance Attribute Details
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
2 3 4 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 2 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”
47 48 49 50 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 47 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
64 65 66 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 64 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
55 56 57 58 59 60 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 55 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
36 37 38 39 40 41 42 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 36 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
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 13 def start_process(counters, interval, output_queue) cmd = get_typeperf_command(counters, interval) IO.popen(cmd) do |f| @pid = f.pid f.each do |line| 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
29 30 31 32 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 29 def stop_process Process.kill(9, @pid) @pid = nil end |
#wait_for_process_to_start ⇒ Object
Waits until the typeperf process is running
69 70 71 |
# File 'lib/logstash/inputs/perfmon_proc_getter.rb', line 69 def wait_for_process_to_start sleep 0.5 until proc_is_running? end |