notifyme 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,2 +1,5 @@
1
+ v0.2
2
+ auto restart the service by the restart_command if it's failed
3
+
1
4
  v0.1-dev
2
5
  the first dev-version.
data/README.rdoc CHANGED
@@ -2,28 +2,35 @@
2
2
 
3
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
4
 
5
- == Instalation
5
+ == Installation
6
6
 
7
7
  gem install notifyme
8
8
 
9
9
  == Run it
10
10
 
11
11
  # run in the background
12
- $ notifyme_daemon start -- /path/to/notifyme_config.rb
12
+
13
+ $ notifyme_daemon start --(double dash here) /path/to/notifyme_config.rb
13
14
 
14
15
  # debug (use Ctrl + C to stop it)
15
- $ notifyme_daemon run -- /path/to/notifyme_config.rb
16
+
17
+ $ notifyme_daemon run --(double dash here) /path/to/notifyme_config.rb
16
18
 
17
19
  # stop
20
+
18
21
  $ notifyme_daemon stop
19
22
 
23
+ == Example
24
+
25
+ Please check the notifyme_config.rb file.
26
+
20
27
  == Output
21
28
 
22
- The output from every task's command will be processed (send to endpoint) only if the output is not empty, otherwise do noghing.
29
+ The output from every task's command will be processed (send to endpoint) only if the output is not empty, otherwise do nothing.
23
30
 
24
31
  == Version
25
32
 
26
- v 0.1-dev
33
+ v 0.2
27
34
 
28
35
  == Author
29
36
 
@@ -1,15 +1,15 @@
1
1
  module NotifyMe
2
- module Log
3
- class File < Logger
4
- def <<(task)
5
- file_path = @parameters.is_a?(String) ?
6
- @parameters : @parameters[:path]
2
+ module Log
3
+ class File < Logger
4
+ def <<(task)
5
+ file_path = @parameters.is_a?(String) ?
6
+ @parameters : @parameters[:path]
7
7
 
8
- ::File.open(file_path, 'a') do |f|
9
- f.write generate(task)
10
- f.write "\n"
11
- end
12
- end
8
+ ::File.open(file_path, 'a') do |f|
9
+ f.write generate(task)
10
+ f.write "\n"
13
11
  end
12
+ end
14
13
  end
14
+ end
15
15
  end
@@ -1,61 +1,61 @@
1
1
  module NotifyMe
2
- module Log
3
- class Mail < Logger
4
- def <<(task)
5
- require 'vendor/smtp_add_tls_support.rb'
6
-
7
- param = @parameters
8
- param = {} unless param.is_a?(Hash)
9
-
10
- # some default settings
11
-
12
- default_host = 'localhost'
13
- if param[:tls]
14
- default_port = 587
15
- Net::SMTP.enable_tls
16
- else
17
- default_port = 25
18
- Net::SMTP.disable_tls
19
- end
20
-
21
- default_from_email = 'notifyme@' + default_host
22
-
23
- param[:subject] = "NotifyMe report: %s"
24
- param[:subject] = param[:subject] % task.name
25
-
26
- param[:from_email] ||= param[:account]
27
- param[:from_email] ||= default_from_email
28
-
29
- param[:body] = param[:body_header].to_s +
30
- generate(task) +
31
- param[:body_footer].to_s
32
-
33
- smtp = Net::SMTP.new(
34
- (param[:address] || param[:host]) || default_host,
35
- param[:port] || default_port
36
- )
37
-
38
- # go go go!
39
- smtp.start(param[:helo_domain] || default_host,
40
- param[:account],
41
- param[:password],
42
- param[:authtype] || :plain) do |mail|
43
- mail.send_message message(param, task), param[:from_email], param[:to_email]
44
- end
45
- end
46
-
47
- private
48
- def message(param, task)
49
- time = Time.now
50
- "From: #{param[:from_name] || param[:from_email]} <#{param[:from_email]}>\r\n" \
51
- << "To: #{param[:to_name] || param[:to_email]} <#{param[:to_email]}>\r\n" \
52
- << "Subject: #{param[:subject]}\r\n" \
53
- << "Date: #{time.to_s}\r\n" \
54
- << "Content-type: text/plain; charset=UTF-8\r\n" \
55
- << "Message-Id: <notifyme.#{task.name}.#{time.to_i}@example.com>\r\n" \
56
- << "\r\n" \
57
- << param[:body]
58
- end
2
+ module Log
3
+ class Mail < Logger
4
+ def <<(task)
5
+ require 'vendor/smtp_add_tls_support.rb'
6
+
7
+ param = @parameters
8
+ param = {} unless param.is_a?(Hash)
9
+
10
+ # some default settings
11
+
12
+ default_host = 'localhost'
13
+ if param[:tls]
14
+ default_port = 587
15
+ Net::SMTP.enable_tls
16
+ else
17
+ default_port = 25
18
+ Net::SMTP.disable_tls
59
19
  end
20
+
21
+ default_from_email = 'notifyme@' + default_host
22
+
23
+ param[:subject] = "NotifyMe report: %s"
24
+ param[:subject] = param[:subject] % task.name
25
+
26
+ param[:from_email] ||= param[:account]
27
+ param[:from_email] ||= default_from_email
28
+
29
+ param[:body] = param[:body_header].to_s +
30
+ generate(task) +
31
+ param[:body_footer].to_s
32
+
33
+ smtp = Net::SMTP.new(
34
+ (param[:address] || param[:host]) || default_host,
35
+ param[:port] || default_port
36
+ )
37
+
38
+ # go go go!
39
+ smtp.start(param[:helo_domain] || default_host,
40
+ param[:account],
41
+ param[:password],
42
+ param[:authtype] || :plain) do |mail|
43
+ mail.send_message message(param, task), param[:from_email], param[:to_email]
44
+ end
45
+ end
46
+
47
+ private
48
+ def message(param, task)
49
+ time = Time.now
50
+ "From: #{param[:from_name] || param[:from_email]} <#{param[:from_email]}>\r\n" \
51
+ << "To: #{param[:to_name] || param[:to_email]} <#{param[:to_email]}>\r\n" \
52
+ << "Subject: #{param[:subject]}\r\n" \
53
+ << "Date: #{time.to_s}\r\n" \
54
+ << "Content-type: text/plain; charset=UTF-8\r\n" \
55
+ << "Message-Id: <notifyme.#{task.name}.#{time.to_i}@example.com>\r\n" \
56
+ << "\r\n" \
57
+ << param[:body]
58
+ end
60
59
  end
60
+ end
61
61
  end
@@ -1,10 +1,10 @@
1
1
  module NotifyMe
2
- module Log
3
- class Stdout < Logger
4
- def <<(task)
5
- puts generate(task)
6
- puts
7
- end
8
- end
2
+ module Log
3
+ class Stdout < Logger
4
+ def <<(task)
5
+ puts generate(task)
6
+ puts
7
+ end
9
8
  end
9
+ end
10
10
  end
data/lib/notifyme/log.rb CHANGED
@@ -1,84 +1,84 @@
1
1
  module NotifyMe
2
- module Log
3
- class Base
4
- @logger = nil
5
- @parameters = {}
2
+ module Log
3
+ class Base
4
+ @logger = nil
5
+ @parameters = {}
6
6
 
7
- def initialize(args)
8
- @logger = args.first.to_s.downcase
9
- @parameters = args.last
10
- end
7
+ def initialize(args)
8
+ @logger = args.first.to_s.downcase
9
+ @parameters = args.last
10
+ end
11
11
 
12
- def logger
13
- begin
14
- require "notifyme/log/#{@logger}.rb"
15
- rescue Exception => e
16
- end
17
- NotifyMe::Log.const_get(@logger.capitalize).new @parameters
18
- end
19
-
20
- def self.default
21
- require 'notifyme/log/stdout.rb'
22
- NotifyMe::Log::Stdout.new
23
- end
12
+ def logger
13
+ begin
14
+ require "notifyme/log/#{@logger}.rb"
15
+ rescue Exception => e
24
16
  end
17
+ NotifyMe::Log.const_get(@logger.capitalize).new @parameters
18
+ end
25
19
 
26
- class Logger
27
- def initialize(parameters = {})
28
- @parameters = parameters
29
- end
20
+ def self.default
21
+ require 'notifyme/log/stdout.rb'
22
+ NotifyMe::Log::Stdout.new
23
+ end
24
+ end
30
25
 
31
- protected
32
- def generate(task)
33
- method("to_#{task.log_format}").call task
34
- end
26
+ class Logger
27
+ def initialize(parameters = {})
28
+ @parameters = parameters
29
+ end
35
30
 
36
- private
31
+ protected
32
+ def generate(task)
33
+ method("to_#{task.log_format}").call task
34
+ end
37
35
 
38
- def to_json(task)
39
- require 'json'
40
- JSON to_hash(task)
41
- end
36
+ private
42
37
 
