#!/usr/bin/env ruby # # Event Metrics # # Use the /events API to collect events and their severity. # # sensu.events.total 2 1234535436 # sensu.events.warning 0 1234535436 # sensu.events.critical 0 1234535436 # # === # # Authors # === # Bertrand Roussel, @CoRfr # # Copyright 2016 Sierra Wireless, Inc. # # Released under the same terms as Sensu (the MIT license); see # LICENSE for details. require 'sensu-plugin/metric/cli' require 'rest-client' require 'json' class AggregateMetrics < Sensu::Plugin::Metric::CLI::Graphite option :api, short: '-a URL', long: '--api URL', description: 'Sensu API URL', default: 'http://localhost:4567' option :user, short: '-u USER', long: '--user USER', description: 'Sensu API USER' option :password, short: '-p PASSOWRD', long: '--password PASSWORD', description: 'Sensu API PASSWORD' option :timeout, short: '-t SECONDS', long: '--timeout SECONDS', description: 'Sensu API connection timeout in SECONDS', proc: proc(&:to_i), default: 30 option :scheme, description: 'Metric naming scheme', long: '--scheme SCHEME', default: "#{Socket.gethostname}.sensu.events" option :debug, long: '--debug', description: 'Verbose output' def api_request(resource) request = RestClient::Resource.new(config[:api] + resource, timeout: config[:timeout], user: config[:user], password: config[:password]) JSON.parse(request.get, symbolize_names: true) rescue RestClient::ResourceNotFound warning "Resource not found: #{resource}" rescue Errno::ECONNREFUSED warning 'Connection refused' rescue RestClient::RequestFailed warning 'Request failed' rescue RestClient::RequestTimeout warning 'Connection timed out' rescue RestClient::Unauthorized warning 'Missing or incorrect Sensu API credentials' rescue JSON::ParserError warning 'Sensu API returned invalid JSON' end def acquire_events uri = '/events' checks = api_request(uri) puts "Events: #{checks.inspect}" if config[:debug] checks end def run timestamp = Time.now.to_i status_count = {} status_count[1] = 0 status_count[2] = 0 status_names = {} status_names[1] = 'warning' status_names[2] = 'critical' total_count = 0 acquire_events.each do |event| total_count += 1 status_count[event[:check][:status]] ||= 0 status_count[event[:check][:status]] += 1 end output "#{config[:scheme]}.total", total_count, timestamp status_count.each do |status, count| name = status_names[status] if name output "#{config[:scheme]}.#{name}", count, timestamp else output "#{config[:scheme]}.status.#{status}", count, timestamp end end ok end end