sensu-plugins/sensu-plugins-mesos

View on GitHub
bin/check-marathon.rb

Summary

Maintainability
A
25 mins
Test Coverage
#! /usr/bin/env ruby
# frozen_string_literal: false

#
#   check-marathon
#
# DESCRIPTION:
#   This plugin checks that the ping url returns 200 OK
#
# OUTPUT:
#   plain text
#
# PLATFORMS:
#   Linux
#
# DEPENDENCIES:
#   gem: sensu-plugin
#   gem: rest-client
#
# USAGE:
#   #YELLOW
#
# NOTES:
#
# LICENSE:
#   Copyright 2015, Tom Stockton (tom@stocktons.org.uk)
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.
#

require 'sensu-plugin/check/cli'
require 'rest-client'

class MarathonNodeStatus < Sensu::Plugin::Check::CLI
  option :server,
         description: 'Marathon servers, comma separated',
         short: '-s SERVER1,SERVER2,...',
         long: '--server SERVER1,SERVER2,...',
         default: 'localhost'

  option :port,
         description: 'Marathon port',
         short: '-p PORT',
         long: '--port PORT',
         required: false,
         default: '8080'

  option :protocol,
         description: 'Marathon protocol [http/https]',
         short: '-P PROTOCOL',
         long: '--protocol PROTOCOL',
         required: false,
         default: 'http'

  option :uri,
         description: 'Endpoint URI',
         short: '-u URI',
         long: '--uri URI',
         default: '/ping'

  option :timeout,
         description: 'timeout in seconds',
         short: '-t TIMEOUT',
         long: '--timeout TIMEOUT',
         proc: proc(&:to_i),
         default: 5

  def run
    servers = config[:server]
    failures = []
    servers.split(',').each do |server|
      begin
        r = RestClient::Resource.new("#{config[:protocol]}://#{server}:#{config[:port]}#{config[:uri]}", timeout: config[:timeout]).get
        if r.code != 200
          failures << "Marathon Service on #{server} is not responding"
        end
      rescue Errno::ECONNREFUSED, RestClient::ResourceNotFound, SocketError
        failures << "Marathon Service on #{server} is not responding"
      rescue RestClient::RequestTimeout
        failures << "Marathon Service on #{server} connection timed out"
      end
    end
    if failures.empty?
      ok "Marathon Service is up on #{servers}"
    else
      critical failures.join("\n")
    end
  end
end