Global network notifications
Having looked at ruby-growl earlier today, but needing network-wide notifications rather than to individual machines, I hacked the following code together in Ruby using mDNSBrowse (from the Howl project - sadly a dead project replaced more recently by Avahi) to discover and resolve Growl clients on the network:
#!/usr/bin/env ruby
require 'ruby-growl'
require 'optparse'
mdns_browse_cmd = "mDNSBrowse _growl._tcp"
growl_password = "********"
# Kill zombie tasks using ps and kill
# notify() is only called when mDNSBrowse is killed
# Messy, I know, but it works
def kill_tasks(cmd)
tasks = `ps aux | grep "#{cmd}$"`
tasks.each_line do |taskrow|
task = taskrow.split(" ")
pid = task[1]
system("kill #{pid}")
end
end
# The notification method
def notify(host, password, title, message)
type = "ruby-growl Notification"
if (/^([0-9]{1,3}\.){3}[0-9]{1,3}$/.match(host))
g = Growl.new host, "ruby-growl", type, nil, password
g.notify type, title, message
end
end
# Read options
title = "Default title"
message = "Default description"
opts = OptionParser.new do |opts|
opts.banner = "Usage: cmd [options]"
opts.on("-t" "--title [TITLE]", "Title") do |val|
title = val
end
opts.on("-m" "--message [MESSAGE]", "Message") do |val|
message = val
end
end.parse!
# Begin execution here
pid = fork {
p = IO.popen("#{mdns_browse_cmd} 2>&1")
p.readlines.each do |line|
if (line.slice(0,13)=="resolve reply")
data = line.split(" ")
ip = data[(data.length-2)]
notify(ip, growl_password, title, message)
end
end
p.close
}
kill_tasks mdns_browse_cmd I've called mine 'globalgrowl' and stuck it in /usr/bin (chmod 755). Usage example: globalgrowl -t 'My title' -m 'My Message' Works nicely (and entirely functionally) when called in the init scripts on our Ubuntu office server. A future improvement could be to implement this with all options by copying ruby-growl's script and merging it with the mDNS discovery code from my script above, making all of the functionality of growl available to global notifications.
Post new comment