Skip to content

Commit

Permalink
Merge pull request #95 from zianazhao/master
Browse files Browse the repository at this point in the history
waf、lb_private
  • Loading branch information
zero3233 authored Jan 16, 2023
2 parents 6cb72bb + 1c6951f commit 070bfa9
Show file tree
Hide file tree
Showing 12 changed files with 697 additions and 11 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.334
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.334
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cmq v1.0.334
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.531
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.576
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.334
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.413
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc v1.0.334
Expand All @@ -34,6 +34,7 @@ require (
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.413
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.430
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.334
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.576 // indirect
github.com/tencentyun/cos-go-sdk-v5 v0.7.35
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
gopkg.in/alecthomas/kingpin.v2 v2.2.6
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.531 h1:E8vq
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.531/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.534 h1:ZsJCXIZO2KfmEsrVN6a75VncTuaSkD4PNd1iXZ1I8BU=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.534/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.576 h1:6L4MeAnCk1NkuKoSfJvNPxqOrXq/HG9FqV3jl6i5t+w=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.576/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.334 h1:ulfSODMy8rpKa8MfnTIPbe5HyOArnlB4RJ1qmpj09to=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.334/go.mod h1:AqyM/ZZMD7q5mHBqNY9YImbSpEpoEe7E/vrTbUWX+po=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.413 h1:MomwSkFrSLB16s51Yu1h4JO+p3Pzc1yesIj+oNwAVM0=
Expand Down Expand Up @@ -280,6 +282,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.430 h1:Brvrm1Z
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.430/go.mod h1:rJoqfhS+Y0GmdZuS/N4bFXfDJeh/N59W6hojoTRQ6v0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.334 h1:rcM2H2e8kqxv7pZcsBdaIMitNd65+3iTM8aK/q6LS7U=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.334/go.mod h1:SKgeSsIfPEM6BeoIFiGHsWG9UsEXzkK0SkWx51H/OS8=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.576 h1:N9vymXFXpdUx1YNe1D82U0WzV43qg1ZunxjCq5CsygE=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.576/go.mod h1:8wSqOPFWWZabIv00tYbz8WGQIncUc6tC7eN48MLPkik=
github.com/tencentyun/cos-go-sdk-v5 v0.7.35 h1:XVk5GQ4eH1q+DBUJfpaMMdU9TJZWMjwNNwv0PG5nbLQ=
github.com/tencentyun/cos-go-sdk-v5 v0.7.35/go.mod h1:4dCEtLHGh8QPxHEkgq+nFaky7yZxQuYwgSJM87icDaw=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
12 changes: 11 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
tdmq "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq/v20200217"
tse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse/v20201207"
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
waf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125"
cos "github.com/tencentyun/cos-go-sdk-v5"

"github.com/tencentyun/tencentcloud-exporter/pkg/common"
"github.com/tencentyun/tencentcloud-exporter/pkg/config"
)
Expand Down Expand Up @@ -318,3 +318,13 @@ func NewGAAPCommonClient(cred common.CredentialIface, conf *config.TencentConfig
cpf.HttpProfile.ReqMethod = "POST"
return apiCommon.NewCommonClient(cred, regions.Guangzhou, cpf)
}

func NewWafClient(cred common.CredentialIface, conf *config.TencentConfig) (*waf.Client, error) {
cpf := profile.NewClientProfile()
if conf.Credential.IsInternal == true {
cpf.HttpProfile.Endpoint = "waf.internal.tencentcloudapi.com"
} else {
cpf.HttpProfile.Endpoint = "waf.tencentcloudapi.com"
}
return waf.NewClient(cred, conf.Credential.Region, cpf)
}
185 changes: 185 additions & 0 deletions pkg/collector/handler_clb_private.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package collector

import (
"fmt"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/tencentyun/tencentcloud-exporter/pkg/common"
"github.com/tencentyun/tencentcloud-exporter/pkg/instance"
"github.com/tencentyun/tencentcloud-exporter/pkg/metric"
"github.com/tencentyun/tencentcloud-exporter/pkg/util"
)

const (
ClbPrivateNamespace = "QCE/LB_PRIVATE"
ClbPrivateInstanceidKey = "vip"
)

var (
ClbPrivateExcludeMetrics = []string{
"ConnRatio", "OverloadCurConn", "SnatFail", // clb_snat_vip
"PvvInpkg", "PvvOutpkg", "PvvConnum", "PvvIntraffic", "PvvNewConn", "PvvOuttraffic", // new_vpcid_proto_vip_vport
"VvIntraffic", "VvInpkg", "VvNewConn", "VvOutpkg", "VvOuttraffic", "VvConnum", // new_vip_vpcid
}
)

func init() {
registerHandler(ClbPrivateNamespace, defaultHandlerEnabled, NewClbPrivateHandler)
}

type ClbPrivateHandler struct {
baseProductHandler
}

func (h *ClbPrivateHandler) IsMetricMetaVaild(meta *metric.TcmMeta) bool {
return true
}

func (h *ClbPrivateHandler) GetNamespace() string {
return ClbPrivateNamespace
}

