Skip to content

Instantly share code, notes, and snippets.

@r4um
Created April 18, 2012 13:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r4um/2413542 to your computer and use it in GitHub Desktop.
Save r4um/2413542 to your computer and use it in GitHub Desktop.
Enable/disable nagios host and or service notifications
#!/usr/bin/ruby
#
# Enable/disable host and or service notifications
#
require 'net/http'
require 'optparse'
require 'pp'
require 'socket'
require 'uri'
class NagiosCTL
# see include/common.h in nagios source
CMD_ENABLE_SVC_NOTIFICATIONS=22
CMD_DISABLE_SVC_NOTIFICATIONS=23
CMD_ENABLE_HOST_SVC_NOTIFICATIONS=28
CMD_DISABLE_HOST_SVC_NOTIFICATIONS=29
CMD_ENABLE_HOST_NOTIFICATIONS=24
CMD_DISABLE_HOST_NOTIFICATIONS=25
def initialize(host, mnode, user, pass)
@mnode = mnode
@user = user
@pass = pass
@host = host
@nagurl = URI.parse("http://#{@mnode}/nagios/cgi-bin/cmd.cgi")
end
def reqcmd(cmd, service)
params = {
'cmd_typ' => cmd,
'cmd_mod' => 2,
'host' => @host,
'btnSubmit' => 'Commit'
}
params['service'] = service if service
req = Net::HTTP::Post.new(@nagurl.path)
req.basic_auth @user, @pass if @user
req.set_form_data(params)
res = Net::HTTP.new(@nagurl.host, @nagurl.port)
#res.set_debug_output $stderr
resp = res.start { |http| http.request(req) }
return true if resp.body =~ /successful/
return false
end
def do_svc(action, svc=nil)
cmd = nil
case action
when :enable
cmd = CMD_ENABLE_HOST_SVC_NOTIFICATIONS
cmd = CMD_ENABLE_SVC_NOTIFICATIONS if svc
when :disable
cmd = CMD_DISABLE_HOST_SVC_NOTIFICATIONS
cmd = CMD_DISABLE_SVC_NOTIFICATIONS if svc
end
return reqcmd(cmd, svc)
end
def do_host(action)
cmd = nil
case action
when :enable
cmd = CMD_ENABLE_HOST_NOTIFICATIONS
when :disable
cmd = CMD_DISABLE_HOST_NOTIFICATIONS
end
return reqcmd(cmd, nil)
end
end
class Options
def self.parse(args)
options = {}
options[:user] = nil
options[:pass] = nil
options[:svc] = []
options[:node] = Socket.gethostname
options[:host_also] = false
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options]"
opts.separator ""
opts.on("-e", "--enable [SVC]", "Enable service notification SVC, "\
"if missing enable host svc notifications") do |s|
options[:action] = :enable
options[:svc] << s if s
end
opts.on("-d", "--disable [SVC]", "Disable service notification SVC, "\
"if missing disable host svc notifications") do |s|
options[:action] = :disable
options[:svc] << s if s
end
opts.on("-n", "--node [NODE]", "Disable for NODE"\
", default this node") do |n|
options[:node] = n
end
opts.on("-m", "--mnode [MNODE:PORT]", "Nagios mnode) do |m|
options[:mnode] = m
end
opts.on("-c", "--creds USER:PASS", "Credentials for HTTP auth") do |c|
options[:user], options[:pass] = c.split(/:/)
end
opts.on_tail("--host-also", "enable/disable host notifications also") do
options[:host_also] = true
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
opts.parse!(args)
raise OptionParser::MissingArgument, "No action given -e or -d"\
if not options[:action]
raise OptionParser::MissingArgument, "No mnode given -m"\
if not options[:mnode]
options
end
end
if $0 == __FILE__
begin
opts = Options.parse(ARGV)
action = opts[:action]
mnode = opts[:mnode]
node = opts[:node]
svcs = opts[:svc]
user = opts[:user]
pass = opts[:pass]
nctl = NagiosCTL.new(node, mnode, user, pass)
puts "Executing actions for #{node} on nagios endpoint #{mnode}"
if svcs.length != 0
svcs.each do |svc|
print "#{action.to_s} notification for service #{svc}... "
$stdout.flush
ret = nctl.do_svc(action, svc)
puts ( ret == true ? "success" : "failed" )
end
else
print "#{action.to_s} notification for all services... "
$stdout.flush
ret = nctl.do_svc(action)
puts ( ret == true ? "success" : "failed" )
end
if opts[:host_also]
print "#{action.to_s} notification for host... "
$stdout.flush
ret = nctl.do_host(action)
puts ( ret == true ? "success" : "failed" )
end
rescue Exception => e
puts "Exception #{e.class} #{e.message}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment