notifyme 0.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ module NotifyMe
2
+ class Check
3
+ class << self
4
+ def process(args = {})
5
+ unless %x{ps -e}.include? args.name
6
+ raise "Process #{args.name} is not running!"
7
+ end
8
+ end
9
+
10
+ def tcp(args = {})
11
+ require 'socket'
12
+ TCPSocket.new(args[:host], args[:port])
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/notifyme/log.rb CHANGED
@@ -13,6 +13,7 @@ module NotifyMe
13
13
  begin
14
14
  require "notifyme/log/#{@logger}.rb"
15
15
  rescue Exception => e
16
+ raise "The #{@logger} is invalid."
16
17
  end
17
18
  NotifyMe::Log.const_get(@logger.capitalize).new @parameters
18
19
  end
@@ -1,24 +1,54 @@
1
- require 'notifyme/task'
2
- require 'notifyme/log'
3
-
4
1
  module NotifyMe
5
2
 
6
- VERSION = '0.4'
3
+ VERSION = '1.0.0'
4
+
5
+ autoload :Task, 'notifyme/task'
6
+ autoload :Log, 'notifyme/log'
7
+ autoload :Check, 'notifyme/check'
7
8
 
8
9
  class Start
9
10
  class << self
10
11
 
11
12
  # log
12
- @@log_args = nil
13
- @@log_format = nil
14
- @@log_directory = nil
13
+ @@log_args = [:stdout]
14
+ @@log_format = :text
15
+ @@log_directory = "/tmp"
15
16
 
16
17
  # tasks list
17
18
  @@tasks = []
18
19
 
19
20
  def run!
20
21
  puts 'NotifyMe v' + NotifyMe::VERSION
21
- new(ARGV[0]).run
22
+ load_custom_check_functions
23
+ start = new(ARGV[0])
24
+ load_custom_check_tasks
25
+ start.run
26
+ end
27
+
28
+ def load_custom_check_functions
29
+ file = File.join(custom_notifyme_dir, "check.rb")
30
+ if File.exists? file
31
+ load file
32
+ puts "Loaded custom check functions from #{file}."
33
+ end
34
+ end
35
+
36
+ def load_custom_check_tasks
37
+ task_files = File.join(custom_notifyme_dir, 'check', '*.rb')
38
+ Dir[task_files].each do |task_file|
39
+ load task_file
40
+ task_name = File.basename(task_file.sub('.rb', ''))
41
+ task_func = "check_#{task_name}"
42
+ if defined? task_func
43
+ task task_func do |t|
44
+ method(task_func).call t
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ def custom_notifyme_dir
51
+ File.join(ENV['HOME'], ".notifyme")
22
52
  end
23
53
 
24
54
  def config(&block)
@@ -29,6 +59,7 @@ module NotifyMe
29
59
 
30
60
  def task(name)
31
61
  raise 'Invalid task calls' unless block_given?
62
+ puts "Added task #{name}"
32
63
  task = NotifyMe::Task.new
33
64
  task.name = name
34
65
  task.logger ||= NotifyMe::Log::Base.new(@@log_args).logger
@@ -37,6 +68,15 @@ module NotifyMe
37
68
  @@tasks << task
38
69
  end
39
70
 
71
+ def check(name, args = {})
72
+ begin
73
+ NotifyMe::Check.method(name).call args
74
+ nil
75
+ rescue Exception => e
76
+ "Check #{name} failed: #{e}"
77
+ end
78
+ end
79
+
40
80
  def log(*args)
41
81
  @@log_args = args
42
82
  end
@@ -102,7 +142,7 @@ module NotifyMe
102
142
  return if @@log_directory.nil?
103
143
  log_file = File.join(@@log_directory, '/', "#{task_name}.log")
104
144
  File.open(log_file, 'a') do |f|
105
- f.write "(#{Time.new.to_s}) #{command} : #{msg}\n"
145
+ f.write "[#{Time.new.to_s}] #{command} : #{msg}\n"
106
146
  end
107
147
  end
108
148
 
data/lib/notifyme/task.rb CHANGED
@@ -12,7 +12,19 @@ module NotifyMe
12
12
  @command = get_command(cmd)
13
13
  end
14
14
 
15
+ def start_run_time
16
+ format_time @start_run_time
17
+ end
18
+
19
+ def end_run_time
20
+ format_time @start_run_time
21
+ end
22
+
15
23
  private
24
+ def format_time(time)
25
+ Time.at(time).strftime('%Y-%m-%d %H:%M:%S %Z')
26
+ end
27
+
16
28
  def get_command(cmd)
17
29
  return if cmd.nil?
18
30
 
data/notifyme_config.rb CHANGED
@@ -26,9 +26,9 @@ NotifyMe::Start.config do
26
26
  # log :mail, :to_email => 'to@email.com'
27
27
 
28
28
  # :csv, :text, :xml, :json
29
- log_format :json
29
+ log_format :text
30
30
 
31
- # log_directory '/tmp/notifyme'
31
+ log_directory '/tmp/notifyme'
32
32
 
33
33
  # add some tasks
34
34
 
@@ -37,27 +37,19 @@ NotifyMe::Start.config do
37
37
  # if one of the disks' space usage > 95%, notification will be sent.
38
38
  #
39
39
  task :checking_disk_usage do |t|
40
- t.sleep_time = 60
40
+ t.sleep_time = 60
41
41
  t.command = lambda {
42
- if %x{df -h}.scan(/\s+(\d+)%\s+/).find {|pcent| pcent.first.to_i > 95}
43
- "Warnning: at least 1 disk space usage > 95%"
42
+ if %x{df -h}.scan(/\s+(\d+)%\s+/).find {|pcent| pcent.first.to_i > 95}
43
+ "Warning: at least 1 disk space usage > 95%"
44
44
  else
45
- nil
45
+ nil
46
46
  end
47
- }
47
+ }
48
48
  end
49
49
 
50
50
  task :checking_localhost_http do |t|
51
51
  t.sleep_time = 5
52
- t.command = lambda {
53
- require 'socket'
54
- begin
55
- TCPSocket.new('localhost', 80)
56
- nil
57
- rescue Exception => e
58
- e.to_s
59
- end
60
- }
52
+ t.command = lambda { check :tcp, :host => 'localhost', :port => 80 }
61
53
  t.restart_command = lambda { %x{/etc/init.d/httpd start} }
62
54
  end
63
55
  end
metadata CHANGED
@@ -1,11 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notifyme
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 4
8
- version: "0.4"
4
+ prerelease:
5
+ version: 1.0.0
9
6
  platform: ruby
10
7
  authors:
11
8
  - xianhua.zhou
@@ -13,8 +10,7 @@ autorequire:
13
10
  bindir: bin
14
11
  cert_chain: []
15
12
 
16
- date: 2011-09-03 00:00:00 +08:00
17
- default_executable: notifyme_daemon
13
+ date: 2012-12-05 00:00:00 Z
18
14
  dependencies:
19
15
  - !ruby/object:Gem::Dependency
20
16
  name: daemons
@@ -24,10 +20,6 @@ dependencies:
24
20
  requirements:
25
21
  - - ">="
26
22
  - !ruby/object:Gem::Version
27
- segments:
28
- - 1
29
- - 1
30
- - 0
31
23
  version: 1.1.0
32
24
  type: :runtime
33
25
  version_requirements: *id001
@@ -39,14 +31,10 @@ dependencies:
39
31
  requirements:
40
32
  - - ">="
41
33
  - !ruby/object:Gem::Version
42
- segments:
43
- - 1
44
- - 4
45
- - 2
46
34
  version: 1.4.2
47
35
  type: :runtime
48
36
  version_requirements: *id002
49
- description: NotifyMe takes care more than one tasks and process their results for you, it's similar to the *NIX's cronjob.
37
+ description: NotifyMe takes care more than one tasks and process their results for you, it's similar to *NIX's cronjob.
50
38
  email: xianhua.zhou@gmail.com
51
39
  executables:
52
40
  - notifyme_daemon
@@ -57,20 +45,19 @@ extra_rdoc_files: []
57
45
 
58
46
  files:
59
47
  - lib/notifyme.rb
60
- - lib/vendor/smtp_add_tls_support.rb
61
48
  - lib/notifyme/log.rb
62
- - lib/notifyme/task.rb
63
49
  - lib/notifyme/log/mail.rb
64
50
  - lib/notifyme/log/stdout.rb
65
51
  - lib/notifyme/log/file.rb
66
52
  - lib/notifyme/start.rb
67
- - README.rdoc
53
+ - lib/notifyme/check.rb
54
+ - lib/notifyme/task.rb
55
+ - lib/vendor/smtp_add_tls_support.rb
68
56
  - CHANGELOG
69
57
  - INSTALL
70
58
  - notifyme_config.rb
71
59
  - bin/notifyme_daemon
72
60
  - bin/notifyme
73
- has_rdoc: true
74
61
  homepage: http://github.com/xianhuazhou/NotifyMe
75
62
  licenses: []
76
63
 
@@ -84,21 +71,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
71
  requirements:
85
72
  - - ">="
86
73
  - !ruby/object:Gem::Version
87
- segments:
88
- - 0
89
74
  version: "0"
90
75
  required_rubygems_version: !ruby/object:Gem::Requirement
91
76
  none: false
92
77
  requirements:
93
78
  - - ">="
94
79
  - !ruby/object:Gem::Version
95
- segments:
96
- - 0
97
80
  version: "0"
98
81
  requirements: []
99
82
 
100
83
  rubyforge_project: notifyme
101
- rubygems_version: 1.3.7
84
+ rubygems_version: 1.8.24
102
85
  signing_key:
103
86
  specification_version: 3
104
87
  summary: It's a kind of cronjob.
data/README.rdoc DELETED
@@ -1,90 +0,0 @@
1
- == Introduction
2
-
3
- NotifyMe is a script running as a cronjob in background, can take care more than one tasks (by Ruby Threads), and push the result to different endpoints(stdout, mail, file etc.) with different formats such as xml, json, csv etc. depends on what's you need.
4
-
5
- == Installation
6
-
7
- gem install notifyme
8
-
9
- == Run it
10
-
11
- # run in the background
12
-
13
- $ notifyme_daemon start --(double dash here) /absolute/path/to/notifyme_config.rb
14
-
15
- # debug (use Ctrl + C to stop it)
16
-
17
- $ notifyme_daemon run --(double dash here) /absolute/path/to/notifyme_config.rb
18
-
19
- # stop
20
-
21
- $ notifyme_daemon stop
22
-
23
- == Example
24
-
25
- == Check HTTP Server (e.g. Nginx)
26
-
27
- require 'socket'
28
-
29
- NotifyMe::Start.config do
30
- # output to the console
31
- log :stdout
32
-
33
- # output format is text
34
- log_format :text
35
-
36
- # define the task
37
- task :checking_http_server do |t|
38
-
39
- # running every 5 seconds
40
- t.sleep_time = 5
41
-
42
- # checking command
43
- t.command = lambda {
44
- begin
45
- TCPSocket.new('localhost', 80)
46
- nil
47
- rescue Exception => e
48
- e.to_s
49
- end
50
- }
51
-
52
- # if the server is not running, the restart_command will be executed
53
- t.restart_command = lambda { %x{/etc/init.d/nginx start} }
54
- end
55
-
56
- end
57
-
58
- == Check the "cupsd" process (from "ps -e")
59
-
60
- NotifyMe::Start.config do
61
- log :stdout
62
- log_format :json
63
- task :checking_cupsd do |t|
64
- t.sleep_time = 5
65
- t.command = lambda {
66
- if %x{ps -e}.include? " cupsd"
67
- nil
68
- else
69
- "Warnning: the process cupsd is not running!"
70
- end
71
- }
72
- t.restart_command = lambda {
73
- `/etc/init.d/cups start`
74
- }
75
- end
76
- end
77
-
78
- More please check the notifyme_config.rb file.
79
-
80
- == Output
81
-
82
- The output from every task's command will be processed (send to endpoint) only if the output is not empty, otherwise do nothing.
83
-
84
- == Version
85
-
86
- v 0.4
87
-
88
- == TODO
89
-
90
- Add some SPECS