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 {
|
|
|
|
Ok json.Number `json:"ok,omitempty"`
|
|
|
|
Critical json.Number `json:"critical,omitempty"`
|
|
|
|
Warning json.Number `json:"warning,omitempty"`
|
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
NotifyAudit bool `json:"notify_audit,omitempty"`
|
|
|
|
NotifyNoData bool `json:"notify_no_data,omitempty"`
|
|
|
|
RenotifyInterval int `json:"renotify_interval,omitempty"`
|
|
|
|
Silenced map[string]int `json:"silenced,omitempty"`
|
|
|
|
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 {
|
2016-05-19 10:29:23 +02:00
|
|
|
Creator Creator `json:"creator,omitempty"`
|
2016-02-07 23:16:07 +01:00
|
|
|
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"`
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
|
|
|
Options Options `json:"options,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// Creator contains the creator of the monitor
|
|
|
|
type Creator struct {
|
|
|
|
Email string `json:"email,omitempty"`
|
|
|
|
Handle string `json:"handle,omitempty"`
|
|
|
|
Id int `json:"id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
}
|
|
|
|
|
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-02-07 23:16:07 +01:00
|
|
|
func (self *Client) CreateMonitor(monitor *Monitor) (*Monitor, error) {
|
|
|
|
var out Monitor
|
|
|
|
err := self.doJsonRequest("POST", "/v1/monitor", monitor, &out)
|
|
|
|
if err != nil {
|
|
|
|
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-02-07 23:16:07 +01:00
|
|
|
func (self *Client) UpdateMonitor(monitor *Monitor) error {
|
|
|
|
return self.doJsonRequest("PUT", fmt.Sprintf("/v1/monitor/%d", monitor.Id),
|
|
|
|
monitor, nil)
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// GetMonitor retrieves a monitor by identifier
|
2016-02-07 23:16:07 +01:00
|
|
|
func (self *Client) GetMonitor(id int) (*Monitor, error) {
|
|
|
|
var out Monitor
|
|
|
|
err := self.doJsonRequest("GET", fmt.Sprintf("/v1/monitor/%d", id), nil, &out)
|
|
|
|
if err != nil {
|
|
|
|
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-02-07 23:16:07 +01:00
|
|
|
func (self *Client) DeleteMonitor(id int) error {
|
|
|
|
return self.doJsonRequest("DELETE", fmt.Sprintf("/v1/monitor/%d", id),
|
|
|
|
nil, nil)
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// GetMonitors returns a slice of all monitors
|
2016-02-07 23:16:07 +01:00
|
|
|
func (self *Client) GetMonitors() ([]Monitor, error) {
|
|
|
|
var out reqMonitors
|
|
|
|
err := self.doJsonRequest("GET", "/v1/monitor", nil, &out.Monitors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out.Monitors, nil
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// MuteMonitors turns off monitoring notifications
|
2016-02-07 23:16:07 +01:00
|
|
|
func (self *Client) MuteMonitors() error {
|
|
|
|
return self.doJsonRequest("POST", "/v1/monitor/mute_all", nil, nil)
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// UnmuteMonitors turns on monitoring notifications
|
2016-02-07 23:16:07 +01:00
|
|
|
func (self *Client) UnmuteMonitors() error {
|
|
|
|
return self.doJsonRequest("POST", "/v1/monitor/unmute_all", nil, nil)
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// MuteMonitor turns off monitoring notifications for a monitor
|
2016-02-07 23:16:07 +01:00
|
|
|
func (self *Client) MuteMonitor(id int) error {
|
|
|
|
return self.doJsonRequest("POST", fmt.Sprintf("/v1/monitor/%d/mute", id), nil, nil)
|
|
|
|
}
|
|
|
|
|
2016-05-19 10:29:23 +02:00
|
|
|
// UnmuteMonitor turns on monitoring notifications for a monitor
|
2016-02-07 23:16:07 +01:00
|
|
|
func (self *Client) UnmuteMonitor(id int) error {
|
|
|
|
return self.doJsonRequest("POST", fmt.Sprintf("/v1/monitor/%d/unmute", id), nil, nil)
|
|
|
|
}
|