Skip to content

Instantly share code, notes, and snippets.

@yyuu
Created July 14, 2011 10:19
Show Gist options
  • Save yyuu/1082227 to your computer and use it in GitHub Desktop.
Save yyuu/1082227 to your computer and use it in GitHub Desktop.
irc alert script for mon
#!/usr/bin/env ruby
require 'logger'
require 'net/irc'
require 'optparse'
require 'thread'
class IRCNotifier < Net::IRC::Client
def initialize(host, port, opts={})
super
@channel = opts[:channel]
@queue = Queue.new
end
def on_rpl_welcome(m)
join()
end
def on_join(m)
send_it()
end
def on_disconnect()
exit(0)
end
def send_it()
loop do
begin
message = @queue.deq(true)
rescue ThreadError => error
break
end
privmsg(message)
end
quit("Bye")
end
def puts(message)
@queue.enq(message)
end
def join()
post JOIN, @channel
end
def privmsg(message)
post PRIVMSG, @channel, message
end
def quit(message=nil)
post QUIT, message
end
end
if $0 == __FILE__
options = {
:service => '',
:group => '',
:hosts => [],
:tmnow => 0,
:upalert => false,
:trap => false,
:traptimeout => false,
:join => false,
:channel => '#mon',
:server => 'localhost',
:user => nil,
:nick => 'mon',
:detail => false,
:nick_tries => 5,
:port => 6667,
:password => nil,
:logger => Logger.new(STDERR).tap { |logger| logger.level = Logger::ERROR }
}
opt_parser = OptionParser.new
opt_parser.on('-s MANDATORY', String, 'service') { |value|
options[:service] = value
}
opt_parser.on('-g MANDATORY', String, 'group') { |value|
options[:group] = value
}
opt_parser.on('-h MANDATORY', Array, 'hosts') { |value|
options[:hosts] = value
}
opt_parser.on('-t MANDATORY', Integer, 'tmnow') { |value|
options[:tmnow] = value
}
opt_parser.on('-u', FalseClass, '(if upalert)') { |value|
options[:upalert] = value
}
opt_parser.on('-T', FalseClass, '(if trap)') { |value| # not implemented
options[:trap] = value
}
opt_parser.on('-O', FalseClass, '(if traptimeout)') { |value| # not implemented
options[:traptimeout] = value
}
opt_parser.on('-j', FalseClass, 'join the channel before doing PRIVMSG') { |value| # not implemented
options[:join] = value
}
opt_parser.on('-c MANDATORY', String, 'name of the channel (without leading #)') { |value|
value = '#' + value unless /\A#/ =~ value
options[:channel] = value
}
opt_parser.on('-S MANDATORY', String, 'irc server') { |value|
options[:server] = value
}
opt_parser.on('-U MANDATORY', String, 'user for irc server') { |value|
options[:user] = value
}
opt_parser.on('-n MANDATORY', String, 'nick') { |value|
options[:nick] = value
}
opt_parser.on('-d', FalseClass, 'detail') { |value|
options[:detail] = value
}
opt_parser.on('-N MANDATORY', Integer, 'nick_tries') { |value| # not implemented
options[:nick_tries] = value
}
opt_parser.on('-p MANDATORY', Integer, 'port') { |value|
options[:port] = value
}
opt_parser.on('-P MANDATORY', String, 'password') { |value|
options[:pass] = value
}
args = opt_parser.parse(ARGV)
options[:user] ||= options[:nick]
if options[:upalert]
alert = "UPALERT"
else
alert = "ALERT"
end
begin
summary = STDIN.gets()
raise(IOError) if summary.nil? or summary.length == 0
details = STDIN.read()
rescue IOError => error
summary = 'UNKNOWN'
details = ''
end
begin
host = options[:server]
port = options[:port]
opts = [ :channel, :user, :nick, :pass, :logger, ].reduce({}) { |o, k| o[k]=options[k]; o }
irc = IRCNotifier.new(host, port, opts)
rescue Net::IRC::IRCException => error
STDERR.puts("#{$0}: #{error.inspect}")
exit(1)
end
irc.puts("%s %s (%s/%s): %s" % [alert, Time.at(options[:tmnow]), options[:service], options[:group], summary])
if options[:detail]
details.each do |detail|
irc.puts("%s (%s/%s): %s" % [message, Time.at(options[:tmnow]), options[:service], options[:group], detail])
end
end
irc.start()
end
# vim:set ft=ruby :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment