class CircActivator::CheckUpdater
Attributes
check_bundle[R]
name_regex[RW]
Public Class Methods
new(check_bundle_id)
click to toggle source
# File lib/circactivator/checkupdater.rb, line 28 def initialize(check_bundle_id) @check_bundle_id = check_bundle_id @check_bundle = Hash.new @name_regex = '.*' end
Public Instance Methods
activate_metrics()
click to toggle source
# File lib/circactivator/checkupdater.rb, line 74 def activate_metrics return if @check_bundle['metrics'].nil? updated_metrics = Array.new @check_bundle['metrics'].select { |metric| metric['status'] == 'available' && metric['name'] =~ /#{self.name_regex}/ }.each do |metric| updated_metrics << metric['name'] metric['status'] = 'active' end updated_metrics end
call_circonus(method, url, options={})
click to toggle source
# File lib/circactivator/checkupdater.rb, line 55 def call_circonus(method, url, options={}) options[:headers] = http_headers options[:verify] = CircActivator::Config.circonus.fetch('verify', true) attempt = 0 begin attempt += 1 response = HTTParty.send(method, url, options) raise_exceptions!(response) rescue Net::OpenTimeout, Net::ReadTimeout, CircActivator::Exception::CirconusError raise if attempt >= attempts retry rescue CircActivator::Exception::CheckNotFound raise end response end
fetch()
click to toggle source
# File lib/circactivator/checkupdater.rb, line 46 def fetch response = call_circonus(:get, url + '?query_broker=1') @check_bundle = JSON.load(response.body) end
http_headers()
click to toggle source
# File lib/circactivator/checkupdater.rb, line 34 def http_headers { 'X-Circonus-Auth-Token' => CircActivator::Config.circonus.api_key, 'X-Circonus-App-Name' => CircActivator::Config.circonus.api_app_name, 'Accept' => 'application/json' } end
payload_hash()
click to toggle source
# File lib/circactivator/checkupdater.rb, line 86 def payload_hash @check_bundle.select { |k,v| k =~ /brokers|config|display_name|metrics|notes|period|status|tags|target|timeout|type/ } end
raise_exceptions!(response)
click to toggle source
# File lib/circactivator/checkupdater.rb, line 90 def raise_exceptions!(response) case response.code.to_s when /^2/ return when '404' raise CircActivator::Exception::CheckNotFound, "Check bundle ID #{@check_bundle_id} not found" else raise CircActivator::Exception::CirconusError, "Server error when handling check bundle #{@check_bundle_id}: #{response.body}" end end
run(logging=false)
click to toggle source
# File lib/circactivator/checkupdater.rb, line 102 def run(logging=false) fetch updated_metrics = activate_metrics update if updated_metrics.length > 0 updated_metrics end
update()
click to toggle source
# File lib/circactivator/checkupdater.rb, line 51 def update response = call_circonus(:put, url, body: payload_hash.to_json) end
url()
click to toggle source
# File lib/circactivator/checkupdater.rb, line 42 def url CircActivator::Config.circonus.base_url + '/check_bundle/' + @check_bundle_id.to_s end
Private Instance Methods
attempts()
click to toggle source
# File lib/circactivator/checkupdater.rb, line 112 def attempts CircActivator::Config.circonus['attempts'] || 3 end