Client library for UptimeRobot v2 API

GoDocGo Report CardMentioned in Awesome GoCircleCI

uptimerobot

uptimerobot is a Go library and command-line client for the Uptime Robot website monitoring service. It allows you to search for existing monitors, delete monitors, create new monitors, and also inspect your account details and alert contacts.

Installing the command-line client

To install the client binary, run:

go get -u github.com/bitfield/uptimerobot

Running the command-line client in Docker

To use the client in a Docker container, run:

docker container run bitfield/uptimerobot

Using the command-line client

To see help on using the client, run:

uptimerobot -h

Setting your API key

To use the client with your Uptime Robot account, you will need the Main API Key for the account. Go to the Uptime Robot Settings page and click 'Show/hide it' under the 'Main API Key' section.

There are three ways to pass your API key to the client: in a config file, in an environment variable, or on the command line.

In a config file

The uptimerobot client will read a config file named .uptimerobot.yaml (or .uptimerobot.json, or any other extension that Viper supports) in your home directory, or in the current directory.

For example, you can put your API key in the file $HOME/.uptimerobot.yaml, and uptimerobot will find and read it automatically (replace XXX with your own API key):

apiKey: XXX

In an environment variable

uptimerobot will look for the API key in an environment variable named UPTIMEROBOT_API_KEY:

export UPTIMEROBOT_API_KEY=XXX
uptimerobot ...

(For historical reasons, the variable can also be named UPTIMEROBOT_APIKEY.)

On the command line

You can also pass your API key to the uptimerobot client using the --apiKey flag like this:

uptimerobot --apiKey XXX ...

Testing your configuration

To test that your API key is correct and uptimerobot is reading it properly, run:

uptimerobot account

You should see your account details listed:

Email: [email protected]
Monitor limit: 300
Monitor interval: 1
Up monitors: 208
Down monitors: 2
Paused monitors: 0

If you get an error message, double-check you have the correct API key:

2018/07/12 16:04:26 API error: {
 "message": "api_key not found.",
 "parameter_name": "api_key",
 "passed_value": "XXX",
 "type": "invalid_parameter"
}

Listing contacts

The uptimerobot contacts command will list your configured alert contacts by ID number:

uptimerobot contacts
ID: 0102759
Name: Jay Random
Type: 2
Status: 2
Value: [email protected]

ID: 2053888
Name: Slack
Type: 11
Status: 2
Value: https://hooks.slack.com/services/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

This will be useful when you create a new monitor, because you can add the contact IDs which should be alerted when the check fails (see 'Creating a new monitor' below).

Listing or searching for monitors

Use uptimerobot search to list all monitors whose 'friendly name' or check URL match a certain string:

uptimerobot search www.example.com
ID: 780689017
Name: Example.com website
URL: https://www.example.com/
Status: Up
Type: HTTP

(Use uptimerobot monitors to list all existing monitors.)

If there are no monitors found matching your search, the exit status of the command will be 1. Otherwise it will be 0. (If you're checking whether a monitor already exists before creating it, try the ensure command instead.)

Deleting monitors

Note the ID number of the monitor you want to delete, and run uptimerobot delete:

uptimerobot delete 780689017
Monitor ID 780689017 deleted

Pausing or starting monitors

Note the ID number of the monitor you want to pause, and run uptimerobot pause:

uptimerobot pause 780689017
Monitor ID 780689017 paused

To resume a paused monitor, run uptimerobot start with the monitor ID:

uptimerobot start 780689017
Monitor ID 780689017 started

Creating a new monitor

Run uptimerobot new URL NAME to create a new monitor:

uptimerobot new https://www.example.com/ "Example.com website"
New monitor created with ID 780689018

To create a new monitor with alert contacts configured, use the -c flag followed by a comma-separated list of contact IDs, with no spaces:

uptimerobot new -c 0102759,2053888 https://www.example.com/ "Example.com website"
New monitor created with ID 780689019

Ensuring a monitor exists

Sometimes you want to create a new monitor only if a monitor doesn't already exist for the same URL. This is especially useful in automation.

To do this, run uptimerobot ensure URL NAME:

uptimerobot ensure https://www.example.com/ "Example.com website"
Monitor ID 780689018 ensured

If the monitor doesn't already exist, it will be created.

You can use the -c flag to add alert contacts, just as for the uptimerobot new command.

Checking the version number

To see what version of the command-line client you're using, run uptimerobot version.

Viewing debug output

When things aren't going quite as they should, you can add the --debug flag to your command line to see a dump of the HTTP request and response from the server. This is helpful if you want to report problems with the client, for example.

Using the Go library

If the command-line client doesn't do quite what you need, or if you want to use Uptime Robot API access in your own programs, import the library using:

import "github.com/bitfield/uptimerobot/pkg"

Create a new Client object by calling uptimerobot.New() with an API key:

client = uptimerobot.New(apiKey)

Once you have a client, you can use it to call various Uptime Robot API features:

monitors, err := client.AllMonitors()
if err != nil {
        log.Fatal(err)
}
for _, m := range monitors {
        fmt.Println(m)
        fmt.Println()
}

Most API operations use the Monitor struct, which looks like this:

type Monitor struct {
	ID           int64  `json:"id,omitempty"`
        FriendlyName string `json:"friendly_name"`
        URL          string `json:"url"`
        ...
}

For example, to delete a monitor, find the ID of the monitor you want to delete, and pass it to DeleteMonitor():

if err := client.DeleteMonitor(780689017); err != nil {
        log.Fatal(err)
}

To call an Uptime Robot API verb not implemented by the uptimerobot library, you can use the MakeAPICall() method directly, passing it some suitable JSON data:

r := uptimerobot.Response{}
data := []byte(fmt.Sprintf("{\"id\": \"%d\"}", m.ID))
if err := client.MakeAPICall("deleteMonitor", &r, data); err != nil {
    log.Fatal(err)
}
fmt.Println(r.Monitor.ID)

The API response is returned in the Response struct. If the call fails, MakeAPICall() will return the error message. Otherwise, the requested data will be available in the appropriate field of the Response struct:

type Response struct {
        Stat          string         `json:"stat"`
        Account       Account        `json:"account"`
        Monitors      []Monitor      `json:"monitors"`
        Monitor       Monitor        `json:"monitor"`
        AlertContacts []AlertContact `json:"alert_contacts"`
        Error         Error          `json:"error"`
}

For example, when creating a new monitor, the ID of the created monitor will be returned as r.Monitor.ID.

If things aren't working as you expect, you can use the debug facility to dump the raw request and response data from every API call. To do this, set the environment variable UPTIMEROBOT_DEBUG, which will dump debug information to the standard output, or set client.Debug to any io.Writer to send output to that writer.

Here's an example of the debug output shown when creating a new monitor:

POST /v2/newMonitor HTTP/1.1
Host: api.uptimerobot.com
User-Agent: Go-http-client/1.1
Content-Length: 221
Content-Type: application/json
Accept-Encoding: gzip

{
  "alert_contacts": "0335551_0_0-2416450_0_0",
  "api_key": "XXX",
  "format": "json",
  "friendly_name": "Example check",
  "port": 443,
  "type": 1,
  "url": "https://www.example.com"
}

HTTP/2.0 200 OK
Access-Control-Allow-Origin: *
Cf-Ray: 505422654b04dbf3-LHR
Content-Type: application/json; charset=utf-8
Date: Mon, 12 Aug 2019 17:22:57 GMT
Etag: W/"33-NlNt8dOhQvno31TtQYsI0xTJ9w"
Expect-Ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
Set-Cookie: __cfduid=d9ec99b8a777d9f806956432718fb5c81565630577; expires=Tue, 11-Aug-20 17:22:57 GMT; path=/; domain=.uptimerobot.com; HttpOnly
Vary: Accept-Encoding

{"stat":"ok","monitor":{"id":783263671,"status":1}}

Bugs and feature requests

If you find a bug in the uptimerobot client or library, please open an issue. Similarly, if you'd like a feature added or improved, let me know via an issue.

Not all the functionality of the Uptime Robot API is implemented yet.

Pull requests welcome!

Owner
John Arundel
Go writer and mentor. Author, 'For the Love of Go'. Say hello at [email protected]
John Arundel
Comments
  • Pagination for getMonitors

    Pagination for getMonitors

    This adds pagination for getting monitors. It also returns the monitors uptime status based on the reported status from uptimerobot.

    It also updates the CLI command to stream the responses using GetMonitorsChan.

  • Issue when use monitors command

    Issue when use monitors command

    When I run docker run -it --rm bitfield/uptimerobot --apiKey XXXX monitors

    It return 2018/09/26 23:29:39 decoding error: json: cannot unmarshal number into Go struct field Monitor.sub_type of type string

  • Add Dockerfile

    Add Dockerfile

    This PR adds a Dockerfile to build or use uptimerobot easily without a golang environment available.

    It would be useful to publish an image on docker hub as well.

  • Implement pagination for monitors

    Implement pagination for monitors

    The UptimeRobot API paginates responses to getMonitors and getAlertContacts, sending (I think) 50 items at a time. The library needs to handle this and return all results (currently it returns only the first page of results).

  • Refactor, improve JSON handling, and extend tests

    Refactor, improve JSON handling, and extend tests

    • [x] Add basic integration tests
    • [x] Use httptest for unit tests
    • [x] Implement UPTIMEROBOT_DEBUG environment variable
    • [x] Send form data as JSON
    • [x] Implement custom JSON unmarshaler to work around interface{} issue
    • [x] Define constants for monitor types and subtypes
  • Fixes inconsistent API typing for sub_type

    Fixes inconsistent API typing for sub_type

    UptimeRobot API returns inconsistent types on sub_type (just like keyword_type). This is addressed using interface{} instead of int

    A test case has been added.

    Should fix #5

  • Pass API key as an environment variable

    Pass API key as an environment variable

    Passing an API key or password via an argument is not safe as all users on the system can see it in the process list.

    Passing this type of secrets via an environment variable (or config file) is safer. It would be something like:

    $ cat apikey
    UPTIMEROBOT_API_KEY=foobar
    $ source apikey
    $ uptimerobot ...
    
  • Improve new monitor tests

    Improve new monitor tests

    Improve TestCreate method:

    • remove port mapping from HTTP test - HTTP monitor should be created even without port specification
    • add HTTPS test
    • add IMAP test
    • add custom port test
  • Add way to set monitoring interval and contact delay on monitors via cli

    Add way to set monitoring interval and contact delay on monitors via cli

    Hi @bitfield,

    When I create monitors manually I like to set the following features:

    • Set the monitoring interval to 1 minute instead of the default 5.
    • Add a delay to certain contacts. E.g. Pagerduty: If down for 5 minutes, trigger once.

    Would it be possible to add parameters to the CLI ensure and new commands to apply those settings?

Client-go - Clusterpedia-client supports the use of native client-go mode to call the clusterpedia API

clusterpedia-client supports the use of native client-go mode to call the cluste

Dec 5, 2022
A Go client implementing a client-side distributed consumer group client for Amazon Kinesis.
A Go client implementing a client-side distributed consumer group client for Amazon Kinesis.

Kinesumer is a Go client implementing a client-side distributed consumer group client for Amazon Kinesis.

Jan 5, 2023
Nutanix-client-go - Go client for the Nutanix Prism V3 API

nutanix-client-go This repository contains portions of the Nutanix API client code in nutanix/terraform-provider-nutanix. It has been extracted to red

Jan 6, 2022
Aoe4-client - Client library for aoe4 leaderboards etc

AOE4 Client Overview This is a go client used to query AOE4 data from either the

Jan 18, 2022
Go client for the YNAB API. Unofficial. It covers 100% of the resources made available by the YNAB API.

YNAB API Go Library This is an UNOFFICIAL Go client for the YNAB API. It covers 100% of the resources made available by the YNAB API. Installation go

Oct 6, 2022
An API client for the Notion API implemented in Golang

An API client for the Notion API implemented in Golang

Dec 30, 2022
A Wrapper Client for Google Spreadsheet API (Sheets API)

Senmai A Wrapper Client for Google Spreadsheet API (Sheets API) PREPARATION Service Account and Key File Create a service account on Google Cloud Plat

Nov 5, 2021
Go Client Library for Amazon Product Advertising API

go-amazon-product-advertising-api Go Client Library for Amazon Product Advertising API How to Use go get -u github.com/ngs/go-amazon-product-advertisi

Sep 27, 2022
A Go client library for the Twitter 1.1 API

Anaconda Anaconda is a simple, transparent Go package for accessing version 1.1 of the Twitter API. Successful API queries return native Go structs th

Jan 1, 2023
Go client library for interacting with Coinpaprika's API

Coinpaprika API Go Client Usage This library provides convenient way to use coinpaprika.com API in Go. Coinpaprika delivers full market data to the wo

Dec 8, 2022
Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://developer.github.com/v4/).

githubv4 Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql). If you're looking for a client

Dec 26, 2022
This project implements a Go client library for the Hipchat API.

Hipchat This project implements a Go client library for the Hipchat API (API version 2 is not supported). Pull requests are welcome as the API is limi

Jan 3, 2023
May 25, 2021
NotionGo is a Go client library for accessing the Notion API v1.

NotionGo (WIP) NotionGo is a Go client library for accessing the Notion API v1. Installation NotionGo is compatible with modern Go releases in module

May 22, 2021
go-ftx go-ftx is a Go client library for accessing the FTX API

go-ftx go-ftx is a Go client library for accessing the FTX API

Nov 14, 2022
(unofficial) TexTra API client library

go-textra This is a library that translates with みんなの自動翻訳(minnano-jidou-honyaku)@textra's API. You need a textra account. package main import ( "fmt

Dec 13, 2021
A Go client library enabling programs to perform CRUD operations on the goharbor API.

goharbor-client A Go client library enabling programs to perform CRUD operations on the goharbor API. This client library utilizes types generated by

Jan 10, 2022
Simple-Weather-API - Simple weather api app created using golang and Open Weather API key
Simple-Weather-API - Simple weather api app created using golang and Open Weather API key

Simple Weather API Simple weather api app created using golang and Open Weather

Feb 6, 2022
Client for the cloud-iso-client

cloud-iso-client Client for the cloud-iso-client. Register an API token Before using this client library, you need to register an API token under your

Dec 6, 2021