2016-02-07 23:16:07 +01:00
|
|
|
/*
|
|
|
|
* Datadog API for Go
|
|
|
|
*
|
|
|
|
* Please see the included LICENSE file for licensing information.
|
|
|
|
*
|
|
|
|
* Copyright 2013 by authors and contributors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package datadog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-12-05 14:16:47 +01:00
|
|
|
"net/url"
|
2016-08-29 22:30:31 +02:00
|
|
|
"strconv"
|
2016-12-05 14:16:47 +01:00
|
|
|
"strings"
|
2016-02-07 23:16:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ThresholdCount struct {
|
2017-02-20 13:48:32 +01:00
|
|
|
Ok *json.Number `json:"ok,omitempty"`
|
|
|
|
Critical *json.Number `json:"critical,omitempty"`
|
|
|
|
Warning *json.Number `json:"warning,omitempty"`
|
2016-02-07 23:16:07 +01:00
|
|
|
}
|
|
|
|
|
2016-08-29 22:30:31 +02:00
|
|
|
type NoDataTimeframe int
|
|
|
|
|
|
|
|
func (tf *NoDataTimeframe) UnmarshalJSON(data []byte) error {
|
|
|
|
s := string(data)
|
|
|
|
if s == "false" {
|
|
|
|
*tf = 0
|
|
|
|
} else {
|
|
|
|
i, err := strconv.ParseInt(s, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*tf = NoDataTimeframe(i)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-07 23:16:07 +01:00
|
|
|
type Options struct {
|
2016-08-29 22:30:31 +02:00
|
|
|
NoDataTimeframe NoDataTimeframe `json:"no_data_timeframe,omitempty"`
|
2017-02-20 13:48:32 +01:00
|
|
|
NotifyAudit *bool `json:"notify_audit,omitempty"`
|
|
|
|
NotifyNoData *bool `json:"notify_no_data,omitempty"`
|
|
|
|
RenotifyInterval *int `json:"renotify_interval,omitempty"`
|
2017-02-17 16:08:31 +01:00
|
|
|
NewHostDelay *int `json:"new_host_delay,omitempty"`
|
2017-06-05 13:47:15 +02:00
|
|
|
EvaluationDelay *int `json:"evaluation_delay,omitempty"`
|
2016-08-29 22:30:31 +02:00
|
|
|
Silenced map[string]int `json:"silenced,omitempty"`
|
2017-02-20 13:48:32 +01:00
|
|
|
TimeoutH *int `json:"timeout_h,omitempty"`
|
|
|
|
EscalationMessage *string `json:"escalation_message,omitempty"`
|
|
|
|
Thresholds *ThresholdCount `json:"thresholds,omitempty"`
|
|
|
|
IncludeTags *bool `json:"include_tags,omitempty"`
|
|
|
|
RequireFullWindow *bool `json:"require_full_window,omitempty"`
|
|
|
|
Locked *bool `json:"locked,omitempty"`
|
2016-02-07 23:16:07 +01:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// Monitor allows watching a metric or check that you care about,
|
|
|
|
// notifying your team when some defined threshold is exceeded
|
2016-02-07 23:16:07 +01:00
|
|
|
type Monitor struct {
|
2017-02-20 13:48:32 +01:00
|
|
|
Creator *Creator `json:"creator,omitempty"`
|
|
|
|
Id *int `json:"id,omitempty"`
|
|
|
|
Type *string `json:"type,omitempty"`
|
|
|
|
Query *string `json:"query,omitempty"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
Message *string `json:"message,omitempty"`
|
2016-02-07 23:16:07 +01:00
|
|
|
Tags []string `json:"tags,omitempty"`
|
2017-02-20 13:48:32 +01:00
|
|
|
Options *Options `json:"options,omitempty"`
|
2016-02-07 23:16:07 +01:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// Creator contains the creator of the monitor
|
|
|
|
type Creator struct {
|
2017-02-20 13:48:32 +01:00
|
|
|
Email *string `json:"email,omitempty"`
|
|
|
|
Handle *string `json:"handle,omitempty"`
|
|
|
|
Id *int `json:"id,omitempty"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
2016-05-19 10:29:23 +02:00
|
|
|
}
|
|
|
|
|
2016-02-07 23:16:07 +01:00
|
|
|
// reqMonitors receives a slice of all monitors
|
|
|
|
type reqMonitors struct {
|
|
|
|
Monitors []Monitor `json:"monitors,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// CreateMonitor adds a new monitor to the system. This returns a pointer to a
|
|
|
|
// monitor so you can pass that to UpdateMonitor later if needed
|
2016-12-08 11:17:42 +01:00
|
|
|
func (client *Client) CreateMonitor(monitor *Monitor) (*Monitor, error) {
|
2016-02-07 23:16:07 +01:00
|
|
|
var out Monitor
|
2016-12-08 11:17:42 +01:00
|
|
|
// TODO: is this more pretty of frowned upon?
|
|
|
|
if err := client.doJsonRequest("POST", "/v1/monitor", monitor, &out); err != nil {
|
2016-02-07 23:16:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// UpdateMonitor takes a monitor that was previously retrieved through some method
|
|
|
|
// and sends it back to the server
|
2016-12-08 11:17:42 +01:00
|
|
|
func (client *Client) UpdateMonitor(monitor *Monitor) error {
|
2017-02-20 13:48:32 +01:00
|
|
|
return client.doJsonRequest("PUT", fmt.Sprintf("/v1/monitor/%d", *monitor.Id),
|
2016-02-07 23:16:07 +01:00
|
|
|
monitor, nil)
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// GetMonitor retrieves a monitor by identifier
|
2016-12-08 11:17:42 +01:00
|
|
|
func (client *Client) GetMonitor(id int) (*Monitor, error) {
|
2016-02-07 23:16:07 +01:00
|
|
|
var out Monitor
|
2016-12-08 11:17:42 +01:00
|
|
|
if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/monitor/%d", id), nil, &out); err != nil {
|
2016-02-07 23:16:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
2016-12-05 14:16:47 +01:00
|
|
|
// GetMonitor retrieves monitors by name
|
|
|
|
func (self *Client) GetMonitorsByName(name string) ([]Monitor, error) {
|
|
|
|
var out reqMonitors
|
|
|
|
query, err := url.ParseQuery(fmt.Sprintf("name=%v", name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = self.doJsonRequest("GET", fmt.Sprintf("/v1/monitor?%v", query.Encode()), nil, &out.Monitors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out.Monitors, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMonitor retrieves monitors by a slice of tags
|
|
|
|
func (self *Client) GetMonitorsByTags(tags []string) ([]Monitor, error) {
|
|
|
|
var out reqMonitors
|
|
|
|
query, err := url.ParseQuery(fmt.Sprintf("monitor_tags=%v", strings.Join(tags, ",")))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = self.doJsonRequest("GET", fmt.Sprintf("/v1/monitor?%v", query.Encode()), nil, &out.Monitors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out.Monitors, nil
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// DeleteMonitor removes a monitor from the system
|
2016-12-08 11:17:42 +01:00
|
|
|
func (client *Client) DeleteMonitor(id int) error {
|
|
|
|
return client.doJsonRequest("DELETE", fmt.Sprintf("/v1/monitor/%d", id),
|
2016-02-07 23:16:07 +01:00
|
|
|
nil, nil)
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// GetMonitors returns a slice of all monitors
|
2016-12-08 11:17:42 +01:00
|
|
|
func (client *Client) GetMonitors() ([]Monitor, error) {
|
2016-02-07 23:16:07 +01:00
|
|
|
var out reqMonitors
|
2016-12-08 11:17:42 +01:00
|
|
|
if err := client.doJsonRequest("GET", "/v1/monitor", nil, &out.Monitors); err != nil {
|
2016-02-07 23:16:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out.Monitors, nil
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// MuteMonitors turns off monitoring notifications
|
2016-12-08 11:17:42 +01:00
|
|
|
func (client *Client) MuteMonitors() error {
|
|
|
|
return client.doJsonRequest("POST", "/v1/monitor/mute_all", nil, nil)
|
2016-02-07 23:16:07 +01:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// UnmuteMonitors turns on monitoring notifications
|
2016-12-08 11:17:42 +01:00
|
|
|
func (client *Client) UnmuteMonitors() error {
|
|
|
|
return client.doJsonRequest("POST", "/v1/monitor/unmute_all", nil, nil)
|
2016-02-07 23:16:07 +01:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// MuteMonitor turns off monitoring notifications for a monitor
|
2016-12-08 11:17:42 +01:00
|
|
|
func (client *Client) MuteMonitor(id int) error {
|
|
|
|
return client.doJsonRequest("POST", fmt.Sprintf("/v1/monitor/%d/mute", id), nil, nil)
|
2016-02-07 23:16:07 +01:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// UnmuteMonitor turns on monitoring notifications for a monitor
|
2016-12-08 11:17:42 +01:00
|
|
|
func (client *Client) UnmuteMonitor(id int) error {
|
|
|
|
return client.doJsonRequest("POST", fmt.Sprintf("/v1/monitor/%d/unmute", id), nil, nil)
|
2016-02-07 23:16:07 +01:00
|
|
|
}
|