Module: PingMon

Defined in:
lib/pingmon.rb,
lib/pingmon/config.rb,
lib/pingmon/pinger.rb,
lib/pingmon/monitor.rb,
lib/pingmon/version.rb

Defined Under Namespace

Classes: Config, ConfigFileNotFound, Monitor, Pinger

Constant Summary collapse

DEFAULT_CONFIG_FILE =
File.expand_path('~/.pingmon.yml')
@@log =
nil

Class Method Summary collapse

Class Method Details

.create_log(param) ⇒ Object

Create a log that respond to << like a logger param can be ‘stdout’, ‘stderr’, a string (then we will log to that file) or a logger (then we return it)



52
53
54
55
56
57
58
59
60
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
# File 'lib/pingmon.rb', line 52

def self.create_log param
  if param
    if param.is_a? String
      if param == 'stdout'
        stdout_logger = Class.new do
          def << obj
            STDOUT.puts obj
          end
        end
        stdout_logger.new
      elsif param == 'stderr'
        stderr_logger = Class.new do
          def << obj
            STDERR.puts obj
          end
        end
        stderr_logger.new
      else
        file_logger = Class.new do
          attr_writer :target_file

          def << obj
            File.open(@target_file, 'a') { |f| f.puts obj }
          end
        end
        logger = file_logger.new
        logger.target_file = param
        logger
      end
    else
      param
    end
  end
end

.execute_from_config(config_file = PingMon::DEFAULT_CONFIG_FILE) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pingmon.rb', line 27

def self.execute_from_config(config_file=PingMon::DEFAULT_CONFIG_FILE)
  config = PingMon::Config.new(config_file).load
  raise "wrong mode" unless %w(ping monitor).include?(config.mode)

  case config.mode
    when "ping"
      self.ping(config)
    when "monitor"
      self.monitor(config)
  end
end

.logObject



46
47
48
# File 'lib/pingmon.rb', line 46

def self.log
  @@log
end

.log=(log) ⇒ Object

value may be a logger instance or a string: ‘stdout’|‘stderr’|‘an actual filename’



42
43
44
# File 'lib/pingmon.rb', line 42

def self.log= log
  @@log = create_log log
end

.monitor(config = PingMon::Config.new) ⇒ Object



22
23
24
25
# File 'lib/pingmon.rb', line 22

def self.monitor(config=PingMon::Config.new)
  m = PingMon::Monitor.new(config)
  m.monitor
end

.ping(config = PingMon::Config.new) ⇒ Object



17
18
19
20
# File 'lib/pingmon.rb', line 17

def self.ping(config=PingMon::Config.new)
  p = PingMon::Pinger.new(config)
  p.ping
end

.versionObject



2
3
4
5
6
# File 'lib/pingmon/version.rb', line 2

def self.version
  version_path = File.dirname(__FILE__) + "/../../VERSION"
  return File.read(version_path).chomp if File.file?(version_path)
  "0.0"
end