-
Notifications
You must be signed in to change notification settings - Fork 494
/
nodealert.go
159 lines (141 loc) · 5.6 KB
/
nodealert.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
// Copyright 2019 Yunion
//
// 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 monitor
import (
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/mcclient"
modules "yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
"yunion.io/x/onecloud/pkg/mcclient/options"
)
func init() {
/**
* 创建一条报警规则
*/
type NodealertCreateOptions struct {
Type string `help:"Alert rule type" required:"true" choices:"guest|host"`
Metric string `help:"Metric name, include measurement and field, e.g. vm_cpu.usage_active" required:"true"`
NodeName string `help:"Name of the guest or host" required:"true"`
NodeID string `help:"ID of the guest or host" required:"true"`
Period string `help:"Specify the query time period for the data" required:"true"`
Window string `help:"Specify the query interval for the data" required:"true"`
Threshold float64 `help:"Threshold value of the metric" required:"true"`
Comparator string `help:"Comparison operator for join expressions" choices:">|<|>=|<=|=|!=" required:"true"`
Recipients string `help:"Comma separated recipient ID" required:"true"`
Level string `help:"Alert level" required:"true" choices:"normal|important|fatal"`
Channel string `help:"Ways to send an alarm" required:"true" choices:"email|mobile|dingtalk"`
}
R(&NodealertCreateOptions{}, "nodealert-create", "Create a node alert rule", func(s *mcclient.ClientSession, args *NodealertCreateOptions) error {
params, err := options.StructToParams(args)
if err != nil {
return err
}
rst, err := modules.NodeAlert.Create(s, params)
if err != nil {
return err
}
printObject(rst)
return nil
})
/**
* 修改指定的报警规则
*/
type NodealertUpdateOptions struct {
ID string `help:"ID of the alert rule" required:"true" positional:"true"`
Type string `help:"Alert rule type" choices:"guest|host"`
Metric string `help:"Metric name, include measurement and field, such as vm_cpu.usage_active"`
NodeName string `help:"Name of the guest or host"`
NodeID string `help:"ID of the guest or host"`
Period string `help:"Specify the query time period for the data"`
Window string `help:"Specify the query interval for the data"`
Threshold *float64 `help:"Threshold value of the metric"`
Comparator string `help:"Comparison operator for join expressions" choices:">|<|>=|<=|=|!="`
Recipients string `help:"Comma separated recipient ID"`
Level string `help:"Alert level" choices:"normal|important|fatal"`
Channel string `help:"Ways to send an alarm" choices:"email|mobile"`
}
R(&NodealertUpdateOptions{}, "nodealert-update", "Update the node alert rule", func(s *mcclient.ClientSession, args *NodealertUpdateOptions) error {
params, err := options.StructToParams(args)
if err != nil {
return err
}
rst, err := modules.NodeAlert.Patch(s, args.ID, params)
if err != nil {
return err
}
printObject(rst)
return nil
})
/**
* 删除指定ID的报警规则
*/
type NodealertDeleteOptions struct {
ID []string `help:"ID of node alert" required:"true" positional:"true"`
}
R(&NodealertDeleteOptions{}, "nodealert-delete", "Delete a node alert", func(s *mcclient.ClientSession, args *NodealertDeleteOptions) error {
ret := modules.NodeAlert.BatchDelete(s, args.ID, nil)
printBatchResults(ret, modules.NodeAlert.GetColumns(s))
return nil
})
/**
* 启用指定ID的报警规则状态
*/
type NodealertUpdateStatusOptions struct {
ID string `help:"ID of the node alert" required:"true" positional:"true"`
}
R(&NodealertUpdateStatusOptions{}, "nodealert-enable", "Enable alert rule for the specified ID", func(s *mcclient.ClientSession, args *NodealertUpdateStatusOptions) error {
params := jsonutils.NewDict()
params.Add(jsonutils.NewString("Enabled"), "status")
alarm, err := modules.NodeAlert.Patch(s, args.ID, params)
if err != nil {
return err
}
printObject(alarm)
return nil
})
/**
* 禁用指定ID的报警规则状态
*/
R(&NodealertUpdateStatusOptions{}, "nodealert-disable", "Disaable alert rule for the specified ID", func(s *mcclient.ClientSession, args *NodealertUpdateStatusOptions) error {
params := jsonutils.NewDict()
params.Add(jsonutils.NewString("Disabled"), "status")
alarm, err := modules.NodeAlert.Patch(s, args.ID, params)
if err != nil {
return err
}
printObject(alarm)
return nil
})
/**
* 列出报警规则
*/
type NodealertListOptions struct {
Type string `help:"Alarm rule type" choices:"guest|host"`
Metric string `help:"Metric name, include measurement and field, e.g. vm_cpu.usage_active"`
NodeName string `help:"Name of the guest or host"`
NodeID string `help:"ID of the guest or host"`
options.BaseListOptions
}
R(&NodealertListOptions{}, "nodealert-list", "List node alert", func(s *mcclient.ClientSession, args *NodealertListOptions) error {
params, err := options.ListStructToParams(args)
if err != nil {
return err
}
result, err := modules.NodeAlert.List(s, params)
if err != nil {
return err
}
printList(result, modules.NodeAlert.GetColumns(s))
return nil
})
}