Skip to content

Instantly share code, notes, and snippets.

@majormoses
Created October 12, 2017 23:00
Show Gist options
  • Save majormoses/c110d93c20e810b56bcefcdc7e074bb8 to your computer and use it in GitHub Desktop.
Save majormoses/c110d93c20e810b56bcefcdc7e074bb8 to your computer and use it in GitHub Desktop.

Revisions

  1. majormoses created this gist Oct 12, 2017.
    84 changes: 84 additions & 0 deletions check-http-healthcheck.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    #! /usr/bin/env ruby
    #
    # check-cc-heap-limit
    #
    # DESCRIPTION:
    # A script that checks an http endpoint, parses json, and alerts when healthchecks fail
    #
    # OUTPUT:
    # plain-text
    #
    # PLATFORMS:
    # Linux
    #
    # DEPENDENCIES:
    # gem: sensu-plugin
    # gem: httparty
    #
    # USAGE:
    #
    # NOTES:
    #
    #
    # LICENSE:
    # Copyright (c) 2017, Huy Nguyen <[email protected]>

    require 'sensu-plugin/check/cli'
    require 'httparty'
    require 'json'

    class HealthCheck < Sensu::Plugin::Check::CLI
    option :host,
    short: '-h HOSTNAME',
    long: '--hostname HOSTNAME',
    description: 'Host to connect to query for healthcheck',
    default: 'localhost'

    option :port,
    short: '-p PORT',
    long: '--port PORT',
    description: 'Port to connect to query for healthcheck',
    default: '8081'

    option :timeout,
    short: '-t HTTP_TIMEOUT',
    long: '--timeout HTTP_TIMEOUT',
    description: 'HTTP timeout in case the HTTP request takes too long',
    default: 10,
    proc: proc(&:to_i)

    option :blacklist,
    short: '-b BLACKLIST',
    long: '--blacklist BLACKLIST',
    description: 'comma seperated list of checks that does not need to be alerted out if fails',
    default: nil,
    proc: proc(&:to_s)

    def run
    begin
    response = HTTParty.get("http://#{config[:host]}:#{config[:port]}/healthcheck", timeout: config[:timeout])
    rescue Errno::ECONNREFUSED => e
    critical "failed to connect to service healthcheck on #{config[:host]}:#{config[:port]}. #{e}"
    rescue Net::ReadTimeout => e
    warning "Timed out after #{config[:timeout]} seconds connecing to http://#{config[:port]}/healthcheck. #{e}"
    rescue StandardError => e
    unknown "Something went wrong accessing http://#{config[:host]}:#{config[:port]}/healthcheck. #{e}"
    end

    checks = JSON.parse(response.body)
    failed_checks = {}

    checks.keys.each do |key|
    unless !config[:blacklist].nil? && (config[:blacklist].include? key)
    next unless checks[key]['healthy'] == false
    failed_checks.store(key, checks[key]['message'])
    end
    end

    if failed_checks.any?
    critical "#{failed_checks} healthcheck(s) is/are failing on #{config[:host]}:#{config[:port]}"
    else
    ok "All healthchecks are passing for #{config[:host]}:#{config[:port]}"
    end
    end
    end