module NewRelicManagement::Client

> NewRelic Manager Client

Public Instance Methods

alert_add_entity(entity_id, condition_id, entity_type = 'Server') click to toggle source

> Add an Entitity to an Existing Alert Policy

# File lib/newrelic-management/client.rb, line 62
def alert_add_entity(entity_id, condition_id, entity_type = 'Server')
  nr_api.put do |req|
    req.url url('alerts_entity_conditions', entity_id)
    req.params['entity_type'] = entity_type
    req.params['condition_id'] = condition_id
  end
end
alert_conditions(policy) click to toggle source

> List Alert Conditions

# File lib/newrelic-management/client.rb, line 57
def alert_conditions(policy)
  nr_api.get(url('alerts_conditions'), policy_id: policy).body
end
alert_delete_entity(entity_id, condition_id, entity_type = 'Server') click to toggle source

> Delete an Entitity from an Existing Alert Policy

# File lib/newrelic-management/client.rb, line 71
def alert_delete_entity(entity_id, condition_id, entity_type = 'Server')
  nr_api.delete do |req|
    req.url url('alerts_entity_conditions', entity_id)
    req.params['entity_type'] = entity_type
    req.params['condition_id'] = condition_id
  end
end
alert_policies() click to toggle source

> List Alert Policies

# File lib/newrelic-management/client.rb, line 50
def alert_policies
  nr_api.get(url('alerts_policies')).body['policies']
rescue NoMethodError
  []
end
conn_opts() click to toggle source
# File lib/newrelic-management/client.rb, line 34
def conn_opts
  {
    headers: {
      'Accept' => 'application/json',
      'Content-Type' => 'application/json',
      'X-api-key' => Config.nr_api_key
    },
    # => ssl: ssl_options
  }
end
delete_server(server_id) click to toggle source

> Delete a Server from NewRelic

# File lib/newrelic-management/client.rb, line 129
def delete_server(server_id)
  nr_api.delete(url('servers', server_id)).body
end
get_server(server) click to toggle source

> Get Info for Specific Server

# File lib/newrelic-management/client.rb, line 98
def get_server(server)
  srv = get_server_id(server)
  srv ? srv : get_server_name(server)
end
get_server_id(server_id) click to toggle source

> Get a Server based on ID

# File lib/newrelic-management/client.rb, line 104
def get_server_id(server_id)
  return nil unless server_id =~ /^[0-9]+$/
  ret = nr_api.get(url('servers', server_id)).body
  ret['server']
rescue Faraday::ResourceNotFound, NoMethodError
  nil
end
get_server_name(server, exact = true) click to toggle source

> Get a Server based on Name

# File lib/newrelic-management/client.rb, line 113
def get_server_name(server, exact = true)
  ret = nr_api.get(url('servers'), 'filter[name]' => server).body
  return ret['servers'] unless exact
  ret['servers'].find { |x| x['name'].casecmp(server).zero? }
rescue NoMethodError
  nil
end
get_servers_labeled(labels) click to toggle source

> List the Servers with a Label

# File lib/newrelic-management/client.rb, line 122
def get_servers_labeled(labels)
  label_query = Array(labels).reject { |x| !x.include?(':') }.join(';')
  return [] unless label_query
  nr_api.get(url('servers'), 'filter[labels]' => label_query).body
end
labels() click to toggle source

> List the Labels

# File lib/newrelic-management/client.rb, line 80
def labels
  nr_api.get(url('labels')).body['labels']
rescue NoMethodError
  []
end
nr_api() click to toggle source

> Build the HTTP Connection

# File lib/newrelic-management/client.rb, line 23
def nr_api
  # => Build the Faraday Connection
  @conn ||= Faraday::Connection.new('https://api.newrelic.com', conn_opts) do |client|
    client.use Faraday::Response::RaiseError
    client.use FaradayMiddleware::EncodeJson
    client.use FaradayMiddleware::ParseJson, content_type: /\bjson$/
    client.response :logger if Config.environment.to_s.casecmp('development').zero? # => Log Requests to STDOUT
    client.adapter Faraday.default_adapter #:net_http_persistent
  end
end
servers() click to toggle source

> List the Servers Reporting to NewRelic

# File lib/newrelic-management/client.rb, line 91
def servers
  nr_api.get(url('servers')).body['servers']
rescue NoMethodError
  []
end

Private Instance Methods

url(*args) click to toggle source
# File lib/newrelic-management/client.rb, line 133
def url(*args)
  '/v2/' + args.map { |a| URI.encode_www_form_component a.to_s }.join('/') + '.json'
end