1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Added rackspace monitoring with correct namespace

These files were merged from github.com/racker/rackspace-monitoring-rb. The
namespace was different than the convetion in fog so the namespace was modified
to be compliant with the rest of the fog library.
This commit is contained in:
Daniel Reichert 2013-07-03 14:04:58 -07:00
parent 82258177e8
commit 166803de25
42 changed files with 1265 additions and 0 deletions

View file

@ -0,0 +1,37 @@
require 'fog/core/model'
require 'fog/rackspace/models/monitoring/base'
module Fog
module Rackspace
class Monitoring
class AgentToken < Fog::Rackspace::Monitoring::Base
identity :id
attribute :label
attribute :token
def prep
options = {
'label' => label,
'token' => token
}
options = options.reject {|key, value| value.nil?}
options
end
def save
options = prep
if identity then
data = service.update_agent_token(identity, options)
else
data = service.create_agent_token(options)
end
true
end
end
end
end
end

View file

@ -0,0 +1,33 @@
require 'fog/core/collection'
require 'fog/rackspace/models/monitoring/agent_token'
module Fog
module Rackspace
class Monitoring
class AgentTokens < Fog::Collection
model Fog::Rackspace::Monitoring::AgentToken
def all
data = []
opts = {}
begin
new_tokens = service.list_agent_tokens(opts)
data.concat(new_tokens.body['values'])
opts = {:marker => new_tokens.body['metadata']['next_marker']}
end until opts[:marker].nil?
load(data)
end
def get(id)
data = service.get_agent_token(id).body
new(data)
rescue Fog::Rackspace::Monitoring::NotFound
nil
end
end
end
end
end

View file

@ -0,0 +1,46 @@
require 'fog/core/model'
require 'fog/rackspace/models/monitoring/base'
module Fog
module Rackspace
class Monitoring
class Alarm < Fog::Rackspace::Monitoring::Base
identity :id
attribute :entity
attribute :entity_id
attribute :label
attribute :criteria
attribute :check_type
attribute :check_id
attribute :notification_plan_id
def prep
options = {
'label' => label,
'criteria' => criteria,
'notification_plan_id' => notification_plan_id,
}
options = options.reject {|key, value| value.nil?}
options
end
def save
requires :notification_plan_id
options = prep
if identity then
data = service.update_alarm(get_entity_id, identity, options)
else
options['check_type'] = check_type if check_type
options['check_id'] = check_id if check_id
data = service.create_alarm(get_entity_id, options)
end
true
end
end
end
end
end

View file

@ -0,0 +1,26 @@
require 'fog/core/model'
require 'fog/rackspace/models/monitoring/base'
module Fog
module Rackspace
class Monitoring
class AlarmExample < Fog::Rackspace::Monitoring::Base
identity :id
attribute :label
attribute :description
attribute :check_type
attribute :criteria
attribute :fields
attribute :bound_criteria
def bound?
!bound_criteria.nil?
end
end
end
end
end

View file

@ -0,0 +1,31 @@
require 'fog/core/collection'
require 'fog/rackspace/models/monitoring/alarm_example'
module Fog
module Rackspace
class Monitoring
class AlarmExamples < Fog::Collection
model Fog::Rackspace::Monitoring::AlarmExample
def all
data = service.list_alarm_examples.body['values']
load(data)
end
def get(alarm_example_id)
data = service.get_alarm_example(alarm_example_id).body
new(data)
rescue Fog::Rackspace::Monitoring::NotFound
nil
end
def evaluate(alarm_example_id, options={})
data = service.evaluate_alarm_example(alarm_example_id, options).body
new(data)
end
end
end
end
end

View file

@ -0,0 +1,40 @@
require 'fog/core/collection'
require 'fog/rackspace/models/monitoring/alarm'
module Fog
module Rackspace
class Monitoring
class Alarms < Fog::Collection
attribute :entity
model Fog::Rackspace::Monitoring::Alarm
def all
requires :entity
data = service.list_alarms(entity.identity).body['values']
load(data)
end
def get(alarm_id)
requires :entity
data = service.get_alarm(entity.identity, alarm_id).body
new(data)
rescue Fog::Rackspace::Monitoring::NotFound
nil
end
def new(attributes = {})
requires :entity
super({ :entity => entity }.merge!(attributes))
end
def create(attributes = {})
requires :entity
super({ :entity => entity }.merge!(attributes))
end
end
end
end
end

View file

@ -0,0 +1,42 @@
require 'digest/md5'
module Fog
module Rackspace
class Monitoring
class Base < Fog::Model
attribute :created_at
attribute :updated_at
# Back to drawing board on this one I think
def hash
attrs = attributes.dup
attrs.delete_if {|key, value| [:created_at, :updated_at, :id].include?(key)}
attrs.delete_if {|key, value| value.kind_of?(Base) }
keys = attrs.keys.map{|sym| sym.to_s}.sort.join ''
values = attrs.values.map{|sym| sym.to_s}.sort.join ''
Digest::MD5.hexdigest(keys + values)
end
def compare?(b)
a_o = prep
b_o = b.prep
remain = a_o.reject {|key, value| b_o[key] === value}
remain.empty?
end
def get_entity_id
requires :entity
begin
requires :entity
entity_id = entity.identity
rescue
requires :entity_id
end
entity_id
end
end
end
end
end

View file

@ -0,0 +1,73 @@
require 'fog/core/model'
require 'fog/rackspace/models/monitoring/base'
module Fog
module Rackspace
class Monitoring
class Check < Fog::Rackspace::Monitoring::Base
identity :id
attribute :entity
attribute :entity_id
attribute :label
attribute :metadata
attribute :target_alias
attribute :target_resolver
attribute :target_hostname
attribute :period
attribute :timeout
attribute :type
attribute :details
attribute :disabled
attribute :monitoring_zones_poll
def prep
options = {
'label' => label,
'metadata' => metadata,
'target_alias'=> target_alias,
'target_resolver' => target_resolver,
'target_hostname' => target_hostname,
'period' => period,
'timeout'=> timeout,
'details'=> details,
'monitoring_zones_poll'=> monitoring_zones_poll,
'disabled'=> disabled
}
options = options.reject {|key, value| value.nil?}
options
end
def save
begin
requires :entity
entity_id = entity.identity
rescue
requires :entity_id
end
options = prep
if identity then
data = service.update_check(entity_id, identity, options)
else
requires :type
options['type'] = type
data = service.create_check(entity_id, options)
end
true
end
def metrics
@metrics ||= begin
Fog::Rackspace::Monitoring::Metrics.new(
:check => self,
:service => service
)
end
end
end
end
end
end

View file

@ -0,0 +1,15 @@
require 'fog/core/model'
require 'fog/rackspace/models/monitoring/base'
module Fog
module Rackspace
class Monitoring
class CheckType < Fog::Rackspace::Monitoring::Base
identity :id
attribute :type
attribute :fields
attribute :channel
end
end
end
end

View file

@ -0,0 +1,23 @@
require 'fog/core/collection'
require 'fog/rackspace/models/monitoring/check_type'
module Fog
module Rackspace
class Monitoring
class CheckTypes < Fog::Collection
model Fog::Rackspace::Monitoring::CheckType
def all
data = service.list_check_types.body['values']
load(data)
end
def new(attributes = {})
super({ }.merge!(attributes))
end
end
end
end
end

View file

@ -0,0 +1,40 @@
require 'fog/core/collection'
require 'fog/rackspace/models/monitoring/check'
module Fog
module Rackspace
class Monitoring
class Checks < Fog::Collection
attribute :entity
model Fog::Rackspace::Monitoring::Check
def all
requires :entity
data = service.list_checks(entity.identity).body['values']
load(data)
end
def get(check_id)
requires :entity
data = service.get_check(entity.identity, check_id).body
new(data)
rescue Fog::Rackspace::Monitoring::NotFound
nil
end
def new(attributes = {})
requires :entity
super({ :entity => entity }.merge!(attributes))
end
def create(attributes = {})
requires :entity
super({ :entity => entity }.merge!(attributes))
end
end
end
end
end

View file

@ -0,0 +1,18 @@
require 'fog/core/model'
require 'fog/rackspace/models/monitoring/base'
module Fog
module Rackspace
class Monitoring
class DataPoint < Fog::Rackspace::Monitoring::Base
attribute :num_points, :aliases => "numPoints"
attribute :average
attribute :variance
attribute :min
attribute :max
attribute :timestamp
attribute :metric
end
end
end
end

View file

@ -0,0 +1,49 @@
require 'fog/core/collection'
require 'fog/rackspace/models/monitoring/data_point'
module Fog
module Rackspace
class Monitoring
class DataPoints < Fog::Collection
attribute :metric
model Fog::Rackspace::Monitoring::DataPoint
def all
self.fetch(:resolution => :full)
end
# Fetch the datapoints for a metric
# ==== Parameters
#
# * options<~Hash> - optional paramaters
# * from<~Integer> - timestamp in milliseconds
# * to<~Integer> - timestamp in milliseconds
# * points<~Integer> - Number of points to fetch
# * resolution<~String> - Should be one of :full, :min5, :min20, :min60, :min240, :min1440
# * select<~Array> - Should be an array of :average, :max, :min, :variance
#
# ==== Returns
# * datapoints<~Fog::Rackspace::Monitoring::Datapoints>:
def fetch(options={})
requires :metric
options[:from] ||= (Time.now.to_i * 1000) - (3600 * 1000)
options[:to] ||= Time.now.to_i * 1000
options[:points] ||= 1 unless options[:resolution]
if options[:resolution]
options[:resolution] = options[:resolution].upcase
end
data = service.list_data_points(metric.check.entity.id, metric.check.id, metric.name, options).body['values']
load(data)
end
def new(attributes = {})
requires :metric
super({ :metric => metric }.merge!(attributes))
end
end
end
end
end

View file

@ -0,0 +1,48 @@
require 'fog/core/collection'
require 'fog/rackspace/models/monitoring/entity'
require 'fog/rackspace/models/monitoring/check'
module Fog
module Rackspace
class Monitoring
class Entities < Fog::Collection
model Fog::Rackspace::Monitoring::Entity
def all
data = service.list_entities.body['values']
load(data)
end
def get(entity_id)
data = service.get_entity(entity_id).body
new(data)
rescue Fog::Rackspace::Monitoring::NotFound
nil
end
def overview
entities = []
opts = {}
begin
new_entities = service.list_overview(opts)
entities.concat(new_entities.body['values'])
opts = {:marker => new_entities.body['metadata']['next_marker']}
end while(!opts[:marker].nil?)
loadAll(entities)
end
def loadAll(objects)
clear
for object in objects
en = new(object['entity'])
self << en
en.checks.load(object['checks'])
en.alarms.load(object['alarms'])
end
self
end
end
end
end
end

View file

@ -0,0 +1,65 @@
require 'fog/core/model'
require 'fog/rackspace/models/monitoring/base'
module Fog
module Rackspace
class Monitoring
class Entity < Fog::Rackspace::Monitoring::Base
identity :id
attribute :label
attribute :metadata
attribute :ip_addresses
attribute :agent_id
attribute :managed, :default => false
attribute :uri
def prep
options = {
'label' => label,
'metadata' => metadata,
'ip_addresses'=> ip_addresses,
'agent_id' => agent_id
}
options = options.reject {|key, value| value.nil?}
options
end
def save
options = prep
if identity then
data = service.update_entity(identity, options)
else
data = service.create_entity(options)
end
true
end
def checks
@checks ||= begin
Fog::Rackspace::Monitoring::Checks.new(
:entity => self,
:service => service
)
end
end
def alarms
@alarms ||= begin
Fog::Rackspace::Monitoring::Alarms.new(
:entity => self,
:service => service
)
end
end
def destroy
requires :id
service.delete_entity(id)
end
end
end
end
end

View file

@ -0,0 +1,25 @@
require 'fog/core/model'
require 'fog/rackspace/models/monitoring/base'
module Fog
module Rackspace
class Monitoring
class Metric < Fog::Rackspace::Monitoring::Base
identity :name
attribute :check
def datapoints(options={})
@datapoints ||= begin
Fog::Rackspace::Monitoring::DataPoints.new(
:metric => self,
:service => service
)
end
end
end
end
end
end

View file

@ -0,0 +1,27 @@
require 'fog/core/collection'
require 'fog/rackspace/models/monitoring/metric'
module Fog
module Rackspace
class Monitoring
class Metrics < Fog::Collection
attribute :check
model Fog::Rackspace::Monitoring::Metric
def all
requires :check
data = service.list_metrics(check.entity.id, check.id).body['values']
load(data)
end
def new(attributes = {})
requires :check
super({ :check => check }.merge!(attributes))
end
end
end
end
end

View file

