Class: Sermont

Inherits:
Object
  • Object
show all
Includes:
Handler
Defined in:
lib/sermont.rb

Constant Summary collapse

W =
80

Instance Method Summary collapse

Methods included from Handler

#ftp, #http, #mysql, #open_port, #ping

Constructor Details

#initializeSermont

Returns a new instance of Sermont.



33
34
35
36
# File 'lib/sermont.rb', line 33

def initialize
  @raw = !(defined?(Term::ANSIColor) && self.is_a?(Term::ANSIColor))
  @can_daemon = defined?(Daemonize) && self.is_a?(Daemonize)
end

Instance Method Details

#add_to_output(out, file, last) ⇒ Object



69
70
71
72
73
# File 'lib/sermont.rb', line 69

def add_to_output(out, file, last)
  File.open(file, last ? "wb" : "ab") do |f|
    f << out
  end
end

#dotspace(str, f_indent, b_indent) ⇒ Object



104
105
106
107
108
109
# File 'lib/sermont.rb', line 104

def dotspace(str, f_indent, b_indent)
  spaces = W - 4 - str.size - f_indent - b_indent
  w = ""
  spaces.times {w << "."}
  w
end

#handle_host(servername, ip) ⇒ Object



127
128
129
# File 'lib/sermont.rb', line 127

def handle_host(servername, ip)
  host_str(servername) + host_status(self.ping(ip))
end

#handle_service(handler, ip, port) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/sermont.rb', line 147

def handle_service(handler, ip, port)
  running = begin
    self.send(handler, ip, port)
  rescue NoMethodError
    self.open_port(ip, port)
  end
  service_str(handler) + service_status(running)
end

#host_status(alive) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/sermont.rb', line 119

def host_status(alive)
  if alive
    @raw ? "Alive     \n" : bold + on_green + white + "Alive     " + clear + "\n"
  else
    @raw ? "In Trouble\n" : bold + on_red + white + "In Trouble" + clear + "\n"
  end
end

#host_str(hostname) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/sermont.rb', line 111

def host_str(hostname)
  if @raw
    "#{hostname} is " + dotspace(hostname, 0, 10)
  else
    on_yellow + black + bold + "#{hostname} is " + dotspace(hostname, 0, 10) + clear
  end
end

#load_setupObject



38
39
40
41
42
43
44
45
# File 'lib/sermont.rb', line 38

def load_setup
  config_file = File.expand_path("~/.sermont.yml")
  unless File.exists?(config_file) && @servers = YAML.load(IO.read(config_file))
    puts "Config file .sermont.yml doesn't exists or invalid."
    puts "Run  'sermont --setup' to create example config file in your home directory"
    exit
  end
end

#notification(notif) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/sermont.rb', line 89

def notification(notif)
  if @raw
    notif + whitespace(notif) + "\n"
  else
    on_black + dark + notif + whitespace(notif) + clear + "\n"
  end
end

#reportObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sermont.rb', line 75

def report
  report = notification(Time.now.to_s)
  @servers.each do |server|
    servername = server["servername"]
    ip = server["ip"]
    report << handle_host(servername, ip)
    services = server["services"]
    services.each do |k, v|
      report << handle_service(k, ip, v)
    end
  end
  report
end

#run(raw = nil, time = nil, output = nil, daemon = nil, last = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sermont.rb', line 47

def run(raw = nil, time = nil, output = nil, daemon = nil, last = nil)
  load_setup
  @raw = @raw || raw
  unless time
    output ? add_to_output(report, output, last) : print(report)
  else
    if daemon && @can_daemon && output
      pwd = Dir.pwd
      daemonize
      Dir.chdir pwd
    end
    loop do
      output ? add_to_output(report, output, last) : print(report)
      begin
        sleep time.to_i
      rescue Exception
        exit
      end
    end
  end
end

#service_status(running) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/sermont.rb', line 139

def service_status(running)
  if running
    @raw ? "Running   \n" : bold + on_green + white + "Running   " + clear + "\n"
  else
    @raw ? "Stopped   \n" : bold + on_red + white + "Stopped   " + clear + "\n"
  end
end

#service_str(service) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/sermont.rb', line 131

def service_str(service)
  if @raw
    "     #{service} is " + dotspace(service, 5, 10)
  else
    on_blue + yellow + bold + "     #{service} is " + dotspace(service, 5, 10) + clear
  end
end

#whitespace(str) ⇒ Object



97
98
99
100
101
102
# File 'lib/sermont.rb', line 97

def whitespace(str)
  spaces = W - str.size
  w = ""
  spaces.times {w << " "}
  w
end