Skip to content

Commit 070bfa9

Browse files
authored
Merge pull request #95 from zianazhao/master
waf、lb_private
2 parents 6cb72bb + 1c6951f commit 070bfa9

File tree

12 files changed

+697
-11
lines changed

12 files changed

+697
-11
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.334
1616
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.334
1717
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cmq v1.0.334
18-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.531
18+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.576
1919
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.334
2020
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.413
2121
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc v1.0.334
@@ -34,6 +34,7 @@ require (
3434
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.413
3535
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.430
3636
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.334
37+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.576 // indirect
3738
github.com/tencentyun/cos-go-sdk-v5 v0.7.35
3839
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
3940
gopkg.in/alecthomas/kingpin.v2 v2.2.6

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.531 h1:E8vq
239239
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.531/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
240240
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.534 h1:ZsJCXIZO2KfmEsrVN6a75VncTuaSkD4PNd1iXZ1I8BU=
241241
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.534/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
242+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.576 h1:6L4MeAnCk1NkuKoSfJvNPxqOrXq/HG9FqV3jl6i5t+w=
243+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.576/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
242244
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.334 h1:ulfSODMy8rpKa8MfnTIPbe5HyOArnlB4RJ1qmpj09to=
243245
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.334/go.mod h1:AqyM/ZZMD7q5mHBqNY9YImbSpEpoEe7E/vrTbUWX+po=
244246
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.413 h1:MomwSkFrSLB16s51Yu1h4JO+p3Pzc1yesIj+oNwAVM0=
@@ -280,6 +282,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.430 h1:Brvrm1Z
280282
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.430/go.mod h1:rJoqfhS+Y0GmdZuS/N4bFXfDJeh/N59W6hojoTRQ6v0=
281283
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.334 h1:rcM2H2e8kqxv7pZcsBdaIMitNd65+3iTM8aK/q6LS7U=
282284
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.334/go.mod h1:SKgeSsIfPEM6BeoIFiGHsWG9UsEXzkK0SkWx51H/OS8=
285+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.576 h1:N9vymXFXpdUx1YNe1D82U0WzV43qg1ZunxjCq5CsygE=
286+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.576/go.mod h1:8wSqOPFWWZabIv00tYbz8WGQIncUc6tC7eN48MLPkik=
283287
github.com/tencentyun/cos-go-sdk-v5 v0.7.35 h1:XVk5GQ4eH1q+DBUJfpaMMdU9TJZWMjwNNwv0PG5nbLQ=
284288
github.com/tencentyun/cos-go-sdk-v5 v0.7.35/go.mod h1:4dCEtLHGh8QPxHEkgq+nFaky7yZxQuYwgSJM87icDaw=
285289
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

pkg/client/client.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import (
3232
tdmq "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq/v20200217"
3333
tse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse/v20201207"
3434
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
35+
waf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125"
3536
cos "github.com/tencentyun/cos-go-sdk-v5"
36-
3737
"github.com/tencentyun/tencentcloud-exporter/pkg/common"
3838
"github.com/tencentyun/tencentcloud-exporter/pkg/config"
3939
)
@@ -318,3 +318,13 @@ func NewGAAPCommonClient(cred common.CredentialIface, conf *config.TencentConfig
318318
cpf.HttpProfile.ReqMethod = "POST"
319319
return apiCommon.NewCommonClient(cred, regions.Guangzhou, cpf)
320320
}
321+
322+
func NewWafClient(cred common.CredentialIface, conf *config.TencentConfig) (*waf.Client, error) {
323+
cpf := profile.NewClientProfile()
324+
if conf.Credential.IsInternal == true {
325+
cpf.HttpProfile.Endpoint = "waf.internal.tencentcloudapi.com"
326+
} else {
327+
cpf.HttpProfile.Endpoint = "waf.tencentcloudapi.com"
328+
}
329+
return waf.NewClient(cred, conf.Credential.Region, cpf)
330+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package collector
2+
3+
import (
4+
"fmt"
5+
"github.com/go-kit/log"
6+
"github.com/go-kit/log/level"
7+
"github.com/tencentyun/tencentcloud-exporter/pkg/common"
8+
"github.com/tencentyun/tencentcloud-exporter/pkg/instance"
9+
"github.com/tencentyun/tencentcloud-exporter/pkg/metric"
10+
"github.com/tencentyun/tencentcloud-exporter/pkg/util"
11+
)
12+
13+
const (
14+
ClbPrivateNamespace = "QCE/LB_PRIVATE"
15+
ClbPrivateInstanceidKey = "vip"
16+
)
17+
18+
var (
19+
ClbPrivateExcludeMetrics = []string{
20+
"ConnRatio", "OverloadCurConn", "SnatFail", // clb_snat_vip
21+
"PvvInpkg", "PvvOutpkg", "PvvConnum", "PvvIntraffic", "PvvNewConn", "PvvOuttraffic", // new_vpcid_proto_vip_vport
22+
"VvIntraffic", "VvInpkg", "VvNewConn", "VvOutpkg", "VvOuttraffic", "VvConnum", // new_vip_vpcid
23+
}
24+
)
25+
26+
func init() {
27+
registerHandler(ClbPrivateNamespace, defaultHandlerEnabled, NewClbPrivateHandler)
28+
}
29+
30+
type ClbPrivateHandler struct {
31+
baseProductHandler
32+
}
33+
34+
func (h *ClbPrivateHandler) IsMetricMetaVaild(meta *metric.TcmMeta) bool {
35+
return true
36+
}
37+
38+
func (h *ClbPrivateHandler) GetNamespace() string {
39+
return ClbPrivateNamespace
40+
}
41+
42+
func (h *ClbPrivateHandler) IsMetricVaild(m *metric.TcmMetric) bool {
43+
if util.IsStrInList(ClbPrivateExcludeMetrics, m.Meta.MetricName) {
44+
return false
45+
}
46+
var dimensions []string
47+
for _, v := range m.Meta.SupportDimensions {
48+
dimensions = append(dimensions, v)
49+
}
50+
if len(dimensions) == 0 {
51+
return false
52+
}
53+
_, ok := excludeMetricName[m.Meta.MetricName]
54+
if ok {
55+
return false
56+
}
57+
p, err := m.Meta.GetPeriod(m.Conf.StatPeriodSeconds)
58+
if err != nil {
59+
return false
60+
}
61+
if p != m.Conf.StatPeriodSeconds {
62+
return false
63+
}
64+
return true
65+
}
66+
func (h *ClbPrivateHandler) GetSeries(m *metric.TcmMetric) ([]*metric.TcmSeries, error) {
67+
if m.Conf.IsIncludeOnlyInstance() {
68+
return h.GetSeriesByOnly(m)
69+
}
70+
71+
if m.Conf.IsIncludeAllInstance() {
72+
return h.GetSeriesByAll(m)
73+
}
74+
75+
if m.Conf.IsCustomQueryDimensions() {
76+
return h.GetSeriesByCustom(m)
77+
}
78+
79+
return nil, fmt.Errorf("must config all_instances or only_include_instances or custom_query_dimensions")
80+
}
81+
82+
func (h *ClbPrivateHandler) GetSeriesByOnly(m *metric.TcmMetric) ([]*metric.TcmSeries, error) {
83+
var slist []*metric.TcmSeries
84+
for _, insId := range m.Conf.OnlyIncludeInstances {
85+
ins, err := h.collector.InstanceRepo.Get(insId)
86+
if err != nil {
87+
level.Error(h.logger).Log("msg", "Instance not found", "id", insId)
88+
continue
89+
}
90+
sl, err := h.getSeriesByMetricType(m, ins)
91+
if err != nil {
92+
level.Error(h.logger).Log("msg", "Create metric series fail",
93+
"metric", m.Meta.MetricName, "instacne", ins.GetInstanceId())
94+
continue
95+
}
96+
slist = append(slist, sl...)
97+
}
98+
return slist, nil
99+
}
100+
101+
func (h *ClbPrivateHandler) GetSeriesByAll(m *metric.TcmMetric) ([]*metric.TcmSeries, error) {
102+
var slist []*metric.TcmSeries
103+
insList, err := h.collector.InstanceRepo.ListByFilters(m.Conf.InstanceFilters)
104+
if err != nil {
105+
return nil, err
106+
}
107+
for _, ins := range insList {
108+
if len(m.Conf.ExcludeInstances) != 0 && util.IsStrInList(m.Conf.ExcludeInstances, ins.GetInstanceId()) {
109+
continue
110+
}
111+
sl, err := h.getSeriesByMetricType(m, ins)
112+
if err != nil {
113+
level.Error(h.logger).Log("msg", "Create metric series fail",
114+
"metric", m.Meta.MetricName, "instacne", ins.GetInstanceId(), "error", err)
115+
continue
116+
}
117+
slist = append(slist, sl...)
118+
}
119+
return slist, nil
120+
}
121+
122+
func (h *ClbPrivateHandler) GetSeriesByCustom(m *metric.TcmMetric) ([]*metric.TcmSeries, error) {
123+
var slist []*metric.TcmSeries
124+
for _, ql := range m.Conf.CustomQueryDimensions {
125+
v, ok := ql[h.monitorQueryKey]
126+
if !ok {
127+
level.Error(h.logger).Log(
128+
"msg", fmt.Sprintf("not found %s in queryDimensions", h.monitorQueryKey),
129+
"ql", fmt.Sprintf("%v", ql))
130+
continue
131+
}
132+
ins, err := h.collector.InstanceRepo.Get(v)
133+
if err != nil {
134+
level.Error(h.logger).Log("msg", "Instance not found", "err", err, "id", v)
135+
continue
136+
}
137+
138+
sl, err := h.getSeriesByMetricType(m, ins)
139+
if err != nil {
140+
level.Error(h.logger).Log("msg", "Create metric series fail",
141+
"metric", m.Meta.MetricName, "instacne", ins.GetInstanceId())
142+
continue
143+
}
144+
slist = append(slist, sl...)
145+
}
146+
return slist, nil
147+
}
148+
149+
func (h *ClbPrivateHandler) getSeriesByMetricType(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) {
150+
var dimensions []string
151+
for _, v := range m.Meta.SupportDimensions {
152+
dimensions = append(dimensions, v)
153+
}
154+
return h.getClbPrivateSeries(m, ins)
155+
}
156+
157+
func (h *ClbPrivateHandler) getClbPrivateSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) {
158+
var series []*metric.TcmSeries
159+
vpcId, err := ins.GetFieldValueByName("VpcId")
160+
if err != nil {
161+
level.Error(h.logger).Log("msg", "ClusterId not found")
162+
}
163+
ql := map[string]string{
164+
h.monitorQueryKey: ins.GetMonitorQueryKey(),
165+
"vpcId": vpcId,
166+
}
167+
s, err := metric.NewTcmSeries(m, ql, ins)
168+
if err != nil {
169+
return nil, err
170+
}
171+
series = append(series, s)
172+
return series, nil
173+
}
174+
175+
func NewClbPrivateHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) {
176+
handler = &ClbPrivateHandler{
177+
baseProductHandler{
178+
monitorQueryKey: ClbPrivateInstanceidKey,
179+
collector: c,
180+
logger: logger,
181+
},
182+
}
183+
return
184+
185+
}

pkg/collector/handler_cynosdb.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ func (h *CynosdbHandler) IsMetricVaild(m *metric.TcmMetric) bool {
4747
return true
4848
}
4949

50-
// func (h *CynosdbHandler) GetSeries(m *metric.TcmMetric) (slist []*metric.TcmSeries, err error) {
51-
// if m.Conf.StatPeriodSeconds < 60 {
52-
// m.Conf.StatPeriodSeconds = 60
53-
// }
54-
// return h.baseProductHandler.GetSeries(m)
55-
// }
56-
5750
func (h *CynosdbHandler) GetSeries(m *metric.TcmMetric) ([]*metric.TcmSeries, error) {
5851
if m.Conf.IsIncludeOnlyInstance() {
5952
return h.GetSeriesByOnly(m)

0 commit comments

Comments
 (0)