The aim of this guide is to demonstrate some basic functionality of the Netping API using the Ruby language. This is by no means a full revision of the Netping API. To learn all the methods and parameters of the Netping API, please visit the API Reference document.
This guide will takeIn this guide we will use Ruby 2.6 in addition to the Faraday HTTP Gem.
Install this ruby gem with the following command:
gem install faraday
After installation, you need to require Faraday in your Ruby Code:
require 'faraday'
To operate most of the Netping API, you will need an API token. The following code sample creates a Netping user and generates an API token to use for the remainder of this guide. It's also possible to register an account manually and then visit the “Settings > API” page.
Please note that all Netping accounts need to be validated by clicking the activation link sent via email.
For the full specs, read about Users in the API reference.
# url to netping api
url = 'https://netping.com/api/v1/users'
# define parameters
parameters = '{
"email": "john@gmail.com",
"password": "johns new password 123",
"create_token": 1
}'
begin
# connect
resp = Faraday.post(url, parameters)
# print response
puts resp.body
rescue Faraday::Error => e
# print error
puts e
end
Nodes are what we call the servers that connect to check our customers services. To create a Check, we need to first select a Node. The following sample fetches a list of all nodes from the Nodes API. Use the API-token acquired in step 2 above.
For the full specs, read about Nodes in the API reference.
# url to netping api
url = 'https://netping.com/api/v1/nodes'
begin
# replace YOUR-API-TOKEN with your API token
resp = Faraday.new(url, headers: { 'Authorization' => 'Bearer YOUR-API-TOKEN' }).get
# print response
puts resp.body
rescue Faraday::Error => e
# print error
puts e
end
A Check is what we call the action that connects to your web service to check it. The Check defines the URL, HTTP method and other parameters used when auditing your service. The following code sample creates a Check using the node_id acquired in step 3 above.
For the full specs, read about Checks in the API reference.
# url to netping api
url = 'https://netping.com/api/v1/checks'
# define parameters
parameters = '{
"name": "My website",
"interval": 60,
"type": "http",
"method": "get",
"url": "http://www.my-website",
"node_id": 123
}'
begin
# replace YOUR-API-TOKEN with your API token
resp = Faraday.post(url, parameters, "Content-Type" => "application/json", "Authorization" => "Bearer YOUR-API-TOKEN")
# print response
puts resp.body
rescue Faraday::Error => e
# print error
puts e
end
A Recipient is an end-point for alerts. In the sample below, we will create an SMS recipient called “Johns Phone”.
There are all kinds of simple and complex recipient types. For the full specs, read about Recipients in the API reference.
# url to netping api
url = 'https://netping.com/api/v1/recipients'
# define parameters
parameters = '{
"name": "Johns phone",
"type": "sms",
"msisdn": "46701234567"
}'
begin
# replace YOUR-API-TOKEN with your API token
resp = Faraday.post(url, parameters, "Content-Type" => "application/json", "Authorization" => "Bearer YOUR-API-TOKEN")
# print response
puts resp.body
rescue Faraday::Error => e
# print error
puts e
end
For the final step, we will define when to send an alarm by creating a Trigger. The Trigger created below fires when the response time of the check exceeds five seconds. An alert will be sent to the recipient ("Johns Phone") created in step 5 above.
For the full specs, read about Triggers in the API reference.
# url to netping api
url = 'https://netping.com/api/v1/checktriggers'
# define parameters. The recipient is 'johns phone'.
parameters = '{
"check_id": "45a6490a-7354-47d9-aec3-658c6dec548b",
"name": "My Trigger",
"type": "http",
"value": "5000",
"minimum_duration": 0,
"recipients": [
"8fffd18d-002b-48bc-96aa-558c50c2db56"
]
}'
begin
# replace YOUR-API-TOKEN with your API token
resp = Faraday.post(url, parameters, "Content-Type" => "application/json", "Authorization" => "Bearer YOUR-API-TOKEN")
# print response
puts resp.body
rescue Faraday::Error => e
# print error
puts e
end
You have reached the end of the guide. At this point, we have created a fully functional Check using Ruby. If we fetch it using the Checks API, we will receive the following Check Object as a result. As you can see, the below results contain the Triggers and Recipient we created above.
Thanks for trying Netping!
{
"id": "45a6490a-7354-47d9-aec3-658c6dec548b",
"name": "My website",
"interval": 60,
"active": true,
"type": "http",
"node_id": 123,
"url": "https://www.my-website.com",
"method": "get",
"triggers": {
"data": [
{
"id": "9d1e416b-9057-4d9d-bc68-3d6f1c416d83",
"name": "My Trigger",
"type": "http",
"check_id": "45a6490a-7354-47d9-aec3-658c6dec548b",
"value": 5000,
"minimum_duration": 0,
"days_warning": null,
"recipients": {
"data": [
{
"id": "8fffd18d-002b-48bc-96aa-558c50c2db56",
"name": "John phone",
"type": "sms",
"msisdn": "46701234567"
}
]
}
}
]
}
}