Sha256: 4b168c1a1ea4f212a1058aa3f23ef8b143932374cd8125f30676054884a8af6a

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

#!/usr/bin/env ruby
#
#   newrelic-metrics
#
# DESCRIPTION:
#   Pull new relic metrics
#
# OUTPUT:
#   metric data
#
# PLATFORMS:
#   Linux
#
# DEPENDENCIES:
#   gem: sensu-plugin
#   gem: <?>
#
# USAGE:
#   example commands
#
# NOTES:
#   this is setup to only work if you have a single account under
#   your api key.
#
# LICENSE:
#   Copyright 2012 Pete Shima <me@peteshima.com>
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.
#

require 'sensu-plugin/metric/cli'
require 'net/http'
require 'net/https'
require 'uri'
require 'socket'
require 'crack'

class NewRelicMetrics < Sensu::Plugin::Metric::CLI::Graphite
  option :apikey,
         short: '-k APIKEY',
         long: '--apikey APIKEY',
         description: 'Your New Relic API Key',
         required: true

  option :appname,
         short: '-n APPNAME',
         long: '--name APPNAME',
         description: 'Name of the new relic app you want',
         required: true

  option :scheme,
         description: 'Metric naming scheme, text to prepend to metric',
         short: '-s SCHEME',
         long: '--scheme SCHEME',
         default: "#{Socket.gethostname}.newrelic"

  def run
    rpm = 'https://rpm.newrelic.com'

    url = URI.parse(rpm)

    res = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
      req = Net::HTTP::Get.new('/accounts.xml?include=application_health')
      req.add_field('x-api-key', config[:apikey])
      http.request(req)
    end

    stats = Crack::XML.parse(res.body)

    app = stats['accounts'].first['applications'].find { |v| v['name'] == config[:appname] }
    app['threshold_values'].each do |v|
      metric_name = v['name'].gsub(/\s+/, '_').downcase
      output "#{config[:scheme]}.#{metric_name}", v['metric_value']
    end

    ok
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sensu-plugins-newrelic-boutetnico-1.1.0 bin/metrics-newrelic.rb