notifyme 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/notifyme/log.rb CHANGED
@@ -25,10 +25,35 @@ module NotifyMe
25
25
  end
26
26
 
27
27
  class Logger
28
+ # store the time of the errors for each task
29
+ @@log_history = {}
30
+
31
+ # 30s, 1min, 5min, 10min, 30min, 1hour, 6hours, 12hours, 1day, 1week, 1month (unit: seconds)
32
+ LOG_FREQUENCE = [30, 60, 300, 600, 1800, 3600, 21600, 43200, 86400, 604800, 2592000]
33
+
28
34
  def initialize(parameters = {})
29
35
  @parameters = parameters
30
36
  end
31
37
 
38
+ def can_log?(task)
39
+ history = @@log_history[task.name]
40
+ return true if history.nil?
41
+
42
+ checkpoint = LOG_FREQUENCE[history.size - 1] || LOG_FREQUENCE.last
43
+ return true if Time.now.to_i - history.last > checkpoint
44
+
45
+ false
46
+ end
47
+
48
+ def add_log_history(task)
49
+ now = Time.now.to_i
50
+ if @@log_history[task.name].nil?
51
+ @@log_history[task.name] = [now]
52
+ else
53
+ @@log_history[task.name] << now
54
+ end
55
+ end
56
+
32
57
  protected
33
58
  def generate(task)
34
59
  method("to_#{task.log_format}").call task
@@ -1,6 +1,6 @@
1
1
  module NotifyMe
2
2
 
3
- VERSION = '1.0.2'
3
+ VERSION = '1.1.0'
4
4
  DEFAULT_CONFIG_FILE = "#{ENV['HOME']}/.notifyme/notifyme_config.rb"
5
5
 
6
6
  autoload :Task, 'notifyme/task'
@@ -14,6 +14,9 @@ module NotifyMe
14
14
  @@log_args = [:stdout]
15
15
  @@log_format = :text
16
16
 
17
+ # nagios
18
+ @@nagios_directory = "/usr/local/nagios/libexec"
19
+
17
20
  # tasks list
18
21
  @@tasks = []
19
22
 
@@ -118,6 +121,20 @@ end
118
121
  end
119
122
  end
120
123
 
124
+ def nagios_check(name, args = '')
125
+ cmd = "#{@@nagios_directory}/check_#{name} #{args}"
126
+ begin
127
+ result = `#{cmd}`
128
+ if $?.to_i == 0
129
+ nil
130
+ else
131
+ "#{result.strip} (nagios check)"
132
+ end
133
+ rescue Exception => e
134
+ "Nagios check_#{name} failed: #{e}"
135
+ end
136
+ end
137
+
121
138
  def log(*args)
122
139
  @@log_args = args
123
140
  end
@@ -126,8 +143,12 @@ end
126
143
  @@log_format = format
127
144
  end
128
145
 
146
+ def nagios_directory(directory)
147
+ @@nagios_directory = directory
148
+ end
149
+
129
150
  def log_directory(directory)
130
- $stderr.puts "Warn: the \"log_direcotry\" has been deprecated. You can find the error messages in the /var/log/ directory."
151
+ $stderr.puts "Warn: the \"log_direcotry\" has been deprecated. You can find the error messages in the /var/log/syslog file."
131
152
  end
132
153
  end
133
154
 
@@ -156,7 +177,6 @@ end
156
177
  task.result = task.command.call
157
178
  task.end_run_time = Time.now.to_i
158
179
  rescue Exception => e
159
- log_error task.name, task.command, e.to_s
160
180
  task.result = e.to_s
161
181
  end
162
182
 
@@ -167,20 +187,26 @@ end
167
187
  begin
168
188
  task.restart_command.call if task.restart_command
169
189
  rescue Exception => e
170
- log_error task.name, task.restart_command, e.to_s
190
+ syslog_error task.name, task.restart_command, e.to_s
171
191
  end
172
192
 
173
193
  @mutex.synchronize do
174
194
  begin
175
- task.logger << task
176
- rescue Exception => e
177
- puts e.backtrace.join("\n")
195
+ if task.logger.can_log? task
196
+ task.logger << task
197
+ syslog_error task.name, task.command, task.result.to_s
198
+ task.logger.add_log_history task
199
+ end
200
+ rescue Exception => e
201
+ syslog_error task.name, "save_error_log", e.to_s + "\n" + e.backtrace.join("\n")
178
202
  end
179
203
  end
180
204
  end
