Puppet Function: find_pids

Defined in:
lib/puppet/parser/functions/dynatrace_functions.rb
Function type:
Ruby 3.x API

Overview

find_pids()Any

Returns:

  • (Any)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/puppet/parser/functions/dynatrace_functions.rb', line 37

newfunction(:find_pids, :type => :rvalue) do |args|
  pattern = args[0]
  user    = args[1]
  platform_family = args[2]
  pids = []
  if %w(debian fedora rhel).include? platform_family
    pgrep_pattern_opt = !pattern.nil? ? "-f \"#{pattern}\"" : ''
    pgrep_user_opt = !user.nil? ? "-u #{user}" : ''
    search_processes_cmd = "pgrep #{pgrep_pattern_opt} #{pgrep_user_opt}"

    #################################################################
    # code below doesn't work if workstation is on windows
    #        %x[#{search_processes_cmd}].each_line do |pidStr|
    #          if !pidStr.empty?
    #            puts 'pid:' + pidStr
    #            pids << pidStr.to_i
    #          end
    #          return pids
    #        end
    # this part working and fixes code above
    pidStr = `#{search_processes_cmd}`
    unless pidStr.empty?
      text = []
      text << pidStr.lines.map(&:chomp)
      text.each do |x|
        x.each do |y|
          pids << y.to_i
        end
      end
    end
    #################################################################

  else
    raise 'ERROR: Unsupported platform'
  end
  pids
end