CFINSPEC-291: Initial fix to consider processes without path on Windows

Signed-off-by: Sonu Saha <sonu.saha@progress.com>
This commit is contained in:
Sonu Saha 2022-06-01 13:09:30 +05:30
parent ed171586eb
commit 39b4f0a7a5

View file

@ -41,9 +41,10 @@ module Inspec::Resources
grep = Regexp.new(grep)
end
# require "byebug"; byebug
all_cmds = ps_axo
@list = all_cmds.find_all do |hm|
hm[:command] =~ grep
hm[:command] =~ grep || hm[:process_name] =~ grep
end
end
@ -84,6 +85,7 @@ module Inspec::Resources
.register_column(:time, field: "time")
.register_column(:users, field: "user")
.register_column(:commands, field: "command")
.register_column(:process_name, field: "process_name")
.install_filter_methods_on_resource(self, :filtered_processes)
private
@ -98,9 +100,9 @@ module Inspec::Resources
if os.linux?
command, regex, field_map = ps_configuration_for_linux
elsif os.windows?
command = '$Proc = Get-Process -IncludeUserName | Where-Object {$_.Path -ne $null } | Select-Object PriorityClass,Id,CPU,PM,VirtualMemorySize,NPM,SessionId,Responding,StartTime,TotalProcessorTime,UserName,Path | ConvertTo-Csv -NoTypeInformation;$Proc.Replace("""","").Replace("`r`n","`n")'
command = '$Proc = Get-Process -IncludeUserName | Select-Object PriorityClass,Id,CPU,PM,VirtualMemorySize,NPM,SessionId,Responding,StartTime,TotalProcessorTime,UserName,Path,ProcessName | ConvertTo-Csv -NoTypeInformation;$Proc.Replace("""","").Replace("`r`n","`n")'
# Wanted to use /(?:^|,)([^,]*)/; works on rubular.com not sure why here?
regex = /^(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+)$/
regex = /^(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)$/
field_map = {
pid: 2,
cpu: 3,
@ -113,6 +115,7 @@ module Inspec::Resources
time: 10,
user: 11,
command: 12,
process_name: 13,
}
else
command = "ps axo pid,pcpu,pmem,vsz,rss,tty,stat,start,time,user,command"
@ -187,6 +190,7 @@ module Inspec::Resources
end
def build_process_list(command, regex, field_map)
# require "byebug"; byebug
cmd = inspec.command(command)
all = cmd.stdout.split("\n")[1..-1]
return [] if all.nil?
@ -204,7 +208,7 @@ module Inspec::Resources
# build a hash of process data that we'll turn into a struct for FilterTable
process_data = {}
%i{label pid cpu mem vsz rss tty stat start time user command}.each do |param|
%i{label pid cpu mem vsz rss tty stat start time user command process_name}.each do |param|
# not all operating systems support all fields, so skip the field if we don't have it
process_data[param] = line[field_map[param]] if field_map.key?(param)
end