43
- def to_xml(task)
44
- require 'rexml/document'
45
- xml = REXML::Element.new 'task'
46
- fields.each do |f|
47
- el = REXML::Element.new f.to_s
48
- el.text = task.send(f)
49
- xml.add_element el
50
- end
51
- xml.to_s
52
- end
38
+ def to_json(task)
39
+ require 'json'
40
+ JSON to_hash(task)
41
+ end
53
42
 
54
- def to_csv(task)
55
- require 'csv'
56
- row = []
57
- fields.each do |f|
58
- row << task.send(f)
59
- end
60
- CSV.generate_line row
61
- end
43
+ def to_xml(task)
44
+ require 'rexml/document'
45
+ xml = REXML::Element.new 'task'
46
+ fields.each do |f|
47
+ el = REXML::Element.new f.to_s
48
+ el.text = task.send(f)
49
+ xml.add_element el
50
+ end
51
+ xml.to_s
52
+ end
62
53
 
63
- def to_text(task)
64
- output = ''
65
- fields.each do |f|
66
- output << "#{f}: #{task.send(f)}\n"
67
- end
68
- output << "\n"
69
- end
54
+ def to_csv(task)
55
+ require 'csv'
56
+ row = []
57
+ fields.each do |f|
58
+ row << task.send(f)
59
+ end
60
+ CSV.generate_line row
61
+ end
70
62
 
71
- def to_hash(task)
72
- hash = {}
73
- fields.each do |f|
74
- hash[f] = task.send(f)
75
- end
76
- hash
77
- end
63
+ def to_text(task)
64
+ output = ''
65
+ fields.each do |f|
66
+ output << "#{f}: #{task.send(f)}\n"
67
+ end
68
+ output << "\n"
69
+ end
78
70
 
79
- def fields
80
- [:name, :sleep_time, :start_run_time, :end_run_time, :result]
81
- end
71
+ def to_hash(task)
72
+ hash = {}
73
+ fields.each do |f|
74
+ hash[f] = task.send(f)
82
75
  end
76
+ hash
77
+ end
78
+
79
+ def fields
80
+ [:name, :sleep_time, :start_run_time, :end_run_time, :result]
81
+ end
83
82
  end
83
+ end
84
84
  end
@@ -1,85 +1,94 @@
1
1
  module NotifyMe
2
2
 
3
- VERSION = '0.1'
3
+ VERSION = '0.1'
4
4
 
5
- autoload :Task, 'notifyme/task'
6
- autoload :Log, 'notifyme/log'
5
+ autoload :Task, 'notifyme/task'
6
+ autoload :Log, 'notifyme/log'
7
7
 
8
- class Start
9
- class << self
8
+ class Start
9
+ class << self
10
10
 
11
- # log
12
- @@log_args = nil
13
- @@log_format = nil
11
+ # log
12
+ @@log_args = nil
13
+ @@log_format = nil
14
14
 
15
- # tasks list
16
- @@tasks = []
15
+ # tasks list
16
+ @@tasks = []
17
17
 
18
- def run!
19
- puts 'NotifyMe v' + NotifyMe::VERSION
20
- new(ARGV[0]).run
21
- end
18
+ def run!
19
+ puts 'NotifyMe v' + NotifyMe::VERSION
20
+ new(ARGV[0]).run
21
+ end
22
22
 
23
- def config(&block)
24
- class_eval &block
25
- end
23
+ def config(&block)
24
+ class_eval &block
25
+ end
26
26
 
27
- private
27
+ private
28
28
 
29
- def task(name)
30
- raise 'Invalid task calls' unless block_given?
31
- task = Task.new
32
- task.name = name
33
- task.logger ||= NotifyMe::Log::Base.new(@@log_args).logger
34
- task.log_format ||= @@log_format
35
- yield task
36
- @@tasks << task
37
- end
29
+ def task(name)
30
+ raise 'Invalid task calls' unless block_given?
31
+ task = Task.new
32
+ task.name = name
33
+ task.logger ||= NotifyMe::Log::Base.new(@@log_args).logger
34
+ task.log_format ||= @@log_format
35
+ yield task
36
+ @@tasks << task
37
+ end
38
38
 
39
- def log(*args)
40
- @@log_args = args
41
- end
39
+ def log(*args)
40
+ @@log_args = args
41
+ end
42
42
 
43
- def log_format(format)
44
- @@log_format = format
45
- end
46
- end
43
+ def log_format(format)
44
+ @@log_format = format
45
+ end
46
+ end
47
47
 
