Created
January 7, 2015 07:13
-
-
Save halkeye/9d79687b10f08c3820d0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| # | |
| # check-supervisor-socket | |
| # | |
| # | |
| # DESCRIPTION: | |
| # Check that all supervisor processes are running using its UNIX domain socket. See unix_http_server section in | |
| # http://supervisord.org/configuration.html. | |
| # | |
| # OUTPUT: | |
| # Plain text, 'All processes running' or eg. 'redis-server not running: FATAL' | |
| # | |
| # PLATFORMS: | |
| # All | |
| # | |
| # DEPENDENCIES: | |
| # gem: sensu-plugin | |
| # | |
| # USAGE: | |
| # check-supervisor-socket.rb | |
| # | |
| # CHANGES: | |
| # Switch to using XMLRPC::Client instead of requiring libxml - Gavin Mogan, gavin@gavinmogan.com | |
| # | |
| # LICENSE: | |
| # Mathias Bogaert, mathias.bogaert@gmail.com | |
| # Released under the same terms as Sensu (the MIT license); see LICENSE | |
| # for details. | |
| require 'rubygems' if RUBY_VERSION < '1.9' | |
| require 'sensu-plugin/check/cli' | |
| require 'net/http' | |
| require 'socket' | |
| require 'xmlrpc/client' | |
| class ClientSocket < XMLRPC::Client | |
| def initialize(socket, username = nil, password = nil) | |
| @super = Net::BufferedIO.new(UNIXSocket.new(socket)) | |
| @http = Net::HTTP::Post.new('/RPC2') | |
| @http.content_type = 'text/xml' | |
| @http.basic_auth username, password if username | |
| end | |
| def do_rpc(request, async = false) | |
| @http.body = request | |
| @http.exec(@super, '1.1', '/RPC2').inspect | |
| response = nil | |
| # wait for and parse the http response | |
| loop do | |
| response = Net::HTTPResponse.read_new(@super) | |
| break unless response.is_a?(Net::HTTPContinue) | |
| end | |
| response.reading_body(@super, @http.response_body_permitted?) {} | |
| return response.body | |
| end | |
| end | |
| class CheckSupervisorSocket < Sensu::Plugin::Check::CLI | |
| option :socket, | |
| description: 'Supervisor UNIX domain socket', | |
| short: '-s SOCKET', | |
| long: '--socket SOCKET', | |
| default: '/var/run/supervisor.sock' | |
| option :username, | |
| description: 'Supervisor UNIX domain socket username', | |
| short: '-u USERNAME', | |
| long: '--username USERNAME' | |
| option :password, | |
| description: 'Supervisor UNIX domain socket password', | |
| short: '-p PASSWORD', | |
| long: '--password PASSWORD' | |
| option :critical, | |
| description: 'Supervisor states to consider critical', | |
| short: '-c STATE[,STATE...]', | |
| long: '--critical STATE[,STATE...]', | |
| proc: proc { |v| v.upcase.split(',') }, | |
| default: ['FATAL'] | |
| option :help, | |
| description: 'Show this message', | |
| short: '-h', | |
| long: '--help' | |
| def run | |
| if config[:help] | |
| puts opt_parser | |
| exit | |
| end | |
| @xmlrpc_client = ClientSocket.new(config[:socket], config[:username], config[:password]) | |
| @response = @xmlrpc_client.call("supervisor.getAllProcessInfo") | |
| @response.each do |process| | |
| critical "#{process[:name]} not running: #{process[:statename].upcase}" if config[:critical].include?(process[:statename]) | |
| end | |
| ok 'All processes running' | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment