notifyme 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ module NotifyMe
2
2
  class Check
3
3
  class << self
4
4
  def process(args = {})
5
- unless %x{ps aux | grep #{args[:name]} | grep -v grep}.strip == ''
5
+ if %x{ps aux | grep #{args[:name]} | grep -v grep}.strip == ''
6
6
  raise "Process #{args[:name]} is not running!"
7
7
  end
8
8
  end
@@ -1,6 +1,7 @@
1
1
  module NotifyMe
2
2
 
3
- VERSION = '1.0.1'
3
+ VERSION = '1.0.2'
4
+ DEFAULT_CONFIG_FILE = "#{ENV['HOME']}/.notifyme/notifyme_config.rb"
4
5
 
5
6
  autoload :Task, 'notifyme/task'
6
7
  autoload :Log, 'notifyme/log'
@@ -12,15 +13,15 @@ module NotifyMe
12
13
  # log
13
14
  @@log_args = [:stdout]
14
15
  @@log_format = :text
15
- @@log_directory = "/tmp"
16
16
 
17
17
  # tasks list
18
18
  @@tasks = []
19
19
 
20
20
  def run!
21
- puts 'NotifyMe v' + NotifyMe::VERSION
21
+ puts 'NotifyMe v' + VERSION
22
+ @@config_file = ARGV[0] || DEFAULT_CONFIG_FILE
22
23
  load_custom_check_functions
23
- start = new(ARGV[0])
24
+ start = new(@@config_file)
24
25
  load_custom_check_tasks
25
26
  start.run
26
27
  end
@@ -30,7 +31,7 @@ module NotifyMe
30
31
  if File.exists? file
31
32
  load file
32
33
  puts "Loaded custom check functions from #{file}."
33
- end
34
+ end
34
35
  end
35
36
 
36
37
  def load_custom_check_tasks
@@ -48,7 +49,47 @@ module NotifyMe
48
49
  end
49
50
 
50
51
  def custom_notifyme_dir
51
- File.join(ENV['HOME'], ".notifyme")
52
+ notifyme_dir = File.join(ENV['HOME'], ".notifyme")
53
+ return notifyme_dir if File.directory?(notifyme_dir)
54
+
55
+ setup notifyme_dir
56
+ end
57
+
58
+ def setup(notifyme_dir)
59
+ FileUtils.mkdir_p notifyme_dir
60
+ FileUtils.mkdir_p File.join(notifyme_dir, 'check')
61
+
62
+ if @@config_file == DEFAULT_CONFIG_FILE
63
+ default_config_file = File.expand_path(File.dirname(__FILE__) + '/../../notifyme_config.rb')
64
+ basic_config = File.read(default_config_file).split('# add some tasks').first
65
+ File.open("#{notifyme_dir}/notifyme_config.rb", "w") do |f|
66
+ f.write basic_config + "\nend"
67
+ end
68
+ end
69
+
70
+ File.open("#{notifyme_dir}/check.rb", "w") do |f|
71
+ f.write <<-EOF
72
+ class NotifyMe::Check
73
+ class << self
74
+ # def something(args = {})
75
+ # return "Something went wrong of your system" if not true
76
+ # end
77
+ end
78
+ end
79
+ EOF
80
+ end
81
+
82
+ File.open("#{notifyme_dir}/check/mytask.rb", "w") do |f|
83
+ f.write <<-EOF
84
+ def check_mytask(t)
85
+ # t.sleep_time = 5
86
+ # t.command = lambda { check :something }
87
+ # t.restart_command = lambda { `/etc/init.d/something restart` }
88
+ end
89
+ EOF
90
+ end
91
+
92
+ notifyme_dir
52
93
  end
53
94
 
54
95
  def config(&block)
@@ -86,8 +127,7 @@ module NotifyMe
86
127
  end
87
128
 
88
129
  def log_directory(directory)
89
- FileUtils.mkdir_p directory unless File.directory? directory
90
- @@log_directory = directory
130
+ $stderr.puts "Warn: the \"log_direcotry\" has been deprecated. You can find the error messages in the /var/log/ directory."
91
131
  end
92
132
  end
93
133
 
@@ -96,6 +136,7 @@ module NotifyMe
96
136
  @mutex = Mutex.new
97
137
  @@tasks.each do |task|
98
138
  tasks_thread << Thread.new(task) do
139
+ next if task.name.nil? || task.sleep_time.nil?
99
140
  loop do
100
141
  Thread.current[:name] = task.name
101
142
  sleep task.sleep_time
@@ -139,11 +180,7 @@ module NotifyMe
139
180
  end
140
181
 
141
182
  def log_error(task_name, command, msg)
142
- return if @@log_directory.nil?
143
- log_file = File.join(@@log_directory, '/', "#{task_name}.log")
144
- File.open(log_file, 'a') do |f|
145
- f.write "[#{Time.new.to_s}] #{command} : #{msg}\n"
146
- end
183
+ Syslog.log Syslog::LOG_ERR, "[#{Time.new.to_s}] #{task_name} # #{command} : #{msg}"
147
184
  end
148
185
 
149
186
  def initialize(config_file)
data/lib/notifyme.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
2
  require 'thread'
3
3
  require 'fileutils'
4
+ require 'syslog'
4
5
  require 'notifyme/start'
data/notifyme_config.rb CHANGED
@@ -28,8 +28,6 @@ NotifyMe::Start.config do
28
28
  # :csv, :text, :xml, :json
29
29
  log_format :text
30
30
 
31
- log_directory '/tmp/notifyme'
32
-
33
31
  # add some tasks
34
32
 
35
33
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notifyme
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-10 00:00:00.000000000 Z
12
+ date: 2012-12-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: daemons