Class: NotifyMe::Start

Inherits:
Object
  • Object
show all
Defined in:
lib/notifyme/start.rb

Constant Summary collapse

@@log_args =

log

[:stdout]
@@log_format =
:text
@@nagios_directory =

nagios

"/usr/local/nagios/libexec"
@@tasks =

tasks list

[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config(&block) ⇒ Object



98
99
100
# File 'lib/notifyme/start.rb', line 98

def config(&block)
  class_eval &block
end

.custom_notifyme_dirObject



54
55
56
57
58
59
# File 'lib/notifyme/start.rb', line 54

def custom_notifyme_dir
  notifyme_dir = File.join(ENV['HOME'], ".notifyme")
  return notifyme_dir if File.directory?(notifyme_dir)

  setup notifyme_dir
end

.load_custom_check_functionsObject



32
33
34
35
36
37
38
# File 'lib/notifyme/start.rb', line 32

def load_custom_check_functions
  file = File.join(custom_notifyme_dir, "check.rb")
  if File.exists? file
    load file
    puts "Loaded custom check functions from #{file}."
  end
end

.load_custom_check_tasksObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/notifyme/start.rb', line 40

def load_custom_check_tasks
  task_files = File.join(custom_notifyme_dir, 'check', '*.rb')
  Dir[task_files].each do |task_file|
    load task_file
    task_name = File.basename(task_file.sub('.rb', ''))
    task_func = "check_#{task_name}"
    if defined? task_func
      task task_func do |t|
        method(task_func).call t
      end
    end
  end
end

.run!Object



23
24
25
26
27
28
29
30
# File 'lib/notifyme/start.rb', line 23

def run!
  puts 'NotifyMe v' + VERSION
  @@config_file = ARGV[0] || DEFAULT_CONFIG_FILE 
  load_custom_check_functions
  start = new(@@config_file)
  load_custom_check_tasks
  start.run
end

.setup(notifyme_dir) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/notifyme/start.rb', line 61

def setup(notifyme_dir)
  FileUtils.mkdir_p notifyme_dir
  FileUtils.mkdir_p File.join(notifyme_dir, 'check')

  if @@config_file == DEFAULT_CONFIG_FILE 
    default_config_file = File.expand_path(File.dirname(__FILE__) + '/../../notifyme_config.rb')
    basic_config = File.read(default_config_file).split('# add some tasks').first
    File.open("#{notifyme_dir}/notifyme_config.rb", "w") do |f|
      f.write basic_config + "\nend"
    end
  end

  File.open("#{notifyme_dir}/check.rb", "w") do |f|
    f.write <<-EOF
class NotifyMe::Check
  class << self
    # def something(args = {}) 
    #  return "Something went wrong of your system" if not true 
    # end 
  end 
end
    EOF
  end

  File.open("#{notifyme_dir}/check/mytask.rb", "w") do |f|
    f.write <<-EOF
def check_mytask(t)
  # t.sleep_time = 5
  # t.command = lambda { check :something }
  # t.restart_command = lambda { `/etc/init.d/something restart` }
end
    EOF
  end

  notifyme_dir
end

Instance Method Details

#runObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/notifyme/start.rb', line 155

def run
  tasks_thread = []
  @mutex = Mutex.new
  @@tasks.each do |task|
    tasks_thread << Thread.new(task) do
      next if task.name.nil? || task.sleep_time.nil?
      loop do
        Thread.current[:name] = task.name
        sleep task.sleep_time
        run_task(task) if task.command.respond_to? :call
      end
    end
  end

  tasks_thread.each do |t| t.join end
end