Class: ProcessObserver::WindowsProcess

Inherits:
Process
  • Object
show all
Defined in:
lib/process_observer/process.rb

Overview

Class representing process in Windows.

Instance Attribute Summary collapse

Attributes inherited from Process

#memory, #name

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WindowsProcess

Initialize new process.

Parameters:

  • options (Hash)

Options Hash (options):

  • image_name (String)

    name of the executable.

  • pid (Integer)

    process ID.

  • session_name (String, nil)

    session name.

  • session (Integer, nil)

    session number.

  • mem_usage (Integer, nil)

    memory usage in KB.

  • status (String, nil)

    process status.

  • user_name (String, nil)

    user which started process.

  • cpu_time (String, nil)

    process runtime.

  • window_title (String, nil)

    title of process window.

  • services (Array<String>, nil)

    services.

  • modules (Array<String>, nil)

    used DLLs.

  • package_name (String, nil)

    name of app package name.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/process_observer/process.rb', line 104

def initialize(options)
  @image_name   = options[:image_name].to_s
  @pid          = options[:pid].to_i
  @session_name = options[:session_name] ? options[:session_name].to_s                     : nil
  @session      = options[:session]      ? options[:session].to_i                          : nil
  @mem_usage    = options[:mem_usage]    ? options[:mem_usage].to_s.gsub(/[^\d]/, "").to_i : nil
  @status       = options[:status]       ? options[:status].to_s                           : nil
  @user_name    = options[:user_name]    ? options[:user_name].to_s                        : nil
  @cpu_time     = options[:cpu_time]     ? options[:cpu_time].to_s                         : nil
  @window_title = options[:window_title] ? options[:window_title].to_s                     : nil
  @services     = options[:services]     ? options[:services].to_s.split(",")              : []
  @modules      = options[:modules]      ? options[:modules].to_s.split(",")               : []
  @package_name = options[:package_name] ? options[:package_name].to_s                     : nil

  super(
    name: @image_name,
    pid: @pid,
    memory: @mem_usage
  )
end

Instance Attribute Details

#cpu_timeString? (readonly)

Returns process runtime.

Returns:

  • (String, nil)

    process runtime.



69
70
71
# File 'lib/process_observer/process.rb', line 69

def cpu_time
  @cpu_time
end

#image_nameString (readonly)

Returns name of the executable.

Returns:

  • (String)

    name of the executable.



41
42
43
# File 'lib/process_observer/process.rb', line 41

def image_name
  @image_name
end

#mem_usageInteger? (readonly)

Returns memory usage in KB.

Returns:

  • (Integer, nil)

    memory usage in KB.



57
58
59
# File 'lib/process_observer/process.rb', line 57

def mem_usage
  @mem_usage
end

#modulesArray<String> (readonly)

Returns used DLLs.

Returns:

  • (Array<String>)

    used DLLs.



81
82
83
# File 'lib/process_observer/process.rb', line 81

def modules
  @modules
end

#package_nameString? (readonly)

Returns name of app package name.

Returns:

  • (String, nil)

    name of app package name.



85
86
87
# File 'lib/process_observer/process.rb', line 85

def package_name
  @package_name
end

#pidInteger (readonly)

Returns process ID.

Returns:

  • (Integer)

    process ID.



45
46
47
# File 'lib/process_observer/process.rb', line 45

def pid
  @pid
end

#servicesArray<String> (readonly)

Returns services.

Returns:

  • (Array<String>)

    services.



77
78
79
# File 'lib/process_observer/process.rb', line 77

def services
  @services
end

#sessionInteger? (readonly)

Returns session number.

Returns:

  • (Integer, nil)

    session number.



53
54
55
# File 'lib/process_observer/process.rb', line 53

def session
  @session
end

#session_nameString? (readonly)

Returns session name.

Returns:

  • (String, nil)

    session name.



49
50
51
# File 'lib/process_observer/process.rb', line 49

def session_name
  @session_name
end

#statusString? (readonly)

Returns process status.

Returns:

  • (String, nil)

    process status.



61
62
63
# File 'lib/process_observer/process.rb', line 61

def status
  @status
end

#user_nameString? (readonly)

Returns user which started process.

Returns:

  • (String, nil)

    user which started process.



65
66
67
# File 'lib/process_observer/process.rb', line 65

def user_name
  @user_name
end

#window_titleString? (readonly)

Returns title of process window.

Returns:

  • (String, nil)

    title of process window.



73
74
75
# File 'lib/process_observer/process.rb', line 73

def window_title
  @window_title
end

Instance Method Details

#==(other) ⇒ Object

Compare with other process by PID.



151
152
153
# File 'lib/process_observer/process.rb', line 151

def ==(other)
  WindowsProcess === other && other.pid == @pid
end

#inspect(splitter = "; ") ⇒ String

Inspect all stored data.

Parameters:

  • splitter (String) (defaults to: "; ")

Returns:

  • (String)

    all accessable info in human-readable form.



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/process_observer/process.rb', line 137

def inspect(splitter = "; ")
  to_s +
  (@session_name || @session ? "#{splitter}Session: #{@session_name}(#{@session})" : "") +
  (@mem_usage ? "#{splitter}Memory usage: #{@mem_usage} KB" : "") +
  (@status ? "#{splitter}Status: #{@status}" : "") +
  (@user_name || @cpu_time ? "#{splitter}Launched by: #{@user_name}, runs for #{@cpu_time}" : "") +
  (@window_title ? "#{splitter}Title: #{@window_title}" : "") + 
  (@services.empty? ? "" : "#{splitter}Services: #{@services.join(",")}") +
  (@modules.empty? ? "" : "#{splitter}Modules: #{@modules.join(",")}") +
  (@package_name ? "#{splitter}Package name: #{@package_name}" : "")
end

#to_sString

Returns PID and name of process.

Returns:

  • (String)

    PID and name of process.



127
128
129
# File 'lib/process_observer/process.rb', line 127

def to_s
  "Process ##{@pid} #{@image_name}"
end