Sha256: da3fcf739a2653aea2959d3632207bb5ae888edee4304d2b572ebd6ae5fca6ea

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

#!/usr/bin/env ruby
#
#   metrics-curl-json
#
# DESCRIPTION:
#   Simple wrapper around curl for querying a JSON endpoint and return metrics.
#
# OUTPUT:
#   metric data
#
# PLATFORMS:
#   Linux
#
# DEPENDENCIES:
#   gem: sensu-plugin
#   gem: oj
#   gem: socket
#
# USAGE:
#   #YELLOW
#
# NOTES:
#
# LICENSE:
#   Copyright 2020 Nicolas Boutet
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.
#

require 'socket'
require 'English'
require 'sensu-plugin/metric/cli'
require 'oj'

#
# Curl Metrics
#
class CurlMetrics < Sensu::Plugin::Metric::CLI::Graphite
  option :url,
         short: '-u URL',
         long: '--url URL',
         description: 'valid cUrl url to connect',
         default: 'http://127.0.0.1:80/'

  option :curl_args,
         short: '-a "CURL ARGS"',
         long: '--curl_args "CURL ARGS"',
         description: 'Additional arguments to pass to curl',
         default: ''

  option :scheme,
         description: 'Metric naming scheme, text to prepend to metric',
         short: '-s SCHEME',
         long: '--scheme SCHEME',
         required: true,
         default: Socket.gethostname.to_s

  def deep_value(hash, scheme = '')
    hash.each do |key, value|
      if value.is_a?(Hash)
        deep_value(value, "#{scheme}.#{key}")
      else
        output "#{scheme}.#{key}", value
      end
    end
  end

  def run
    cmd = "curl --silent #{config[:curl_args]} "
    cmd += config[:url]

    output = `#{cmd}`

    metrics = Oj.load(output, mode: :compat)
    deep_value(metrics, config[:scheme])

    if $CHILD_STATUS.to_i == 0
      ok
    else
      warning
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sensu-plugins-http-boutetnico-1.2.0 bin/metrics-curl-json.rb
sensu-plugins-http-boutetnico-1.1.0 bin/metrics-curl-json.rb