Created
August 10, 2016 21:13
-
-
Save mnaser/65d3dff4c751bc3e9ace288d9e080cdf to your computer and use it in GitHub Desktop.
Sensu check to ensure all Nova services are up
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
| #!/opt/sensu/embedded/bin/ruby | |
| require 'sensu-plugin/check/cli' | |
| require 'net/http' | |
| require 'json' | |
| class CheckNovaServices < Sensu::Plugin::Check::CLI | |
| option :auth_url, | |
| short: '-a AUTH-URL', | |
| long: '--auth-url AUTH-URL', | |
| default: 'http://localhost:5000' | |
| option :username, | |
| short: '-user USERNAME', | |
| long: '--username USERNAME', | |
| default: 'sensu' | |
| option :password, | |
| short: '-p PASSWORD', | |
| long: '--password PASSWORD' | |
| option :tenant, | |
| short: '-t TENANT', | |
| long: '--tenant-name TENANT', | |
| default: 'sensu' | |
| def run | |
| unknown 'No password provided' unless config[:password] | |
| # Get token from Keystone | |
| auth_uri = URI.parse(config[:auth_url]) | |
| request = Net::HTTP::Post.new('/v3/auth/tokens', initheader={'Content-Type' => 'application/json'}) | |
| request.body = { | |
| auth: { | |
| identity: { | |
| methods: ['password'], | |
| password: { | |
| user: { | |
| name: config[:username], | |
| domain: {id: 'default'}, | |
| password: config[:password] | |
| } | |
| } | |
| }, | |
| scope: { | |
| project: { | |
| name: config[:tenant], | |
| domain: {id: 'default'} | |
| } | |
| } | |
| } | |
| }.to_json | |
| response = Net::HTTP.new(auth_uri.hostname, auth_uri.port).start { |http| http.request(request) } | |
| warning "Failed to generate token from Keystone" if response.code != "201" | |
| data = JSON.parse(response.body) | |
| token = response['X-Subject-Token'] | |
| tenant_id = data['token']['project']['id'] | |
| # Make request to service | |
| uri = URI.parse('https://localhost:8774/v2/' + tenant_id + '/os-services') | |
| request = Net::HTTP::Get.new(uri, initheader={'X-Auth-Token' => token}) | |
| response = Net::HTTP.new(uri.hostname, uri.port).start { |http| http.request(request) } | |
| code = Integer(response.code) | |
| size = response.body.nil? ? '0' : response.body.size | |
| data = JSON.parse(response.body) | |
| # Go through all services, check who is down but not disabled | |
| down = "" | |
| data['services'].each do |s| | |
| down << "#{s['host']}[#{s['binary']}] " if s['state'] == 'down' and s['status'] == 'enabled' | |
| end | |
| ok "all services up" if down.empty? | |
| critical down | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment