Merge pull request #1225 from alexpop/ag-ap/processes-user-comm

Fix processes resource user and command truncation
This commit is contained in:
Alex Pop 2016-10-18 10:12:59 +01:00 committed by GitHub
commit 41d31163ac
7 changed files with 53 additions and 54 deletions

View file

@ -26,7 +26,7 @@ module Inspec::Resources
grep = '(/[^/]*)*'+grep if grep[0] != '/'
grep = Regexp.new('^' + grep + '(\s|$)')
end
all_cmds = ps_aux
all_cmds = ps_axo
@list = all_cmds.find_all do |hm|
hm[:command] =~ grep
end
@ -43,39 +43,37 @@ module Inspec::Resources
private
def ps_aux
def ps_axo
os = inspec.os
if os.linux?
command = 'ps auxZ'
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+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(.*)$/
else
command = 'ps aux'
regex = /^([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(.*)$/
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+(.*)$/
end
build_process_list(command, regex, os)
end
Process = Struct.new(:label, :user, :pid,
Process = Struct.new(:label, :pid,
:cpu, :mem, :vsz,
:rss, :tty, :stat,
:start, :time, :command)
:start, :time, :user, :command)
def build_process_list(command, regex, os)
cmd = inspec.command(command)
all = cmd.stdout.split("\n")[1..-1]
return [] if all.nil?
lines = all.map do |line|
line.match(regex)
end.compact
lines.map do |m|
a = m.to_a[1..-1] # grab all matching groups
a.unshift(nil) unless os.linux?
a[2] = a[2].to_i
a[1] = a[1].to_i
a[4] = a[4].to_i
a[5] = a[5].to_i
a[6] = a[6].to_i
Process.new(*a)
end
end

View file

@ -140,8 +140,8 @@ class MockLoader
}
mock.commands = {
'ps aux' => cmd.call('ps-aux'),
'ps auxZ' => cmd.call('ps-auxZ'),
'ps axo pid,pcpu,pmem,vsz,rss,tty,stat,start,time,user,command' => cmd.call('ps-axo'),
'ps axo label,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,user:32,command' => cmd.call('ps-axoZ'),
'Get-Content win_secpol.cfg' => cmd.call('secedit-export'),
'secedit /export /cfg win_secpol.cfg' => cmd.call('success'),
'Remove-Item win_secpol.cfg' => cmd.call('success'),

View file

@ -1,5 +0,0 @@
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18084 3228 ? Ss 14:15 0:00 /bin/bash
root 13 0.0 0.0 15284 2148 ? R+ 15:08 0:00 ps aux
noot 19 0.0 0.0 24521 1536 s001 Ss 09:23 0:00 svc
noot 23 0.0 0.0 25044 1908 s000 S 08:46 0:00 svc

View file

@ -1,3 +0,0 @@
LABEL USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
system_u:system_r:kernel_t:s0 root 1 0.0 0.0 19232 1492 ? Ss May04 0:01 /sbin/init
system_u:system_r:kernel_t:s0 root 39 0.0 0.0 0 0 ? S May04 0:00 crypto/0

View file

@ -0,0 +1,4 @@
PID %CPU %MEM VSZ RSS TTY STAT STARTED TIME USER COMMAND
7115 0.3 0.0 2516588 3052 ttys008 U Fri05PM 0:00.05 root login -fp apop
7116 0.0 0.0 2499948 1292 ttys008 S+ Fri05PM 0:00.97 apop -bash
7853 0.0 0.1 2526272 8804 ttys009 Ss Fri05PM 0:00.06 apop /Users/apop/Applications/iTerm.app/Contents/MacOS/iTerm2 --server login -fp apop

View file

@ -0,0 +1,4 @@
LABEL PID %CPU %MEM VSZ RSS TT STAT STARTED TIME USER COMMAND
system_u:system_r:init_t:s0 5127 0.0 0.2 547208 5376 ? Ss 10:54:22 00:00:00 opscode-pgsql postgres: bifrost bifrost 127.0.0.1(43699) idle
system_u:system_r:init_t:s0 5140 1.9 11.2 2908400 215308 ? Ssl 10:54:23 00:00:05 opscode java -Xmx466M -Xms466M -XX:NewSize=32M -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -Xloggc:/var/log/opscode/opscode-solr4/gclog.log -verbose:gc -XX:+PrintHeapAtGC -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCApplicationConcurrentTime -XX:+PrintTenuringDistribution -Dsolr.data.dir=/var/opt/opscode/opscode-solr4/data -Dsolr.solr.home=/var/opt/opscode/opscode-solr4/home -Djava.io.tmpdir=/var/opt/opscode/opscode-solr4/ -server -jar /opt/opscode/embedded/service/opscode-solr4/jetty/start.jar
system_u:system_r:init_t:s0 5169 0.0 0.0 4084 536 ? S 10:54:23 00:00:00 opscode svlogd -tt /var/log/opscode/opscode-solr4

View file

@ -12,61 +12,62 @@ describe 'Inspec::Resources::Processes' do
end
it 'verify processes resource' do
resource = MockLoader.new(:freebsd10).load_resource('processes', '/bin/bash')
_(resource.list.length).must_equal 1
resource = MockLoader.new(:freebsd10).load_resource('processes', 'login -fp apop')
_(resource.list.length).must_equal 2
_(resource.list[0].to_h).must_equal({
label: nil,
user: 'root',
pid: 1,
cpu: '0.0',
pid: 7115,
cpu: '0.3',
mem: '0.0',
vsz: 18084,
rss: 3228,
tty: '?',
stat: 'Ss',
start: '14:15',
time: '0:00',
command: '/bin/bash',
vsz: 2516588,
rss: 3052,
tty: 'ttys008',
stat: 'U',
start: 'Fri05PM',
time: '0:00.05',
user: 'root',
command: 'login -fp apop',
})
end
it 'verify processes resource on linux os' do
resource = MockLoader.new(:centos6).load_resource('processes', '/sbin/init')
resource = MockLoader.new(:centos6).load_resource('processes', 'postgres: bifrost bifrost')
_(resource.list.length).must_equal 1
_(resource.list[0].to_h).must_equal({
label: 'system_u:system_r:kernel_t:s0',
user: 'root',
pid: 1,
label: 'system_u:system_r:init_t:s0',
pid: 5127,
cpu: '0.0',
mem: '0.0',
vsz: 19232,
rss: 1492,
mem: '0.2',
vsz: 547208,
rss: 5376,
tty: '?',
stat: 'Ss',
start: 'May04',
time: '0:01',
command: '/sbin/init',
start: '10:54:22',
time: '00:00:00',
user: 'opscode-pgsql',
command: 'postgres: bifrost bifrost 127.0.0.1(43699) idle',
})
end
it 'access attributes of a process' do
resource = MockLoader.new(:centos6).load_resource('processes', '/sbin/init')
resource = MockLoader.new(:centos6).load_resource('processes', 'postgres: bifrost bifrost')
process = resource.list[0]
process.user.must_equal 'root'
process[:user].must_equal 'root'
process['user'].must_equal 'root'
process[1].must_equal 'root'
process.user.must_equal 'opscode-pgsql'
process[:user].must_equal 'opscode-pgsql'
process['user'].must_equal 'opscode-pgsql'
process[-1].must_equal 'postgres: bifrost bifrost 127.0.0.1(43699) idle'
process[1].must_equal 5127
end
it 'retrieves the users and states as arrays' do
resource = MockLoader.new(:freebsd10).load_resource('processes', 'svc')
_(resource.users.sort).must_equal ['noot']
_(resource.states.sort).must_equal ['S', 'Ss']
resource = MockLoader.new(:freebsd10).load_resource('processes', 'login -fp apop')
_(resource.users.sort).must_equal ['apop', 'root']
_(resource.states.sort).must_equal ['Ss', 'U']
end
it 'retrieves the users and states as arrays on linux os' do
resource = MockLoader.new(:centos6).load_resource('processes', 'crypto/0')
_(resource.users.sort).must_equal ['root']
_(resource.states.sort).must_equal ['S']
resource = MockLoader.new(:centos6).load_resource('processes', 'postgres: bifrost bifrost')
_(resource.users.sort).must_equal ['opscode-pgsql']
_(resource.states.sort).must_equal ['Ss']
end
end