forked from zorkian/go-datadog-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitors.go
176 lines (154 loc) · 5.54 KB
/
monitors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* 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"
"net/url"
"strconv"
"strings"
)
type ThresholdCount struct {
Ok *json.Number `json:"ok,omitempty"`
Critical *json.Number `json:"critical,omitempty"`
Warning *json.Number `json:"warning,omitempty"`
Unknown *json.Number `json:"unknown,omitempty"`
CriticalRecovery *json.Number `json:"critical_recovery,omitempty"`
WarningRecovery *json.Number `json:"warning_recovery,omitempty"`
}
type NoDataTimeframe int
func (tf *NoDataTimeframe) UnmarshalJSON(data []byte) error {
s := string(data)
if s == "false" || s == "null" {
*tf = 0
} else {
i, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return err
}
*tf = NoDataTimeframe(i)
}
return nil
}
type Options struct {
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"`
NewHostDelay *int `json:"new_host_delay,omitempty"`
EvaluationDelay *int `json:"evaluation_delay,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"`
}
// Monitor allows watching a metric or check that you care about,
// notifying your team when some defined threshold is exceeded
type Monitor struct {
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"`
Tags []string `json:"tags"`
Options *Options `json:"options,omitempty"`
}
// 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"`
}
// reqMonitors receives a slice of all monitors
type reqMonitors struct {
Monitors []Monitor `json:"monitors,omitempty"`
}
// 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
func (client *Client) CreateMonitor(monitor *Monitor) (*Monitor, error) {
var out Monitor
// TODO: is this more pretty of frowned upon?
if err := client.doJsonRequest("POST", "/v1/monitor", monitor, &out); err != nil {
return nil, err
}
return &out, nil
}
// UpdateMonitor takes a monitor that was previously retrieved through some method
// and sends it back to the server
func (client *Client) UpdateMonitor(monitor *Monitor) error {
return client.doJsonRequest("PUT", fmt.Sprintf("/v1/monitor/%d", *monitor.Id),
monitor, nil)
}
// GetMonitor retrieves a monitor by identifier
func (client *Client) GetMonitor(id int) (*Monitor, error) {
var out Monitor
if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/monitor/%d", id), nil, &out); err != nil {
return nil, err
}
return &out, nil
}
// 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
}
// DeleteMonitor removes a monitor from the system
func (client *Client) DeleteMonitor(id int) error {
return client.doJsonRequest("DELETE", fmt.Sprintf("/v1/monitor/%d", id),
nil, nil)
}
// GetMonitors returns a slice of all monitors
func (client *Client) GetMonitors() ([]Monitor, error) {
var out reqMonitors
if err := client.doJsonRequest("GET", "/v1/monitor", nil, &out.Monitors); err != nil {
return nil, err
}
return out.Monitors, nil
}
// MuteMonitors turns off monitoring notifications
func (client *Client) MuteMonitors() error {
return client.doJsonRequest("POST", "/v1/monitor/mute_all", nil, nil)
}
// UnmuteMonitors turns on monitoring notifications
func (client *Client) UnmuteMonitors() error {
return client.doJsonRequest("POST", "/v1/monitor/unmute_all", nil, nil)
}
// MuteMonitor turns off monitoring notifications for a monitor
func (client *Client) MuteMonitor(id int) error {
return client.doJsonRequest("POST", fmt.Sprintf("/v1/monitor/%d/mute", id), nil, nil)
}
// UnmuteMonitor turns on monitoring notifications for a monitor
func (client *Client) UnmuteMonitor(id int) error {
return client.doJsonRequest("POST", fmt.Sprintf("/v1/monitor/%d/unmute", id), nil, nil)
}