Class: HawatelPS::Linux::ProcTable

Inherits:
Object
  • Object
show all
Defined in:
lib/hawatel_ps/linux/proc_table.rb

Class Method Summary collapse

Class Method Details

.childs_treeObject (private)

Find and add childs attribute for process



111
112
113
114
115
116
117
118
119
# File 'lib/hawatel_ps/linux/proc_table.rb', line 111

def childs_tree
  @proc_table.each do |proc_parent|
    @proc_table.each do |proc_child|
      if proc_parent.pid == proc_child.ppid
        proc_parent[:childs].push(proc_child)
      end
    end
  end
end

.proc_tableArray<ProcInfo>

Return all process instances

Examples:

proc_table()

Returns:

  • (Array<ProcInfo>)

    array of ProcInfo objects



89
90
91
92
# File 'lib/hawatel_ps/linux/proc_table.rb', line 89

def proc_table
  refresh
  return @proc_table
end

.refreshArray<ProcInfo> (private)

Refresh list of current running processes

Examples:

refresh()

Returns:

  • (Array<ProcInfo>)

    array of ProcInfo objects



101
102
103
104
105
106
107
# File 'lib/hawatel_ps/linux/proc_table.rb', line 101

def refresh
  @proc_table = Array.new
  ProcFetch.get_process.each do |proc_attrs|
    @proc_table.push(ProcInfo.new(proc_attrs))
  end
  childs_tree
end

.search_by_condition(args) ⇒ Array<ProcInfo>

Return attributes of searched process based on specified condition

Examples:

search_by_condition(:attrs => 'vmsize', :oper => '<', value => '10000')

Parameters:

  • args (Hash)

    attributes for search condition

  • :attr (Hash)

    a customizable set of options

  • :oper (Hash)

    a customizable set of options

  • :value (Hash)

    a customizable set of options

Returns:

  • (Array<ProcInfo>)

    array of ProcInfo objects



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hawatel_ps/linux/proc_table.rb', line 57

def search_by_condition(args)
  refresh
  attr = args[:attr]
  oper  = args[:oper]
  value = args[:value]
  process_list = Array.new
  @proc_table.each do |process|
    value = value.to_i if process[:"#{attr}"].is_a? Fixnum
    value = value.to_f if process[:"#{attr}"].is_a? Float
    if oper == '>'
      process_list << process if process[:"#{attr}"] > value if process[:"#{attr}"]
    elsif oper == '<'
      process_list << process if process[:"#{attr}"] < value if process[:"#{attr}"]
    elsif oper == '>='
      process_list << process if process[:"#{attr}"] >= value if process[:"#{attr}"]
    elsif oper == '<='
      process_list << process if process[:"#{attr}"] <= value if process[:"#{attr}"]
    elsif oper == '=='
      process_list << process if process[:"#{attr}"] == value if process[:"#{attr}"]
    elsif oper == '!='
      process_list << process if process[:"#{attr}"] != value if process[:"#{attr}"]
    end
  end
  return process_list
end

.search_by_name(process_name) ⇒ Array<ProcInfo>

Return attributes of searched process based on name or cmdline

Examples:

search_by_name('java')
search_by_name('/^regex/')

Parameters:

  • process_name (String)

    name of process

Returns:

  • (Array<ProcInfo>)

    array of ProcInfo objects



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hawatel_ps/linux/proc_table.rb', line 29

def search_by_name(process_name)
  refresh
  process_list = Array.new
  if process_name =~ /^\/.*\/$/
    process_name.slice!(0)
    process_name = Regexp.new(/#{process_name.chop}/)
  end
  @proc_table.each do |process|
   if process_name.is_a? Regexp
     process_list << process if process.name =~ process_name || process.cmdline =~ process_name
   else
     process_list << process if process.name == "#{process_name}" || process.cmdline == "#{process_name}"
   end
  end
  return process_list
end

.search_by_pid(pid) ⇒ ProcInfo

Return attributes of searched process based on pid

Examples:

search_by_pid(1761)

Parameters:

  • pid (Integer)

    of process

Returns:

  • (ProcInfo)

    hash with process attributes



13
14
15
16
17
18
19
# File 'lib/hawatel_ps/linux/proc_table.rb', line 13

def search_by_pid(pid)
  refresh
  @proc_table.each do |process|
    return process if process.pid.to_s == pid.to_s
  end
  return nil
end