process_monitor 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/VERSION +1 -1
  2. data/lib/process_monitor.rb +101 -17
  3. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -1,10 +1,11 @@
1
1
  class ProcessMonitor
2
-
2
+
3
3
  ##
4
4
  # This method fetches the process ids of the process matching the process_type and
5
5
  # process_pattern
6
- #
7
- def self.get_pid(process_type,process_pattern="")
6
+ # This method returns an array of hash as [{:process => {:pid => pid,:command => "Command to invoke the process",:status => "Status of the process"},...]
7
+
8
+ def self.get_pid(process_type,process_pattern="",get_status=true)
8
9
  # Parse the output to fetch the process id.
9
10
  process_id_reg_exp = %r{^(.*)\s*}
10
11
 
@@ -17,45 +18,128 @@ class ProcessMonitor
17
18
  # This is to make sure we don't kill any other ruby process other than spider.
18
19
  if process_line =~ /#{process_pattern}/
19
20
  process_id_reg_exp.match(process_line)
20
- pid = $1.gsub(/\s*/, "").to_i
21
- pids << pid unless $$ == pid
21
+ pid = $1.gsub(/\s*/, "").to_i
22
+ unless $$ == pid
23
+ pid_command = `cat /proc/#{pid}/cmdline`
24
+ if get_status
25
+ pids << {:process => {:pid => pid, :command => pid_command, :status => get_process_status(pid)}}
26
+ else
27
+ pids << {:process => {:pid => pid, :command => pid_command}}
28
+ end
29
+ end
22
30
  end
23
31
  end
24
- if pids.length > 1
25
- pids
26
- else
27
- pids.first
28
- end
32
+
33
+ pids
34
+ rescue => e
35
+ "Exception occurred. Exception => #{e.inspect}. Backtrace => #{e.backtrace} "
29
36
  end
30
37
 
31
38
 
32
39
  ##
33
40
  # Gets the process status given the process id
34
- #
41
+
35
42
  def self.get_process_status(pid=nil)
36
- unless pid.nil?
43
+ if process_is_up? pid
37
44
  status_regexp = %r{\s\((.*)\)\n}
38
45
  complete_status = `cat /proc/#{pid}/status|grep State`
39
46
  status_regexp.match(complete_status)
40
47
  status = $1.capitalize
41
48
  else
42
- p "No pid given!"
49
+ "Process is not running!"
50
+ end
51
+ rescue => e
52
+ "Exception occurred. Details => #{e}"
53
+ end
54
+
55
+
56
+ ##
57
+ # Fetch the IO details of the process
58
+
59
+ def self.get_io_details(pid=nil)
60
+ if process_is_up? pid
61
+ io = `cat /proc/#{pid}/io`
62
+ io
63
+ else
64
+ "Process is not running!"
65
+ end
66
+ rescue => e
67
+ "Exception occurred. Details => #{e}"
68
+ end
69
+
70
+
71
+ ##
72
+ # Fetch the stack trace of the process
73
+
74
+ def self.get_stack_details(pid=nil)
75
+ if process_is_up? pid
76
+ stack = `cat /proc/#{pid}/stack`
77
+ stack
78
+ else
79
+ "Process is not running!"
43
80
  end
81
+ rescue => e
82
+ "Exception occurred. Details => #{e}"
83
+ end
84
+
85
+
86
+ ##
87
+ # Fetch the system call made by the process
88
+
89
+ def self.get_syscall_details(pid=nil)
90
+ if process_is_up? pid
91
+ syscall = `cat /proc/#{pid}/syscall`
92
+ syscall
93
+ else
94
+ "Process is not running!"
95
+ end
96
+ rescue => e
97
+ "Exception occurred. Details => #{e}"
98
+ end
99
+
100
+
101
+ ##
102
+ # Fetch the limits of the process.
103
+
104
+ def self.get_limits_details(pid=nil)
105
+ if process_is_up? pid
106
+ limits = `cat /proc/#{pid}/limits`
107
+ limits
108
+ else
109
+ "Process is not running!"
110
+ end
111
+ rescue => e
112
+ "Exception occurred. Details => #{e}"
44
113
  end
45
114
 
46
115
 
47
116
  ##
48
117
  # Returns true if the process is up else returns false.
49
- #
118
+
50
119
  def self.process_is_up?(pid)
51
- unless pid.nil?
120
+ if !pid.nil? && pid.is_a?(Integer)
52
121
  # The folder /proc/<pid>/ contains information about the process
53
122
  # The file /proc/<pid>/status gives information about the process as to if it is sleeping and so on.
54
123
  # If the folder exists it would mean the process exists.
55
124
  (pid.nil? || !File.exists?("/proc/#{pid}"))? false : true
56
125
  else
57
- p "No pid given!"
126
+ false
58
127
  end
128
+ rescue => e
129
+ puts "Exception #{e} occurred. Details => #{e.backtrace}"
130
+ return false
131
+ end
132
+
133
+
134
+ ##
135
+ # Implements method missing. If get_first_pid is requested it returns a single hash of the first process to match.
136
+
137
+ def self.method_missing(sym, * args, & block)
138
+ if sym.to_s =~ /get_first_pid/
139
+ return self.send(:get_pid, * args,& block).first
140
+ end
141
+
142
+ raise NoMethodError.new("Method \'#{sym.to_s}\' does not exist", sym.to_s, args)
59
143
  end
60
144
 
61
- end
145
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: process_monitor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sriram Varahan
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-24 00:00:00 +05:30
18
+ date: 2010-06-25 00:00:00 +05:30
19
19
  default_executable:
20
20
  dependencies: []
21
21