48
- def run
49
- tasks_thread = []
50
- @mutex = Mutex.new
51
- @@tasks.each do |task|
52
- tasks_thread << Thread.new(task) do
53
- loop do
54
- Thread.current[:name] = task.name
55
- sleep task.sleep_time
56
- run_task(task) if task.command.respond_to? :call
57
- end
58
- end
59
- end
60
-
61
- tasks_thread.each do |t| t.join end
48
+ def run
49
+ tasks_thread = []
50
+ @mutex = Mutex.new
51
+ @@tasks.each do |task|
52
+ tasks_thread << Thread.new(task) do
53
+ loop do
54
+ Thread.current[:name] = task.name
55
+ sleep task.sleep_time
56
+ run_task(task) if task.command.respond_to? :call
57
+ end
62
58
  end
59
+ end
63
60
 
64
- private
65
-
66
- def run_task(task)
67
- begin
68
- task.start_run_time = Time.now.to_i
69
- task.result = task.command.call
70
- task.end_run_time = Time.now.to_i
71
- rescue Exception => e
72
- task.result = e.to_s
73
- end
74
- unless task.result.to_s.empty?
75
- @mutex.synchronize do
76
- task.logger << task
77
- end
78
- end
79
- end
61
+ tasks_thread.each do |t| t.join end
62
+ end
63
+
64
+ private
80
65
 
81
- def initialize(config_file)
82
- require config_file
66
+ def run_task(task)
67
+ begin
68
+ task.start_run_time = Time.now.to_i
69
+ task.result = task.command.call
70
+ task.end_run_time = Time.now.to_i
71
+ rescue Exception => e
72
+ task.result = e.to_s
73
+ end
74
+
75
+ # works fine.
76
+ return if task.result.to_s.empty?
77
+
78
+ # restart the command if need
79
+ task.restart_command.call if task.restart_command
80
+
81
+ @mutex.synchronize do
82
+ begin
83
+ task.logger << task
84
+ rescue Exception => e
85
+ puts e.backtrace.join("\n")
83
86
  end
87
+ end
88
+ end
89
+
90
+ def initialize(config_file)
91
+ require config_file
84
92
  end
93
+ end
85
94
  end
data/lib/notifyme/task.rb CHANGED
@@ -1,22 +1,28 @@
1
1
  module NotifyMe
2
- class Task
3
- attr_accessor :name, :sleep_time, :start_run_time, :end_run_time,
4
- :logger, :command, :result,
5
- :log_format
2
+ class Task
3
+ attr_accessor :name, :sleep_time, :start_run_time, :end_run_time,
4
+ :logger, :command, :restart_command, :result,
5
+ :log_format
6
6
 
7
- def command=(cmd)
8
- return if cmd.nil?
7
+ def restart_command=(cmd)
8
+ @restart_command = get_command(cmd)
9
+ end
10
+
11
+ def command=(cmd)
12
+ @command = get_command(cmd)
13
+ end
14
+
15
+ private
16
+ def get_command(cmd)
17
+ return if cmd.nil?
9
18
 
10
- unless cmd.is_a?(Proc)
11
- cmd = cmd.new if cmd.class.is_a?(Class)
12
- end
19
+ unless cmd.is_a? Proc
20
+ cmd = cmd.new if cmd.class.is_a?(Class)
21
+ end
13
22
 
14
- if cmd.respond_to? :call
15
- @command = cmd
16
- return
17
- end
23
+ return cmd if cmd.respond_to? :call
18
24
 
19
- raise 'Invalid command parameter'
20
- end
25
+ raise 'Invalid command parameter'
21
26
  end
27
+ end
22
28
  end
@@ -52,14 +52,14 @@ Net::SMTP.class_eval do
52
52
  check_auth_args user, secret if user or secret
53
53
 
54
54
  sock = timeout(@open_timeout) {
55
- TCPSocket.open(@address, @port)
55
+ TCPSocket.open(@address, @port)
56
56
  }
57
57
  @socket = Net::InternetMessageIO.new(sock)
58
58
  @socket.read_timeout = @read_timeout
59
59
  @socket.debug_output = nil
60
60
 
61
61
  check_response(critical{
62
- recv_response()
62
+ recv_response()
63
63
  })
64
64
  do_helo(helodomain)
65
65
 
data/notifyme_config.rb CHANGED
@@ -1,46 +1,44 @@
1
1
  class MyTask
2
- def call
3
- Time.now.to_s
4
- end
2
+ def call
3
+ Time.now.to_s
4
+ end
5
5
  end
