Created
February 20, 2011 11:59
-
-
Save fbehrens/835926 to your computer and use it in GitHub Desktop.
litte script which monitors my internetconnection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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