Skip to content

Instantly share code, notes, and snippets.

@woodybrood
Created February 17, 2014 03:26
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 woodybrood/9044212 to your computer and use it in GitHub Desktop.
Save woodybrood/9044212 to your computer and use it in GitHub Desktop.
My Modem Log Script, including push notification via pushover.net. It leverages nokogiri, rushover, sqlite3 and more.
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'sqlite3'
require_relative 'push_notifier.rb'
logsPageUrl = "http://192.168.100.1/cmLogsData.htm"
def parse_row(row)
cells = []
row.xpath("td").map do |cell|
cells.push(cell.text)
end
return cells
end
def get_highest_timestamp(database)
return database.get_first_value("select timestamp from log where id = (
SELECT max(id) FROM log)")
end
def parse_timestamp(ts)
return DateTime.strptime(ts, "%b %d %Y %H:%M:%S")
end
def newer?(old_ts, new_ts)
if old_ts.nil?
return true
elsif new_ts[0,11] == "Jan 01 1970"
return false
elsif parse_timestamp(new_ts) > parse_timestamp(old_ts)
return true
else
return false
end
end
def log_reboots (url,pusher)
db = SQLite3::Database.new( "modem_log.db" )
begin
doc = Nokogiri::HTML(open(url))
doc.xpath("//table/tbody/tr[not(th)]").reverse_each do |row|
timestamp, level, code, message = parse_row(row)
if newer?(get_highest_timestamp(db), timestamp)
db.execute("insert into log (timestamp, level, code, message) values(?,?,?,?); ", timestamp, level, code, message )
if code == 'Z00.0'
pusher.push_notification("The modem rebooted at #{timestamp}.", "Modem reboot alert")
end
else
# do nothing
end
end
rescue Exception
# do nothing
end
puts "reboot count: " + db.get_first_value("select count(*) from log where code = 'Z00.0';").to_s
end
pusher = PushNotifier.new( "/pushover_config.yaml" )
log_reboots(logsPageUrl, pusher)
require 'rushover'
require 'yaml'
class PushNotifier
def initialize( rushover_config_filename)
@rushover_config = YAML.load_file(File.dirname(__FILE__) + rushover_config_filename)
@app_token = @rushover_config['app_token']
@user_key = @rushover_config['user_key']
@sound = @rushover_config['sound']
@priority = @rushover_config['priority']
@limit_to_working_hours = @rushover_config['limit_to_working_hours']
@working_hours_start = @rushover_config['working_hours_start']
@working_hours_end = @rushover_config['working_hours_end']
@weekends_off = @rushover_config['weekends_off']
@rushover_client = Rushover::Client.new(@app_token)
end
def push_notification(message, title )
push_notification_impl(message, title, Time.now)
end
def is_during_quiet_period?(current_time)
if @weekends_off & is_the_weekend?(current_time)
return true
elsif @limit_to_working_hours
current_hour = current_time.hour()
return (current_hour < @working_hours_start) || (current_hour > @working_hours_end)
else
return false
end
end
def is_the_weekend?(current_time)
return current_time.saturday? || current_time.sunday?
end
private
def push_notification_impl(message, title, current_time)
unless is_during_quiet_period?(current_time)
resp = @rushover_client.notify(@user_key, message, :priority => @priority, :title => title, :sound =>@sound)
resp.ok? # => true =
end
end
end
app_token : token
user_key : key
priority : 0
sound : magic
limit_to_working_hours: false
working_hours_start: 7
working_hours_end: 17
weekends_off: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment