Skip to content

Instantly share code, notes, and snippets.

@jhawthorn
Created April 7, 2018 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhawthorn/7bd68dd027a1b8e1ae2bdc56efed2414 to your computer and use it in GitHub Desktop.
Save jhawthorn/7bd68dd027a1b8e1ae2bdc56efed2414 to your computer and use it in GitHub Desktop.
# A stable ping target
# pretty much anything is more reliable than shaw
target_ip = "1.1.1.1"
# Maximum wait time in seconds.
wait = 1
interval = 10
require 'fileutils'
class BufferedLogByTime
def initialize(dir, format="%F.log")
FileUtils.mkdir_p dir
@dir = dir
@format = format
@filename = nil
@file = nil
maybe_update_filename
end
def maybe_update_filename
new_filename = Time.now.utc.strftime(@format)
if new_filename != @filename
@filename = new_filename
@file.close if @file
@file = File.open(File.join(@dir, @filename), 'w')
end
end
def log(data)
maybe_update_filename
@file.puts data
@file.flush
end
end
logger = BufferedLogByTime.new("logs/")
loop do
time_str = Time.now.utc.to_i
result = `ping #{target_ip} -W #{wait} -c 1 -q`
last_line = result.lines.last.strip
if last_line.empty?
logger.log "#{time_str},"
puts "#{time_str}\tSHAW DOWN!"
else
time = last_line.split(' = ').last.split('/').first
logger.log "#{time_str},#{time}"
puts "#{time_str}\t#{time}ms"
end
sleep interval
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment