diff --git a/readme.md b/README.md similarity index 98% rename from readme.md rename to README.md index 8da7869..e3d2a16 100644 --- a/readme.md +++ b/README.md @@ -21,7 +21,7 @@ NAT|QCE/NAT_GATEWAY|[指标详情](https://cloud.tencent.com/document/product/24 物理专线|QCE/DC|[指标详情](https://cloud.tencent.com/document/product/248/45102) 专用通道|QCE/DCX|[指标详情](https://cloud.tencent.com/document/product/248/45101) 云硬盘|QCE/CBS|[指标详情](https://cloud.tencent.com/document/product/248/45411) - +SqlServer|QCE/SQLSERVER|[指标详情](https://cloud.tencent.com/document/product/248/45146) `后续会有更多的产品支持` ## 二、快速开始 diff --git a/configs/qcloud-sqlserver-product.yml b/configs/qcloud-sqlserver-product.yml new file mode 100644 index 0000000..1a325a0 --- /dev/null +++ b/configs/qcloud-sqlserver-product.yml @@ -0,0 +1,17 @@ +credential: + access_key: "access_key" + secret_key: "secret_key" + region: "region" + +rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 + +products: + - namespace: QCE/SQLSERVER #指标详情: https://cloud.tencent.com/document/product/248/45146 + all_metrics: true + all_instances: true + #only_include_metrics: [] + #only_include_instances: [mssql-xxxxxxxx] + #extra_labels: [InstanceId,Name,Region,Vip] + #statistics_types: [last] + period_seconds: 300 + #metric_name_type: 2 diff --git a/pkg/client/client.go b/pkg/client/client.go index 34328e1..9d0cd72 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -11,6 +11,7 @@ import ( mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725" monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724" redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412" + sqlserver "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328" vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" "github.com/tencentyun/tencentcloud-exporter/pkg/config" @@ -105,3 +106,13 @@ func NewCbsClient(conf *config.TencentConfig) (*cbs.Client, error) { cpf.HttpProfile.Endpoint = "cbs.tencentcloudapi.com" return cbs.NewClient(credential, conf.Credential.Region, cpf) } + +func NewSqlServerClient(conf *config.TencentConfig) (*sqlserver.Client, error) { + credential := common.NewCredential( + conf.Credential.AccessKey, + conf.Credential.SecretKey, + ) + cpf := profile.NewClientProfile() + cpf.HttpProfile.Endpoint = "sqlserver.tencentcloudapi.com" + return sqlserver.NewClient(credential, conf.Credential.Region, cpf) +} diff --git a/pkg/collector/handler_sqlserver.go b/pkg/collector/handler_sqlserver.go new file mode 100644 index 0000000..c0ea6dc --- /dev/null +++ b/pkg/collector/handler_sqlserver.go @@ -0,0 +1,43 @@ +package collector + +import ( + "github.com/go-kit/kit/log" + + "github.com/tencentyun/tencentcloud-exporter/pkg/metric" +) + +const ( + SqlServerNamespace = "QCE/SQLSERVER" + SqlServerInstanceidKey = "resourceId" +) + +func init() { + registerHandler(SqlServerNamespace, defaultHandlerEnabled, NewSqlServerHandler) +} + +type sqlServerHandler struct { + baseProductHandler +} + +func (h *sqlServerHandler) IsMetricMetaVaild(meta *metric.TcmMeta) bool { + return true +} + +func (h *sqlServerHandler) GetNamespace() string { + return SqlServerNamespace +} + +func (h *sqlServerHandler) IsMetricVaild(m *metric.TcmMetric) bool { + return true +} + +func NewSqlServerHandler(c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { + handler = &sqlServerHandler{ + baseProductHandler{ + monitorQueryKey: SqlServerInstanceidKey, + collector: c, + logger: logger, + }, + } + return +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 5b7f61a..7a09fa8 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -45,6 +45,7 @@ var ( "cos": "QCE/COS", "cdn": "QCE/CDN", "cbs": "QCE/BLOCK_STORAGE", + "sqlserver": "QCE/SQLSERVER", } SupportStatisticsTypes = map[string]bool{ diff --git a/pkg/instance/instance_sqlserver.go b/pkg/instance/instance_sqlserver.go new file mode 100644 index 0000000..695fd8f --- /dev/null +++ b/pkg/instance/instance_sqlserver.go @@ -0,0 +1,34 @@ +package instance + +import ( + "fmt" + "reflect" + + sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328" +) + +type SqlServerTcInstance struct { + baseTcInstance + meta *sdk.DBInstance +} + +func (ins *SqlServerTcInstance) GetMeta() interface{} { + return ins.meta +} + +func NewSqlServerTcInstance(instanceId string, meta *sdk.DBInstance) (ins *SqlServerTcInstance, err error) { + if instanceId == "" { + return nil, fmt.Errorf("instanceId is empty ") + } + if meta == nil { + return nil, fmt.Errorf("meta is empty ") + } + ins = &SqlServerTcInstance{ + baseTcInstance: baseTcInstance{ + instanceId: instanceId, + value: reflect.ValueOf(*meta), + }, + meta: meta, + } + return +} diff --git a/pkg/instance/repository_sqlserver.go b/pkg/instance/repository_sqlserver.go new file mode 100644 index 0000000..1daf88e --- /dev/null +++ b/pkg/instance/repository_sqlserver.go @@ -0,0 +1,93 @@ +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/sqlserver/v20180328" + + "github.com/tencentyun/tencentcloud-exporter/pkg/client" + "github.com/tencentyun/tencentcloud-exporter/pkg/config" +) + +func init() { + registerRepository("QCE/SQLSERVER", NewSqlServerTcInstanceRepository) +} + +type SqlServerTcInstanceRepository struct { + client *sdk.Client + logger log.Logger +} + +func (repo *SqlServerTcInstanceRepository) GetInstanceKey() string { + return "InstanceId" +} + +func (repo *SqlServerTcInstanceRepository) Get(id string) (instance TcInstance, err error) { + req := sdk.NewDescribeDBInstancesRequest() + req.InstanceIdSet = []*string{&id} + resp, err := repo.client.DescribeDBInstances(req) + if err != nil { + return + } + if len(resp.Response.DBInstances) != 1 { + return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) + } + meta := resp.Response.DBInstances[0] + instance, err = NewSqlServerTcInstance(id, meta) + if err != nil { + return + } + return +} + +func (repo *SqlServerTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { + return +} + +func (repo *SqlServerTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { + req := sdk.NewDescribeDBInstancesRequest() + var offset int64 = 0 + var limit int64 = 100 + var total int64 = -1 + + req.Offset = &offset + req.Limit = &limit + +getMoreInstances: + resp, err := repo.client.DescribeDBInstances(req) + if err != nil { + return + } + if total == -1 { + total = *resp.Response.TotalCount + } + for _, meta := range resp.Response.DBInstances { + ins, e := NewSqlServerTcInstance(*meta.InstanceId, meta) + if e != nil { + level.Error(repo.logger).Log("msg", "Create cdb instance fail", "id", *meta.InstanceId) + continue + } + instances = append(instances, ins) + } + offset += limit + if offset < total { + req.Offset = &offset + goto getMoreInstances + } + + return +} + +func NewSqlServerTcInstanceRepository(c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { + cli, err := client.NewSqlServerClient(c) + if err != nil { + return + } + repo = &SqlServerTcInstanceRepository{ + client: cli, + logger: logger, + } + return +}