Synthetics
Synthetics is a rubygem that interfaces with New Relic Synthetics' HTTP API. It can be used to manage monitors and labels programatically. The above link is useful for finding the possible options that can be passed to each method, particularly creating and updating monitors.
Installation
Add this line to your application's Gemfile:
gem 'synthetics'And then execute:
$ bundleOr install it yourself as:
$ gem install syntheticsUsage
All interaction with the API starts by creating an API client. The client requires an admin API key, which can be generated in the Web UI. Once you have your API key, there are two ways you can pass it to the client:
> # Set API key directly:
> synthetics = Synthetics.new('<YOUR-API-KEY>')
> # Set API key via environment variable:
> ENV['SYNTHETICS_API_KEY'] = 'YOUR-API-KEY'
> synthetics = Synthetics.newLocations
List locations:
> synthetics.locations.list
[
{:private=>false, :name=>"AWS_US_EAST_1", :label=>"Washington, DC, USA"},
...
]Monitors
List monitors:
> synthetics.monitors.list
{
:monitors => [
{
:id => "SOME-VALID-UUID",
:name => "Ping Google",
:type => "SIMPLE",
:frequency => 1440,
:uri => "https://google.com",
:locations => ["AWS_US_EAST_1", "AWS_EU_WEST_1"],
:status => "ENABLED",
:sla_threshold => 7.0,
:modified_at => "2016-02-26T15:10:58.515+0000",
:created_at => "2016-02-26T15:10:58.515+0000",
:user_id => 123,
:api_version => "0.2.2"
}
],
:count => 1
}Create monitor:
> synthetics.monitors.create(
> ... name: 'Google Monitor',
> ... frequency: 15,
> ... uri: 'https://google.com',
> ... locations: %w(AWS_US_WEST_1),
> ... type: 'simple'
> ... )
nilMonitor
These examples assume you have a valid monitor UUID, such as:
> uuid = 'SOME-VAILD-UUID'Show monitor:
> synthetics.monitor(uuid).show
{
:id => "SOME-VALID-UUID",
:name => "Ping Google",
:type => "SIMPLE",
:frequency => 15,
:uri => "https://google.com",
:locations => ["AWS_US_EAST_1", "AWS_EU_WEST_1"],
:status => "ENABLED",
:sla_threshold => 7.0,
:modified_at => "2016-02-26T15:10:58.515+0000",
:created_at => "2016-02-26T15:10:58.515+0000",
:user_id => 123,
:api_version => "0.2.2"
}Update monitor:
> synthetics.monitor(uuid).update(
> ... name: 'Google Monitor',
> ... frequency: 30,
> ... uri: 'https://google.com',
> ... locations: %w(AWS_US_WEST_1),
> ... type: 'simple'
> ... )
{
:id => "SOME-VALID-UUID",
:name => "Ping Google",
:type => "SIMPLE",
:frequency => 30,
:uri => "https://google.com",
:locations => ["AWS_US_EAST_1", "AWS_EU_WEST_1"],
:status => "ENABLED",
:sla_threshold => 7.0,
:modified_at => "2016-02-26T15:10:58.515+0000",
:created_at => "2016-02-26T15:10:58.515+0000",
:user_id => 123,
:api_version => "0.2.2"
}Update monitor script:
> synthetics.monitor(uuid).update_script('var test = function() {};')
nilDestroy monitor:
> synthetics.monitor(uuid).destroy
nilLabels
List labels:
> synthetics.labels.list
{
:labels => [
{
:type => 'sha',
:value => '8ace32a'
}
],
:count => 1
}Label
These examples assume you have a valid label, such as:
> label = 'sample:label'Show monitors for a label:
> synthetics.label(label).monitors
{
:monitors => [
{
:id => "SOME-VALID-UUID",
:name => "Ping Google",
:type => "SIMPLE",
:frequency => 1440,
:uri => "https://google.com",
:locations => ["AWS_US_EAST_1", "AWS_EU_WEST_1"],
:status => "ENABLED",
:sla_threshold => 7.0,
:modified_at => "2016-02-26T15:10:58.515+0000",
:created_at => "2016-02-26T15:10:58.515+0000",
:user_id => 123,
:api_version => "0.2.2"
}
],
:count => 1
}Attach a label to a monitor:
> synthetics.label(label).attach(uuid)
nilRemove a label from a monitor:
> synthetics.label(label).remove(uuid)
nil