-
Notifications
You must be signed in to change notification settings - Fork 19
/
pxAlertops.go
283 lines (245 loc) · 7.31 KB
/
pxAlertops.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Copyright © 2019 Portworx
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package portworx
import (
"fmt"
"io"
"sort"
"time"
"github.com/portworx/pxc/pkg/util"
api "github.com/libopenstorage/openstorage-sdk-clients/sdk/golang"
prototime "github.com/portworx/pxc/pkg/openstorage/proto/time"
)
type PxAlertOps interface {
GetPxAlerts(cliAlertInputs CliAlertInputs) (AlertResp, error)
DeletePxAlerts(alert string) error
}
type CliAlertInputs struct {
util.BaseFormatOutput
Wide bool
AlertType string
AlertId string
StartTime string
EndTime string
Severity string
ResourceId string
}
type pxAlertOps struct{}
type AlertResp struct {
AlertResp []*api.Alert
AlertNameToId map[string]int64
AlertIdToName map[int64]string
}
type getAlertsOpts struct {
req *api.SdkAlertsEnumerateWithFiltersRequest
}
type delAlertsOpts struct {
req *api.SdkAlertsDeleteRequest
}
type alertsList []*api.Alert
func (g alertsList) Len() int { return len(g) }
func (g alertsList) Less(i, j int) bool { return g[i].Timestamp.Seconds < g[j].Timestamp.Seconds }
func (g alertsList) Swap(i, j int) { g[i], g[j] = g[j], g[i] }
func NewPxAlertOps() PxAlertOps {
return &pxAlertOps{}
}
func (p *pxAlertOps) GetPxAlerts(cliAlertInputs CliAlertInputs) (AlertResp, error) {
alertResp := AlertResp{}
ctx, conn, err := PxConnectDefault()
if err != nil {
return alertResp, err
}
defer conn.Close()
getAlertsGetReq := getAlertsOpts{
req: &api.SdkAlertsEnumerateWithFiltersRequest{},
}
var myAlerts []*api.Alert
var opts []*api.SdkAlertsOption
if (len(cliAlertInputs.StartTime) > 0) && (len(cliAlertInputs.EndTime) > 0) {
st, err := time.Parse(time.RFC3339, cliAlertInputs.StartTime)
if err != nil {
return alertResp, fmt.Errorf("Invaid start-time timestamp format.")
}
et, err := time.Parse(time.RFC3339, cliAlertInputs.EndTime)
if err != nil {
return alertResp, fmt.Errorf("Invaid end-time timestamp format.")
}
opts = append(opts, &api.SdkAlertsOption{
Opt: &api.SdkAlertsOption_TimeSpan{
TimeSpan: &api.SdkAlertsTimeSpan{
StartTime: prototime.TimeToTimestamp(
st),
EndTime: prototime.TimeToTimestamp(et),
},
},
})
}
// Appending severity level if provided
if len(cliAlertInputs.Severity) > 0 {
var severity api.SeverityType
switch cliAlertInputs.Severity {
case "notify":
severity = api.SeverityType_SEVERITY_TYPE_NOTIFY
case "warning", "warn":
severity = api.SeverityType_SEVERITY_TYPE_WARNING
case "alarm":
severity = api.SeverityType_SEVERITY_TYPE_ALARM
default:
return alertResp, fmt.Errorf("Invalid severity level.")
}
opts = append(opts, &api.SdkAlertsOption{
Opt: &api.SdkAlertsOption_MinSeverityType{
MinSeverityType: severity,
},
})
}
alertResp.AlertNameToId = make(map[string]int64)
alertResp.AlertIdToName = make(map[int64]string)
for k, v := range TypeToSpec() {
id := int64(k)
name := v.Name
alertResp.AlertNameToId[name] = id
alertResp.AlertIdToName[id] = name
}
alterType := getAlertType(cliAlertInputs.AlertType)
if len(alterType) == 0 {
return alertResp, fmt.Errorf("Invalid type provided.")
}
for _, resourceType := range alterType {
if len(cliAlertInputs.AlertId) > 0 {
id, ok := alertResp.AlertNameToId[cliAlertInputs.AlertId]
if !ok {
return alertResp, nil
}
getAlertsGetReq.req.Queries = []*api.SdkAlertsQuery{
{
Query: &api.SdkAlertsQuery_AlertTypeQuery{
AlertTypeQuery: &api.SdkAlertsAlertTypeQuery{
ResourceType: resourceType,
AlertType: id,
},
},
Opts: opts,
},
}
} else if len(cliAlertInputs.ResourceId) > 0 {
getAlertsGetReq.req.Queries = make([]*api.SdkAlertsQuery, 0, len(typeToSpec))
// The Portworx Alerts server may have a "bug" where it requires that the
// Alert type is given. To satisfy the server, we have a filter of each alert
// type for that specific resource
for alertType, alertDefinition := range typeToSpec {
if alertDefinition.ResourceType == resourceType {
getAlertsGetReq.req.Queries = append(getAlertsGetReq.req.Queries, &api.SdkAlertsQuery{
Query: &api.SdkAlertsQuery_ResourceIdQuery{
ResourceIdQuery: &api.SdkAlertsResourceIdQuery{
ResourceType: resourceType,
ResourceId: cliAlertInputs.ResourceId,
AlertType: int64(alertType),
},
},
Opts: opts,
})
}
}
} else {
getAlertsGetReq.req.Queries = []*api.SdkAlertsQuery{
{
Query: &api.SdkAlertsQuery_ResourceTypeQuery{
ResourceTypeQuery: &api.SdkAlertsResourceTypeQuery{
ResourceType: resourceType,
},
},
Opts: opts,
},
}
}
// Send request
client := api.NewOpenStorageAlertsClient(conn)
resp, err := client.EnumerateWithFilters(ctx, getAlertsGetReq.req)
if err != nil {
return alertResp, fmt.Errorf("Failed to fetch alerts.")
}
for {
res, err := resp.Recv()
if err == io.EOF {
break
}
if res.GetAlerts() != nil {
myAlerts = append(myAlerts, res.Alerts...)
}
}
}
sort.Sort(alertsList(myAlerts))
alertResp.AlertResp = myAlerts
return alertResp, nil
}
func (p *pxAlertOps) DeletePxAlerts(alert string) error {
alertResp := AlertResp{}
ctx, conn, err := PxConnectDefault()
_ = ctx
if err != nil {
return err
}
defer conn.Close()
delAlertsGetReq := delAlertsOpts{
req: &api.SdkAlertsDeleteRequest{},
}
alertResp.AlertNameToId = make(map[string]int64)
alertResp.AlertIdToName = make(map[int64]string)
for k, v := range TypeToSpec() {
id := int64(k)
name := v.Name
alertResp.AlertNameToId[name] = id
alertResp.AlertIdToName[id] = name
}
// TODO: For now making it all, will change once PR#69 gets merged
alterType := getAlertType(alert)
for _, resourceType := range alterType {
delAlertsGetReq.req.Queries = []*api.SdkAlertsQuery{
{
Query: &api.SdkAlertsQuery_ResourceTypeQuery{
ResourceTypeQuery: &api.SdkAlertsResourceTypeQuery{
ResourceType: resourceType,
},
},
},
}
// Send request
client := api.NewOpenStorageAlertsClient(conn)
_, err = client.Delete(ctx, delAlertsGetReq.req)
if err != nil {
return fmt.Errorf("Failed to delete alerts")
}
}
return err
}
func getAlertType(alr string) []api.ResourceType {
var resourceTypes []api.ResourceType
switch alr {
case "volume":
resourceTypes = append(resourceTypes, api.ResourceType_RESOURCE_TYPE_VOLUME)
case "node":
resourceTypes = append(resourceTypes, api.ResourceType_RESOURCE_TYPE_NODE)
case "cluster":
resourceTypes = append(resourceTypes, api.ResourceType_RESOURCE_TYPE_CLUSTER)
case "drive":
resourceTypes = append(resourceTypes, api.ResourceType_RESOURCE_TYPE_DRIVE)
case "all":
resourceTypes = append(resourceTypes,
api.ResourceType_RESOURCE_TYPE_VOLUME,
api.ResourceType_RESOURCE_TYPE_NODE,
api.ResourceType_RESOURCE_TYPE_CLUSTER,
api.ResourceType_RESOURCE_TYPE_DRIVE)
}
return resourceTypes
}