Class: ProcessMonitor
- Inherits:
-
Object
- Object
- ProcessMonitor
- Defined in:
- lib/process_monitor.rb
Class Method Summary collapse
-
.get_io_details(pid = nil) ⇒ Object
Fetch the IO details of the process.
-
.get_limits_details(pid = nil) ⇒ Object
Fetch the limits of the process.
-
.get_pid(process_type, process_pattern = "", get_status = true) ⇒ Object
This method fetches the process ids of the process matching the process_type and process_pattern This method returns an array of hash as [=> {:pid => pid,:command => “Command to invoke the process”,:status => “Status of the process”,…].
-
.get_process_status(pid = nil) ⇒ Object
Gets the process status given the process id.
-
.get_stack_details(pid = nil) ⇒ Object
Fetch the stack trace of the process.
-
.get_syscall_details(pid = nil) ⇒ Object
Fetch the system call made by the process.
-
.method_missing(sym, *args, &block) ⇒ Object
Implements method missing.
-
.process_is_up?(pid) ⇒ Boolean
Returns true if the process is up else returns false.
Class Method Details
.get_io_details(pid = nil) ⇒ Object
Fetch the IO details of the process
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/process_monitor.rb', line 63 def self.get_io_details(pid=nil) if process_is_up? pid io = `cat /proc/#{pid}/io` io else "Process is not running!" end rescue => e "Exception occurred. Details => #{e}" end |
.get_limits_details(pid = nil) ⇒ Object
Fetch the limits of the process.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/process_monitor.rb', line 108 def self.get_limits_details(pid=nil) if process_is_up? pid limits = `cat /proc/#{pid}/limits` limits else "Process is not running!" end rescue => e "Exception occurred. Details => #{e}" end |
.get_pid(process_type, process_pattern = "", get_status = true) ⇒ Object
This method fetches the process ids of the process matching the process_type and process_pattern This method returns an array of hash as [=> {:pid => pid,:command => “Command to invoke the process”,:status => “Status of the process”,…]
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/process_monitor.rb', line 8 def self.get_pid(process_type,process_pattern="",get_status=true) return [] if process_type.nil? || process_type.empty? # Parse the output to fetch the process id. process_id_reg_exp = %r{^(.*)\s*} process_information_lines = `pgrep #{process_type}|xargs ps`.split("\n") pids = [] process_information_lines.each_with_index do |process_line,index| next if index == 0 # The name of the process will be verity-spider # This is to make sure we don't kill any other ruby process other than spider. if process_line =~ /#{process_pattern}/ process_id_reg_exp.match(process_line) pid = $1.gsub(/\s*/, "").to_i unless $$ == pid if process_is_up?(pid) pid_command = `cat /proc/#{pid}/cmdline` if get_status pids << {:process => {:pid => pid, :command => pid_command, :status => get_process_status(pid)}} else pids << {:process => {:pid => pid, :command => pid_command}} end end end end end pids rescue => e "Exception occurred. Exception => #{e.inspect}. Backtrace => #{e.backtrace} " end |
.get_process_status(pid = nil) ⇒ Object
Gets the process status given the process id
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/process_monitor.rb', line 46 def self.get_process_status(pid=nil) if process_is_up? pid status_regexp = %r{\s\((.*)\)\n} complete_status = `cat /proc/#{pid}/status|grep State` status_regexp.match(complete_status) status = $1.capitalize else "Process is not running!" end rescue => e "Exception occurred. Details => #{e}" end |
.get_stack_details(pid = nil) ⇒ Object
Fetch the stack trace of the process
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/process_monitor.rb', line 78 def self.get_stack_details(pid=nil) if process_is_up? pid stack = `cat /proc/#{pid}/stack` stack else "Process is not running!" end rescue => e "Exception occurred. Details => #{e}" end |
.get_syscall_details(pid = nil) ⇒ Object
Fetch the system call made by the process
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/process_monitor.rb', line 93 def self.get_syscall_details(pid=nil) if process_is_up? pid syscall = `cat /proc/#{pid}/syscall` syscall else "Process is not running!" end rescue => e "Exception occurred. Details => #{e}" end |
.method_missing(sym, *args, &block) ⇒ Object
Implements method missing. If get_first_pid is requested it returns a single hash of the first process to match.
141 142 143 144 145 146 147 |
# File 'lib/process_monitor.rb', line 141 def self.method_missing(sym, * args, & block) if sym.to_s =~ /get_first_pid/ return self.send(:get_pid, * args,& block).first end raise NoMethodError.new("Method \'#{sym.to_s}\' does not exist", sym.to_s, args) end |
.process_is_up?(pid) ⇒ Boolean
Returns true if the process is up else returns false.
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/process_monitor.rb', line 123 def self.process_is_up?(pid) if !pid.nil? && pid.is_a?(Integer) # The folder /proc/<pid>/ contains information about the process # The file /proc/<pid>/status gives information about the process as to if it is sleeping and so on. # If the folder exists it would mean the process exists. (pid.nil? || !File.exists?("/proc/#{pid}"))? false : true else false end rescue => e puts "Exception #{e} occurred. Details => #{e.backtrace}" return false end |