181
205
 
182
- def log_error(task_name, command, msg)
206
+ def syslog_error(task_name, command, msg)
207
+ Syslog.open 'notifyme', Syslog::LOG_NDELAY, Syslog::LOG_USER
183
208
  Syslog.log Syslog::LOG_ERR, "[#{Time.new.to_s}] #{task_name} # #{command} : #{msg}"
209
+ Syslog.close
184
210
  end
185
211
 
186
212
  def initialize(config_file)
data/notifyme_config.rb CHANGED
@@ -28,6 +28,9 @@ NotifyMe::Start.config do
28
28
  # :csv, :text, :xml, :json
29
29
  log_format :text
30
30
 
31
+ # set nagios directory
32
+ # nagios_directory "/usr/local/nagios"
33
+
31
34
  # add some tasks
32
35
 
33
36
  #
metadata CHANGED
@@ -1,109 +1,99 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: notifyme
3
- version: !ruby/object:Gem::Version
4
- version: 1.0.2
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 1.1.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - xianhua.zhou
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-20 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
12
+
13
+ date: 2013-01-02 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
15
16
  name: daemons
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: 1.1.0
22
- type: :runtime
23
17
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
18
+ requirement: &id001 !ruby/object:Gem::Requirement
25
19
  none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
29
23
  version: 1.1.0
30
- - !ruby/object:Gem::Dependency
31
- name: json_pure
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: 1.4.2
38
24
  type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: json_pure
39
28
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
29
+ requirement: &id002 !ruby/object:Gem::Requirement
41
30
  none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
45
34
  version: 1.4.2
46
- - !ruby/object:Gem::Dependency
47
- name: http_request.rb
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: 1.1.13
54
35
  type: :runtime
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: http_request.rb
55
39
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
40
+ requirement: &id003 !ruby/object:Gem::Requirement
57
41
  none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
61
45
  version: 1.1.13
62
- description: NotifyMe takes care more than one tasks and process their results for
63
- you, it's similar to *NIX's cronjob.
46
+ type: :runtime
47
+ version_requirements: *id003
48
+ description: NotifyMe takes care more than one tasks and process their results for you, it's similar to *NIX's cronjob but can do more.
64
49
  email: xianhua.zhou@gmail.com
65
- executables:
50
+ executables:
66
51
  - notifyme_daemon
67
52
  - notifyme
68
53
  extensions: []
54
+
69
55
  extra_rdoc_files: []
70
- files:
71
- - lib/vendor/smtp_add_tls_support.rb
72
- - lib/notifyme/check.rb
73
- - lib/notifyme/log/file.rb
56
+
57
+ files:
58
+ - lib/notifyme.rb
59
+ - lib/notifyme/log.rb
74
60
  - lib/notifyme/log/mail.rb
75
61
  - lib/notifyme/log/stdout.rb
76
- - lib/notifyme/log.rb
62
+ - lib/notifyme/log/file.rb
77
63
  - lib/notifyme/start.rb
64
+ - lib/notifyme/check.rb
78
65
  - lib/notifyme/task.rb
79
- - lib/notifyme.rb
80
- - CHANGELOG
66
+ - lib/vendor/smtp_add_tls_support.rb
81
67
  - INSTALL
82
68
  - notifyme_config.rb
83
69
  - bin/notifyme_daemon
84
70
  - bin/notifyme
85
71
  homepage: http://github.com/xianhuazhou/NotifyMe
86
72
  licenses: []
73
+
87
74
  post_install_message:
88
75
  rdoc_options: []
89
- require_paths:
76
+
77
+ require_paths:
90
78
  - lib
91
- required_ruby_version: !ruby/object:Gem::Requirement
79
+ required_ruby_version: !ruby/object:Gem::Requirement
92
80
  none: false
93
- requirements:
94
- - - ! '>='
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
86
  none: false
99
- requirements:
100
- - - ! '>='
101
- - !ruby/object:Gem::Version
102
- version: '0'
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
103
91
  requirements: []
92
+
104
93
  rubyforge_project: notifyme
105
94
  rubygems_version: 1.8.24
106
95
  signing_key:
107
96
  specification_version: 3
108
97
  summary: It's a kind of cronjob.
109
98
  test_files: []
99
+
data/CHANGELOG DELETED
@@ -1,5 +0,0 @@
1
- v0.2
2
- auto restart the service by the restart_command if it's failed
3
-
4
- v0.1-dev
5
- the first dev-version.