procmon 0.0.3 → 0.0.4
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.
- data/README.md +49 -41
- data/lib/procmon/process_conditions/cpu_usage.rb +38 -0
- data/lib/procmon/process_conditions/mem_usage.rb +1 -4
- data/lib/procmon/process_conditions/process_condition.rb +2 -0
- data/lib/procmon/process_conditions/process_health.rb +26 -0
- data/lib/procmon/system.rb +13 -0
- data/lib/procmon/version.rb +1 -1
- data/lib/test.rb +20 -3
- metadata +6 -3
data/README.md
CHANGED
@@ -8,45 +8,53 @@ It's hosted on [rubygems.org][rubygems].
|
|
8
8
|
|
9
9
|
sudo gem install procmon
|
10
10
|
|
11
|
-
##
|
12
|
-
|
13
|
-
```
|
14
|
-
require '
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
#
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
#
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'rubygems'
|
15
|
+
require 'procmon'
|
16
|
+
|
17
|
+
notifier = Proc.new do
|
18
|
+
puts "I NEED TO SEND OUT AN EMAIL"
|
19
|
+
end
|
20
|
+
|
21
|
+
notifier2 = Proc.new do
|
22
|
+
puts "I NEED TO SEND OUT AN SMS"
|
23
|
+
end
|
24
|
+
|
25
|
+
# action can be a Proc object. You can define whatever you want to do
|
26
|
+
Procmon.process("Mail") do |process|
|
27
|
+
process.checks :mem_usage, :above => 100.megabytes, :actions => [notifier, notifier2]
|
28
|
+
end
|
29
|
+
|
30
|
+
# You can specify a code block as well
|
31
|
+
Procmon.process("Mail") do |process|
|
32
|
+
process.pid = 29124
|
33
|
+
process.checks :mem_usage, :above => 100.megabytes do
|
34
|
+
puts "I'm in your base"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Using built-in email notifier
|
39
|
+
NOTIFICATION_TARGET='ddao@example.com'
|
40
|
+
Procmon.process("Mail") do |process|
|
41
|
+
process.checks :mem_usage, :above => 100.megabytes, :actions => [Procmon::Notifiers::Email]
|
42
|
+
end
|
43
|
+
|
44
|
+
# Check if a process is alive
|
45
|
+
Procmon.process("libvirtd") do |process|
|
46
|
+
process.checks :process_health, :status => "down" do
|
47
|
+
puts "Oh no! My process is not running"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Would be nice if we can do this as well
|
52
|
+
# Procmon.process("Mail") do |process|
|
53
|
+
# process.checks :mem_usage, :above => 100.megabytes do |action|
|
54
|
+
# action.perform(notifier)
|
55
|
+
# action.perform(Procmon::Notifiers::Email, 'someone@example.com')
|
56
|
+
# end
|
57
|
+
# process.checks :mem_usage, :above => 100.megabytes, :actions => [notifier, notifier2]
|
58
|
+
# process.checks :mem_usage, :above => 100.megabytes, :actions => [Procmon::Notifiers::Email]
|
59
|
+
#end
|
52
60
|
```
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Procmon
|
2
|
+
module ProcessConditions
|
3
|
+
class CpuUsage < ProcessCondition
|
4
|
+
def initialize(options = {})
|
5
|
+
super(options)
|
6
|
+
@below = options[:below]
|
7
|
+
@above = options[:above]
|
8
|
+
if @below && @above && @below < @above
|
9
|
+
raise "Invalid range for mem check condition"
|
10
|
+
elsif @below.nil? && @above.nil?
|
11
|
+
raise "Invalid range for mem check condition"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def run(pid)
|
16
|
+
System.cpu_usage(pid).to_f
|
17
|
+
end
|
18
|
+
|
19
|
+
def check(value)
|
20
|
+
if @below && @above
|
21
|
+
value < @below && value > @above
|
22
|
+
elsif @below
|
23
|
+
value < @below
|
24
|
+
elsif @above
|
25
|
+
value > @above
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def description
|
30
|
+
ret = "Cpu usage is "
|
31
|
+
conditions = {}
|
32
|
+
conditions[:below] = @options[:below] if @options[:below]
|
33
|
+
conditions[:above] = @options[:above] if @options[:above]
|
34
|
+
ret += conditions.inspect
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -6,11 +6,8 @@ module Procmon
|
|
6
6
|
MB_LABEL = "MB"
|
7
7
|
KB_LABEL = "KB"
|
8
8
|
|
9
|
-
attr_reader :actions
|
10
|
-
|
11
9
|
def initialize(options = {})
|
12
|
-
|
13
|
-
@actions = options[:actions]
|
10
|
+
super(options)
|
14
11
|
@below = options[:below]
|
15
12
|
@above = options[:above]
|
16
13
|
if @below && @above && @below < @above
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Procmon
|
2
|
+
module ProcessConditions
|
3
|
+
class ProcessHealth < ProcessCondition
|
4
|
+
def initialize(options = {})
|
5
|
+
super(options)
|
6
|
+
@status = options[:status]
|
7
|
+
if @status != "up" and @status != "down"
|
8
|
+
raise "Don't know what to do for process_health check. You need to specify :status => \"down\" or :status => \"up\""
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def run(pid)
|
13
|
+
return false if pid.nil?
|
14
|
+
System.pid_alive?(pid)
|
15
|
+
end
|
16
|
+
|
17
|
+
def check(value)
|
18
|
+
!(value ^ (@status == "up"))
|
19
|
+
end
|
20
|
+
|
21
|
+
def description
|
22
|
+
"Checking to see if is_up = #{@is_up}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/procmon/system.rb
CHANGED
@@ -15,10 +15,23 @@ module Procmon
|
|
15
15
|
@store ||= Hash.new
|
16
16
|
end
|
17
17
|
|
18
|
+
def pid_alive?(pid)
|
19
|
+
begin
|
20
|
+
::Process.kill(0, pid)
|
21
|
+
true
|
22
|
+
rescue Errno::ESRCH
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
18
27
|
def memory_usage(pid)
|
19
28
|
ps_axu[pid] && ps_axu[pid][IDX_MAP[:rss]].to_f
|
20
29
|
end
|
21
30
|
|
31
|
+
def cpu_usage(pid)
|
32
|
+
ps_axu[pid] && ps_axu[pid][IDX_MAP[:pcpu]].to_f
|
33
|
+
end
|
34
|
+
|
22
35
|
def ps_axu
|
23
36
|
# TODO: need a mutex here
|
24
37
|
store[:ps_axu] ||= begin
|
data/lib/procmon/version.rb
CHANGED
data/lib/test.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'procmon'
|
2
2
|
|
3
|
-
NOTIFICATION_TARGET='ddao@
|
3
|
+
NOTIFICATION_TARGET='ddao@eharmony.com'
|
4
4
|
|
5
5
|
notifier = Proc.new do
|
6
6
|
puts "I NEED TO SEND OUT AN EMAIL"
|
@@ -11,7 +11,20 @@ notifier2 = Proc.new do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
Procmon.process("Mail") do |process|
|
14
|
-
process.pid =
|
14
|
+
process.pid = 8264
|
15
|
+
process.checks :cpu_usage, :above => 3 do
|
16
|
+
puts "OOOO SHOOT"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Procmon.process("Mailerious") do |process|
|
21
|
+
process.checks :process_health, :status => "up" do
|
22
|
+
puts "OH SHOOT PROCESS IS UP"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
Procmon.process("Mail") do |process|
|
27
|
+
# process.pid = 29124
|
15
28
|
process.checks :mem_usage, :above => 100.megabytes do
|
16
29
|
puts "I'm in your base"
|
17
30
|
end
|
@@ -21,8 +34,12 @@ Procmon.process("Mail") do |process|
|
|
21
34
|
process.checks :mem_usage, :above => 100.megabytes, :actions => [notifier, notifier2]
|
22
35
|
end
|
23
36
|
|
37
|
+
#Procmon.process("Mail") do |process|
|
38
|
+
# process.checks :mem_usage, :above => 100.megabytes, :actions => [Procmon::Notifiers::Email]
|
39
|
+
#end
|
40
|
+
|
24
41
|
Procmon.process("Mail") do |process|
|
25
|
-
process.checks :mem_usage, :above => 100.megabytes, :actions => [
|
42
|
+
process.checks :mem_usage, :above => 100.megabytes, :actions => [notifier, notifier2]
|
26
43
|
end
|
27
44
|
|
28
45
|
# Would be nice if we can do this
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: procmon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Darren Dao
|
@@ -70,8 +70,10 @@ files:
|
|
70
70
|
- lib/procmon/notifiers/notifier.rb
|
71
71
|
- lib/procmon/process.rb
|
72
72
|
- lib/procmon/process_conditions.rb
|
73
|
+
- lib/procmon/process_conditions/cpu_usage.rb
|
73
74
|
- lib/procmon/process_conditions/mem_usage.rb
|
74
75
|
- lib/procmon/process_conditions/process_condition.rb
|
76
|
+
- lib/procmon/process_conditions/process_health.rb
|
75
77
|
- lib/procmon/process_notification.rb
|
76
78
|
- lib/procmon/system.rb
|
77
79
|
- lib/procmon/version.rb
|
@@ -112,3 +114,4 @@ specification_version: 3
|
|
112
114
|
summary: A process monitor written in Ruby. Concepts and design are based on Bluepill and God, but with emphasis on simplicity and extensibility.
|
113
115
|
test_files: []
|
114
116
|
|
117
|
+
has_rdoc:
|