Last active
December 22, 2015 06:48
-
-
Save kvannotten/6433327 to your computer and use it in GitHub Desktop.
Due to having a very unstable connection in China, I wrote a script to alert me when my internet is down. If the internet is down, it will say so every 5 seconds until it is back up. If it is up, it will say this once and then shut up. The log messages are a delta.
This file contains hidden or 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/env ruby | |
require 'logger' | |
class SimpleDaemon | |
def self.start pid, pidfile, outfile, errfile | |
unless pid.nil? | |
raise "Fork failed" if pid == -1 | |
write pid, pidfile if kill pid, pidfile | |
exit | |
else | |
redirect outfile, errfile | |
end | |
end | |
def self.write pid, pidfile | |
File.open pidfile, "w" do |f| | |
f.write pid | |
end | |
rescue ::Exception => e | |
$stderr.puts "While writing the PID to file, unexpected #{e.class}: #{e}" | |
Process.kill "HUP", pid | |
end | |
def self.kill(pid, pidfile) | |
opid = open(pidfile).read.strip.to_i | |
Process.kill "HUP", opid | |
true | |
rescue Errno::ENOENT | |
$stdout.puts "#{pidfile} did not exist: Errno::ENOENT" | |
true | |
rescue Errno::ESRCH | |
$stdout.puts "The process #{opid} did not exist: Errno::ESRCH" | |
true | |
rescue Errno::EPERM | |
$stderr.puts "Lack of privileges to manage the process #{opid}: Errno::EPERM" | |
false | |
rescue ::Exception => e | |
$stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}" | |
false | |
end | |
def self.redirect outfile, errfile | |
$stdin.reopen '/dev/null' | |
out = File.new outfile, "a" | |
err = File.new errfile, "a" | |
$stdout.reopen out | |
$stderr.reopen err | |
$stdout.sync = $stderr.sync = true | |
end | |
end | |
$0 = "Internet Status Checker" | |
SimpleDaemon.start fork, (ARGV[0] || '/tmp/daemon.pid'), (ARGV[1] || '/tmp/daemon.stdout.log'), (ARGV[2] || '/tmp/daemon.stderr.log') | |
Signal.trap("HUP") { $stdout.puts "SIGHUP and exit"; exit } | |
Signal.trap("INT") { $stdout.puts "SIGINT and exit"; exit } | |
Signal.trap("QUIT") { $stdout.puts "SIGQUIT and exit"; exit } | |
log = Logger.new('status_internet.txt', 'daily') | |
log.level = Logger::INFO | |
log.info "Script started..." | |
ping_count = 3 | |
server = "www.baidu.com" | |
server2 = "8.8.8.8" | |
down = true | |
loop do | |
log.debug "Pinging #{server}" | |
result = `ping -qoc #{ping_count} #{server}` | |
if ($?.exitstatus == 0) then | |
log.info "Internet is up!" unless down == false | |
`say Internet up` if down == true | |
down = false | |
else | |
result = `ping -qoc #{ping_count} #{server2}` | |
if($?.exitstatus == 0) then | |
log.info "Baidu.com not reachable, probably a DNS issue" | |
`say Baidu down` | |
else | |
log.info "Internet down!" unless down == true | |
`say Internet down` | |
down = true | |
end | |
end | |
sleep 5 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment