diff --git a/uptime-notifier.conf b/uptime-notifier.conf new file mode 100644 index 0000000..baed1bd --- /dev/null +++ b/uptime-notifier.conf @@ -0,0 +1,5 @@ +http://this-website-should-be-down.com +# Comments are allowed +# Also, if you forget the protocol, http:// will be added +google.com +http://another-website-that-should-be-down.com diff --git a/uptime-notifier.rb b/uptime-notifier.rb index 6142022..9a15a3c 100644 --- a/uptime-notifier.rb +++ b/uptime-notifier.rb @@ -1,29 +1,50 @@ -#!/usr/bin/ruby +#!/usr/bin/env ruby +## # Uptime Notifier # -# Super simplistic website monitoring. Crontab is up and enjoy! +# Super simplistic website monitoring. Crontab it up and enjoy! # -# Usage: ruby uptime-notifier.rb +# Note: Conf file should contain one website per line # -# p.s. This was just a quick Thanksgiving proof of concept, I do plan on -# expanding this and giving is some sick configuration options. +# Another Note: Requires terminal-notifier: +# https://github.com/alloy/terminal-notifier # -# Requires terminal-notifier: -# https://github.com/alloy/terminal-notifier +# @author Josh Sherman +# @link https://github.com/joshtronic/uptime-notifier +# @usage ruby uptime-notifier.rb /path/to/uptime-notifier.conf require 'net/http' -url = 'http://this-website-should-be-down.com' - -resource = Net::HTTP.get_response(URI.parse(url.to_s)) - -unless (resource.code =~ /2|3\d{2}/) +unless ARGV[0].nil? then - `terminal-notifier \ - -title "Uptime Notifier" \ - -message "OH NOES, it looks like #{URI.parse(url).host} is down!!~!" \ - -open #{url} \ - -group Uptime Notifier \ - -remove Uptime Notifier` + conf_file = ARGV[0] +else + conf_file = File.expand_path(File.dirname(__FILE__)) + '/uptime-notifier.conf' +end + +if File.exists?(conf_file) +then + File.open(conf_file).each { |url| + url.chomp! + + next if (url[0..0] == '#' || url.empty?) + + url.insert(0, 'http://') unless(url.match(/^http\:\/\//)) + + resource = Net::HTTP.get_response(URI.parse(url)) + + unless (resource.code =~ /2|3\d{2}/) + then + `terminal-notifier \ + -title "Uptime Notifier" \ + -subtitle "Site Down!" \ + -message "#{URI.parse(url).host}" \ + -open "http://downforeveryoneorjustme.com/#{URI.parse(url).host}" \ + -group "Uptime Notifier #{url}" \ + -remove "Uptime Notifier #{url}"` + end + } +else + puts 'Unable to load configuration.' end