@ -0,0 +1,181 @@
require 'fog'
require 'fog/core'
module Fog
module Rackspace
class Monitoring < Fog::Service
ENDPOINT = 'https://monitoring.api.rackspacecloud.com/v1.0'
requires :rackspace_api_key, :rackspace_username
recognizes :rackspace_auth_url, :persistent, :raise_errors
recognizes :rackspace_auth_token, :rackspace_service_url, :rackspace_account_id
model_path 'rackspace-monitoring/monitoring/models'
model :entity
collection :entities
model :check
collection :checks
model :alarm
collection :alarms
model :alarm_example
collection :alarm_examples
model :agent_token
collection :agent_tokens
model :metric
collection :metrics
model :data_point
collection :data_points
model :check_type
collection :check_types
request_path 'rackspace-monitoring/monitoring/requests'
request :list_agent_tokens
request :list_alarms
request :list_alarm_examples
request :list_checks
request :list_entities
request :list_metrics
request :list_data_points
request :list_check_types
request :list_overview
request :get_agent_token
request :get_alarm
request :get_alarm_example
request :get_check
request :get_entity
request :create_agent_token
request :create_alarm
request :create_check
request :create_entity
request :update_check
request :update_entity
request :update_alarm
request :delete_check
request :delete_entity
request :evaluate_alarm_example
class Mock
end
class Real
def initialize(options={})
@rackspace_api_key = options[:rackspace_api_key]
@rackspace_username = options[:rackspace_username]
@rackspace_auth_url = options[:rackspace_auth_url]
@rackspace_auth_token = options[:rackspace_auth_token]
@rackspace_account_id = options[:rackspace_account_id]
@rackspace_service_url = options[:rackspace_service_url] || ENDPOINT
@rackspace_must_reauthenticate = false
@connection_options = options[:connection_options] || {}
if options.has_key?(:raise_errors)
@raise_errors = options[:raise_errors]
else
@raise_errors = true
end
begin
authenticate
rescue Exception => error
raise_error(error)
end
@persistent = options[:persistent] || false
begin
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
rescue Exception => error
raise_error(error)
end
end
def reload
@connection.reset
end
def raise_error(error)
if @raise_errors
raise error
else
print "Error occurred: " + error.message
end
end
def request(params)
begin
begin
response = @connection.request(params.merge({
:headers => {
'Content-Type' => 'application/json',
'X-Auth-Token' => @auth_token
}.merge!(params[:headers] || {}),
:host => @host,
:path => "#{@path}/#{params[:path]}"
}))
rescue Excon::Errors::Unauthorized => error
if error.response.body != 'Bad username or password' # token expiration
@rackspace_must_reauthenticate = true
authenticate
retry
else # bad credentials
raise error
end
rescue Excon::Errors::HTTPStatusError => error
raise case error
when Excon::Errors::NotFound
Fog::Monitoring::Rackspace::NotFound.slurp(error)
else
error
end
end
unless response.body.empty?
response.body = JSON.decode(response.body)
end
response
rescue Exception => error
raise_error(error)
end
end
private
def authenticate
if @rackspace_must_reauthenticate || @rackspace_auth_token.nil? || @account_id.nil?
options = {
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url
}
begin
credentials = Fog::Rackspace.authenticate(options)
rescue Exception => error
raise_error(error)
return
end
@auth_token = credentials['X-Auth-Token']
@account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
else
@auth_token = @rackspace_auth_token
@account_id = @rackspace_account_id
end
uri = URI.parse("#{@rackspace_service_url}/#{@account_id}")
@host = uri.host
@path = uri.path
@port = uri.port
@scheme = uri.scheme
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Rackspace
class Monitoring
class Real
def create_agent_token(options = {})
data = options.dup
request(
:body => JSON.encode(data),
:expects => [201],
:method => 'POST',
:path => 'agent_tokens'
)
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Rackspace
class Monitoring
class Real
def create_alarm(entity_id, options = {})
data = options.dup
request(
:body => JSON.encode(data),
:expects => [201],
:method => 'POST',
:path => "entities/#{entity_id}/alarms"
)
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Rackspace
class Monitoring
class Real
def create_check(entity_id, options = {})
data = options.dup
request(
:body => JSON.encode(data),
:expects => [201],
:method => 'POST',
:path => "entities/#{entity_id}/checks"
)
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Rackspace
class Monitoring
class Real
def create_entity(options = {})
data = options.dup
request(
:body => JSON.encode(data),
:expects => [201],
:method => 'POST',
:path => 'entities'
)
end
end
end
end
end

View file

@ -0,0 +1,17 @@
module Fog
module Rackspace
class Monitoring
class Real
def delete_check(entity_id, check_id)
request(
:expects => [204],
:method => 'DELETE',
:path => "entities/#{entity_id}/checks/#{check_id}"
)
end
end
end
end
end

View file

@ -0,0 +1,17 @@
module Fog
module Rackspace
class Monitoring
class Real
def delete_entity(entity_id)
request(
:expects => [204],
:method => 'DELETE',
:path => "entities/#{entity_id}"
)
end
end
end
end
end

View file

@ -0,0 +1,21 @@
module Fog
module Rackspace
class Monitoring
class Real
def evaluate_alarm_example(id, options = {})
options ||= {}
data = {:values => options.dup}
request(
:body => JSON.encode(data),
:expects => [200],
:method => 'POST',
:path => "alarm_examples/#{id}"
)
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Rackspace
class Monitoring
class Real
def get_agent_token(id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "agent_tokens/#{id}"
)
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Rackspace
class Monitoring
class Real
def get_alarm(entity_id, alarm_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "entities/#{entity_id}/alarms/#{alarm_id}"
)
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Rackspace
class Monitoring
class Real
def get_alarm_example(id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "alarm_examples/#{id}"
)
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Rackspace
class Monitoring
class Real
def get_check(entity_id, check_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "entities/#{entity_id}/checks/#{check_id}"
)
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Rackspace
class Monitoring
class Real
def get_entity(entity_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "entities/#{entity_id}"
)
end
end
end
end
end

View file

@ -0,0 +1,19 @@
module Fog
module Rackspace
class Monitoring
class Real
def list_agent_tokens(options={})
request(
:expects => [200, 203],
:method => 'GET',
:path => 'agent_tokens',
:query => options
)
end
end
end
end
end

View file

@ -0,0 +1,18 @@
module Fog
module Rackspace
class Monitoring
class Real
def list_alarm_examples
request(
:expects => [200, 203],
:method => 'GET',
:path => 'alarm_examples'
)
end
end
end
end
end

View file

@ -0,0 +1,18 @@
module Fog
module Rackspace
class Monitoring
class Real
def list_alarms(entity_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "entities/#{entity_id}/alarms"
)
end
end
end
end
end

View file

@ -0,0 +1,17 @@
module Fog
module Rackspace
class Monitoring
class Real
def list_check_types
request(
:expects => [200, 203],
:method => 'GET',
:path => "check_types"
)
end
end
end
end
end

View file

@ -0,0 +1,18 @@
module Fog
module Rackspace
class Monitoring
class Real
def list_checks(entity_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "entities/#{entity_id}/checks"
)
end
end
end
end
end

View file

@ -0,0 +1,18 @@
module Fog
module Rackspace
class Monitoring
class Real
def list_data_points(entity_id, check_id, metric_name, options)
request(
:expects => [200, 203],
:method => 'GET',
:path => "entities/#{entity_id}/checks/#{check_id}/metrics/#{metric_name}/plot",
:query => options
)
end
end
end
end
end

View file

@ -0,0 +1,18 @@
module Fog
module Rackspace
class Monitoring
class Real
def list_entities
request(
:expects => [200, 203],
:method => 'GET',
:path => 'entities'
)
end
end
end
end
end

View file

@ -0,0 +1,18 @@
module Fog
module Rackspace
class Monitoring
class Real
def list_metrics(entity_id, check_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "entities/#{entity_id}/checks/#{check_id}/metrics"
)
end
end
end
end
end

View file

@ -0,0 +1,22 @@
module Fog
module Rackspace
class Monitoring
class Real
def list_overview(opts={})
path = "views/overview"
opts.keys.each do |key|
path = path + "?#{key}=#{opts[key]}"
end
request(
:expects => [200, 203],
:method => 'GET',
:path => path
)
end
end
end
end
end

View file

@ -0,0 +1,18 @@
module Fog
module Rackspace
class Monitoring
class Real
def update_alarm(entity_id, id, options)
request(
:body => JSON.encode(options),
:expects => [204],
:method => 'PUT',
:path => "entities/#{entity_id}/alarms/#{id}"
)
end
end
end
end
end

View file

@ -0,0 +1,18 @@
module Fog
module Rackspace
class Monitoring
class Real
def update_check(entity_id, id, options)
request(
:body => JSON.encode(options),
:expects => [204],
:method => 'PUT',
:path => "entities/#{entity_id}/checks/#{id}"
)
end
end
end
end
end

View file

@ -0,0 +1,18 @@
module Fog
module Rackspace
class Monitoring
class Real
def update_entity(id, options)
request(
:body => JSON.encode(options),
:expects => [204],
:method => 'PUT',
:path => "entities/#{id}"
)
end
end
end
end
end