Skip to content

Commit 4d52db5

Browse files
juexiaolinjuexiaolin(林觉霄)
andauthored
support cbs product (#24)
* feat: add cbs product * docs: add region & product show Co-authored-by: juexiaolin(林觉霄) <[email protected]>
1 parent 2ee85f9 commit 4d52db5

File tree

6 files changed

+184
-6
lines changed

6 files changed

+184
-6
lines changed

pkg/client/client.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package client
22

33
import (
4+
cbs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
45
cdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320"
56
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
67
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
@@ -11,6 +12,7 @@ import (
1112
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
1213
redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412"
1314
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
15+
1416
"github.com/tencentyun/tencentcloud-exporter/pkg/config"
1517
)
1618

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

3839
func NewCdbClient(conf *config.TencentConfig) (*cdb.Client, error) {
@@ -43,7 +44,6 @@ func NewCdbClient(conf *config.TencentConfig) (*cdb.Client, error) {
4344
cpf := profile.NewClientProfile()
4445
cpf.HttpProfile.Endpoint = "cdb.tencentcloudapi.com"
4546
return cdb.NewClient(credential, conf.Credential.Region, cpf)
46-
4747
}
4848

4949
func NewCvmClient(conf *config.TencentConfig) (*cvm.Client, error) {
@@ -54,7 +54,6 @@ func NewCvmClient(conf *config.TencentConfig) (*cvm.Client, error) {
5454
cpf := profile.NewClientProfile()
5555
cpf.HttpProfile.Endpoint = "cvm.tencentcloudapi.com"
5656
return cvm.NewClient(credential, conf.Credential.Region, cpf)
57-
5857
}
5958

6059
func NewRedisClient(conf *config.TencentConfig) (*redis.Client, error) {
@@ -75,7 +74,6 @@ func NewDcClient(conf *config.TencentConfig) (*dc.Client, error) {
7574
cpf := profile.NewClientProfile()
7675
cpf.HttpProfile.Endpoint = "dc.tencentcloudapi.com"
7776
return dc.NewClient(credential, conf.Credential.Region, cpf)
78-
7977
}
8078

8179
func NewClbClient(conf *config.TencentConfig) (*clb.Client, error) {
@@ -86,7 +84,6 @@ func NewClbClient(conf *config.TencentConfig) (*clb.Client, error) {
8684
cpf := profile.NewClientProfile()
8785
cpf.HttpProfile.Endpoint = "clb.tencentcloudapi.com"
8886
return clb.NewClient(credential, conf.Credential.Region, cpf)
89-
9087
}
9188

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

99+
func NewCbsClient(conf *config.TencentConfig) (*cbs.Client, error) {
100+
credential := common.NewCredential(
101+
conf.Credential.AccessKey,
102+
conf.Credential.SecretKey,
103+
)
104+
cpf := profile.NewClientProfile()
105+
cpf.HttpProfile.Endpoint = "cbs.tencentcloudapi.com"
106+
return cbs.NewClient(credential, conf.Credential.Region, cpf)
101107
}

pkg/collector/handler_cbs.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package collector
2+
3+
import (
4+
"github.com/go-kit/kit/log"
5+
"github.com/tencentyun/tencentcloud-exporter/pkg/metric"
6+
)
7+
8+
const (
9+
CbsNamespace = "QCE/BLOCK_STORAGE"
10+
CbsInstanceidKey = "diskId"
11+
)
12+
13+
func init() {
14+
registerHandler(CbsNamespace, defaultHandlerEnabled, NewCbsHandler)
15+
}
16+
17+
type cbsHandler struct {
18+
baseProductHandler
19+
}
20+
21+
func (h *cbsHandler) IsMetricMetaVaild(meta *metric.TcmMeta) bool {
22+
return true
23+
}
24+
25+
func (h *cbsHandler) GetNamespace() string {
26+
return CbsNamespace
27+
}
28+
29+
func (h *cbsHandler) IsMetricVaild(m *metric.TcmMetric) bool {
30+
return true
31+
}
32+
33+
func NewCbsHandler(c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) {
34+
handler = &cbsHandler{
35+
baseProductHandler{
36+
monitorQueryKey: CbsInstanceidKey,
37+
collector: c,
38+
logger: logger,
39+
},
40+
}
41+
return
42+
43+
}

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var (
4444
"nat": "QCE/NAT_GATEWAY",
4545
"cos": "QCE/COS",
4646
"cdn": "QCE/CDN",
47+
"cbs": "QCE/BLOCK_STORAGE",
4748
}
4849

4950
SupportStatisticsTypes = map[string]bool{

pkg/instance/instance_cbs.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package instance
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
7+
sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
8+
)
9+
10+
type CbsTcInstance struct {
11+
baseTcInstance
12+
meta *sdk.Disk
13+
}
14+
15+
func (ins *CbsTcInstance) GetMeta() interface{} {
16+
return ins.meta
17+
}
18+
19+
func NewCbsTcInstance(instanceId string, meta *sdk.Disk) (ins *CbsTcInstance, err error) {
20+
if instanceId == "" {
21+
return nil, fmt.Errorf("instanceId is empty ")
22+
}
23+
if meta == nil {
24+
return nil, fmt.Errorf("meta is empty ")
25+
}
26+
ins = &CbsTcInstance{
27+
baseTcInstance: baseTcInstance{
28+
instanceId: instanceId,
29+
value: reflect.ValueOf(*meta),
30+
},
31+
meta: meta,
32+
}
33+
return
34+
}

pkg/instance/repository_cbs.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package instance
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/go-kit/kit/log"
7+
"github.com/go-kit/kit/log/level"
8+
sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
9+
"github.com/tencentyun/tencentcloud-exporter/pkg/client"
10+
"github.com/tencentyun/tencentcloud-exporter/pkg/config"
11+
)
12+
13+
func init() {
14+
registerRepository("QCE/BLOCK_STORAGE", NewCbsTcInstanceRepository)
15+
}
16+
17+
type CbsTcInstanceRepository struct {
18+
client *sdk.Client
19+
logger log.Logger
20+
}
21+
22+
func (repo *CbsTcInstanceRepository) GetInstanceKey() string {
23+
return "DiskId"
24+
}
25+
26+
func (repo *CbsTcInstanceRepository) Get(id string) (instance TcInstance, err error) {
27+
req := sdk.NewDescribeDisksRequest()
28+
req.DiskIds = []*string{&id}
29+
resp, err := repo.client.DescribeDisks(req)
30+
if err != nil {
31+
return
32+
}
33+
if len(resp.Response.DiskSet) != 1 {
34+
return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id)
35+
}
36+
meta := resp.Response.DiskSet[0]
37+
instance, err = NewCbsTcInstance(id, meta)
38+
if err != nil {
39+
return
40+
}
41+
return
42+
}
43+
44+
func (repo *CbsTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) {
45+
return
46+
}
47+
48+
func (repo *CbsTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) {
49+
req := sdk.NewDescribeDisksRequest()
50+
var offset uint64 = 0
51+
var limit uint64 = 100
52+
var total uint64 = 0
53+
54+
req.Offset = &offset
55+
req.Limit = &limit
56+
57+
getMoreInstances:
58+
resp, err := repo.client.DescribeDisks(req)
59+
if err != nil {
60+
return
61+
}
62+
if total == 0 {
63+
total = *resp.Response.TotalCount
64+
}
65+
for _, meta := range resp.Response.DiskSet {
66+
ins, e := NewCbsTcInstance(*meta.DiskId, meta)
67+
if e != nil {
68+
level.Error(repo.logger).Log("msg", "Create cbs instance fail", "id", *meta.DiskId)
69+
continue
70+
}
71+
instances = append(instances, ins)
72+
}
73+
offset += limit
74+
if offset < total {
75+
req.Offset = &offset
76+
goto getMoreInstances
77+
}
78+
return
79+
}
80+
81+
func NewCbsTcInstanceRepository(c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) {
82+
cli, err := client.NewCbsClient(c)
83+
if err != nil {
84+
return
85+
}
86+
repo = &CbsTcInstanceRepository{
87+
client: cli,
88+
logger: logger,
89+
}
90+
return
91+
}

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
MongoDB |QCE/CMONGO|[指标详情](https://cloud.tencent.com/document/product/248/45104)
1111
CDB|QCE/CDB|[指标详情](https://cloud.tencent.com/document/product/248/45147)
1212
Redis标准版|QCE/REDIS|[指标详情](https://cloud.tencent.com/document/product/248/45111)
13-
Redis集群版|QCE/REDIS|[指标详情](https://cloud.tencent.com/document/product/248/45111)
13+
Redis集群版|QCE/REDIS_CLUSTER|[指标详情](https://cloud.tencent.com/document/product/248/45111)
1414
Redis内存版监控指标|QCE/REDIS_MEM|[指标详情](https://cloud.tencent.com/document/product/248/49729)
1515
CVM|QCE/CVM|[指标详情](https://cloud.tencent.com/document/product/248/6843)
1616
COS|QCE/COS|[指标详情](https://cloud.tencent.com/document/product/248/45140)
@@ -20,6 +20,7 @@ CLB(7层)|QCE/LOADBALANCE|[指标详情](https://cloud.tencent.com/document/prod
2020
NAT|QCE/NAT_GATEWAY|[指标详情](https://cloud.tencent.com/document/product/248/45069)
2121
物理专线|QCE/DC|[指标详情](https://cloud.tencent.com/document/product/248/45102)
2222
专用通道|QCE/DCX|[指标详情](https://cloud.tencent.com/document/product/248/45101)
23+
云硬盘|QCE/BLOCK_STORAGE|[指标详情](https://cloud.tencent.com/document/product/248/45411)
2324

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

@@ -126,6 +127,8 @@ export TENCENTCLOUD_SECRET_KEY="YOUR_ACCESS_SECRET"
126127
export TENCENTCLOUD_REGION="REGION"
127128
```
128129

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

131134
命令行参数|说明|默认值

0 commit comments

Comments
 (0)