Skip to content

Commit

Permalink
Added windows support to the processes resource
Browse files Browse the repository at this point in the history
Signed-off-by: username-is-already-taken2 <digitalgaz@hotmail.com>
  • Loading branch information
username-is-already-taken2 committed Jun 5, 2017
1 parent 75728f7 commit ab651c0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
21 changes: 17 additions & 4 deletions lib/resources/processes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,24 @@ def initialize(grep)
@grep = grep
# turn into a regexp if it isn't one yet
if grep.class == String
grep = '(/[^/]*)*' + grep if grep[0] != '/'
grep = Regexp.new('^' + grep + '(\s|$)')
# if windows ignore case as we can't make up our minds
if inspec.os.windows?
grep = '(?i)' + grep
else
grep = '(/[^/]*)*' + grep unless grep[0] == '/'
grep = '^' + grep + '(\s|$)'
end
grep = Regexp.new(grep)
end

all_cmds = ps_axo
@list = all_cmds.find_all do |hm|
hm[:command] =~ grep
end
end

return skip_resource 'The `processes` resource is not supported on your OS yet.' if inspec.os.windows?
def exists?
!@list.empty?
end

def to_s
Expand Down Expand Up @@ -74,6 +83,10 @@ def ps_axo
if os.linux?
command = 'ps axo label,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,user:32,command'
regex = /^([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(\w{3} \d{2}|\d{2}:\d{2}:\d{2})\s+([^ ]+)\s+([^ ]+)\s+(.*)$/
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")'
# Wanted to use /(?:^|,)([^,]*)/; works on rubular.com not sure why here?
regex = /^(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+)$/
else
command = 'ps axo pid,pcpu,pmem,vsz,rss,tty,stat,start,time,user,command'
regex = /^\s*([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(.*)$/
Expand All @@ -95,7 +108,7 @@ def build_process_list(command, regex, os)
end.compact
lines.map do |m|
a = m.to_a[1..-1] # grab all matching groups
a.unshift(nil) unless os.linux?
a.unshift(nil) unless os.linux? || os.windows?
a[1] = a[1].to_i
a[4] = a[4].to_i
a[5] = a[5].to_i
Expand Down
14 changes: 9 additions & 5 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,22 @@ def md.directory?
'crontab -l' => cmd.call('crontab-root'),
# crontab display for non-current user
'crontab -l -u foouser' => cmd.call('crontab-foouser'),
# zfs output for dataset tank/tmp
'/sbin/zfs get -Hp all tank/tmp' => cmd.call('zfs-get-all-tank-tmp'),
# zfs output for pool tank
'/sbin/zpool get -Hp all tank' => cmd.call('zpool-get-all-tank'),
# zfs output for dataset tank/tmp
'/sbin/zfs get -Hp all tank/tmp' => cmd.call('zfs-get-all-tank-tmp'),
# zfs output for pool tank
'/sbin/zpool get -Hp all tank' => cmd.call('zpool-get-all-tank'),
# docker
"docker ps -a --no-trunc --format '{{ json . }}'" => cmd.call('docker-ps-a'),
"docker version --format '{{ json . }}'" => cmd.call('docker-version'),
"docker info --format '{{ json . }}'" => cmd.call('docker-info'),
"docker inspect 71b5df59442b" => cmd.call('docker-inspec'),
# docker images
"83c36bfade9375ae1feb91023cd1f7409b786fd992ad4013bf0f2259d33d6406" => cmd.call('docker-images'),
}
# get-process cmdlet for processes resource
'$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")' => cmd.call('get-process_processes')
}
#require 'pry'
#binding.pry

@backend
end
Expand Down
3 changes: 3 additions & 0 deletions test/unit/mock/cmd/get-process_processes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PriorityClass,Id,CPU,PM,VirtualMemorySize,NPM,SessionId,Responding,StartTime,TotalProcessorTime,UserName,Path
Normal,2456,0.296875,4808704,118202368,14576,1,True,5/31/2017 9:13:17 AM,00:00:00.2968750,WINVAGR-QQQNHPN\Administrator,C:\Windows\system32\mmc.exe
High,396,0.15625,1323008,53710848,7776,1,True,5/31/2017 9:12:56 AM,00:00:00.1562500,NT AUTHORITY\SYSTEM,C:\Windows\system32\winlogon.exe
21 changes: 21 additions & 0 deletions test/unit/resources/processes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
resource = MockLoader.new(:centos6).load_resource('processes', 'postgres: bifrost bifrost')
_(resource.users.sort).must_equal ['opscode-pgsql']
_(resource.states.sort).must_equal ['Ss']
_(resource.exists?).must_equal true
end

it 'command name matches with output (string)' do
Expand All @@ -120,4 +121,24 @@
resource = MockLoader.new(:centos6).load_resource('processes', /mysqld/)
_(resource.to_s).must_equal 'Processes /mysqld/'
end

it 'command name matches with output (string)' do
resource = MockLoader.new(:windows).load_resource('processes', 'winlogon.exe')
_(resource.to_s).must_equal 'Processes winlogon.exe'
end

it 'retrieves the users and states as arrays on windows os' do
resource = MockLoader.new(:windows).load_resource('processes', 'winlogon.exe')
_(resource.users.sort).must_equal ['NT AUTHORITY\\SYSTEM']
end

it 'process should exist' do
resource = MockLoader.new(:windows).load_resource('processes', 'winlogon.exe')
_(resource.exists?).must_equal true
end

it 'process should_not exist' do
resource = MockLoader.new(:windows).load_resource('processes', 'unicorn.exe')
_(resource.exists?).must_equal false
end
end

0 comments on commit ab651c0

Please sign in to comment.