Class: Inspec::Resources::Processes

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/resources/processes.rb

Instance Method Summary collapse

Constructor Details

#initialize(grep = /.*/) ⇒ Processes

Returns a new instance of Processes.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/inspec/resources/processes.rb', line 30

def initialize(grep = /.*/)
  @grep = grep
  # turn into a regexp if it isn't one yet
  if grep.class == String
    # 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 || hm[:process_name] =~ grep
  end
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/inspec/resources/processes.rb', line 50

def exists?
  !@list.empty?
end

#listObject



58
59
60
61
# File 'lib/inspec/resources/processes.rb', line 58

def list
  Inspec.deprecate(:property_processes_list, "The processes `list` property is deprecated. Please use `entries` instead.")
  @list
end

#running?Boolean

Matcher to check if the process is running

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
# File 'lib/inspec/resources/processes.rb', line 64

def running?
  # A process is considered running if:
  # unix: it is in running(R) state or either of sleep state(D: Uninterruptible or S: Interruptible)
  # windows: it is responding i.e. state is True.

  # Other codes like <(high priorty), N(low priority), +(foreground process group) etc. may appear after the state code in unix.
  # Hence the regex used is /^statecode+/ where statecode is either R, S, or D.
  states.any? and !!(states[0] =~ /True/ || states[0] =~ /^R+/ || states[0] =~ /^D+/ || states[0] =~ /^S+/)
end

#to_sObject



54
55
56
# File 'lib/inspec/resources/processes.rb', line 54

def to_s
  "Processes #{@grep.class == String ? @grep : @grep.inspect}"
end