Skip to content

Instantly share code, notes, and snippets.

@fbehrens
Created February 20, 2011 11:59
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 fbehrens/835926 to your computer and use it in GitHub Desktop.
Save fbehrens/835926 to your computer and use it in GitHub Desktop.
litte script which monitors my internetconnection
#!/usr/bin/ruby -w
# this is an example of uncommunicative code
# would be a good example for refactoring to make clear what it does
class Monitor
@@host = "heise.de"
@@tries = 4
@@results_per_line = 60
@@log = __FILE__.gsub(/rb$/,"log")
def run
last_ping = Time.now - 140
column = @@results_per_line
while true
if column >= @@results_per_line or (Time.now - last_ping > 130)
File.open( @@log, 'a') {|f|
f.write("\n") if Time.now - last_ping > 130
f.write("\n#{Time.now.strftime("%m-%d %H:%M")}")
}
column = 0
end
File.open( @@log, 'a') {|f|
f.write( ping )
}
last_ping = Time.now
column += 1
sleep 60
end
end
def ping
try = 1
online = false
while try <= @@tries and online == false
online = system("ping -c 1 #{@@host}")
try += 1
end
online ? "." : "E"
end
end
Monitor.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment