class Downtime::DowntimeCheck

Attributes

host[RW]
ip[RW]
log_file[RW]

Public Class Methods

new() click to toggle source
# File lib/downtime.rb, line 10
def initialize
  @ip = "8.8.8.8"
  @host = "http://siebenlinden.de"
  @log_file_dig = "downtime_dig.log"
  @log_file_wget = "downtime_wget.log"
end

Public Instance Methods

perform() click to toggle source
# File lib/downtime.rb, line 17
def perform
  ensure_logfiles
  timestamp!
  check_and_update_files
end

Private Instance Methods

append_to_logfiles(text) click to toggle source
# File lib/downtime.rb, line 88
def append_to_logfiles text
  File.open(@log_file_wget, 'a') do |f|
    f.puts text
  end
  File.open(@log_file_dig, 'a') do |f|
    f.puts text
  end
end
check_and_update_file(log_file, &check_mthd) click to toggle source
# File lib/downtime.rb, line 30
def check_and_update_file(log_file, &check_mthd)
  lines = File.readlines log_file
  was_down = lines[-1] =~ /down/
  up = check_mthd.call
  minutes = 0
  if lines.length > 1
    first_timestamp = lines[-1][/^[0-9-]*/]
    minutes = (@timestamp - Timestamp.from_s(first_timestamp))
  end
  if was_down || lines.length <= 1
    if up
      # "went up"
      lines << "! went up after #{minutes} minutes of downtime."
      lines << "#{@timestamp} up till #{@timestamp}"
    else
      # "stayed down"
      # Modify last line.
      lines[-1].gsub!(/till.*/, "till #{@timestamp} (#{minutes} minutes)")
    end
  else
    # was up before
    if up
      # "stayed up."
      # Modify last line.
      lines[-1].gsub!(/till.*/, "till #{@timestamp} (#{minutes} minutes)")
    else
      # "went down."
      lines << "! went down after #{minutes} minutes of uptime."
      lines << "#{@timestamp} down till #{@timestamp}"
    end
  end
  File.open(log_file, 'w') do |f|
    f.puts lines
  end
  up
end
check_and_update_files() click to toggle source
# File lib/downtime.rb, line 25
def check_and_update_files
  check_and_update_file(@log_file_dig, &method(:is_up_dig?))
  check_and_update_file(@log_file_wget, &method(:is_up_wget?))
end
ensure_logfiles() click to toggle source
# File lib/downtime.rb, line 79
def ensure_logfiles
  return if(File.exist?(@log_file_dig) && File.exist?(@log_file_wget))
  append_to_logfiles "# This file is created by the downtime #{Downtime::VERSION} ruby gem."
end
is_up_dig?(ip=nil) click to toggle source
# File lib/downtime.rb, line 67
def is_up_dig? ip=nil
  ip = @ip if ip.nil?
  dig = `dig +time=1 +tries=1 #{ip}`
  dig.lines.find {|l| l =~ /time.*ms/}
end
is_up_wget?(host=nil) click to toggle source
# File lib/downtime.rb, line 73
def is_up_wget? host=nil
  host = @host if host.nil?
  wget = `wget -q -t 1 --timeout 1 --spider #{host}`
  return $?.exitstatus
end
timestamp!() click to toggle source
# File lib/downtime.rb, line 84
def timestamp!
  @timestamp = Timestamp.new
end