func (h *ClbPrivateHandler) IsMetricVaild(m *metric.TcmMetric) bool {
if util.IsStrInList(ClbPrivateExcludeMetrics, m.Meta.MetricName) {
return false
}
var dimensions []string
for _, v := range m.Meta.SupportDimensions {
dimensions = append(dimensions, v)
}
if len(dimensions) == 0 {
return false
}
_, ok := excludeMetricName[m.Meta.MetricName]
if ok {
return false
}
p, err := m.Meta.GetPeriod(m.Conf.StatPeriodSeconds)
if err != nil {
return false
}
if p != m.Conf.StatPeriodSeconds {
return false
}
return true
}
func (h *ClbPrivateHandler) GetSeries(m *metric.TcmMetric) ([]*metric.TcmSeries, error) {
if m.Conf.IsIncludeOnlyInstance() {
return h.GetSeriesByOnly(m)
}

if m.Conf.IsIncludeAllInstance() {
return h.GetSeriesByAll(m)
}

if m.Conf.IsCustomQueryDimensions() {
return h.GetSeriesByCustom(m)
}

return nil, fmt.Errorf("must config all_instances or only_include_instances or custom_query_dimensions")
}

func (h *ClbPrivateHandler) GetSeriesByOnly(m *metric.TcmMetric) ([]*metric.TcmSeries, error) {
var slist []*metric.TcmSeries
for _, insId := range m.Conf.OnlyIncludeInstances {
ins, err := h.collector.InstanceRepo.Get(insId)
if err != nil {
level.Error(h.logger).Log("msg", "Instance not found", "id", insId)
continue
}
sl, err := h.getSeriesByMetricType(m, ins)
if err != nil {
level.Error(h.logger).Log("msg", "Create metric series fail",
"metric", m.Meta.MetricName, "instacne", ins.GetInstanceId())
continue
}
slist = append(slist, sl...)
}
return slist, nil
}

func (h *ClbPrivateHandler) GetSeriesByAll(m *metric.TcmMetric) ([]*metric.TcmSeries, error) {
var slist []*metric.TcmSeries
insList, err := h.collector.InstanceRepo.ListByFilters(m.Conf.InstanceFilters)
if err != nil {
return nil, err
}
for _, ins := range insList {
if len(m.Conf.ExcludeInstances) != 0 && util.IsStrInList(m.Conf.ExcludeInstances, ins.GetInstanceId()) {
continue
}
sl, err := h.getSeriesByMetricType(m, ins)
if err != nil {
level.Error(h.logger).Log("msg", "Create metric series fail",
"metric", m.Meta.MetricName, "instacne", ins.GetInstanceId(), "error", err)
continue
}
slist = append(slist, sl...)
}
return slist, nil
}

func (h *ClbPrivateHandler) GetSeriesByCustom(m *metric.TcmMetric) ([]*metric.TcmSeries, error) {
var slist []*metric.TcmSeries
for _, ql := range m.Conf.CustomQueryDimensions {
v, ok := ql[h.monitorQueryKey]
if !ok {
level.Error(h.logger).Log(
"msg", fmt.Sprintf("not found %s in queryDimensions", h.monitorQueryKey),
"ql", fmt.Sprintf("%v", ql))
continue
}
ins, err := h.collector.InstanceRepo.Get(v)
if err != nil {
level.Error(h.logger).Log("msg", "Instance not found", "err", err, "id", v)
continue
}

sl, err := h.getSeriesByMetricType(m, ins)
if err != nil {
level.Error(h.logger).Log("msg", "Create metric series fail",
"metric", m.Meta.MetricName, "instacne", ins.GetInstanceId())
continue
}
slist = append(slist, sl...)
}
return slist, nil
}

func (h *ClbPrivateHandler) getSeriesByMetricType(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) {
var dimensions []string
for _, v := range m.Meta.SupportDimensions {
dimensions = append(dimensions, v)
}
return h.getClbPrivateSeries(m, ins)
}

func (h *ClbPrivateHandler) getClbPrivateSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) {
var series []*metric.TcmSeries
vpcId, err := ins.GetFieldValueByName("VpcId")
if err != nil {
level.Error(h.logger).Log("msg", "ClusterId not found")
}
ql := map[string]string{
h.monitorQueryKey: ins.GetMonitorQueryKey(),
"vpcId": vpcId,
}
s, err := metric.NewTcmSeries(m, ql, ins)
if err != nil {
return nil, err
}
series = append(series, s)
return series, nil
}

func NewClbPrivateHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) {
handler = &ClbPrivateHandler{
baseProductHandler{
monitorQueryKey: ClbPrivateInstanceidKey,
collector: c,
logger: logger,
},
}
return

}
7 changes: 0 additions & 7 deletions pkg/collector/handler_cynosdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ func (h *CynosdbHandler) IsMetricVaild(m *metric.TcmMetric) bool {
return true
}

// func (h *CynosdbHandler) GetSeries(m *metric.TcmMetric) (slist []*metric.TcmSeries, err error) {
// if m.Conf.StatPeriodSeconds < 60 {
// m.Conf.StatPeriodSeconds = 60
// }
// return h.baseProductHandler.GetSeries(m)
// }

func (h *CynosdbHandler) GetSeries(m *metric.TcmMetric) ([]*metric.TcmSeries, error) {
if m.Conf.IsIncludeOnlyInstance() {
return h.GetSeriesByOnly(m)
Expand Down
Loading

0 comments on commit 070bfa9

Please sign in to comment.