Skip to content

Commit

Permalink
support cbs product (#24)
Browse files Browse the repository at this point in the history
* feat: add cbs product

* docs: add region & product show

Co-authored-by: juexiaolin(林觉霄) <[email protected]>
  • Loading branch information
juexiaolin and juexiaolin(林觉霄) authored Apr 9, 2021
1 parent 2ee85f9 commit 4d52db5
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 6 deletions.
16 changes: 11 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
cbs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
cdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320"
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
Expand All @@ -11,6 +12,7 @@ import (
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412"
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"

"github.com/tencentyun/tencentcloud-exporter/pkg/config"
)

Expand All @@ -32,7 +34,6 @@ func NewMongodbClient(conf *config.TencentConfig) (*mongodb.Client, error) {
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "mongodb.tencentcloudapi.com"
return mongodb.NewClient(credential, conf.Credential.Region, cpf)

}

func NewCdbClient(conf *config.TencentConfig) (*cdb.Client, error) {
Expand All @@ -43,7 +44,6 @@ func NewCdbClient(conf *config.TencentConfig) (*cdb.Client, error) {
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "cdb.tencentcloudapi.com"
return cdb.NewClient(credential, conf.Credential.Region, cpf)

}

func NewCvmClient(conf *config.TencentConfig) (*cvm.Client, error) {
Expand All @@ -54,7 +54,6 @@ func NewCvmClient(conf *config.TencentConfig) (*cvm.Client, error) {
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "cvm.tencentcloudapi.com"
return cvm.NewClient(credential, conf.Credential.Region, cpf)

}

func NewRedisClient(conf *config.TencentConfig) (*redis.Client, error) {
Expand All @@ -75,7 +74,6 @@ func NewDcClient(conf *config.TencentConfig) (*dc.Client, error) {
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "dc.tencentcloudapi.com"
return dc.NewClient(credential, conf.Credential.Region, cpf)

}

func NewClbClient(conf *config.TencentConfig) (*clb.Client, error) {
Expand All @@ -86,7 +84,6 @@ func NewClbClient(conf *config.TencentConfig) (*clb.Client, error) {
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "clb.tencentcloudapi.com"
return clb.NewClient(credential, conf.Credential.Region, cpf)

}

func NewVpvClient(conf *config.TencentConfig) (*vpc.Client, error) {
Expand All @@ -97,5 +94,14 @@ func NewVpvClient(conf *config.TencentConfig) (*vpc.Client, error) {
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "vpc.tencentcloudapi.com"
return vpc.NewClient(credential, conf.Credential.Region, cpf)
}

func NewCbsClient(conf *config.TencentConfig) (*cbs.Client, error) {
credential := common.NewCredential(
conf.Credential.AccessKey,
conf.Credential.SecretKey,
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "cbs.tencentcloudapi.com"
return cbs.NewClient(credential, conf.Credential.Region, cpf)
}
43 changes: 43 additions & 0 deletions pkg/collector/handler_cbs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package collector

import (
"github.com/go-kit/kit/log"
"github.com/tencentyun/tencentcloud-exporter/pkg/metric"
)

const (
CbsNamespace = "QCE/BLOCK_STORAGE"
CbsInstanceidKey = "diskId"
)

func init() {
registerHandler(CbsNamespace, defaultHandlerEnabled, NewCbsHandler)
}

type cbsHandler struct {
baseProductHandler
}

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

func (h *cbsHandler) GetNamespace() string {
return CbsNamespace
}

func (h *cbsHandler) IsMetricVaild(m *metric.TcmMetric) bool {
return true
}

func NewCbsHandler(c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) {
handler = &cbsHandler{
baseProductHandler{
monitorQueryKey: CbsInstanceidKey,
collector: c,
logger: logger,
},
}
return

}
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var (
"nat": "QCE/NAT_GATEWAY",
"cos": "QCE/COS",
"cdn": "QCE/CDN",
"cbs": "QCE/BLOCK_STORAGE",
}

SupportStatisticsTypes = map[string]bool{
Expand Down
34 changes: 34 additions & 0 deletions pkg/instance/instance_cbs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package instance

import (
"fmt"
"reflect"

sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
)

type CbsTcInstance struct {
baseTcInstance
meta *sdk.Disk
}

func (ins *CbsTcInstance) GetMeta() interface{} {
return ins.meta
}

func NewCbsTcInstance(instanceId string, meta *sdk.Disk) (ins *CbsTcInstance, err error) {
if instanceId == "" {
return nil, fmt.Errorf("instanceId is empty ")
}
if meta == nil {
return nil, fmt.Errorf("meta is empty ")
}
ins = &CbsTcInstance{
baseTcInstance: baseTcInstance{
instanceId: instanceId,
value: reflect.ValueOf(*meta),
},
meta: meta,
}
return
}
91 changes: 91 additions & 0 deletions pkg/instance/repository_cbs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package instance

import (
"fmt"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
"github.com/tencentyun/tencentcloud-exporter/pkg/client"
"github.com/tencentyun/tencentcloud-exporter/pkg/config"
)

func init() {
registerRepository("QCE/BLOCK_STORAGE", NewCbsTcInstanceRepository)
}

type CbsTcInstanceRepository struct {
client *sdk.Client
logger log.Logger
}

func (repo *CbsTcInstanceRepository) GetInstanceKey() string {
return "DiskId"
}

func (repo *CbsTcInstanceRepository) Get(id string) (instance TcInstance, err error) {
req := sdk.NewDescribeDisksRequest()
req.DiskIds = []*string{&id}
resp, err := repo.client.DescribeDisks(req)
if err != nil {
return
}
if len(resp.Response.DiskSet) != 1 {
return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id)
}
meta := resp.Response.DiskSet[0]
instance, err = NewCbsTcInstance(id, meta)
if err != nil {
return
}
return
}

func (repo *CbsTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) {
return
}

func (repo *CbsTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) {
req := sdk.NewDescribeDisksRequest()
var offset uint64 = 0
var limit uint64 = 100
var total uint64 = 0

req.Offset = &offset
req.Limit = &limit

getMoreInstances:
resp, err := repo.client.DescribeDisks(req)
if err != nil {
return
}
if total == 0 {
total = *resp.Response.TotalCount
}
for _, meta := range resp.Response.DiskSet {
ins, e := NewCbsTcInstance(*meta.DiskId, meta)
if e != nil {
level.Error(repo.logger).Log("msg", "Create cbs instance fail", "id", *meta.DiskId)
continue
}
instances = append(instances, ins)
}
offset += limit
if offset < total {
req.Offset = &offset
goto getMoreInstances
}
return
}

func NewCbsTcInstanceRepository(c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) {
cli, err := client.NewCbsClient(c)
if err != nil {
return
}
repo = &CbsTcInstanceRepository{
client: cli,
logger: logger,
}
return
}
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
MongoDB |QCE/CMONGO|[指标详情](https://cloud.tencent.com/document/product/248/45104)
CDB|QCE/CDB|[指标详情](https://cloud.tencent.com/document/product/248/45147)
Redis标准版|QCE/REDIS|[指标详情](https://cloud.tencent.com/document/product/248/45111)
Redis集群版|QCE/REDIS|[指标详情](https://cloud.tencent.com/document/product/248/45111)
Redis集群版|QCE/REDIS_CLUSTER|[指标详情](https://cloud.tencent.com/document/product/248/45111)
Redis内存版监控指标|QCE/REDIS_MEM|[指标详情](https://cloud.tencent.com/document/product/248/49729)
CVM|QCE/CVM|[指标详情](https://cloud.tencent.com/document/product/248/6843)
COS|QCE/COS|[指标详情](https://cloud.tencent.com/document/product/248/45140)
Expand All @@ -20,6 +20,7 @@ CLB(7层)|QCE/LOADBALANCE|[指标详情](https://cloud.tencent.com/document/prod
NAT|QCE/NAT_GATEWAY|[指标详情](https://cloud.tencent.com/document/product/248/45069)
物理专线|QCE/DC|[指标详情](https://cloud.tencent.com/document/product/248/45102)
专用通道|QCE/DCX|[指标详情](https://cloud.tencent.com/document/product/248/45101)
云硬盘|QCE/BLOCK_STORAGE|[指标详情](https://cloud.tencent.com/document/product/248/45411)

`后续会有更多的产品支持`

Expand Down Expand Up @@ -126,6 +127,8 @@ export TENCENTCLOUD_SECRET_KEY="YOUR_ACCESS_SECRET"
export TENCENTCLOUD_REGION="REGION"
```

5. **region**
地域可选值参考[地域可选值](https://cloud.tencent.com/document/api/248/30346#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8)
## 四、qcloud_exporter支持的命令行参数说明

命令行参数|说明|默认值
Expand Down

0 comments on commit 4d52db5

Please sign in to comment.