From a18066d2f4c8fead2221645a7a0e680c407aba58 Mon Sep 17 00:00:00 2001 From: Pete Fritchman Date: Tue, 6 Mar 2012 12:15:38 -0500 Subject: [PATCH] add dynect_gslb_manage --- README.md | 45 ++++++++++++++++++++++ bin/dynect_gslb_manage | 84 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100755 bin/dynect_gslb_manage diff --git a/README.md b/README.md index f3352e9..5970c6a 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ monitor and manage the service. ## Nagios plugin: check_dynect_gslb_region +Checks the health-check status of the addresses in a GSLB region's pool. + Usage: Options: @@ -24,3 +26,46 @@ Example output: $ check_dynect_gslb_region -c Customer -u user -p pass123 -z example.org -r bar.example.org CRITICAL: GSLB addresses unhealthy: 1.2.3.4=down + +## Utility: dynect_gslb_manage + +Drains/undrains a specific address in a GSLB region's pool. + +Usage: + + Usage: dynect_gslb_manage [options] command [command opts] + + Possible sub-commands: + + list + List all GSLB IPs + + drain + Set the serving mode to "no" for a GSLB address, identified by IP + address or label. + + undrain + Set the serving mode to "obey" for a GSLB address, identified by IP + address or label. + + Main options: + + --customer, -c : Dynect customer name + --user, -u : Dynect user name + --pass, -p : Dynect password + --zone, -z : Zone name + --record, -r : GSLB record name + --region, -R : GSLB region (default: global) + --help, -h: Show this message + +Example output: + + $ dynect_gslb_manage -c Customer -u user -p pass123 -z example.org -r foo.example.org list + foo-backend-1 | 1.2.3.4 | status=up | weight=1 | serve_mode=obey + foo-backend-2 | 1.2.3.5 | status=up | weight=1 | serve_mode=obey + + $ dynect_gslb_manage -c Customer -u user -p pass123 -z example.org -r foo.example.org drain foo-backend-1 + 1.2.3.4 serve_mode changed from obey to no + + $ dynect_gslb_manage -c Customer -u user -p pass123 -z example.org -r foo.example.org undrain foo-backend-1 + 1.2.3.4 serve_mode changed from no to obey diff --git a/bin/dynect_gslb_manage b/bin/dynect_gslb_manage new file mode 100755 index 0000000..a0c4b72 --- /dev/null +++ b/bin/dynect_gslb_manage @@ -0,0 +1,84 @@ +#!/usr/bin/env ruby +# Nagios plugin to monitor the status of IP addresses in a GSLB region. + +require "rubygems" +require "bundler/setup" + +require "dynect_rest" +require "trollop" + +progname = File.basename($0) + +opts = Trollop::options do + banner <<-EOF +Usage: #{progname} [options] command [command opts] + +Possible sub-commands: + + list + List all GSLB IPs + + drain + Set the serving mode to "no" for a GSLB address, identified by IP + address or label. + +Main options: +EOF + banner "" + + opt :customer, "Dynect customer name", :short => "-c", :type => :string, + :required => true + opt :user, "Dynect user name", :short => "-u", :type => :string, + :required => true + opt :pass, "Dynect password", :short => "-p", :type => :string, + :required => true + opt :zone, "Zone name", :short => "-z", :type => :string, + :required => true + opt :record, "GSLB record name", :short => "-r", :type => :string, + :required => true + opt :region, "GSLB region", :short => "-R", :type => :string, + :default => "global" + +end + +dyn = DynectRest.new(opts[:customer], opts[:user], opts[:pass], opts[:zone]) + +command = ARGV.shift + +pool = dyn.get("GSLBRegionPoolEntry/#{opts[:zone]}/#{opts[:record]}/#{opts[:region]}/").collect { |p| p.sub("/REST/", "") } + +# Takes an IP or label. +def find_address(dyn, pool, match) + pool.each do |path| + entry = dyn.get(path) + if entry["label"] == match or entry["address"] == match + return path, entry + end + end + + raise "can't find GSLB entry with address or label matching #{match}" +end + +case command +when "list" + pool.each do |path| + entry = dyn.get(path) + puts "#{entry["label"]} | #{entry["address"]} | " \ + "status=#{entry["status"]} | weight=#{entry["weight"]} | " \ + "serve_mode=#{entry["serve_mode"]}" + end +when "drain", "undrain" + match = ARGV.shift + if !match + Trollop::die "must specify an address to drain" + end + path, entry = find_address(dyn, pool, match) + serve_mode = (command == "drain") ? "no" : "obey" + res = dyn.put(path, {"serve_mode" => serve_mode}) + puts "#{entry["address"]} serve_mode changed from #{entry["serve_mode"]} " \ + "to #{res["serve_mode"]}" +when nil + Trollop::die "must specify a command" +else + Trollop::die "invalid command #{command}" +end