-
Notifications
You must be signed in to change notification settings - Fork 496
/
meteralert.go
113 lines (101 loc) · 3.62 KB
/
meteralert.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
// 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 MeterAlertCreateOptions struct {
Type string `help:"Alert rule type" required:"true" choices:"balance|resFee|monthFee"`
Threshold float64 `help:"Threshold value of the metric" required:"true"`
Comparator string `help:"Comparison operator for join expressions" required:"true" choices:">|<|>=|<=|=|!="`
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"`
Provider string `help:"Name of the cloud platform"`
AccountId string `help:"ID of the cloud platform"`
ProjectId string `help:"ID of the project" required:"true"`
}
R(&MeterAlertCreateOptions{}, "meteralert-create", "Create a meter alert rule", func(s *mcclient.ClientSession, args *MeterAlertCreateOptions) error {
params, err := options.StructToParams(args)
if err != nil {
return err
}
rst, err := modules.MeterAlert.Create(s, params)
if err != nil {
return err
}
printObject(rst)
return nil
})
/**
* 删除指定ID的报警规则
*/
type MeterAlertDeleteOptions struct {
ID string `help:"ID of alarm"`
}
R(&MeterAlertDeleteOptions{}, "meteralert-delete", "Delete a meter alert", func(s *mcclient.ClientSession, args *MeterAlertDeleteOptions) error {
alarm, e := modules.MeterAlert.Delete(s, args.ID, nil)
if e != nil {
return e
}
printObject(alarm)
return nil
})
/**
* 修改指定ID的报警规则状态
*/
type MeterAlertUpdateOptions struct {
ID string `help:"ID of the meter alert"`
STATUS string `help:"Name of the new alarm" choices:"Enabled|Disabled"`
}
R(&MeterAlertUpdateOptions{}, "meteralert-change-status", "Change status of a meter alert", func(s *mcclient.ClientSession, args *MeterAlertUpdateOptions) error {
params := jsonutils.NewDict()
params.Add(jsonutils.NewString(args.STATUS), "status")
alarm, err := modules.MeterAlert.Patch(s, args.ID, params)
if err != nil {
return err
}
printObject(alarm)
return nil
})
/**
* 列出报警规则
*/
type MeterAlertListOptions struct {
Type string `help:"Alarm rule type" choices:"balance|resFee|monthFee"`
CloudProvider string `help:"Name of cloud provider, case sensitive"`
AccountId string `help:"Cloud account ID"`
ProjectId string `help:"ID of the project" required:"true"`
options.BaseListOptions
}
R(&MeterAlertListOptions{}, "meteralert-list", "List meter alert", func(s *mcclient.ClientSession, args *MeterAlertListOptions) error {
params, err := options.ListStructToParams(args)
if err != nil {
return err
}
result, err := modules.MeterAlert.List(s, params)
if err != nil {
return err
}
printList(result, modules.MeterAlert.GetColumns(s))
return nil
})
}