6
6
 
7
-
8
7
  NotifyMe::Start.config do
9
-
10
- log :stdout
8
+ log :stdout
11
9
 
12
10
  =begin
13
- log :mail,
14
- :host => 'smtp.gmail.com',
15
- :helo_domain => 'gmail.com',
16
- :tls => true,
17
-
18
- :account => 'xxx@gmail.com',
19
- :password => '***',
20
-
21
- :from_email => 'from@gmail.com',
22
- :from_name => 'from name',
23
- :to_email => 'to@gmail.com',
24
- :to_name => 'to name'
25
-
11
+ log :mail,
12
+ :host => 'smtp.gmail.com',
13
+ :helo_domain => 'gmail.com',
14
+ :tls => true,
15
+
16
+ :account => 'xxx@gmail.com',
17
+ :password => '***',
18
+
19
+ :from_email => 'from@gmail.com',
20
+ :from_name => 'from name',
21
+ :to_email => 'to@gmail.com',
22
+ :to_name => 'to name'
26
23
  =end
27
- # log :file, '/tmp/test.txt'
28
- # log :stdout
29
- # log :mail, :to_email => 'to@email.com'
30
24
 
31
- # :csv, :text, :xml, :json
32
- log_format :json
25
+ # log :file, '/tmp/test.txt'
26
+ # log :stdout
27
+ # log :mail, :to_email => 'to@email.com'
33
28
 
29
+ # :csv, :text, :xml, :json
30
+ log_format :json
34
31
 
35
- # add some tasks
32
+ # add some tasks
36
33
 
37
- task :checking_disk do |t|
38
- t.sleep_time = 1
39
- t.command = Proc.new { %x{df -h} }
40
- end
34
+ task :checking_disk do |t|
35
+ t.sleep_time = 1
36
+ t.command = Proc.new { %x{df -h} }
37
+ end
41
38
 
42
- task :checking_http do |t|
43
- t.sleep_time = 2
44
- t.command = MyTask
45
- end
39
+ task :checking_http do |t|
40
+ t.sleep_time = 2
41
+ t.command = MyTask
42
+ t.restart_command = lambda { puts "restarted !" }
43
+ end
46
44
  end
metadata CHANGED
@@ -1,12 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notifyme
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 1
9
- version: "0.1"
7
+ - 2
8
+ version: "0.2"
10
9
  platform: ruby
11
10
  authors:
12
11
  - xianhua.zhou
@@ -14,7 +13,7 @@ autorequire:
14
13
  bindir: bin
15
14
  cert_chain: []
16
15
 
17
- date: 2010-07-10 00:00:00 +08:00
16
+ date: 2010-11-06 00:00:00 +08:00
18
17
  default_executable: notifyme_daemon
19
18
  dependencies:
20
19
  - !ruby/object:Gem::Dependency
@@ -25,7 +24,6 @@ dependencies:
25
24
  requirements:
26
25
  - - ">="
27
26
  - !ruby/object:Gem::Version
28
- hash: 19
29
27
  segments:
30
28
  - 1
31
29
  - 1
@@ -41,7 +39,6 @@ dependencies:
41
39
  requirements:
42
40
  - - ">="
43
41
  - !ruby/object:Gem::Version
44
- hash: 3
45
42
  segments:
46
43
  - 1
47
44
  - 4
@@ -59,12 +56,12 @@ extensions: []
59
56
  extra_rdoc_files: []
60
57
 
61
58
  files:
59
+ - lib/notifyme/task.rb
62
60
  - lib/notifyme/start.rb
63
- - lib/notifyme/log.rb
64
61
  - lib/notifyme/log/mail.rb
65
62
  - lib/notifyme/log/file.rb
66
63
  - lib/notifyme/log/stdout.rb
67
- - lib/notifyme/task.rb
64
+ - lib/notifyme/log.rb
68
65
  - lib/vendor/smtp_add_tls_support.rb
69
66
  - lib/notifyme.rb
70
67
  - README.rdoc
@@ -87,7 +84,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
87
84
  requirements:
88
85
  - - ">="
89
86
  - !ruby/object:Gem::Version
90
- hash: 3
91
87
  segments:
92
88
  - 0
93
89
  version: "0"
@@ -96,7 +92,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
92
  requirements:
97
93
  - - ">="
98
94
  - !ruby/object:Gem::Version
99
- hash: 3
100
95
  segments:
101
96
  - 0
102
97
  version: "0"