Created
November 7, 2011 10:51
-
-
Save yyuu/1344658 to your computer and use it in GitHub Desktop.
a mon's monitor plugin to monitor msgpack-rpc services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'msgpack/rpc' | |
require 'optparse' | |
require 'yaml' | |
PROGRAM_NAME = File.basename($0) | |
EXIT_SUCCESS = 0 | |
EXIT_FAILURE = 1 | |
options = { | |
:port => 9090, | |
:message => :ping, | |
:arguments => [], | |
:expected => 'ok', | |
} | |
parser = OptionParser.new { |op| | |
op.on('-a ARGUMENT', '--argument ARGUMENT') { |argument| | |
options[:arguments].push(YAML.load(argument)) | |
} | |
op.on('-m MESSAGE', '--message MESSAGE') { |message| | |
options[:message] = message.to_sym() | |
} | |
op.on('-p PORT', '--port PORT', Integer) { |port| | |
if port < 1 or 65535 <= port | |
STDERR.puts("out of port range: #{port}") | |
STDERR.puts(op.help()) | |
exit(EXIT_FAILURE) | |
end | |
options[:port] = port | |
} | |
op.on('-e EXPECTED', '--expected EXPECTED') { |expected| | |
options[:expected] = YAML.load(expected) | |
} | |
} | |
hosts = parser.parse(ARGV) | |
results = hosts.map { |host| | |
begin | |
client = MessagePack::RPC::Client.new(host, options[:port]) | |
returned = client.call(options[:message], *options[:arguments]) | |
result = options[:expected] == returned | |
unless result | |
STDERR.puts("#{host}:#{options[:port]}\##{options[:message]}(#{options[:arguments].join(', ')}): " + | |
"got #{returned}, expected #{options[:expected]}") | |
end | |
result | |
rescue StandardError => error | |
STDERR.puts("#{host}:#{options[:port]}\##{options[:message]}(#{options[:arguments].join(', ')}): " + | |
"#{error}") | |
false | |
end | |
} | |
exit(results.reduce(true) { |all, result| all && result } ? EXIT_SUCCESS : EXIT_FAILURE) | |
# vim:set ft=ruby : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment