Class: Scout::Server

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/scout_api/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, ignore = nil) ⇒ Server

2nd parameter is ignored/a hack because of this open Hashie issue: github.com/intridea/hashie/issues/14



36
37
38
39
# File 'lib/scout_api/server.rb', line 36

def initialize(hash, ignore=nil) #:nodoc:
  @metrics = Scout::MetricProxy.new(self)
  super(hash)
end

Instance Attribute Details

#metricsObject (readonly)

Retrieve metric information. See Metric.average for a list of options for the calculation methods (average, minimum, maximum).

Examples:

# All metrics associated with this server.
Scout::Server.metrics

# Metrics with name =~ 'Memory Used' on this server.
Scout::Server.metrics.all(:name => 'Memory Used')

# Average value of metrics with name =~ 'Memory Used' on this server.
Scout::Server.metrics.average(:name => 'Memory Used')

# Maximum value...
Scout::Server.metrics.maximum(:name => 'Memory Used')

# Minimum value...
Scout::Server.metrics.minimum(:name => 'Memory Used')

# Sum metrics, then take average
Scout::Server.metrics.average(:name => 'request_rate', :aggregate => true)

# Retrieve data starting @ 5 hours ago ending at 2 hours ago
Scout::Server.metrics.average(:name => 'request_rate', :start => Time.now.utc-5*3600, :end => Time.now.utc-2*3600)

# An array of time series values over the past hour.
Scout::Server.metrics.average(:name => 'Memory Used').to_array

# A Url to a Google Sparkline Chart.
Scout::Server.metrics.average(:name => 'Memory Used').to_sparkline


33
34
35
# File 'lib/scout_api/server.rb', line 33

def metrics
  @metrics
end

Class Method Details

.all(options = {}) ⇒ Array

Finds all servers that meets the given conditions. Possible parameter formats:

Scout::Server.all(:name => 'db slaves')
Scout::Server.all(:host => 'web*.geocities')

For the :name and :host options, a MySQL-formatted Regex can be used.

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/scout_api/server.rb', line 96

def self.all(options = {})
  if name=options[:name]
    response = Scout::Account.get("/clients.xml?name=#{CGI.escape(name)}")
  elsif host=options[:host]
    response = Scout::Account.get("/clients.xml?host=#{CGI.escape(host)}")
  elsif name=options[:group_name]
    response = Scout::Account.get("/clients.xml?group_name=#{CGI.escape(name)}")
  elsif options.empty?
    response = Scout::Account.get("/clients.xml")
  else
    raise Scout::Error, "Invalid finder condition"
  end
  response['clients'] ? response['clients'].map { |client| Scout::Server.new(client) } : Array.new
end

.create(name, options = {}) ⇒ Scout::Server

Creates a new server with the given name. If an error occurs, a [Scout::Error] is raised.

An optional existing server id can be used as a template:

Scout::Server.create('web server 12',:id => 99999)

Returns:

Raises:



118
119
120
121
122
123
124
125
# File 'lib/scout_api/server.rb', line 118

def self.create(name,options = {})
  id = options[:id]
  response = Scout::Account.post("/#{Scout::Account.param}/clients.xml", 
  :query => {:client => {:name => name, :copy_plugins_from_client_id => id}, :api_version => Scout::VERSION})
  
  raise Scout::Error, response['errors']['error'] if response['errors']
  first(response.headers['id'].to_i)
end

.delete(id) ⇒ true

Delete a server by id. If an error occurs, a [Scout::Error] is raised.

# Delete server w/id=1
ScoutScout::Server.delete(1)

Returns:

  • (true)


133
134
135
136
137
138
139
140
141
142
143
# File 'lib/scout_api/server.rb', line 133

def self.delete(id)
  response = Scout::Account.delete("/#{Scout::Account.param}/clients/#{id}.xml?api_version=#{Scout::VERSION}")

  if response.headers['status'].match('404')
    raise Scout::Error, "Server Not Found"
  elsif !response.headers['status'].match('200')
    raise Scout::Error, "An error occured"
  else
    return true
  end
end

.first(id_or_options = nil) ⇒ Scout::Server

Finds the first server that meets the given conditions.

Scout::Server.first
Scout::Server.first(1)
Scout::Server.first(:name => 'db slaves')
Scout::Server.first(:host => 'web*.geocities')

For the :name and :host options, a MySQL-formatted Regex can be used.

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/scout_api/server.rb', line 59

def self.first(id_or_options = nil)
  if id_or_options.nil?
    response = Scout::Account.get("/clients.xml?limit=1")
    Scout::Server.new(response['clients'].first)
  elsif id_or_options.is_a?(Hash)
    if name=id_or_options[:name]
      response = Scout::Account.get("/clients.xml?name=#{CGI.escape(name)}")
      raise Scout::Error, 'Not Found' if response['clients'].nil?
      Scout::Server.new(response['clients'].first)
    elsif host=id_or_options[:host]
      response = Scout::Account.get("/clients.xml?host=#{CGI.escape(host)}")
      raise Scout::Error, 'Not Found' if response['clients'].nil?
      Scout::Server.new(response['clients'].first)
    else
      raise Scout::Error, "Invalid finder condition"
    end
  elsif id_or_options.is_a?(Fixnum)
    response = Scout::Account.get("/clients/#{id_or_options}.xml")
    Scout::Server.new(response['client'])
  elsif id_or_options.is_a?(String)
    warn "Server#first(hostname) will be deprecated. Use Server#first(:host => hostname)"
    response = Scout::Account.get("/clients.xml?host=#{CGI.escape(id_or_options)}")
    raise Scout::Error, 'Not Found' if response['clients'].nil?
    Scout::Server.new(response['clients'].first)
  else
    raise Scout::Error, "Invalid finder condition"
  end
end

Instance Method Details

#alertsArray

Recent alerts for this server

Returns:

  • (Array)

    An array of Alert objects



148
149
150
151
# File 'lib/scout_api/server.rb', line 148

def alerts
  response = Scout::Account.get("/clients/#{self.id}/activities.xml")
  response['alerts'].map { |alert| decorate_with_server(Scout::Alert.new(alert)) }
end

#keyObject

Returns the key identifier for this server (ex: ‘6ecad322-0d17-4cb8-9b2c-a12c4541853f’).

Ruby 1.9.2 Hash#key is a method. This overrides so key returns Server#key.



44
45
46
# File 'lib/scout_api/server.rb', line 44

def key #:nodoc:
  self[:key]
end

#plugin(id) ⇒ Scout::Plugin

Details about a specific plugin

Returns:



164
165
166
167
# File 'lib/scout_api/server.rb', line 164

def plugin(id)
  response = Scout::Account.get("/clients/#{self.id}/plugins/#{id}.xml")
  decorate_with_server(Scout::Plugin.new(response['plugin']))
end

#pluginsArray

Details about all plugins for this server

Returns:

  • (Array)

    An array of {Scout::Plugin }objects



156
157
158
159
# File 'lib/scout_api/server.rb', line 156

def plugins
  response = Scout::Account.get("/clients/#{self.id}/plugins.xml")
  response['plugins'].map { |plugin| decorate_with_server(Scout::Plugin.new(plugin)) }
end

#triggersArray

Details about all triggers for this server

Returns:

  • (Array)

    An array of Trigger objects



172
173
174
175
# File 'lib/scout_api/server.rb', line 172

def triggers
  response = Scout::Account.get("/clients/#{self.id}/triggers.xml")
  response['triggers'].map { |trigger| decorate_with_server(Scout::Trigger.new(trigger)) }
end