├── .github └── workflows │ └── docker-publish.yml ├── .gitignore ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── cmd └── qcloud-exporter │ └── qcloud_exporter.go ├── configs ├── qcloud-cbs-product.yml ├── qcloud-cdb-product.yml ├── qcloud-cdn-product.yml ├── qcloud-ckafka-product.yml ├── qcloud-clb-product.yml ├── qcloud-clb7-product.yml ├── qcloud-cmongo-product.yml ├── qcloud-cmq-product.yml ├── qcloud-cmqtopic-product.yml ├── qcloud-cos-product.yml ├── qcloud-cvm-product.yml ├── qcloud-cynosdbmysql-product.yml ├── qcloud-dc-product.yml ├── qcloud-dcx-product.yml ├── qcloud-eip-product.yml ├── qcloud-es-product.yml ├── qcloud-lighthouse-product.yml ├── qcloud-mariadb-product.yml ├── qcloud-memcached-product.yml ├── qcloud-nat-product.yml ├── qcloud-pg-product.yml ├── qcloud-redis-cluster-product.yml ├── qcloud-redis-product.yml ├── qcloud-sqlserver-product.yml ├── qcloud-tdmq-product.yml ├── qcloud-tdmysql-product.yml ├── qcloud-vpngw-product.yml └── qcloud-vpnx-product.yml ├── go.mod ├── go.sum └── pkg ├── .DS_Store ├── cachedtransactiongather ├── cachedtransactiongather.go └── cachedtransactiongather_test.go ├── client └── client.go ├── collector ├── collector.go ├── handler.go ├── handler_cbs.go ├── handler_cdb.go ├── handler_cdn.go ├── handler_cfs.go ├── handler_clb.go ├── handler_clb7.go ├── handler_clb_private.go ├── handler_cmq.go ├── handler_cmqtopic.go ├── handler_cos.go ├── handler_cvm.go ├── handler_cynosdb.go ├── handler_dc.go ├── handler_dcdb.go ├── handler_dcg.go ├── handler_dcx.go ├── handler_dts.go ├── handler_eip.go ├── handler_es.go ├── handler_kafka.go ├── handler_lighthouse.go ├── handler_mariadb.go ├── handler_memcached.go ├── handler_mongo.go ├── handler_nacos.go ├── handler_nat.go ├── handler_pg.go ├── handler_qaap.go ├── handler_redis.go ├── handler_redis_mem.go ├── handler_sqlserver.go ├── handler_tdmq.go ├── handler_vbc.go ├── handler_vpngw.go ├── handler_vpnx.go ├── handler_waf.go ├── handler_zookeeper.go └── product.go ├── common └── credentials.go ├── config └── config.go ├── constant ├── cache.go ├── instance.go └── metric.go ├── instance ├── cache.go ├── cache_test.go ├── instance.go ├── instance_cbs.go ├── instance_cdb.go ├── instance_cdn.go ├── instance_cfs.go ├── instance_clb.go ├── instance_clb_private.go ├── instance_cmq.go ├── instance_cmqtopic.go ├── instance_cos.go ├── instance_cvm.go ├── instance_cynosdb.go ├── instance_dc.go ├── instance_dcdb.go ├── instance_dcg.go ├── instance_dcx.go ├── instance_dts.go ├── instance_eip.go ├── instance_es.go ├── instance_kafka.go ├── instance_lighthouse.go ├── instance_mariadb.go ├── instance_memcached.go ├── instance_mongo.go ├── instance_nat.go ├── instance_pg.go ├── instance_qaap.go ├── instance_redis.go ├── instance_sqlserver.go ├── instance_tdmq.go ├── instance_tse.go ├── instance_vbc.go ├── instance_vpngw.go ├── instance_vpnx.go ├── instance_waf.go ├── repository.go ├── repository_cbs.go ├── repository_cdb.go ├── repository_cdn.go ├── repository_cfs.go ├── repository_clb.go ├── repository_clb_private.go ├── repository_cmq.go ├── repository_cmqtopic.go ├── repository_cos.go ├── repository_cvm.go ├── repository_cynosdb.go ├── repository_dc.go ├── repository_dcdb.go ├── repository_dcg.go ├── repository_dcx.go ├── repository_dts.go ├── repository_eip.go ├── repository_es.go ├── repository_kafka.go ├── repository_lighthouse.go ├── repository_mariadb.go ├── repository_memcached.go ├── repository_mongo.go ├── repository_nacos.go ├── repository_nat.go ├── repository_pg.go ├── repository_qaap.go ├── repository_redis.go ├── repository_sqlserver.go ├── repository_tdmq.go ├── repository_vbc.go ├── repository_vpngw.go ├── repository_vpnx.go ├── repository_waf.go └── repository_zookeeper.go ├── metric ├── cache.go ├── conf.go ├── label.go ├── meta.go ├── metric.go ├── query.go ├── repository.go ├── sample.go └── series.go └── util ├── label.go ├── label_test.go ├── list.go ├── map.go ├── set.go ├── str.go └── time.go /.github/workflows/docker-publish.yml: -------------------------------------------------------------------------------- 1 | name: Docker 2 | 3 | # This workflow uses actions that are not certified by GitHub. 4 | # They are provided by a third-party and are governed by 5 | # separate terms of service, privacy policy, and support 6 | # documentation. 7 | 8 | on: 9 | push: 10 | branches: [ master ] 11 | # Publish semver tags as releases. 12 | tags: [ 'v*.*.*' ] 13 | pull_request: 14 | branches: [ master ] 15 | 16 | env: 17 | # Use docker.io for Docker Hub if empty 18 | REGISTRY: ghcr.io 19 | # github.repository as / 20 | IMAGE_NAME: ${{ github.repository }} 21 | 22 | 23 | jobs: 24 | build: 25 | 26 | runs-on: ubuntu-latest 27 | permissions: 28 | contents: read 29 | packages: write 30 | # This is used to complete the identity challenge 31 | # with sigstore/fulcio when running outside of PRs. 32 | id-token: write 33 | 34 | steps: 35 | - name: Checkout repository 36 | uses: actions/checkout@v2 37 | 38 | # Install the cosign tool except on PR 39 | # https://github.com/sigstore/cosign-installer 40 | - name: Install cosign 41 | if: github.event_name != 'pull_request' 42 | uses: sigstore/cosign-installer@main 43 | with: 44 | cosign-release: 'v1.13.1' 45 | 46 | 47 | # Workaround: https://github.com/docker/build-push-action/issues/461 48 | - name: Setup Docker buildx 49 | uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf 50 | 51 | # Login against a Docker registry except on PR 52 | # https://github.com/docker/login-action 53 | - name: Log into registry ${{ env.REGISTRY }} 54 | if: github.event_name != 'pull_request' 55 | uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c 56 | with: 57 | registry: ${{ env.REGISTRY }} 58 | username: ${{ github.actor }} 59 | password: ${{ secrets.GITHUB_TOKEN }} 60 | 61 | # Extract metadata (tags, labels) for Docker 62 | # https://github.com/docker/metadata-action 63 | - name: Extract Docker metadata 64 | id: meta 65 | uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 66 | with: 67 | images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} 68 | 69 | # Build and push Docker image with Buildx (don't push on PR) 70 | # https://github.com/docker/build-push-action 71 | - name: Build and push Docker image 72 | id: build-and-push 73 | uses: docker/build-push-action@965c6a410d446a30e95d35052c67d6eded60dad6 74 | with: 75 | context: . 76 | push: ${{ github.event_name != 'pull_request' }} 77 | tags: ${{ steps.meta.outputs.tags }} 78 | labels: ${{ steps.meta.outputs.labels }} 79 | 80 | # Sign the resulting Docker image digest except on PRs. 81 | # This will only write to the public Rekor transparency log when the Docker 82 | # repository is public to avoid leaking data. If you would like to publish 83 | # transparency data even for private images, pass --force to cosign below. 84 | # https://github.com/sigstore/cosign 85 | - name: Sign the published Docker image 86 | if: ${{ github.event_name != 'pull_request' }} 87 | env: 88 | COSIGN_EXPERIMENTAL: "true" 89 | # This step uses the identity token to provision an ephemeral certificate 90 | # against the sigstore community Fulcio instance. 91 | run: cosign sign ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }} 92 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .scannerwork 2 | /.idea 3 | build/* 4 | qcloud.yml 5 | .DS_Store 6 | *mock.go 7 | /bin -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:alpine as build-env 2 | 3 | # Copy source + vendor 4 | COPY . /go/src/github.com/tencentyun/qcloud-exporter 5 | WORKDIR /go/src/github.com/tencentyun/qcloud-exporter 6 | 7 | # Build 8 | ENV GOPATH=/go 9 | RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -v -a -ldflags "-s -w" -o /go/bin/qcloud_exporter ./cmd/qcloud-exporter/ 10 | 11 | FROM alpine 12 | COPY --from=build-env /go/bin/qcloud_exporter /usr/bin/qcloud_exporter 13 | RUN apk update 14 | #RUN apk add git 15 | RUN apk add curl 16 | RUN apk add tcpdump 17 | ENTRYPOINT ["qcloud_exporter"] 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 腾讯云 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: help build lint 2 | 3 | GOOS ?= $(shell go env GOOS) 4 | GOARCH ?= $(shell go env GOARCH) 5 | GOLANGCI_LINT_VERSION ?= "v1.35.2" 6 | 7 | build: 8 | env GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o "build/$(version)/qcloud_exporter" ./cmd/qcloud-exporter/ 9 | 10 | lint: 11 | if [[ ! -e ./bin/golangci-lint ]]; then \ 12 | curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s $(GOLANGCI_LINT_VERSION); \ 13 | fi; \ 14 | ./bin/golangci-lint run ./... 15 | 16 | deploy: 17 | env GOOS=linux GOARCH=amd64 go build -o "build/qcloud_exporter" ./cmd/qcloud-exporter/ 18 | 19 | deps: ## Update vendor. 20 | go mod verify 21 | go mod tidy -v 22 | -------------------------------------------------------------------------------- /configs/qcloud-cbs-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CBS #指标详情: https://cloud.tencent.com/document/product/248/45411 10 | all_metrics: true 11 | all_instances: true 12 | -------------------------------------------------------------------------------- /configs/qcloud-cdb-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CDB #指标详情: https://cloud.tencent.com/document/product/248/45147 10 | all_metrics: true 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [cdb-xxxxxxxx] 14 | #extra_labels: [InstanceName] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-cdn-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CDN #指标详情: https://cloud.tencent.com/document/product/248/45138 10 | custom_query_dimensions: #CDN只支持自定义查询 11 | - projectId: xxx1 12 | domain: xxx1 13 | - projectId: xxx2 14 | domain: xxx2 -------------------------------------------------------------------------------- /configs/qcloud-ckafka-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 10 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CKAFKA #指标详情: https://cloud.tencent.com/document/product/248/45121 10 | all_metrics: true 11 | all_instances: true 12 | extra_labels: [InstanceName] -------------------------------------------------------------------------------- /configs/qcloud-clb-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/LB_PUBLIC #指标详情: https://cloud.tencent.com/document/product/248/45047 10 | all_metrics: true 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [lb-xxxxxxxx] 14 | #extra_labels: [InstanceName] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 -------------------------------------------------------------------------------- /configs/qcloud-clb7-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/LOADBALANCE #指标详情: https://cloud.tencent.com/document/product/248/45045 10 | all_metrics: true 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [lb-xxxxxxxx] 14 | #extra_labels: [InstanceName] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 -------------------------------------------------------------------------------- /configs/qcloud-cmongo-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CMONGO #指标详情: https://cloud.tencent.com/document/product/248/45104 10 | all_metrics: true #自动导出所有实例的集群、副本集、节点3个纬度的指标数据 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [cmgo-xxxxxxxx] 14 | #extra_labels: [InstanceName] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-cmq-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 10 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CMQ #指标详情: https://cloud.tencent.com/document/product/248/45114 10 | all_metrics: true 11 | all_instances: true 12 | extra_labels: [XXX] # 这里不能再填QueueName,因为指标里面已经存在 13 | # only_include_instances: [xxxx] # 填队列名 -------------------------------------------------------------------------------- /configs/qcloud-cmqtopic-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 10 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CMQTOPIC #指标详情: https://cloud.tencent.com/document/product/248/45113 10 | all_metrics: true 11 | all_instances: true 12 | only_include_instances: [topic-xxxx] 13 | -------------------------------------------------------------------------------- /configs/qcloud-cos-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/COS #指标详情: https://cloud.tencent.com/document/product/248/45140 10 | custom_query_dimensions: #COS只支持自定义查询 11 | - appid: xxxxxxxx 12 | bucket: xxxxxx-xxxxxxx 13 | -------------------------------------------------------------------------------- /configs/qcloud-cvm-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CVM #指标详情: https://cloud.tencent.com/document/product/248/6843 10 | all_metrics: true 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [ins-xxxxxxxx] 14 | #extra_labels: [InstanceId, InstanceName] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-cynosdbmysql-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CYNOSDB_MYSQL #指标详情: https://cloud.tencent.com/document/product/248/45106 10 | all_metrics: true 11 | all_instances: true 12 | -------------------------------------------------------------------------------- /configs/qcloud-dc-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/DC #指标详情: https://cloud.tencent.com/document/product/248/45102 10 | all_metrics: true 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [dc-xxxxxxxx] 14 | #extra_labels: [InstanceName] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-dcx-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/DCX # 指标详情: https://cloud.tencent.com/document/product/248/45101 10 | all_metrics: true 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [dcx-xxxxxxxx] 14 | #extra_labels: [InstanceName] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-eip-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/LB #指标详情: https://cloud.tencent.com/document/product/248/45099 10 | all_metrics: true 11 | only_include_instances: ['eip-xxxx'] 12 | extra_labels: [AddressName] 13 | #all_instances: true 14 | #only_include_metrics: [] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-es-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 10 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CES #指标详情: https://cloud.tencent.com/document/product/248/45129 10 | all_metrics: true 11 | all_instances: true 12 | extra_labels: [InstanceName] -------------------------------------------------------------------------------- /configs/qcloud-lighthouse-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 10 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/LIGHTHOUSE 10 | all_metrics: true 11 | # all_instances: true 12 | only_include_instances: [lhins-xxxxxx] 13 | -------------------------------------------------------------------------------- /configs/qcloud-mariadb-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 10 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/MARIADB #指标详情: https://cloud.tencent.com/document/product/248/54396 10 | all_metrics: true 11 | all_instances: true 12 | extra_labels: [InstanceName] -------------------------------------------------------------------------------- /configs/qcloud-memcached-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 10 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/MEMCACHED 10 | all_metrics: true 11 | # all_instances: true 12 | only_include_instances: [cmemxxxx] 13 | -------------------------------------------------------------------------------- /configs/qcloud-nat-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/NAT_GATEWAY #指标详情: https://cloud.tencent.com/document/product/248/45069 10 | all_metrics: true 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [nat-xxxxxxxx] 14 | #extra_labels: [InstanceName] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-pg-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 10 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/POSTGRES #指标详情: https://cloud.tencent.com/document/product/248/45105 10 | all_metrics: true 11 | # all_instances: true 12 | only_include_instances: [postgres-xxxx] 13 | -------------------------------------------------------------------------------- /configs/qcloud-redis-cluster-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/CLUSTER_REDIS # 指标详情: https://cloud.tencent.com/document/product/248/45111 10 | all_metrics: true 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [crs-xxxxxxxx] 14 | #extra_labels: [InstanceName] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-redis-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/REDIS # 指标详情: https://cloud.tencent.com/document/product/248/45111 10 | all_metrics: true 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [crs-xxxxxxxx] 14 | #extra_labels: [InstanceName] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-sqlserver-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/SQLSERVER #指标详情: https://cloud.tencent.com/document/product/248/45146 10 | all_metrics: true 11 | all_instances: true 12 | #only_include_metrics: [] 13 | #only_include_instances: [mssql-xxxxxxxx] 14 | #extra_labels: [InstanceId,Name,Region,Vip] 15 | #statistics_types: [last] 16 | period_seconds: 300 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-tdmq-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/TDMQ #指标详情: https://cloud.tencent.com/document/product/248/51450#tdmq-rocketmq-.E7.89.88 10 | all_metrics: true 11 | all_instances: true 12 | only_include_metrics: ["MsgAverageSize","MsgRateIn","MsgThroughputIn","InMessagesTotal","StorageSize","TenantInMessagesTotal","TenantMsgAverageSize","TenantRateIn","TenantRateOut","TenantStorageSize"] -------------------------------------------------------------------------------- /configs/qcloud-tdmysql-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/TDMYSQL #指标详情: https://cloud.tencent.com/document/product/248/54401 10 | all_metrics: true 11 | only_include_instances: [ 'tdsqlshard-xxxx' ] 12 | extra_labels: [ InstanceName ] 13 | #all_instances: true 14 | #only_include_metrics: [] 15 | #statistics_types: [last] 16 | #period_seconds: 60 17 | #metric_name_type: 2 18 | -------------------------------------------------------------------------------- /configs/qcloud-vpngw-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/VPNGW #指标详情: https://cloud.tencent.com/document/product/248/45070 10 | all_metrics: true 11 | all_instances: true 12 | -------------------------------------------------------------------------------- /configs/qcloud-vpnx-product.yml: -------------------------------------------------------------------------------- 1 | credential: 2 | access_key: "access_key" 3 | secret_key: "secret_key" 4 | region: "region" 5 | 6 | rate_limit: 15 #云监控拉数据接口最大限制, 20/秒, 1200/分钟, https://cloud.tencent.com/document/product/248/31014 7 | 8 | products: 9 | - namespace: QCE/VPNX #指标详情: https://cloud.tencent.com/document/product/248/45071 10 | all_metrics: true 11 | all_instances: true 12 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tencentyun/tencentcloud-exporter 2 | 3 | go 1.12 4 | 5 | require ( 6 | github.com/go-kit/log v0.2.0 7 | github.com/golang/mock v1.4.4 8 | github.com/prometheus/client_golang v1.12.2-0.20220630150036-810fcb46abcd 9 | github.com/prometheus/client_model v0.2.0 10 | github.com/prometheus/common v0.35.0 11 | github.com/stretchr/testify v1.6.1 12 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs v1.0.334 13 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb v1.0.413 14 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn v1.0.437 15 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs v1.0.634 16 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.334 17 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.334 18 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cmq v1.0.334 19 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.634 20 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.334 21 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.413 22 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc v1.0.334 23 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb v1.0.334 24 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dts v1.0.531 25 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es v1.0.334 26 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.528 27 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.334 28 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.0.334 29 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/memcached v1.0.334 30 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.334 31 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.334 32 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres v1.0.334 33 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.334 34 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver v1.0.334 35 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.413 36 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.430 37 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.334 38 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.576 39 | github.com/tencentyun/cos-go-sdk-v5 v0.7.35 40 | golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e 41 | gopkg.in/alecthomas/kingpin.v2 v2.2.6 42 | gopkg.in/yaml.v2 v2.4.0 43 | 44 | ) 45 | -------------------------------------------------------------------------------- /pkg/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tencentyun/tencentcloud-exporter/f62340a0238420c78f520e072f59207da0086577/pkg/.DS_Store -------------------------------------------------------------------------------- /pkg/cachedtransactiongather/cachedtransactiongather.go: -------------------------------------------------------------------------------- 1 | package cachedtransactiongather 2 | 3 | import ( 4 | "sync" 5 | "time" 6 | 7 | "github.com/go-kit/log" 8 | "github.com/go-kit/log/level" 9 | "github.com/prometheus/client_golang/prometheus" 10 | io_prometheus_client "github.com/prometheus/client_model/go" 11 | ) 12 | 13 | // NewCachedTransactionGather creates cachedTransactionGather, which only allow one request per interval, and pending others 14 | // until response 15 | func NewCachedTransactionGather( 16 | gather prometheus.TransactionalGatherer, 17 | cacheInterval time.Duration, 18 | logger log.Logger, 19 | ) prometheus.TransactionalGatherer { 20 | return &cachedTransactionGather{ 21 | gather: gather, 22 | nextCollectionTime: time.Now(), 23 | cacheInterval: cacheInterval, 24 | logger: logger, 25 | } 26 | } 27 | 28 | type cachedTransactionGather struct { 29 | gather prometheus.TransactionalGatherer 30 | 31 | cache []*io_prometheus_client.MetricFamily 32 | err error 33 | 34 | nextCollectionTime time.Time 35 | cacheInterval time.Duration 36 | 37 | lock sync.RWMutex 38 | 39 | logger log.Logger 40 | } 41 | 42 | func (c *cachedTransactionGather) Gather() ([]*io_prometheus_client.MetricFamily, func(), error) { 43 | c.lock.Lock() 44 | shouldGather := time.Now().After(c.nextCollectionTime) 45 | if shouldGather { 46 | begin := time.Now() 47 | c.nextCollectionTime = c.nextCollectionTime.Add(c.cacheInterval) 48 | metrics, done, err := c.gather.Gather() 49 | if err != nil { 50 | c.err = err 51 | c.cache = []*io_prometheus_client.MetricFamily{} 52 | done() 53 | } else { 54 | c.cache = metrics 55 | c.err = nil 56 | done() 57 | } 58 | c.lock.Unlock() 59 | duration := time.Since(begin) 60 | level.Info(c.logger).Log("msg", "Collect all products done", "duration_seconds", duration.Seconds()) 61 | } else { 62 | c.lock.Unlock() 63 | } 64 | c.lock.RLock() 65 | defer c.lock.RUnlock() 66 | if c.err != nil { 67 | return nil, func() {}, c.err 68 | } 69 | return c.cache, func() {}, nil 70 | } 71 | -------------------------------------------------------------------------------- /pkg/collector/handler_cdb.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 7 | ) 8 | 9 | const ( 10 | CdbNamespace = "QCE/CDB" 11 | CdbInstanceidKey = "InstanceId" 12 | ) 13 | 14 | var excludeMetricName map[string]string 15 | 16 | func init() { 17 | registerHandler(CdbNamespace, defaultHandlerEnabled, NewCdbHandler) 18 | excludeMetricName = map[string]string{ 19 | "LogVolume": "LogVolume", 20 | "CurrentBackupVolume": "CurrentBackupVolume", 21 | "DataVolume": "DataVolume", 22 | "FreeBackupVolume": "FreeBackupVolume", 23 | "BillingBackupVolume": "BillingBackupVolume", 24 | } 25 | } 26 | 27 | type cdbHandler struct { 28 | baseProductHandler 29 | } 30 | 31 | func (h *cdbHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 32 | return true 33 | } 34 | 35 | func (h *cdbHandler) GetNamespace() string { 36 | return CdbNamespace 37 | } 38 | 39 | func (h *cdbHandler) IsMetricValid(m *metric.TcmMetric) bool { 40 | _, ok := excludeMetricName[m.Meta.MetricName] 41 | if ok { 42 | return false 43 | } 44 | p, err := m.Meta.GetPeriod(m.Conf.StatPeriodSeconds) 45 | if err != nil { 46 | return false 47 | } 48 | if p != m.Conf.StatPeriodSeconds { 49 | return false 50 | } 51 | return true 52 | } 53 | 54 | func NewCdbHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 55 | handler = &cdbHandler{ 56 | baseProductHandler{ 57 | monitorQueryKey: CdbInstanceidKey, 58 | collector: c, 59 | logger: logger, 60 | }, 61 | } 62 | return 63 | 64 | } 65 | -------------------------------------------------------------------------------- /pkg/collector/handler_clb.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 7 | "github.com/tencentyun/tencentcloud-exporter/pkg/util" 8 | ) 9 | 10 | const ( 11 | ClbNamespace = "QCE/LB_PUBLIC" 12 | ClbInstanceidKey = "vip" 13 | ) 14 | 15 | func init() { 16 | registerHandler(ClbNamespace, defaultHandlerEnabled, NewClbHandler) 17 | } 18 | 19 | type clbHandler struct { 20 | baseProductHandler 21 | } 22 | 23 | func (h *clbHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 24 | if !util.IsStrInList(meta.SupportDimensions, ClbInstanceidKey) { 25 | meta.SupportDimensions = append(meta.SupportDimensions, ClbInstanceidKey) 26 | } 27 | 28 | return true 29 | } 30 | 31 | func (h *clbHandler) GetNamespace() string { 32 | return ClbNamespace 33 | } 34 | 35 | func (h *clbHandler) IsMetricValid(m *metric.TcmMetric) bool { 36 | return true 37 | } 38 | 39 | func NewClbHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 40 | handler = &clbHandler{ 41 | baseProductHandler{ 42 | monitorQueryKey: ClbInstanceidKey, 43 | collector: c, 44 | logger: logger, 45 | }, 46 | } 47 | return 48 | 49 | } 50 | -------------------------------------------------------------------------------- /pkg/collector/handler_clb7.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "strings" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 8 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 9 | "github.com/tencentyun/tencentcloud-exporter/pkg/util" 10 | ) 11 | 12 | const ( 13 | Clb7Namespace = "QCE/LOADBALANCE" 14 | Clb7InstanceidKey = "vip" 15 | ) 16 | 17 | var ( 18 | Clb7ExcludeMetrics = []string{ 19 | "outpkgratio", 20 | "intrafficratio", 21 | "inpkgratio", 22 | "qpsratio", 23 | "activeconnratio", 24 | "newactiveconnratio", 25 | "outtrafficratio", 26 | "rsptimeout", 27 | "setreqavg", 28 | "setreqmax", 29 | "setrspavg", 30 | "setrspmax", 31 | "settotalreq", 32 | "vrsptimeout", 33 | "vsettotalreq", 34 | } 35 | ) 36 | 37 | func init() { 38 | registerHandler(Clb7Namespace, defaultHandlerEnabled, NewClb7Handler) 39 | } 40 | 41 | type clb7Handler struct { 42 | baseProductHandler 43 | } 44 | 45 | func (h *clb7Handler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 46 | if !util.IsStrInList(meta.SupportDimensions, Clb7InstanceidKey) { 47 | meta.SupportDimensions = append(meta.SupportDimensions, Clb7InstanceidKey) 48 | } 49 | 50 | return true 51 | } 52 | 53 | func (h *clb7Handler) GetNamespace() string { 54 | return Clb7Namespace 55 | } 56 | 57 | func (h *clb7Handler) IsMetricValid(m *metric.TcmMetric) bool { 58 | if util.IsStrInList(Clb7ExcludeMetrics, strings.ToLower(m.Meta.MetricName)) { 59 | return false 60 | } 61 | return true 62 | } 63 | 64 | func NewClb7Handler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 65 | handler = &clb7Handler{ 66 | baseProductHandler{ 67 | monitorQueryKey: Clb7InstanceidKey, 68 | collector: c, 69 | logger: logger, 70 | }, 71 | } 72 | return 73 | } 74 | -------------------------------------------------------------------------------- /pkg/collector/handler_cmq.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/go-kit/log/level" 8 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 9 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/util" 11 | ) 12 | 13 | const ( 14 | CMQNamespace = "QCE/CMQ" 15 | CMQInstanceIDKey = "queueId" 16 | ) 17 | 18 | func init() { 19 | registerHandler(CMQNamespace, defaultHandlerEnabled, NewCMQHandler) 20 | } 21 | 22 | type cmqHandler struct { 23 | baseProductHandler 24 | } 25 | 26 | func (h *cmqHandler) GetNamespace() string { 27 | return CMQNamespace 28 | } 29 | 30 | func (h *cmqHandler) GetSeries(m *metric.TcmMetric) ([]*metric.TcmSeries, error) { 31 | if m.Conf.IsIncludeOnlyInstance() { 32 | return h.GetSeriesByOnly(m) 33 | } 34 | 35 | if m.Conf.IsIncludeAllInstance() { 36 | return h.GetSeriesByAll(m) 37 | } 38 | 39 | if m.Conf.IsCustomQueryDimensions() { 40 | return h.GetSeriesByCustom(m) 41 | } 42 | 43 | return nil, fmt.Errorf("must config all_instances or only_include_instances or custom_query_dimensions") 44 | } 45 | 46 | func (h *cmqHandler) GetSeriesByOnly(m *metric.TcmMetric) ([]*metric.TcmSeries, error) { 47 | var slist []*metric.TcmSeries 48 | for _, insId := range m.Conf.OnlyIncludeInstances { 49 | ins, err := h.collector.InstanceRepo.Get(insId) 50 | if err != nil { 51 | level.Error(h.logger).Log("msg", "Instance not found", "id", insId) 52 | continue 53 | } 54 | queueName, err := ins.GetFieldValueByName("QueueName") 55 | if err != nil { 56 | level.Error(h.logger).Log("msg", "queue name not found") 57 | continue 58 | } 59 | ql := map[string]string{ 60 | h.monitorQueryKey: ins.GetMonitorQueryKey(), 61 | "queueName": queueName, // hack, hardcode 🤮 62 | } 63 | s, err := metric.NewTcmSeries(m, ql, ins) 64 | if err != nil { 65 | level.Error(h.logger).Log("msg", "Create metric series fail", 66 | "metric", m.Meta.MetricName, "instance", insId) 67 | continue 68 | } 69 | slist = append(slist, s) 70 | } 71 | return slist, nil 72 | } 73 | 74 | func (h *cmqHandler) GetSeriesByAll(m *metric.TcmMetric) ([]*metric.TcmSeries, error) { 75 | var slist []*metric.TcmSeries 76 | insList, err := h.collector.InstanceRepo.ListByFilters(m.Conf.InstanceFilters) 77 | if err != nil { 78 | return nil, err 79 | } 80 | for _, ins := range insList { 81 | if len(m.Conf.ExcludeInstances) != 0 && util.IsStrInList(m.Conf.ExcludeInstances, ins.GetInstanceId()) { 82 | continue 83 | } 84 | queueName, err := ins.GetFieldValueByName("QueueName") 85 | if err != nil { 86 | level.Error(h.logger).Log("msg", "queue name not found") 87 | continue 88 | } 89 | ql := map[string]string{ 90 | h.monitorQueryKey: ins.GetMonitorQueryKey(), 91 | "queueName": queueName, // hack, hardcode 🤮 92 | } 93 | s, err := metric.NewTcmSeries(m, ql, ins) 94 | if err != nil { 95 | level.Error(h.logger).Log("msg", "Create metric series fail", 96 | "metric", m.Meta.MetricName, "instance", ins.GetInstanceId()) 97 | continue 98 | } 99 | slist = append(slist, s) 100 | } 101 | return slist, nil 102 | } 103 | 104 | func NewCMQHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 105 | handler = &cmqHandler{ 106 | baseProductHandler{ 107 | monitorQueryKey: CMQInstanceIDKey, 108 | collector: c, 109 | logger: logger, 110 | }, 111 | } 112 | return 113 | 114 | } 115 | -------------------------------------------------------------------------------- /pkg/collector/handler_cmqtopic.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | ) 7 | 8 | const ( 9 | CMQTopicNamespace = "QCE/CMQTOPIC" 10 | CMQTopicInstanceIDKey = "topicId" 11 | ) 12 | 13 | func init() { 14 | registerHandler(CMQTopicNamespace, defaultHandlerEnabled, NewCMQTopicHandler) 15 | } 16 | 17 | type cmqTopicHandler struct { 18 | baseProductHandler 19 | } 20 | 21 | func (h *cmqTopicHandler) GetNamespace() string { 22 | return CMQTopicNamespace 23 | } 24 | func NewCMQTopicHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 25 | handler = &cmqTopicHandler{ 26 | baseProductHandler{ 27 | monitorQueryKey: CMQTopicInstanceIDKey, 28 | collector: c, 29 | logger: logger, 30 | }, 31 | } 32 | return 33 | 34 | } 35 | -------------------------------------------------------------------------------- /pkg/collector/handler_cos.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/go-kit/log/level" 8 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 9 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/util" 11 | ) 12 | 13 | const ( 14 | CosNamespace = "QCE/COS" 15 | ) 16 | 17 | func init() { 18 | registerHandler(CosNamespace, defaultHandlerEnabled, NewCosHandler) 19 | } 20 | 21 | var ( 22 | CosSupportDimensions = []string{"appid", "bucket"} 23 | ) 24 | 25 | type cosHandler struct { 26 | baseProductHandler 27 | } 28 | 29 | func (h *cosHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 30 | return true 31 | } 32 | 33 | func (h *cosHandler) GetNamespace() string { 34 | return CosNamespace 35 | } 36 | 37 | func (h *cosHandler) IsMetricValid(m *metric.TcmMetric) bool { 38 | // cos大部分指标不支持300以下的统计纬度 39 | if m.Conf.StatPeriodSeconds < 300 { 40 | m.Conf.StatPeriodSeconds = 300 41 | } 42 | return true 43 | } 44 | func (h *cosHandler) GetSeries(m *metric.TcmMetric) (slist []*metric.TcmSeries, err error) { 45 | if m.Conf.IsIncludeOnlyInstance() { 46 | return h.GetSeriesByOnly(m) 47 | } 48 | 49 | if m.Conf.IsIncludeAllInstance() { 50 | return h.GetSeriesByAll(m) 51 | } 52 | 53 | if m.Conf.IsCustomQueryDimensions() { 54 | return h.GetSeriesByCustom(m) 55 | } 56 | 57 | return nil, fmt.Errorf("must config all_instances or only_include_instances or custom_query_dimensions") 58 | } 59 | 60 | func (h *cosHandler) GetSeriesByAll(m *metric.TcmMetric) ([]*metric.TcmSeries, error) { 61 | var slist []*metric.TcmSeries 62 | insList, err := h.collector.InstanceRepo.ListByFilters(m.Conf.InstanceFilters) 63 | if err != nil { 64 | return nil, err 65 | } 66 | for _, ins := range insList { 67 | if len(m.Conf.ExcludeInstances) != 0 && util.IsStrInList(m.Conf.ExcludeInstances, ins.GetInstanceId()) { 68 | continue 69 | } 70 | bucket, err := ins.GetFieldValueByName("Name") 71 | if err != nil { 72 | level.Error(h.logger).Log("msg", "projectId not found") 73 | continue 74 | } 75 | ql := map[string]string{ 76 | "bucket": bucket, 77 | } 78 | s, err := metric.NewTcmSeries(m, ql, ins) 79 | if err != nil { 80 | level.Error(h.logger).Log("msg", "Create metric series fail", 81 | "metric", m.Meta.MetricName, "instance", ins.GetInstanceId()) 82 | continue 83 | } 84 | slist = append(slist, s) 85 | } 86 | return slist, nil 87 | } 88 | 89 | func (h *cosHandler) GetSeriesByCustom(m *metric.TcmMetric) (slist []*metric.TcmSeries, err error) { 90 | for _, ql := range m.Conf.CustomQueryDimensions { 91 | if !h.checkMonitorQueryKeys(m, ql) { 92 | continue 93 | } 94 | 95 | s, err := metric.NewTcmSeries(m, ql, nil) 96 | if err != nil { 97 | level.Error(h.logger).Log("msg", "Create metric series fail", "metric", m.Meta.MetricName, 98 | "ql", fmt.Sprintf("%v", ql)) 99 | continue 100 | } 101 | slist = append(slist, s) 102 | } 103 | return 104 | } 105 | 106 | func (h *cosHandler) checkMonitorQueryKeys(m *metric.TcmMetric, ql map[string]string) bool { 107 | for k := range ql { 108 | if !util.IsStrInList(CosSupportDimensions, k) { 109 | level.Error(h.logger).Log("msg", fmt.Sprintf("not found %s in supportQueryDimensions", k), 110 | "ql", fmt.Sprintf("%v", ql), 111 | "sd", fmt.Sprintf("%v", m.Meta.SupportDimensions), 112 | ) 113 | return false 114 | } 115 | } 116 | return true 117 | } 118 | 119 | func NewCosHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 120 | handler = &cosHandler{ 121 | baseProductHandler{ 122 | collector: c, 123 | logger: logger, 124 | }, 125 | } 126 | return 127 | } 128 | -------------------------------------------------------------------------------- /pkg/collector/handler_cvm.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "strings" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 8 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 9 | "github.com/tencentyun/tencentcloud-exporter/pkg/util" 10 | ) 11 | 12 | const ( 13 | CvmNamespace = "QCE/CVM" 14 | CvmInstanceidKey = "InstanceId" 15 | ) 16 | 17 | var ( 18 | CvmInvalidMetricNames = []string{"dccpuusage", "dcmemusage"} 19 | ) 20 | 21 | func init() { 22 | registerHandler(CvmNamespace, defaultHandlerEnabled, NewCvmHandler) 23 | } 24 | 25 | type cvmHandler struct { 26 | baseProductHandler 27 | } 28 | 29 | func (h *cvmHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 30 | if !util.IsStrInList(meta.SupportDimensions, CvmInstanceidKey) { 31 | meta.SupportDimensions = append(meta.SupportDimensions, CvmInstanceidKey) 32 | } 33 | 34 | return true 35 | } 36 | 37 | func (h *cvmHandler) GetNamespace() string { 38 | return CvmNamespace 39 | } 40 | 41 | func (h *cvmHandler) IsMetricValid(m *metric.TcmMetric) bool { 42 | if util.IsStrInList(CvmInvalidMetricNames, strings.ToLower(m.Meta.MetricName)) { 43 | return false 44 | } 45 | return true 46 | } 47 | 48 | func (h *cvmHandler) GetSeries(m *metric.TcmMetric) (slist []*metric.TcmSeries, err error) { 49 | if m.Conf.StatPeriodSeconds < 60 { 50 | m.Conf.StatPeriodSeconds = 60 51 | } 52 | return h.baseProductHandler.GetSeries(m) 53 | } 54 | 55 | func NewCvmHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 56 | handler = &cvmHandler{ 57 | baseProductHandler{ 58 | monitorQueryKey: CvmInstanceidKey, 59 | collector: c, 60 | logger: logger, 61 | }, 62 | } 63 | return 64 | 65 | } 66 | -------------------------------------------------------------------------------- /pkg/collector/handler_dc.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 7 | ) 8 | 9 | const ( 10 | DcNamespace = "QCE/DC" 11 | DcInstanceidKey = "directConnectId" 12 | ) 13 | 14 | func init() { 15 | registerHandler(DcNamespace, defaultHandlerEnabled, NewDcHandler) 16 | } 17 | 18 | type dcHandler struct { 19 | baseProductHandler 20 | } 21 | 22 | func (h *dcHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 23 | return true 24 | } 25 | 26 | func (h *dcHandler) GetNamespace() string { 27 | return DcNamespace 28 | } 29 | 30 | func (h *dcHandler) IsMetricValid(m *metric.TcmMetric) bool { 31 | return true 32 | } 33 | 34 | func NewDcHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 35 | handler = &dcHandler{ 36 | baseProductHandler{ 37 | monitorQueryKey: DcInstanceidKey, 38 | collector: c, 39 | logger: logger, 40 | }, 41 | } 42 | return 43 | 44 | } 45 | -------------------------------------------------------------------------------- /pkg/collector/handler_dcdb.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 7 | ) 8 | 9 | const ( 10 | DcdbNamespace = "QCE/TDMYSQL" 11 | DcdbInstanceidKey = "InstanceId" 12 | ) 13 | 14 | func init() { 15 | registerHandler(DcdbNamespace, defaultHandlerEnabled, NewDcdbHandler) 16 | } 17 | 18 | type dcdbHandler struct { 19 | baseProductHandler 20 | } 21 | 22 | func (h *dcdbHandler) GetNamespace() string { 23 | return DcdbNamespace 24 | } 25 | 26 | func (h *dcdbHandler) IsMetricValid(m *metric.TcmMetric) bool { 27 | // ignore node/shard metric, bug for cloud monitor if filter dim 28 | if len(m.Meta.SupportDimensions) != 1 { 29 | return false 30 | } 31 | return true 32 | } 33 | 34 | func NewDcdbHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 35 | handler = &dcdbHandler{ 36 | baseProductHandler{ 37 | monitorQueryKey: DcdbInstanceidKey, 38 | collector: c, 39 | logger: logger, 40 | }, 41 | } 42 | return 43 | 44 | } 45 | -------------------------------------------------------------------------------- /pkg/collector/handler_dcg.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 7 | ) 8 | 9 | const ( 10 | DcgNamespace = "QCE/DCG" 11 | DcgInstanceidKey = "directConnectGatewayId" 12 | ) 13 | 14 | func init() { 15 | registerHandler(DcgNamespace, defaultHandlerEnabled, NewDcgHandler) 16 | } 17 | 18 | type DcgHandler struct { 19 | baseProductHandler 20 | } 21 | 22 | func (h *DcgHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 23 | return true 24 | } 25 | 26 | func (h *DcgHandler) GetNamespace() string { 27 | return DcgNamespace 28 | } 29 | 30 | func (h *DcgHandler) IsMetricValid(m *metric.TcmMetric) bool { 31 | _, ok := excludeMetricName[m.Meta.MetricName] 32 | if ok { 33 | return false 34 | } 35 | p, err := m.Meta.GetPeriod(m.Conf.StatPeriodSeconds) 36 | if err != nil { 37 | return false 38 | } 39 | if p != m.Conf.StatPeriodSeconds { 40 | return false 41 | } 42 | return true 43 | } 44 | 45 | func NewDcgHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 46 | handler = &DcgHandler{ 47 | baseProductHandler: baseProductHandler{ 48 | monitorQueryKey: DcgInstanceidKey, 49 | collector: c, 50 | logger: logger, 51 | }, 52 | } 53 | return 54 | 55 | } 56 | -------------------------------------------------------------------------------- /pkg/collector/handler_dcx.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "strings" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 8 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 9 | "github.com/tencentyun/tencentcloud-exporter/pkg/util" 10 | ) 11 | 12 | const ( 13 | DcxNamespace = "QCE/DCX" 14 | DcxInstanceidKey = "directConnectConnId" 15 | ) 16 | 17 | var ( 18 | DcxInvalidMetricNames = []string{"rxbytes", "txbytes"} 19 | ) 20 | 21 | func init() { 22 | registerHandler(DcxNamespace, defaultHandlerEnabled, NewDcxHandler) 23 | } 24 | 25 | type dcxHandler struct { 26 | baseProductHandler 27 | } 28 | 29 | func (h *dcxHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 30 | if util.IsStrInList(DcxInvalidMetricNames, strings.ToLower(meta.MetricName)) { 31 | return false 32 | } 33 | return true 34 | } 35 | 36 | func (h *dcxHandler) GetNamespace() string { 37 | return DcxNamespace 38 | } 39 | 40 | func (h *dcxHandler) IsMetricValid(m *metric.TcmMetric) bool { 41 | return true 42 | } 43 | 44 | func NewDcxHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 45 | handler = &dcxHandler{ 46 | baseProductHandler{ 47 | monitorQueryKey: DcxInstanceidKey, 48 | collector: c, 49 | logger: logger, 50 | }, 51 | } 52 | return 53 | 54 | } 55 | -------------------------------------------------------------------------------- /pkg/collector/handler_eip.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 7 | ) 8 | 9 | const ( 10 | EIPNamespace = "QCE/LB" 11 | EIPInstanceidKey = "eip" 12 | ) 13 | 14 | func init() { 15 | registerHandler(EIPNamespace, defaultHandlerEnabled, NewEIPHandler) 16 | } 17 | 18 | type eipHandler struct { 19 | baseProductHandler 20 | } 21 | 22 | func (h *eipHandler) GetNamespace() string { 23 | return EIPNamespace 24 | } 25 | func (h *eipHandler) IsMetricValid(m *metric.TcmMetric) bool { 26 | // ignore node/shard metric, bug for cloud monitor if filter dim 27 | if len(m.Meta.SupportDimensions) != 1 { 28 | return false 29 | } 30 | return true 31 | } 32 | func NewEIPHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 33 | handler = &eipHandler{ 34 | baseProductHandler{ 35 | monitorQueryKey: EIPInstanceidKey, 36 | collector: c, 37 | logger: logger, 38 | }, 39 | } 40 | return 41 | 42 | } 43 | -------------------------------------------------------------------------------- /pkg/collector/handler_es.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | ) 7 | 8 | const ( 9 | ESNamespace = "QCE/CES" 10 | ESInstanceIDKey = "uInstanceId" 11 | ) 12 | 13 | func init() { 14 | registerHandler(ESNamespace, defaultHandlerEnabled, NewESHandler) 15 | } 16 | 17 | type esHandler struct { 18 | baseProductHandler 19 | } 20 | 21 | func (h *esHandler) GetNamespace() string { 22 | return ESNamespace 23 | } 24 | 25 | func NewESHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 26 | handler = &esHandler{ 27 | baseProductHandler{ 28 | monitorQueryKey: ESInstanceIDKey, 29 | collector: c, 30 | logger: logger, 31 | }, 32 | } 33 | return 34 | 35 | } 36 | -------------------------------------------------------------------------------- /pkg/collector/handler_kafka.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 7 | ) 8 | 9 | const ( 10 | KafkaNamespace = "QCE/CKAFKA" 11 | KafkaInstanceIDKey = "instanceId" 12 | ) 13 | 14 | func init() { 15 | registerHandler(KafkaNamespace, defaultHandlerEnabled, NewKafkaHandler) 16 | } 17 | 18 | type kafkaHandler struct { 19 | baseProductHandler 20 | } 21 | 22 | func (h *kafkaHandler) GetNamespace() string { 23 | return MariaDBNamespace 24 | } 25 | func (h *kafkaHandler) IsMetricValid(m *metric.TcmMetric) bool { 26 | if len(m.Meta.SupportDimensions) != 1 { 27 | return false 28 | } 29 | if m.Meta.SupportDimensions[0] != KafkaInstanceIDKey { 30 | return false 31 | } 32 | p, err := m.Meta.GetPeriod(m.Conf.StatPeriodSeconds) 33 | if err != nil { 34 | return false 35 | } 36 | if p != m.Conf.StatPeriodSeconds { 37 | return false 38 | } 39 | return true 40 | } 41 | func NewKafkaHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 42 | handler = &kafkaHandler{ 43 | baseProductHandler{ 44 | monitorQueryKey: KafkaInstanceIDKey, 45 | collector: c, 46 | logger: logger, 47 | }, 48 | } 49 | return 50 | 51 | } 52 | -------------------------------------------------------------------------------- /pkg/collector/handler_lighthouse.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "strings" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 8 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 9 | "github.com/tencentyun/tencentcloud-exporter/pkg/util" 10 | ) 11 | 12 | const ( 13 | LighthouseNamespace = "QCE/LIGHTHOUSE" 14 | LighthouseInstanceIDKey = "InstanceId" 15 | ) 16 | 17 | func init() { 18 | registerHandler(LighthouseNamespace, defaultHandlerEnabled, NewLighthouseHandler) 19 | } 20 | 21 | type lighthouseHandler struct { 22 | baseProductHandler 23 | } 24 | 25 | func (h *lighthouseHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 26 | if !util.IsStrInList(meta.SupportDimensions, LighthouseInstanceIDKey) { 27 | meta.SupportDimensions = append(meta.SupportDimensions, LighthouseInstanceIDKey) 28 | } 29 | 30 | return true 31 | } 32 | 33 | func (h *lighthouseHandler) GetNamespace() string { 34 | return LighthouseNamespace 35 | } 36 | 37 | func (h *lighthouseHandler) IsMetricValid(m *metric.TcmMetric) bool { 38 | if util.IsStrInList(CvmInvalidMetricNames, strings.ToLower(m.Meta.MetricName)) { 39 | return false 40 | } 41 | return true 42 | } 43 | 44 | func (h *lighthouseHandler) GetSeries(m *metric.TcmMetric) (slist []*metric.TcmSeries, err error) { 45 | if m.Conf.StatPeriodSeconds < 60 { 46 | m.Conf.StatPeriodSeconds = 60 47 | } 48 | return h.baseProductHandler.GetSeries(m) 49 | } 50 | 51 | func NewLighthouseHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 52 | handler = &lighthouseHandler{ 53 | baseProductHandler{ 54 | monitorQueryKey: LighthouseInstanceIDKey, 55 | collector: c, 56 | logger: logger, 57 | }, 58 | } 59 | return 60 | 61 | } 62 | -------------------------------------------------------------------------------- /pkg/collector/handler_mariadb.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | ) 7 | 8 | const ( 9 | MariaDBNamespace = "QCE/MARIADB" 10 | MariaDBInstanceIDKey = "InstanceId" 11 | ) 12 | 13 | func init() { 14 | registerHandler(MariaDBNamespace, defaultHandlerEnabled, NewMariaDBHandler) 15 | } 16 | 17 | type mariaDBHandler struct { 18 | baseProductHandler 19 | } 20 | 21 | func (h *mariaDBHandler) GetNamespace() string { 22 | return MariaDBNamespace 23 | } 24 | func NewMariaDBHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 25 | handler = &mariaDBHandler{ 26 | baseProductHandler{ 27 | monitorQueryKey: MariaDBInstanceIDKey, 28 | collector: c, 29 | logger: logger, 30 | }, 31 | } 32 | return 33 | 34 | } 35 | -------------------------------------------------------------------------------- /pkg/collector/handler_memcached.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | ) 7 | 8 | const ( 9 | MemcachedNamespace = "QCE/MEMCACHED" 10 | MemcachedInstanceIDKey = "instanceid" 11 | ) 12 | 13 | func init() { 14 | registerHandler(MemcachedNamespace, defaultHandlerEnabled, NewMemcachedHandler) 15 | } 16 | 17 | type memcachedHandler struct { 18 | baseProductHandler 19 | } 20 | 21 | func (h *memcachedHandler) GetNamespace() string { 22 | return MemcachedNamespace 23 | } 24 | 25 | func NewMemcachedHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 26 | handler = &memcachedHandler{ 27 | baseProductHandler{ 28 | monitorQueryKey: MemcachedInstanceIDKey, 29 | collector: c, 30 | logger: logger, 31 | }, 32 | } 33 | return 34 | 35 | } 36 | -------------------------------------------------------------------------------- /pkg/collector/handler_nat.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 7 | ) 8 | 9 | const ( 10 | NatNamespace = "QCE/NAT_GATEWAY" 11 | NatMonitorQueryKey = "natId" 12 | ) 13 | 14 | func init() { 15 | registerHandler(NatNamespace, defaultHandlerEnabled, NewNatHandler) 16 | } 17 | 18 | type natHandler struct { 19 | baseProductHandler 20 | } 21 | 22 | func (h *natHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 23 | return true 24 | } 25 | 26 | func (h *natHandler) GetNamespace() string { 27 | return NatNamespace 28 | } 29 | 30 | func (h *natHandler) IsMetricValid(m *metric.TcmMetric) bool { 31 | return true 32 | } 33 | 34 | func NewNatHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 35 | handler = &natHandler{ 36 | baseProductHandler{ 37 | monitorQueryKey: NatMonitorQueryKey, 38 | collector: c, 39 | logger: logger, 40 | }, 41 | } 42 | return 43 | } 44 | -------------------------------------------------------------------------------- /pkg/collector/handler_pg.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | ) 7 | 8 | const ( 9 | PGNamespace = "QCE/POSTGRES" 10 | PGDBInstanceIDKey = "resourceId" 11 | ) 12 | 13 | func init() { 14 | registerHandler(PGNamespace, defaultHandlerEnabled, NewPGHandler) 15 | } 16 | 17 | type pgHandler struct { 18 | baseProductHandler 19 | } 20 | 21 | func (h *pgHandler) GetNamespace() string { 22 | return MariaDBNamespace 23 | } 24 | func NewPGHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 25 | handler = &pgHandler{ 26 | baseProductHandler{ 27 | monitorQueryKey: PGDBInstanceIDKey, 28 | collector: c, 29 | logger: logger, 30 | }, 31 | } 32 | return 33 | 34 | } 35 | -------------------------------------------------------------------------------- /pkg/collector/handler_redis.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "strings" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 8 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 9 | "github.com/tencentyun/tencentcloud-exporter/pkg/util" 10 | ) 11 | 12 | const ( 13 | RedisNamespace = "QCE/REDIS" 14 | RedisInstanceidKey = "instanceid" 15 | ) 16 | 17 | func init() { 18 | registerHandler(RedisNamespace, defaultHandlerEnabled, NewRedisHandler) 19 | } 20 | 21 | var ( 22 | RedisMetricNames = []string{ 23 | "cpuusmin", "storagemin", "storageusmin", "keysmin", "expiredkeysmin", "evictedkeysmin", "connectionsmin", "connectionsusmin", 24 | "inflowmin", "inflowusmin", "outflowmin", "outflowusmin", "latencymin", "latencygetmin", "latencysetmin", "latencyothermin", 25 | "qpsmin", "statgetmin", "statsetmin", "statothermin", "bigvaluemin", "slowquerymin", "statsuccessmin", "statmissedmin", 26 | "cmderrmin", "cachehitratiomin", 27 | } 28 | RedisClusterMetricNames = []string{ 29 | "cpuusmin", "cpumaxusmin", "storagemin", "storageusmin", "storagemaxusmin", "keysmin", "expiredkeysmin", "evictedkeysmin", 30 | "connectionsmin", "connectionsusmin", "inflowmin", "inflowusmin", "outflowmin", "outflowusmin", "latencymin", "latencygetmin", 31 | "latencysetmin", "latencyothermin", "qpsmin", "statgetmin", "statsetmin", "statothermin", "bigvaluemin", "slowquerymin", 32 | "statsuccessmin", "statmissedmin", "cmderrmin", "cachehitratiomin", 33 | } 34 | ) 35 | 36 | type redisHandler struct { 37 | baseProductHandler 38 | } 39 | 40 | func (h *redisHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 41 | return true 42 | } 43 | 44 | func (h *redisHandler) GetNamespace() string { 45 | return RedisNamespace 46 | } 47 | 48 | func (h *redisHandler) IsMetricValid(m *metric.TcmMetric) bool { 49 | if strings.ToLower(m.Conf.CustomProductName) == "cluster_redis" { 50 | if util.IsStrInList(RedisClusterMetricNames, strings.ToLower(m.Meta.MetricName)) { 51 | return true 52 | } 53 | } 54 | if strings.ToLower(m.Conf.CustomProductName) == "redis" { 55 | if util.IsStrInList(RedisMetricNames, strings.ToLower(m.Meta.MetricName)) { 56 | return true 57 | } 58 | } 59 | return false 60 | } 61 | 62 | func NewRedisHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 63 | handler = &redisHandler{ 64 | baseProductHandler{ 65 | monitorQueryKey: RedisInstanceidKey, 66 | collector: c, 67 | logger: logger, 68 | }, 69 | } 70 | return 71 | 72 | } 73 | -------------------------------------------------------------------------------- /pkg/collector/handler_sqlserver.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 8 | ) 9 | 10 | const ( 11 | SqlServerNamespace = "QCE/SQLSERVER" 12 | SqlServerInstanceidKey = "resourceId" 13 | ) 14 | 15 | func init() { 16 | registerHandler(SqlServerNamespace, defaultHandlerEnabled, NewSqlServerHandler) 17 | } 18 | 19 | type sqlServerHandler struct { 20 | baseProductHandler 21 | } 22 | 23 | func (h *sqlServerHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 24 | return true 25 | } 26 | 27 | func (h *sqlServerHandler) GetNamespace() string { 28 | return SqlServerNamespace 29 | } 30 | 31 | func (h *sqlServerHandler) IsMetricValid(m *metric.TcmMetric) bool { 32 | return true 33 | } 34 | 35 | func NewSqlServerHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 36 | handler = &sqlServerHandler{ 37 | baseProductHandler{ 38 | monitorQueryKey: SqlServerInstanceidKey, 39 | collector: c, 40 | logger: logger, 41 | }, 42 | } 43 | return 44 | } 45 | -------------------------------------------------------------------------------- /pkg/collector/handler_vpngw.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 7 | ) 8 | 9 | const ( 10 | VpngwNamespace = "QCE/VPNGW" 11 | VpngwInstanceidKey = "vpnGwId" 12 | ) 13 | 14 | func init() { 15 | registerHandler(VpngwNamespace, defaultHandlerEnabled, NewVpngwHandler) 16 | } 17 | 18 | type VpngwHandler struct { 19 | baseProductHandler 20 | } 21 | 22 | func (h *VpngwHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 23 | return true 24 | } 25 | 26 | func (h *VpngwHandler) GetNamespace() string { 27 | return VpngwNamespace 28 | } 29 | 30 | func (h *VpngwHandler) IsMetricValid(m *metric.TcmMetric) bool { 31 | _, ok := excludeMetricName[m.Meta.MetricName] 32 | if ok { 33 | return false 34 | } 35 | p, err := m.Meta.GetPeriod(m.Conf.StatPeriodSeconds) 36 | if err != nil { 37 | return false 38 | } 39 | if p != m.Conf.StatPeriodSeconds { 40 | return false 41 | } 42 | return true 43 | } 44 | 45 | func NewVpngwHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 46 | handler = &VpngwHandler{ 47 | baseProductHandler: baseProductHandler{ 48 | monitorQueryKey: VpngwInstanceidKey, 49 | collector: c, 50 | logger: logger, 51 | }, 52 | } 53 | return 54 | 55 | } 56 | -------------------------------------------------------------------------------- /pkg/collector/handler_vpnx.go: -------------------------------------------------------------------------------- 1 | package collector 2 | 3 | import ( 4 | "github.com/go-kit/log" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/metric" 7 | ) 8 | 9 | const ( 10 | VpnxNamespace = "QCE/VPNX" 11 | VpnxInstanceidKey = "vpnConnId" 12 | ) 13 | 14 | func init() { 15 | registerHandler(VpnxNamespace, defaultHandlerEnabled, NewVpnxHandler) 16 | } 17 | 18 | type VpnxHandler struct { 19 | baseProductHandler 20 | } 21 | 22 | func (h *VpnxHandler) IsMetricMetaValid(meta *metric.TcmMeta) bool { 23 | return true 24 | } 25 | 26 | func (h *VpnxHandler) GetNamespace() string { 27 | return VpnxNamespace 28 | } 29 | 30 | func (h *VpnxHandler) IsMetricValid(m *metric.TcmMetric) bool { 31 | _, ok := excludeMetricName[m.Meta.MetricName] 32 | if ok { 33 | return false 34 | } 35 | p, err := m.Meta.GetPeriod(m.Conf.StatPeriodSeconds) 36 | if err != nil { 37 | return false 38 | } 39 | if p != m.Conf.StatPeriodSeconds { 40 | return false 41 | } 42 | return true 43 | } 44 | 45 | func NewVpnxHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { 46 | handler = &VpnxHandler{ 47 | baseProductHandler: baseProductHandler{ 48 | monitorQueryKey: VpnxInstanceidKey, 49 | collector: c, 50 | logger: logger, 51 | }, 52 | } 53 | return 54 | 55 | } 56 | -------------------------------------------------------------------------------- /pkg/common/credentials.go: -------------------------------------------------------------------------------- 1 | package common 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io/ioutil" 7 | "net/http" 8 | "sync" 9 | "time" 10 | 11 | "github.com/tencentyun/cos-go-sdk-v5" 12 | ) 13 | 14 | type CredentialIface interface { 15 | GetSecretId() string 16 | GetToken() string 17 | GetSecretKey() string 18 | Refresh() error 19 | GetRole() string 20 | } 21 | 22 | type Credential struct { 23 | rwLocker sync.RWMutex 24 | Transport http.RoundTripper 25 | SecretId string 26 | SecretKey string 27 | Token string 28 | ExpiredTime int64 29 | Role string 30 | } 31 | 32 | type metadataResponse struct { 33 | TmpSecretId string 34 | TmpSecretKey string 35 | Token string 36 | ExpiredTime int64 37 | Code string 38 | } 39 | 40 | func (c *Credential) Refresh() error { 41 | tick := time.NewTicker(10 * time.Minute) 42 | defer tick.Stop() 43 | for { 44 | select { 45 | case <-tick.C: 46 | err := c.refresh() 47 | if err != nil { 48 | fmt.Println("refresh credential error: ", err.Error()) 49 | } 50 | } 51 | } 52 | } 53 | 54 | func (c *Credential) refresh() error { 55 | c.rwLocker.RLock() 56 | expiredTime := c.ExpiredTime 57 | c.rwLocker.RUnlock() 58 | if time.Now().Unix() < expiredTime-720 { 59 | return nil 60 | } 61 | res, err := http.Get(fmt.Sprintf("http://metadata.tencentyun.com/meta-data/cam/service-role-security-credentials/%s", c.Role)) 62 | if err != nil { 63 | return err 64 | } 65 | 66 | if res.StatusCode != 200 { 67 | _ = res.Body.Close() 68 | res, err = http.Get(fmt.Sprintf("http://metadata.tencentyun.com/meta-data/cam/security-credentials/%s", c.Role)) 69 | if err != nil { 70 | return err 71 | } 72 | if res.StatusCode != 200 { 73 | return fmt.Errorf("status code is %d", res.StatusCode) 74 | } 75 | } 76 | 77 | defer func() { _ = res.Body.Close() }() 78 | 79 | data, err := ioutil.ReadAll(res.Body) 80 | if err != nil { 81 | return err 82 | } 83 | 84 | metaData := &metadataResponse{} 85 | if err := json.Unmarshal(data, metaData); err != nil { 86 | return err 87 | } 88 | 89 | if metaData.Code != "Success" { 90 | return fmt.Errorf("get Code is %s", metaData.Code) 91 | } 92 | 93 | c.rwLocker.Lock() 94 | defer c.rwLocker.Unlock() 95 | c.SecretId = metaData.TmpSecretId 96 | c.SecretKey = metaData.TmpSecretKey 97 | c.Token = metaData.Token 98 | c.ExpiredTime = metaData.ExpiredTime 99 | return nil 100 | } 101 | 102 | func NewCredential(role string) (*Credential, error) { 103 | c := &Credential{ 104 | Role: role, 105 | } 106 | err := c.refresh() 107 | return c, err 108 | } 109 | 110 | func NewCredentialTransport(role string) *Credential { 111 | return &Credential{ 112 | Role: role, 113 | } 114 | } 115 | 116 | func (c *Credential) GetSecretKey() string { 117 | c.rwLocker.RLock() 118 | defer c.rwLocker.RUnlock() 119 | return c.SecretKey 120 | } 121 | 122 | func (c *Credential) GetSecretId() string { 123 | c.rwLocker.RLock() 124 | defer c.rwLocker.RUnlock() 125 | return c.SecretId 126 | } 127 | 128 | func (c *Credential) GetToken() string { 129 | c.rwLocker.RLock() 130 | defer c.rwLocker.RUnlock() 131 | return c.Token 132 | } 133 | 134 | func (c *Credential) GetRole() string { 135 | return c.Role 136 | } 137 | 138 | func (c *Credential) RoundTrip(req *http.Request) (*http.Response, error) { 139 | err := c.refresh() 140 | if err != nil { 141 | return nil, err 142 | } 143 | req = cloneRequest(req) 144 | // 增加 Authorization header 145 | authTime := cos.NewAuthTime(time.Hour) 146 | cos.AddAuthorizationHeader(c.GetSecretId(), c.GetSecretKey(), c.GetToken(), req, authTime) 147 | 148 | resp, err := c.transport().RoundTrip(req) 149 | return resp, err 150 | } 151 | 152 | func (c *Credential) transport() http.RoundTripper { 153 | if c.Transport != nil { 154 | return c.Transport 155 | } 156 | return http.DefaultTransport 157 | } 158 | 159 | // cloneRequest returns a clone of the provided *http.Request. The clone is a 160 | // shallow copy of the struct and its Header map. 161 | func cloneRequest(r *http.Request) *http.Request { 162 | // shallow copy of the struct 163 | r2 := new(http.Request) 164 | *r2 = *r 165 | // deep copy of the Header 166 | r2.Header = make(http.Header, len(r.Header)) 167 | for k, s := range r.Header { 168 | r2.Header[k] = append([]string(nil), s...) 169 | } 170 | return r2 171 | } 172 | -------------------------------------------------------------------------------- /pkg/constant/cache.go: -------------------------------------------------------------------------------- 1 | package constant 2 | 3 | import "time" 4 | 5 | const ( 6 | DefaultReloadInterval = 60 * time.Minute 7 | ) 8 | -------------------------------------------------------------------------------- /pkg/constant/instance.go: -------------------------------------------------------------------------------- 1 | package constant 2 | 3 | // 不支持实例纬度自动查询的namespace 4 | var NotSupportInstanceNamespaces = []string{ 5 | // "QCE/COS", 6 | // "QCE/CDN", 7 | } 8 | -------------------------------------------------------------------------------- /pkg/constant/metric.go: -------------------------------------------------------------------------------- 1 | package constant 2 | 3 | const ( 4 | DefaultQueryMetricBatchSize = 50 5 | ) 6 | -------------------------------------------------------------------------------- /pkg/instance/cache_test.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "github.com/prometheus/common/promlog" 5 | "testing" 6 | "time" 7 | 8 | "github.com/golang/mock/gomock" 9 | "github.com/stretchr/testify/assert" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412" 11 | ) 12 | 13 | func TestTcRedisInstanceNodeCache_GetNodeInfo(t *testing.T) { 14 | ctrl := gomock.NewController(t) 15 | defer ctrl.Finish() 16 | 17 | mockNodeRepo := NewMockRedisTcInstanceNodeRepository(ctrl) 18 | 19 | loglevel := &promlog.AllowedLevel{} 20 | loglevel.Set("debug") 21 | logformat := &promlog.AllowedFormat{} 22 | logformat.Set("logfmt") 23 | 24 | cache := &TcRedisInstanceNodeCache{ 25 | Raw: mockNodeRepo, 26 | cache: map[string]*sdk.DescribeInstanceNodeInfoResponse{}, 27 | lastReloadTime: map[string]time.Time{}, 28 | reloadInterval: 3 * time.Minute, 29 | logger: promlog.New(&promlog.Config{ 30 | Level: loglevel, 31 | Format: logformat, 32 | }), 33 | } 34 | 35 | mockInstanceId := "crs-12345678" 36 | 37 | // case 1: get from api, init 38 | mockNodeRepo.EXPECT().GetNodeInfo(gomock.Any()).Return(&sdk.DescribeInstanceNodeInfoResponse{}, nil) 39 | _, err := cache.GetNodeInfo(mockInstanceId) 40 | assert.NoError(t, err) 41 | 42 | // case 2: get from api, expire 43 | mockCacheNode := &sdk.DescribeInstanceNodeInfoResponse{} 44 | cache.cache[mockInstanceId] = mockCacheNode 45 | cache.lastReloadTime[mockInstanceId] = time.Now().Add(-4 * time.Minute) 46 | mockNodeRepo.EXPECT().GetNodeInfo(gomock.Any()).Return(&sdk.DescribeInstanceNodeInfoResponse{}, nil) 47 | node, err := cache.GetNodeInfo(mockInstanceId) 48 | assert.NoError(t, err) 49 | 50 | // case 3: get from cache 51 | cache.cache[mockInstanceId] = &sdk.DescribeInstanceNodeInfoResponse{} 52 | cache.lastReloadTime[mockInstanceId] = time.Now().Add(-1 * time.Minute) 53 | 54 | node, err = cache.GetNodeInfo(mockInstanceId) 55 | assert.NoError(t, err) 56 | assert.Equal(t, node, cache.cache[mockInstanceId]) 57 | } 58 | -------------------------------------------------------------------------------- /pkg/instance/instance.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/util" 6 | "reflect" 7 | ) 8 | 9 | // 每个产品的实例对象, 可用于配置导出指标的额外label填充, 根据字段名获取值 10 | type TcInstance interface { 11 | // 获取实例的id 12 | GetInstanceId() string 13 | 14 | // 用于查询云监控数据的主键字段, 一般是实例id 15 | GetMonitorQueryKey() string 16 | 17 | // 根据字段名称获取该字段的值, 由各个产品接口具体实现 18 | GetFieldValueByName(string) (string, error) 19 | 20 | // 根据字段名称获取该字段的值, 由各个产品接口具体实现 21 | GetFieldValuesByName(string) (map[string][]string, error) 22 | 23 | // 获取实例raw元数据, 每个实例类型不一样 24 | GetMeta() interface{} 25 | } 26 | 27 | type baseTcInstance struct { 28 | instanceId string 29 | value reflect.Value 30 | } 31 | 32 | func (ins *baseTcInstance) GetInstanceId() string { 33 | return ins.instanceId 34 | } 35 | 36 | func (ins *baseTcInstance) GetMonitorQueryKey() string { 37 | return ins.instanceId 38 | } 39 | 40 | func (ins *baseTcInstance) GetFieldValueByName(name string) (val string, err error) { 41 | defer func() { 42 | if err := recover(); err != nil { 43 | // nothing ignore err 44 | } 45 | }() 46 | v := ins.value.FieldByName(name) 47 | if v.Kind() == reflect.Ptr { 48 | v = reflect.Indirect(v) 49 | } 50 | return fmt.Sprintf("%v", v.Interface()), nil 51 | } 52 | 53 | func (ins *baseTcInstance) GetFieldValuesByName(name string) (val map[string][]string, err error) { 54 | defer func() { 55 | if err := recover(); err != nil { 56 | // nothing ignore err 57 | } 58 | }() 59 | v := ins.value.FieldByName(name) 60 | if v.Kind() == reflect.Ptr { 61 | v = reflect.Indirect(v) 62 | } 63 | valueMap := make(map[string][]string) 64 | if v.Kind() == reflect.Slice { 65 | for i := 0; i < v.Len(); i++ { 66 | if v.Index(i).Elem().Kind() == reflect.String { 67 | valueMap[name] = append(val[name], fmt.Sprintf("%v", v.Index(i).Elem().Interface())) 68 | } else if v.Index(i).Elem().Kind() == reflect.Struct { 69 | var tagKey, tagValue reflect.Value 70 | if v.Index(i).Elem().FieldByName("TagKey").IsValid() && v.Index(i).Elem().FieldByName("TagValue").IsValid() { 71 | tagKey = v.Index(i).Elem().FieldByName("TagKey") 72 | tagValue = v.Index(i).Elem().FieldByName("TagValue") 73 | } else if v.Index(i).Elem().FieldByName("Key").IsValid() && v.Index(i).Elem().FieldByName("Value").IsValid() { 74 | tagKey = v.Index(i).Elem().FieldByName("Key") 75 | tagValue = v.Index(i).Elem().FieldByName("Value") 76 | } 77 | if tagKey.Kind() == reflect.Ptr { 78 | tagKey = reflect.Indirect(tagKey) 79 | } 80 | if tagValue.Kind() == reflect.Ptr { 81 | tagValue = reflect.Indirect(tagValue) 82 | } 83 | if util.IsValidTagKey(tagKey.String()) { 84 | valueMap[tagKey.String()] = append(val[tagKey.String()], tagValue.String()) 85 | } 86 | } 87 | } 88 | } else { 89 | valueMap[name] = append(val[name], fmt.Sprintf("%v", v.Interface())) 90 | } 91 | return valueMap, nil 92 | } 93 | -------------------------------------------------------------------------------- /pkg/instance/instance_cbs.go: -------------------------------------------------------------------------------- 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 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_cdb.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320" 8 | ) 9 | 10 | type CdbTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.InstanceInfo 13 | } 14 | 15 | func (ins *CdbTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewCdbTcInstance(instanceId string, meta *sdk.InstanceInfo) (ins *CdbTcInstance, 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 = &CdbTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_cdn.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606" 8 | ) 9 | 10 | type CdnTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.BriefDomain 13 | } 14 | 15 | func (ins *CdnTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewCdnTcInstance(instanceId string, meta *sdk.BriefDomain) (ins *CdnTcInstance, 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 = &CdnTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_cfs.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719" 8 | ) 9 | 10 | type CfsTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.FileSystemInfo 13 | } 14 | 15 | func (ins *CfsTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewCfsTcInstance(instanceId string, meta *sdk.FileSystemInfo) (ins *CfsTcInstance, 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 = &CfsTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_clb.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317" 8 | ) 9 | 10 | type ClbInstance struct { 11 | baseTcInstance 12 | meta *sdk.LoadBalancer 13 | } 14 | 15 | func (ins *ClbInstance) GetMonitorQueryKey() string { 16 | if len(ins.meta.LoadBalancerVips) == 1 { 17 | return *ins.meta.LoadBalancerVips[0] 18 | } else if *ins.meta.AddressIPv6 != "" { 19 | return *ins.meta.AddressIPv6 20 | } else { 21 | return "" 22 | } 23 | } 24 | 25 | func (ins *ClbInstance) GetMeta() interface{} { 26 | return ins.meta 27 | } 28 | 29 | func NewClbTcInstance(instanceId string, meta *sdk.LoadBalancer) (ins *ClbInstance, err error) { 30 | if instanceId == "" { 31 | return nil, fmt.Errorf("instanceId is empty ") 32 | } 33 | if meta == nil { 34 | return nil, fmt.Errorf("meta is empty ") 35 | } 36 | ins = &ClbInstance{ 37 | baseTcInstance: baseTcInstance{ 38 | instanceId: instanceId, 39 | value: reflect.ValueOf(*meta), 40 | }, 41 | meta: meta, 42 | } 43 | return 44 | } 45 | -------------------------------------------------------------------------------- /pkg/instance/instance_clb_private.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317" 8 | ) 9 | 10 | type ClbPrivateInstance struct { 11 | baseTcInstance 12 | meta *sdk.LoadBalancer 13 | } 14 | 15 | func (ins *ClbPrivateInstance) GetMonitorQueryKey() string { 16 | if len(ins.meta.LoadBalancerVips) == 1 { 17 | return *ins.meta.LoadBalancerVips[0] 18 | } else if *ins.meta.AddressIPv6 != "" { 19 | return *ins.meta.AddressIPv6 20 | } else { 21 | return "" 22 | } 23 | } 24 | 25 | func (ins *ClbPrivateInstance) GetMeta() interface{} { 26 | return ins.meta 27 | } 28 | 29 | func NewClbPrivateTcInstance(instanceId string, meta *sdk.LoadBalancer) (ins *ClbPrivateInstance, err error) { 30 | if instanceId == "" { 31 | return nil, fmt.Errorf("instanceId is empty ") 32 | } 33 | if meta == nil { 34 | return nil, fmt.Errorf("meta is empty ") 35 | } 36 | ins = &ClbPrivateInstance{ 37 | baseTcInstance: baseTcInstance{ 38 | instanceId: instanceId, 39 | value: reflect.ValueOf(*meta), 40 | }, 41 | meta: meta, 42 | } 43 | return 44 | } 45 | -------------------------------------------------------------------------------- /pkg/instance/instance_cmq.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cmq/v20190304" 8 | ) 9 | 10 | type CMQTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.QueueSet 13 | } 14 | 15 | func (ins *CMQTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewCMQTcInstance(instanceId string, meta *sdk.QueueSet) (ins *CMQTcInstance, 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 = &CMQTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_cmqtopic.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cmq/v20190304" 8 | ) 9 | 10 | type CMQTopicTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.TopicSet 13 | } 14 | 15 | func (ins *CMQTopicTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewCMQTopicTcInstance(instanceId string, meta *sdk.TopicSet) (ins *CMQTopicTcInstance, 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 = &CMQTopicTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_cos.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentyun/cos-go-sdk-v5" 8 | ) 9 | 10 | type CosTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.Bucket 13 | } 14 | 15 | func (ins *CosTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewCosTcInstance(instanceId string, meta *sdk.Bucket) (ins *CosTcInstance, 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 = &CosTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_cvm.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" 8 | ) 9 | 10 | type CvmTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.Instance 13 | } 14 | 15 | func (ins *CvmTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewCvmTcInstance(instanceId string, meta *sdk.Instance) (ins *CvmTcInstance, 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 = &CvmTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_cynosdb.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107" 8 | ) 9 | 10 | type CynosdbTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.CynosdbInstance 13 | } 14 | 15 | func (ins *CynosdbTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewCynosdbTcInstance(instanceId string, meta *sdk.CynosdbInstance) (ins *CynosdbTcInstance, 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 = &CynosdbTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_dc.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc/v20180410" 8 | ) 9 | 10 | type DcTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.DirectConnect 13 | } 14 | 15 | func (ins *DcTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewDcTcInstance(instanceId string, meta *sdk.DirectConnect) (ins *DcTcInstance, 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 = &DcTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_dcdb.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411" 8 | ) 9 | 10 | type DcdbTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.DCDBInstanceInfo 13 | } 14 | 15 | func (ins *DcdbTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewDcdbTcInstance(instanceId string, meta *sdk.DCDBInstanceInfo) (ins *DcdbTcInstance, 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 = &DcdbTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_dcg.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 8 | ) 9 | 10 | type DcgTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.DirectConnectGateway 13 | } 14 | 15 | func (ins *DcgTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewDcgTcInstance(instanceId string, meta *sdk.DirectConnectGateway) (ins *DcgTcInstance, 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 = &DcgTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_dcx.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc/v20180410" 8 | ) 9 | 10 | type DcxTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.DirectConnectTunnel 13 | } 14 | 15 | func (ins *DcxTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewDcxTcInstance(instanceId string, meta *sdk.DirectConnectTunnel) (ins *DcxTcInstance, 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 = &DcxTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_dts.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dts/v20180330" 8 | ) 9 | 10 | type DtsTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.SubscribeInfo 13 | } 14 | 15 | func (ins *DtsTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewDtsTcInstance(instanceId string, meta *sdk.SubscribeInfo) (ins *DtsTcInstance, 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 = &DtsTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_eip.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 8 | ) 9 | 10 | type EIPInstance struct { 11 | baseTcInstance 12 | meta *sdk.Address 13 | } 14 | 15 | func (ins *EIPInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewEIPTcInstance(instanceId string, meta *sdk.Address) (ins *EIPInstance, 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 = &EIPInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_es.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es/v20180416" 8 | ) 9 | 10 | type ESTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.InstanceInfo 13 | } 14 | 15 | func (ins *ESTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewESTcInstance(instanceId string, meta *sdk.InstanceInfo) (ins *ESTcInstance, 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 = &ESTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_kafka.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819" 8 | ) 9 | 10 | type kafkaTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.Instance 13 | } 14 | 15 | func (ins *kafkaTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewKafkaTcInstance(instanceId string, meta *sdk.Instance) (ins *kafkaTcInstance, 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 = &kafkaTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_lighthouse.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324" 8 | ) 9 | 10 | type LighthouseTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.Instance 13 | } 14 | 15 | func (ins *LighthouseTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewLighthouseTcInstance(instanceId string, meta *sdk.Instance) (ins *LighthouseTcInstance, 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 = &LighthouseTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_mariadb.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb/v20170312" 8 | ) 9 | 10 | type MariaDBTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.DBInstance 13 | } 14 | 15 | func (ins *MariaDBTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewMariaDBTcInstance(instanceId string, meta *sdk.DBInstance) (ins *MariaDBTcInstance, 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 = &MariaDBTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_memcached.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/memcached/v20190318" 8 | ) 9 | 10 | type MemcachedTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.InstanceListInfo 13 | } 14 | 15 | func (ins *MemcachedTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewMemcachedTcInstance(instanceId string, meta *sdk.InstanceListInfo) (ins *MemcachedTcInstance, 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 = &MemcachedTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_mongo.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725" 8 | ) 9 | 10 | type MongoTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.InstanceDetail 13 | } 14 | 15 | func (ins *MongoTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewMongoTcInstance(instanceId string, meta *sdk.InstanceDetail) (ins *MongoTcInstance, 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 = &MongoTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_nat.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 8 | ) 9 | 10 | type NatTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.NatGateway 13 | } 14 | 15 | func (ins *NatTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewNatTcInstance(instanceId string, meta *sdk.NatGateway) (ins *NatTcInstance, 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 = &NatTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_pg.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312" 8 | ) 9 | 10 | type PGTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.DBInstance 13 | } 14 | 15 | func (ins *PGTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewPGTcInstance(instanceId string, meta *sdk.DBInstance) (ins *PGTcInstance, 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 = &PGTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_qaap.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap/v20180529" 8 | ) 9 | 10 | type QaapTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.ProxyInfo 13 | } 14 | 15 | func (ins *QaapTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewQaapTcInstance(instanceId string, meta *sdk.ProxyInfo) (ins *QaapTcInstance, 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 = &QaapTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_redis.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412" 6 | "reflect" 7 | ) 8 | 9 | type RedisTcInstance struct { 10 | baseTcInstance 11 | meta *sdk.InstanceSet 12 | } 13 | 14 | func NewRedisTcInstance(instanceId string, meta *sdk.InstanceSet) (ins *RedisTcInstance, err error) { 15 | if instanceId == "" { 16 | return nil, fmt.Errorf("instanceId is empty ") 17 | } 18 | if meta == nil { 19 | return nil, fmt.Errorf("meta is empty ") 20 | } 21 | ins = &RedisTcInstance{ 22 | baseTcInstance: baseTcInstance{ 23 | instanceId: instanceId, 24 | value: reflect.ValueOf(*meta), 25 | }, 26 | meta: meta, 27 | } 28 | return 29 | } 30 | 31 | func (ins *RedisTcInstance) GetMeta() interface{} { 32 | return ins.meta 33 | } 34 | -------------------------------------------------------------------------------- /pkg/instance/instance_sqlserver.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328" 8 | ) 9 | 10 | type SqlServerTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.DBInstance 13 | } 14 | 15 | func (ins *SqlServerTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewSqlServerTcInstance(instanceId string, meta *sdk.DBInstance) (ins *SqlServerTcInstance, 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 = &SqlServerTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_tdmq.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq/v20200217" 8 | ) 9 | 10 | type TdmqTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.RocketMQClusterDetail 13 | } 14 | 15 | func (ins *TdmqTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewTdmqTcInstance(instanceId string, meta *sdk.RocketMQClusterDetail) (ins *TdmqTcInstance, 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 = &TdmqTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_tse.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse/v20201207" 8 | ) 9 | 10 | type TseTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.SREInstance 13 | } 14 | 15 | func (ins *TseTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewTseTcInstance(instanceId string, meta *sdk.SREInstance) (ins *TseTcInstance, 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 = &TseTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_vbc.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 8 | ) 9 | 10 | type VbcTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.CCN 13 | } 14 | 15 | func (ins *VbcTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewVbcTcInstance(instanceId string, meta *sdk.CCN) (ins *VbcTcInstance, 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 = &VbcTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_vpngw.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 8 | ) 9 | 10 | type VpngwTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.VpnGateway 13 | } 14 | 15 | func (ins *VpngwTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewVpngwTcInstance(instanceId string, meta *sdk.VpnGateway) (ins *VpngwTcInstance, 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 = &VpngwTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_vpnx.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 8 | ) 9 | 10 | type VpnxTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.VpnConnection 13 | } 14 | 15 | func (ins *VpnxTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewVpnxTcInstance(instanceId string, meta *sdk.VpnConnection) (ins *VpnxTcInstance, 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 = &VpnxTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/instance_waf.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | 7 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125" 8 | ) 9 | 10 | type WafTcInstance struct { 11 | baseTcInstance 12 | meta *sdk.DomainInfo 13 | } 14 | 15 | func (ins *WafTcInstance) GetMeta() interface{} { 16 | return ins.meta 17 | } 18 | 19 | func NewWafTcInstance(instanceId string, meta *sdk.DomainInfo) (ins *WafTcInstance, 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 = &WafTcInstance{ 27 | baseTcInstance: baseTcInstance{ 28 | instanceId: instanceId, 29 | value: reflect.ValueOf(*meta), 30 | }, 31 | meta: meta, 32 | } 33 | return 34 | } 35 | -------------------------------------------------------------------------------- /pkg/instance/repository.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 10 | ) 11 | 12 | var ( 13 | factoryMap = make(map[string]func(common.CredentialIface, *config.TencentConfig, log.Logger) (TcInstanceRepository, error)) 14 | ) 15 | 16 | // 每个产品的实例对象的Repository 17 | type TcInstanceRepository interface { 18 | // 获取实例id 19 | GetInstanceKey() string 20 | // 根据id, 获取实例对象 21 | Get(id string) (TcInstance, error) 22 | // 根据id列表, 获取所有的实例对象 23 | ListByIds(ids []string) ([]TcInstance, error) 24 | // 根据filters, 获取符合条件的所有实例对象 25 | ListByFilters(filters map[string]string) ([]TcInstance, error) 26 | } 27 | 28 | func NewTcInstanceRepository(namespace string, cred common.CredentialIface, conf *config.TencentConfig, logger log.Logger) (TcInstanceRepository, error) { 29 | f, exists := factoryMap[namespace] 30 | if !exists { 31 | return nil, fmt.Errorf("Namespace not support, namespace=%s ", namespace) 32 | } 33 | return f(cred, conf, logger) 34 | } 35 | 36 | // 将TcInstanceRepository注册到factoryMap中 37 | func registerRepository(namespace string, factory func(common.CredentialIface, *config.TencentConfig, log.Logger) (TcInstanceRepository, error)) { 38 | factoryMap[namespace] = factory 39 | } 40 | -------------------------------------------------------------------------------- /pkg/instance/repository_cbs.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/go-kit/log/level" 8 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312" 9 | cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/BLOCK_STORAGE", NewCbsTcInstanceRepository) 17 | } 18 | 19 | type CbsTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | c *config.TencentConfig 22 | client *sdk.Client 23 | logger log.Logger 24 | } 25 | 26 | func (repo *CbsTcInstanceRepository) GetInstanceKey() string { 27 | return "DiskId" 28 | } 29 | 30 | func (repo *CbsTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 31 | req := sdk.NewDescribeDisksRequest() 32 | req.DiskIds = []*string{&id} 33 | resp, err := repo.client.DescribeDisks(req) 34 | if err != nil { 35 | return 36 | } 37 | if len(resp.Response.DiskSet) != 1 { 38 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 39 | } 40 | meta := resp.Response.DiskSet[0] 41 | instance, err = NewCbsTcInstance(id, meta) 42 | if err != nil { 43 | return 44 | } 45 | return 46 | } 47 | 48 | func (repo *CbsTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 49 | return 50 | } 51 | 52 | func (repo *CbsTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 53 | req := sdk.NewDescribeDisksRequest() 54 | var offset uint64 = 0 55 | var limit uint64 = 100 56 | var total uint64 = 0 57 | 58 | req.Offset = &offset 59 | req.Limit = &limit 60 | 61 | getMoreInstances: 62 | resp, err := repo.client.DescribeDisks(req) 63 | if err != nil { 64 | return 65 | } 66 | if total == 0 { 67 | total = *resp.Response.TotalCount 68 | } 69 | for _, meta := range resp.Response.DiskSet { 70 | ins, e := NewCbsTcInstance(*meta.DiskId, meta) 71 | if e != nil { 72 | level.Error(repo.logger).Log("msg", "Create cbs instance fail", "id", *meta.DiskId) 73 | continue 74 | } 75 | instances = append(instances, ins) 76 | } 77 | offset += limit 78 | if offset < total { 79 | req.Offset = &offset 80 | goto getMoreInstances 81 | } 82 | return 83 | } 84 | 85 | func NewCbsTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 86 | cli, err := client.NewCbsClient(cred, c) 87 | if err != nil { 88 | return 89 | } 90 | repo = &CbsTcInstanceRepository{ 91 | credential: cred, 92 | c: c, 93 | client: cli, 94 | logger: logger, 95 | } 96 | return 97 | } 98 | 99 | // cvm instance 100 | type CbsTcInstanceInfosRepository interface { 101 | Get(id string) ([]string, error) 102 | GetInstanceInfosInfoByFilters([]string) (*cvm.DescribeInstancesResponse, error) 103 | } 104 | 105 | type CbsTcInstanceInfosRepositoryImpl struct { 106 | client *cvm.Client 107 | logger log.Logger 108 | } 109 | 110 | func (repo *CbsTcInstanceInfosRepositoryImpl) Get(id string) (instanceIds []string, err error) { 111 | req := cvm.NewDescribeInstancesRequest() 112 | req.InstanceIds = []*string{&id} 113 | resp, err := repo.client.DescribeInstances(req) 114 | if err != nil { 115 | return 116 | } 117 | for _, instanceInfo := range resp.Response.InstanceSet { 118 | instanceIds = append(instanceIds, *instanceInfo.InstanceId) 119 | } 120 | return 121 | } 122 | 123 | func (repo *CbsTcInstanceInfosRepositoryImpl) GetInstanceInfosInfoByFilters(ids []string) (instances *cvm.DescribeInstancesResponse, err error) { 124 | req := cvm.NewDescribeInstancesRequest() 125 | var offset int64 = 0 126 | var limit int64 = 100 127 | 128 | req.Offset = &offset 129 | req.Limit = &limit 130 | if ids!=nil{ 131 | for _, id := range ids { 132 | req.InstanceIds = []*string{&id} 133 | } 134 | } 135 | return repo.client.DescribeInstances(req) 136 | } 137 | 138 | func NewCbsTcInstanceInfosRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (CbsTcInstanceInfosRepository, error) { 139 | cli, err := client.NewCvmClient(cred, c) 140 | if err != nil { 141 | return nil, err 142 | } 143 | repo := &CbsTcInstanceInfosRepositoryImpl{ 144 | client: cli, 145 | logger: logger, 146 | } 147 | return repo, nil 148 | } 149 | -------------------------------------------------------------------------------- /pkg/instance/repository_cdb.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/CDB", NewCdbTcInstanceRepository) 17 | } 18 | 19 | type CdbTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *CdbTcInstanceRepository) GetInstanceKey() string { 26 | return "InstanceId" 27 | } 28 | 29 | func (repo *CdbTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeDBInstancesRequest() 31 | req.InstanceIds = []*string{&id} 32 | resp, err := repo.client.DescribeDBInstances(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.Items) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.Items[0] 40 | instance, err = NewCdbTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *CdbTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *CdbTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeDBInstancesRequest() 53 | var offset uint64 = 0 54 | var limit uint64 = 2000 55 | var total int64 = -1 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeDBInstances(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == -1 { 66 | total = *resp.Response.TotalCount 67 | } 68 | for _, meta := range resp.Response.Items { 69 | ins, e := NewCdbTcInstance(*meta.InstanceId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create cdb instance fail", "id", *meta.InstanceId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < uint64(total) { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | 82 | return 83 | } 84 | 85 | func NewCdbTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 86 | cli, err := client.NewCdbClient(cred, c) 87 | if err != nil { 88 | return 89 | } 90 | repo = &CdbTcInstanceRepository{ 91 | credential: cred, 92 | client: cli, 93 | logger: logger, 94 | } 95 | return 96 | } 97 | -------------------------------------------------------------------------------- /pkg/instance/repository_cdn.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/go-kit/log/level" 8 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606" 9 | 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/CDN", NewCdnTcInstanceRepository) 17 | } 18 | 19 | type CdnTcInstanceRepository struct { 20 | client *sdk.Client 21 | logger log.Logger 22 | } 23 | 24 | func (repo *CdnTcInstanceRepository) GetInstanceKey() string { 25 | return "InstanceId" 26 | } 27 | 28 | func (repo *CdnTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 29 | req := sdk.NewDescribeDomainsRequest() 30 | // req.Filters.Name = []*string{&id} 31 | resp, err := repo.client.DescribeDomains(req) 32 | if err != nil { 33 | return 34 | } 35 | if len(resp.Response.Domains) != 1 { 36 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 37 | } 38 | meta := resp.Response.Domains[0] 39 | instance, err = NewCdnTcInstance(id, meta) 40 | if err != nil { 41 | return 42 | } 43 | return 44 | } 45 | 46 | func (repo *CdnTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 47 | return 48 | } 49 | 50 | func (repo *CdnTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 51 | req := sdk.NewDescribeDomainsRequest() 52 | var offset int64 = 0 53 | var limit int64 = 100 54 | var total int64 = -1 55 | 56 | req.Offset = &offset 57 | req.Limit = &limit 58 | 59 | getMoreInstances: 60 | resp, err := repo.client.DescribeDomains(req) 61 | if err != nil { 62 | return 63 | } 64 | if total == -1 { 65 | total = *resp.Response.TotalNumber 66 | } 67 | for _, meta := range resp.Response.Domains { 68 | ins, e := NewCdnTcInstance(*meta.Domain, meta) 69 | if e != nil { 70 | level.Error(repo.logger).Log("msg", "Create Cdn instance fail", "id", *meta.Domain) 71 | continue 72 | } 73 | instances = append(instances, ins) 74 | } 75 | offset += limit 76 | if offset < total { 77 | req.Offset = &offset 78 | goto getMoreInstances 79 | } 80 | 81 | return 82 | } 83 | 84 | func NewCdnTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewCdnClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &CdnTcInstanceRepository{ 90 | client: cli, 91 | logger: logger, 92 | } 93 | return 94 | } 95 | -------------------------------------------------------------------------------- /pkg/instance/repository_cfs.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 6 | 7 | "github.com/go-kit/log" 8 | "github.com/go-kit/log/level" 9 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719" 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 12 | ) 13 | 14 | func init() { 15 | registerRepository("QCE/CFS", NewCfsTcInstanceRepository) 16 | } 17 | 18 | type CfsTcInstanceRepository struct { 19 | credential common.CredentialIface 20 | client *sdk.Client 21 | logger log.Logger 22 | } 23 | 24 | func (repo *CfsTcInstanceRepository) GetInstanceKey() string { 25 | return "FileSystemId" 26 | } 27 | 28 | func (repo *CfsTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 29 | req := sdk.NewDescribeCfsFileSystemsRequest() 30 | req.FileSystemId = &id 31 | resp, err := repo.client.DescribeCfsFileSystems(req) 32 | if err != nil { 33 | return 34 | } 35 | if len(resp.Response.FileSystems) != 1 { 36 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 37 | } 38 | meta := resp.Response.FileSystems[0] 39 | instance, err = NewCfsTcInstance(id, meta) 40 | if err != nil { 41 | return 42 | } 43 | return 44 | } 45 | 46 | func (repo *CfsTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 47 | return 48 | } 49 | 50 | func (repo *CfsTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 51 | req := sdk.NewDescribeCfsFileSystemsRequest() 52 | 53 | // getMoreInstances: 54 | resp, err := repo.client.DescribeCfsFileSystems(req) 55 | if err != nil { 56 | return 57 | } 58 | for _, meta := range resp.Response.FileSystems { 59 | ins, e := NewCfsTcInstance(*meta.FileSystemId, meta) 60 | if e != nil { 61 | level.Error(repo.logger).Log("msg", "Create Cfs instance fail", "id", *meta.FileSystemId) 62 | continue 63 | } 64 | instances = append(instances, ins) 65 | } 66 | // goto getMoreInstances 67 | 68 | return 69 | } 70 | 71 | func NewCfsTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 72 | cli, err := client.NewCfsClient(cred, c) 73 | if err != nil { 74 | return 75 | } 76 | repo = &CfsTcInstanceRepository{ 77 | credential: cred, 78 | client: cli, 79 | logger: logger, 80 | } 81 | return 82 | } 83 | 84 | // Replications 85 | type CfsSnapshotsRepository interface { 86 | GetCfsSnapshotsInfo(instanceId string) (*sdk.DescribeCfsSnapshotsResponse, error) 87 | } 88 | 89 | type CfsSnapshotsRepositoryImpl struct { 90 | client *sdk.Client 91 | logger log.Logger 92 | } 93 | 94 | func (repo *CfsSnapshotsRepositoryImpl) GetCfsSnapshotsInfo(instanceId string) (*sdk.DescribeCfsSnapshotsResponse, error) { 95 | req := sdk.NewDescribeCfsSnapshotsRequest() 96 | var offset uint64 = 0 97 | var limit uint64 = 100 98 | req.Limit = &limit 99 | req.Offset = &offset 100 | req.FileSystemId = &instanceId 101 | return repo.client.DescribeCfsSnapshots(req) 102 | } 103 | 104 | func NewCfsSnapshotsRepositoryRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (CfsSnapshotsRepository, error) { 105 | cli, err := client.NewCfsClient(cred, c) 106 | if err != nil { 107 | return nil, err 108 | } 109 | repo := &CfsSnapshotsRepositoryImpl{ 110 | client: cli, 111 | logger: logger, 112 | } 113 | return repo, nil 114 | } 115 | -------------------------------------------------------------------------------- /pkg/instance/repository_clb.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | 7 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 8 | 9 | "github.com/go-kit/log" 10 | "github.com/go-kit/log/level" 11 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 13 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 14 | ) 15 | 16 | func init() { 17 | // LB_PUBLIC、LOADBALANCE实例对象是一样的 18 | registerRepository("QCE/LB_PUBLIC", NewClbTcInstanceRepository) 19 | registerRepository("QCE/LOADBALANCE", NewClbTcInstanceRepository) 20 | } 21 | 22 | var open = "OPEN" 23 | 24 | type ClbTcInstanceRepository struct { 25 | credential common.CredentialIface 26 | client *sdk.Client 27 | logger log.Logger 28 | } 29 | 30 | func (repo *ClbTcInstanceRepository) GetInstanceKey() string { 31 | return "LoadBalancerVip" 32 | } 33 | 34 | func (repo *ClbTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 35 | req := sdk.NewDescribeLoadBalancersRequest() 36 | 37 | ip := net.ParseIP(id) 38 | if ip != nil { 39 | ipstr := ip.String() 40 | req.LoadBalancerVips = []*string{&ipstr} 41 | } else { 42 | req.LoadBalancerIds = []*string{&id} 43 | } 44 | req.LoadBalancerType = &open 45 | resp, err := repo.client.DescribeLoadBalancers(req) 46 | if err != nil { 47 | return 48 | } 49 | 50 | if len(resp.Response.LoadBalancerSet) == 0 { 51 | return nil, fmt.Errorf("loadBalancer instance not found") 52 | } else if len(resp.Response.LoadBalancerSet) > 1 { 53 | return nil, fmt.Errorf("response instanceDetails size != 1") 54 | } 55 | meta := resp.Response.LoadBalancerSet[0] 56 | instance, err = NewClbTcInstance(id, meta) 57 | if err != nil { 58 | return 59 | } 60 | return 61 | } 62 | 63 | func (repo *ClbTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 64 | return 65 | } 66 | 67 | func (repo *ClbTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 68 | req := sdk.NewDescribeLoadBalancersRequest() 69 | var offset int64 = 0 70 | var limit int64 = 100 71 | var total int64 = -1 72 | 73 | req.Offset = &offset 74 | req.Limit = &limit 75 | req.LoadBalancerType = &open 76 | 77 | getMoreInstances: 78 | resp, err := repo.client.DescribeLoadBalancers(req) 79 | if err != nil { 80 | return 81 | } 82 | if total == -1 { 83 | total = int64(*resp.Response.TotalCount) 84 | } 85 | for _, meta := range resp.Response.LoadBalancerSet { 86 | ins, e := NewClbTcInstance(*meta.LoadBalancerId, meta) 87 | if e != nil { 88 | level.Error(repo.logger).Log("msg", "Create clb instance fail", "id", *meta.LoadBalancerId) 89 | continue 90 | } 91 | if (meta.LoadBalancerVips == nil || len(meta.LoadBalancerVips) == 0) && meta.AddressIPv6 == nil { 92 | level.Warn(repo.logger).Log("msg", "clb instance no include vip", "id", *meta.LoadBalancerId) 93 | continue 94 | } 95 | instances = append(instances, ins) 96 | } 97 | offset += limit 98 | if offset < total { 99 | req.Offset = &offset 100 | goto getMoreInstances 101 | } 102 | 103 | return 104 | } 105 | 106 | func NewClbTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 107 | cli, err := client.NewClbClient(cred, c) 108 | if err != nil { 109 | return 110 | } 111 | repo = &ClbTcInstanceRepository{ 112 | credential: cred, 113 | client: cli, 114 | logger: logger, 115 | } 116 | return 117 | } 118 | -------------------------------------------------------------------------------- /pkg/instance/repository_clb_private.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | 7 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 8 | 9 | "github.com/go-kit/log" 10 | "github.com/go-kit/log/level" 11 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 13 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 14 | ) 15 | 16 | func init() { 17 | registerRepository("QCE/LB_PRIVATE", NewClbPrivateTcInstanceRepository) 18 | } 19 | 20 | var internal = "INTERNAL" 21 | 22 | type ClbPrivateTcInstanceRepository struct { 23 | credential common.CredentialIface 24 | client *sdk.Client 25 | logger log.Logger 26 | } 27 | 28 | func (repo *ClbPrivateTcInstanceRepository) GetInstanceKey() string { 29 | return "LoadBalancerVip" 30 | } 31 | 32 | func (repo *ClbPrivateTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 33 | req := sdk.NewDescribeLoadBalancersRequest() 34 | 35 | ip := net.ParseIP(id) 36 | if ip != nil { 37 | ipstr := ip.String() 38 | req.LoadBalancerVips = []*string{&ipstr} 39 | } else { 40 | req.LoadBalancerIds = []*string{&id} 41 | } 42 | req.LoadBalancerType = &internal 43 | resp, err := repo.client.DescribeLoadBalancers(req) 44 | if err != nil { 45 | return 46 | } 47 | 48 | if len(resp.Response.LoadBalancerSet) == 0 { 49 | return nil, fmt.Errorf("loadBalancer instance not found") 50 | } else if len(resp.Response.LoadBalancerSet) > 1 { 51 | return nil, fmt.Errorf("response instanceDetails size != 1") 52 | } 53 | meta := resp.Response.LoadBalancerSet[0] 54 | instance, err = NewClbPrivateTcInstance(id, meta) 55 | if err != nil { 56 | return 57 | } 58 | return 59 | } 60 | 61 | func (repo *ClbPrivateTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 62 | return 63 | } 64 | 65 | func (repo *ClbPrivateTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 66 | req := sdk.NewDescribeLoadBalancersRequest() 67 | var offset int64 = 0 68 | var limit int64 = 100 69 | var total int64 = -1 70 | 71 | req.Offset = &offset 72 | req.Limit = &limit 73 | req.LoadBalancerType = &internal 74 | 75 | getMoreInstances: 76 | resp, err := repo.client.DescribeLoadBalancers(req) 77 | if err != nil { 78 | return 79 | } 80 | if total == -1 { 81 | total = int64(*resp.Response.TotalCount) 82 | } 83 | for _, meta := range resp.Response.LoadBalancerSet { 84 | ins, e := NewClbPrivateTcInstance(*meta.LoadBalancerId, meta) 85 | if e != nil { 86 | level.Error(repo.logger).Log("msg", "Create ClbPrivate instance fail", "id", *meta.LoadBalancerId) 87 | continue 88 | } 89 | if (meta.LoadBalancerVips == nil || len(meta.LoadBalancerVips) == 0) && meta.AddressIPv6 == nil { 90 | level.Warn(repo.logger).Log("msg", "ClbPrivate instance no include vip", "id", *meta.LoadBalancerId) 91 | continue 92 | } 93 | instances = append(instances, ins) 94 | } 95 | offset += limit 96 | if offset < total { 97 | req.Offset = &offset 98 | goto getMoreInstances 99 | } 100 | 101 | return 102 | } 103 | 104 | func NewClbPrivateTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 105 | cli, err := client.NewClbClient(cred, c) 106 | if err != nil { 107 | return 108 | } 109 | repo = &ClbPrivateTcInstanceRepository{ 110 | credential: cred, 111 | client: cli, 112 | logger: logger, 113 | } 114 | return 115 | } 116 | -------------------------------------------------------------------------------- /pkg/instance/repository_cmq.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cmq/v20190304" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/CMQ", NewCMQTcInstanceRepository) 17 | } 18 | 19 | type CMQTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *CMQTcInstanceRepository) GetInstanceKey() string { 26 | return "QueueId" 27 | } 28 | 29 | func (repo *CMQTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeQueueDetailRequest() 31 | req.QueueName = &id 32 | resp, err := repo.client.DescribeQueueDetail(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.QueueSet) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.QueueSet[0] 40 | instance, err = NewCMQTcInstance(*meta.QueueId, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *CMQTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *CMQTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeQueueDetailRequest() 53 | var offset uint64 = 0 54 | var limit uint64 = 100 55 | var total uint64 = 0 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeQueueDetail(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == 0 { 66 | total = *resp.Response.TotalCount 67 | } 68 | for _, meta := range resp.Response.QueueSet { 69 | ins, e := NewCMQTcInstance(*meta.QueueId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create cmq instance fail", "id", *meta.QueueId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < total { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | return 82 | } 83 | 84 | func NewCMQTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewCMQClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &CMQTcInstanceRepository{ 90 | credential: cred, 91 | client: cli, 92 | logger: logger, 93 | } 94 | return 95 | } 96 | -------------------------------------------------------------------------------- /pkg/instance/repository_cmqtopic.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cmq/v20190304" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/CMQTOPIC", NewCMQTopicTcInstanceRepository) 17 | } 18 | 19 | type CMQTopicTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *CMQTopicTcInstanceRepository) GetInstanceKey() string { 26 | return "TopicId" 27 | } 28 | 29 | func (repo *CMQTopicTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeTopicDetailRequest() 31 | req.TopicName = &id 32 | resp, err := repo.client.DescribeTopicDetail(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.TopicSet) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.TopicSet[0] 40 | instance, err = NewCMQTopicTcInstance(*meta.TopicId, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *CMQTopicTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *CMQTopicTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeTopicDetailRequest() 53 | var offset uint64 = 0 54 | var limit uint64 = 100 55 | var total uint64 = 0 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeTopicDetail(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == 0 { 66 | total = *resp.Response.TotalCount 67 | } 68 | for _, meta := range resp.Response.TopicSet { 69 | ins, e := NewCMQTopicTcInstance(*meta.TopicId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create cmq topic fail", "id", *meta.TopicId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < total { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | return 82 | } 83 | 84 | func NewCMQTopicTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewCMQClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &CMQTopicTcInstanceRepository{ 90 | credential: cred, 91 | client: cli, 92 | logger: logger, 93 | } 94 | return 95 | } 96 | -------------------------------------------------------------------------------- /pkg/instance/repository_cos.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | 7 | "github.com/go-kit/log" 8 | "github.com/go-kit/log/level" 9 | sdk "github.com/tencentyun/cos-go-sdk-v5" 10 | 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 13 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 14 | ) 15 | 16 | func init() { 17 | registerRepository("QCE/COS", NewCosTcInstanceRepository) 18 | } 19 | 20 | type CosTcInstanceRepository struct { 21 | region string 22 | client *sdk.Client 23 | logger log.Logger 24 | } 25 | 26 | func (repo *CosTcInstanceRepository) GetInstanceKey() string { 27 | return "InstanceId" 28 | } 29 | 30 | func (repo *CosTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 31 | resp, _, err := repo.client.Service.Get(context.Background()) 32 | if err != nil { 33 | return 34 | } 35 | 36 | if len(resp.Buckets) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Buckets[0] 40 | instance, err = NewCosTcInstance(id, &meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *CosTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *CosTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | // req := sdk.NewDescribeVpnConnectionsRequest() 53 | // var offset uint64 = 0 54 | // var limit uint64 = 100 55 | // var total int64 = -1 56 | // 57 | // req.Offset = &offset 58 | // req.Limit = &limit 59 | 60 | // getMoreInstances: 61 | resp, _, err := repo.client.Service.Get(context.Background()) 62 | if err != nil { 63 | return 64 | } 65 | for _, meta := range resp.Buckets { 66 | // when region is ap-guangzhou, will get all buckets in every region. 67 | // need to filter by region 68 | if meta.Region != repo.region { 69 | continue 70 | } 71 | ins, e := NewCosTcInstance(meta.Name, &meta) 72 | if e != nil { 73 | level.Error(repo.logger).Log("msg", "Create Cos instance fail", "id", meta.Name) 74 | continue 75 | } 76 | instances = append(instances, ins) 77 | } 78 | // offset += limit 79 | // if offset < uint64(total) { 80 | // req.Offset = &offset 81 | // goto getMoreInstances 82 | // } 83 | 84 | return 85 | } 86 | 87 | func NewCosTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 88 | cli, err := client.NewCosClient(cred, c) 89 | if err != nil { 90 | return 91 | } 92 | repo = &CosTcInstanceRepository{ 93 | region: c.Credential.Region, 94 | client: cli, 95 | logger: logger, 96 | } 97 | return 98 | } 99 | -------------------------------------------------------------------------------- /pkg/instance/repository_cvm.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/CVM", NewCvmTcInstanceRepository) 17 | } 18 | 19 | type CvmTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *CvmTcInstanceRepository) GetInstanceKey() string { 26 | return "InstanceId" 27 | } 28 | 29 | func (repo *CvmTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeInstancesRequest() 31 | req.InstanceIds = []*string{&id} 32 | resp, err := repo.client.DescribeInstances(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.InstanceSet) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.InstanceSet[0] 40 | instance, err = NewCvmTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *CvmTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *CvmTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeInstancesRequest() 53 | var offset int64 = 0 54 | var limit int64 = 100 55 | var total int64 = -1 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeInstances(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == -1 { 66 | total = *resp.Response.TotalCount 67 | } 68 | for _, meta := range resp.Response.InstanceSet { 69 | ins, e := NewCvmTcInstance(*meta.InstanceId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create cvm instance fail", "id", *meta.InstanceId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < total { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | return 82 | } 83 | 84 | func NewCvmTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewCvmClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &CvmTcInstanceRepository{ 90 | credential: cred, 91 | client: cli, 92 | logger: logger, 93 | } 94 | return 95 | } 96 | -------------------------------------------------------------------------------- /pkg/instance/repository_cynosdb.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/go-kit/log/level" 8 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107" 9 | 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/CYNOSDB_MYSQL", NewCynosdbTcInstanceRepository) 17 | } 18 | 19 | var dbType = "MYSQL" 20 | var status = "running" 21 | 22 | type CynosdbTcInstanceRepository struct { 23 | client *sdk.Client 24 | logger log.Logger 25 | } 26 | 27 | func (repo *CynosdbTcInstanceRepository) GetInstanceKey() string { 28 | return "InstanceId" 29 | } 30 | 31 | func (repo *CynosdbTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 32 | level.Info(repo.logger).Log("start CynosdbTcInstanceRepository") 33 | req := sdk.NewDescribeInstancesRequest() 34 | req.InstanceIds = []*string{&id} 35 | req.DbType = &dbType 36 | req.Status = &status 37 | resp, err := repo.client.DescribeInstances(req) 38 | if err != nil { 39 | return 40 | } 41 | if len(resp.Response.InstanceSet) != 1 { 42 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 43 | } 44 | meta := resp.Response.InstanceSet[0] 45 | instance, err = NewCynosdbTcInstance(id, meta) 46 | if err != nil { 47 | return 48 | } 49 | return 50 | } 51 | 52 | func (repo *CynosdbTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 53 | return 54 | } 55 | 56 | func (repo *CynosdbTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 57 | req := sdk.NewDescribeInstancesRequest() 58 | var offset int64 = 0 59 | var limit int64 = 100 60 | var total int64 = -1 61 | 62 | req.Offset = &offset 63 | req.Limit = &limit 64 | req.DbType = &dbType 65 | req.Status = &status 66 | getMoreInstances: 67 | resp, err := repo.client.DescribeInstances(req) 68 | if err != nil { 69 | return 70 | } 71 | if total == -1 { 72 | total = int64(*resp.Response.TotalCount) 73 | } 74 | for _, meta := range resp.Response.InstanceSet { 75 | ins, e := NewCynosdbTcInstance(*meta.InstanceId, meta) 76 | if e != nil { 77 | level.Error(repo.logger).Log("msg", "Create Cynosdb instance fail", "id", *meta.InstanceId) 78 | continue 79 | } 80 | instances = append(instances, ins) 81 | 82 | } 83 | offset += limit 84 | if offset < total { 85 | req.Offset = &offset 86 | goto getMoreInstances 87 | } 88 | 89 | return 90 | } 91 | 92 | func NewCynosdbTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 93 | cli, err := client.NewCynosdbClient(cred, c) 94 | if err != nil { 95 | return 96 | } 97 | repo = &CynosdbTcInstanceRepository{ 98 | client: cli, 99 | logger: logger, 100 | } 101 | return 102 | } 103 | -------------------------------------------------------------------------------- /pkg/instance/repository_dc.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc/v20180410" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/DC", NewDcTcInstanceRepository) 17 | } 18 | 19 | type DcTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *DcTcInstanceRepository) GetInstanceKey() string { 26 | return "directConnectId" 27 | } 28 | 29 | func (repo *DcTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeDirectConnectsRequest() 31 | req.DirectConnectIds = []*string{&id} 32 | resp, err := repo.client.DescribeDirectConnects(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.DirectConnectSet) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.DirectConnectSet[0] 40 | instance, err = NewDcTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *DcTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *DcTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeDirectConnectsRequest() 53 | var offset int64 = 0 54 | var limit int64 = 100 55 | var total int64 = -1 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeDirectConnects(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == -1 { 66 | total = *resp.Response.TotalCount 67 | } 68 | for _, meta := range resp.Response.DirectConnectSet { 69 | ins, e := NewDcTcInstance(*meta.DirectConnectId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create dc instance fail", "id", *meta.DirectConnectId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < total { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | 82 | return 83 | } 84 | 85 | func NewDcTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 86 | cli, err := client.NewDcClient(cred, c) 87 | if err != nil { 88 | return 89 | } 90 | repo = &DcTcInstanceRepository{ 91 | credential: cred, 92 | client: cli, 93 | logger: logger, 94 | } 95 | return 96 | } 97 | -------------------------------------------------------------------------------- /pkg/instance/repository_dcdb.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/TDMYSQL", NewDcdbTcInstanceRepository) 17 | } 18 | 19 | type DcdbTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *DcdbTcInstanceRepository) GetInstanceKey() string { 26 | return "InstanceId" 27 | } 28 | 29 | func (repo *DcdbTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeDCDBInstancesRequest() 31 | req.InstanceIds = []*string{&id} 32 | resp, err := repo.client.DescribeDCDBInstances(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.Instances) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.Instances[0] 40 | instance, err = NewDcdbTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *DcdbTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *DcdbTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeDCDBInstancesRequest() 53 | var offset int64 = 0 54 | var limit int64 = 100 55 | var total int64 = -1 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeDCDBInstances(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == -1 { 66 | total = *resp.Response.TotalCount 67 | } 68 | for _, meta := range resp.Response.Instances { 69 | ins, e := NewDcdbTcInstance(*meta.InstanceId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create cdb instance fail", "id", *meta.InstanceId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < total { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | 82 | return 83 | } 84 | 85 | func NewDcdbTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 86 | cli, err := client.NewDCDBClient(cred, c) 87 | if err != nil { 88 | return 89 | } 90 | repo = &DcdbTcInstanceRepository{ 91 | credential: cred, 92 | client: cli, 93 | logger: logger, 94 | } 95 | return 96 | } 97 | -------------------------------------------------------------------------------- /pkg/instance/repository_dcg.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/go-kit/log/level" 8 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 9 | 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/DCG", NewDcgTcInstanceRepository) 17 | } 18 | 19 | type DcgTcInstanceRepository struct { 20 | client *sdk.Client 21 | logger log.Logger 22 | } 23 | 24 | func (repo *DcgTcInstanceRepository) GetInstanceKey() string { 25 | return "InstanceId" 26 | } 27 | 28 | func (repo *DcgTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 29 | req := sdk.NewDescribeDirectConnectGatewaysRequest() 30 | // req.Filters.Name = []*string{&id} 31 | resp, err := repo.client.DescribeDirectConnectGateways(req) 32 | if err != nil { 33 | return 34 | } 35 | if len(resp.Response.DirectConnectGatewaySet) != 1 { 36 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 37 | } 38 | meta := resp.Response.DirectConnectGatewaySet[0] 39 | instance, err = NewDcgTcInstance(id, meta) 40 | if err != nil { 41 | return 42 | } 43 | return 44 | } 45 | 46 | func (repo *DcgTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 47 | return 48 | } 49 | 50 | func (repo *DcgTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 51 | req := sdk.NewDescribeDirectConnectGatewaysRequest() 52 | var offset uint64 = 0 53 | var limit uint64 = 100 54 | var total int64 = -1 55 | 56 | req.Offset = &offset 57 | req.Limit = &limit 58 | 59 | getMoreInstances: 60 | resp, err := repo.client.DescribeDirectConnectGateways(req) 61 | if err != nil { 62 | return 63 | } 64 | if total == -1 { 65 | total = int64(*resp.Response.TotalCount) 66 | } 67 | for _, meta := range resp.Response.DirectConnectGatewaySet { 68 | ins, e := NewDcgTcInstance(*meta.DirectConnectGatewayId, meta) 69 | if e != nil { 70 | level.Error(repo.logger).Log("msg", "Create Dcg instance fail", "id", *meta.DirectConnectGatewayId) 71 | continue 72 | } 73 | instances = append(instances, ins) 74 | } 75 | offset += limit 76 | if offset < uint64(total) { 77 | req.Offset = &offset 78 | goto getMoreInstances 79 | } 80 | 81 | return 82 | } 83 | 84 | func NewDcgTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewVpvClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &DcgTcInstanceRepository{ 90 | client: cli, 91 | logger: logger, 92 | } 93 | return 94 | } 95 | -------------------------------------------------------------------------------- /pkg/instance/repository_dcx.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc/v20180410" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/DCX", NewDcxTcInstanceRepository) 17 | } 18 | 19 | type DcxTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *DcxTcInstanceRepository) GetInstanceKey() string { 26 | return "directConnectConnId" 27 | } 28 | 29 | func (repo *DcxTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeDirectConnectTunnelsRequest() 31 | req.DirectConnectTunnelIds = []*string{&id} 32 | resp, err := repo.client.DescribeDirectConnectTunnels(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.DirectConnectTunnelSet) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.DirectConnectTunnelSet[0] 40 | instance, err = NewDcxTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *DcxTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *DcxTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeDirectConnectTunnelsRequest() 53 | var offset int64 = 0 54 | var limit int64 = 100 55 | var total int64 = -1 56 | req.Offset = &offset 57 | req.Limit = &limit 58 | 59 | getMoreInstances: 60 | resp, err := repo.client.DescribeDirectConnectTunnels(req) 61 | if err != nil { 62 | return 63 | } 64 | if total == -1 { 65 | total = *resp.Response.TotalCount 66 | } 67 | for _, meta := range resp.Response.DirectConnectTunnelSet { 68 | ins, e := NewDcxTcInstance(*meta.DirectConnectTunnelId, meta) 69 | if e != nil { 70 | level.Error(repo.logger).Log("msg", "Create dcx instance fail", "id", *meta.DirectConnectId) 71 | continue 72 | } 73 | instances = append(instances, ins) 74 | } 75 | offset += limit 76 | if offset < total { 77 | req.Offset = &offset 78 | goto getMoreInstances 79 | } 80 | 81 | return 82 | } 83 | 84 | func NewDcxTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewDcClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &DcxTcInstanceRepository{ 90 | credential: cred, 91 | client: cli, 92 | logger: logger, 93 | } 94 | return 95 | } 96 | -------------------------------------------------------------------------------- /pkg/instance/repository_eip.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/LB", NewEIPTcInstanceRepository) 17 | } 18 | 19 | type EIPTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *EIPTcInstanceRepository) GetInstanceKey() string { 26 | return "eip" 27 | } 28 | 29 | func (repo *EIPTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeAddressesRequest() 31 | req.AddressIds = []*string{&id} 32 | resp, err := repo.client.DescribeAddresses(req) 33 | if err != nil { 34 | return 35 | } 36 | 37 | var meta *sdk.Address 38 | if len(resp.Response.AddressSet) == 0 { 39 | reqV6 := sdk.NewDescribeIp6AddressesRequest() 40 | reqV6.Ip6AddressIds = []*string{&id} 41 | respV6, err := repo.client.DescribeIp6Addresses(reqV6) 42 | if err != nil { 43 | return nil, err 44 | } 45 | if len(respV6.Response.AddressSet) == 1 { 46 | meta = respV6.Response.AddressSet[0] 47 | } else { 48 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 49 | } 50 | } else if len(resp.Response.AddressSet) == 1 { 51 | meta = resp.Response.AddressSet[0] 52 | } else { 53 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 54 | } 55 | instance, err = NewEIPTcInstance(*meta.AddressIp, meta) 56 | if err != nil { 57 | return 58 | } 59 | return 60 | } 61 | 62 | func (repo *EIPTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 63 | return 64 | } 65 | 66 | func (repo *EIPTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 67 | req := sdk.NewDescribeAddressesRequest() 68 | var offset int64 = 0 69 | var limit int64 = 100 70 | var total int64 = -1 71 | 72 | req.Offset = &offset 73 | req.Limit = &limit 74 | 75 | getMoreInstances: 76 | resp, err := repo.client.DescribeAddresses(req) 77 | if err != nil { 78 | return 79 | } 80 | if total == -1 { 81 | total = *resp.Response.TotalCount 82 | } 83 | for _, meta := range resp.Response.AddressSet { 84 | ins, e := NewEIPTcInstance(*meta.AddressIp, meta) 85 | if e != nil { 86 | level.Error(repo.logger).Log("msg", "Create cdb instance fail", "id", *meta.InstanceId) 87 | continue 88 | } 89 | instances = append(instances, ins) 90 | } 91 | offset += limit 92 | if offset < total { 93 | req.Offset = &offset 94 | goto getMoreInstances 95 | } 96 | 97 | reqV6 := sdk.NewDescribeIp6AddressesRequest() 98 | offset, limit, total = 0, 100, -1 99 | reqV6.Offset = &offset 100 | reqV6.Limit = &limit 101 | getMoreV6Instances: 102 | respV6, err := repo.client.DescribeIp6Addresses(reqV6) 103 | if err != nil { 104 | return 105 | } 106 | if total == -1 { 107 | total = *respV6.Response.TotalCount 108 | } 109 | for _, meta := range respV6.Response.AddressSet { 110 | ins, e := NewEIPTcInstance(*meta.AddressIp, meta) 111 | if e != nil { 112 | level.Error(repo.logger).Log("msg", "Create cdb instance fail", "id", *meta.InstanceId) 113 | continue 114 | } 115 | instances = append(instances, ins) 116 | } 117 | offset += limit 118 | if offset < total { 119 | req.Offset = &offset 120 | goto getMoreV6Instances 121 | } 122 | 123 | return 124 | } 125 | 126 | func NewEIPTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 127 | cli, err := client.NewVpvClient(cred, c) 128 | if err != nil { 129 | return 130 | } 131 | repo = &EIPTcInstanceRepository{ 132 | credential: cred, 133 | client: cli, 134 | logger: logger, 135 | } 136 | return 137 | } 138 | -------------------------------------------------------------------------------- /pkg/instance/repository_es.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es/v20180416" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/CES", NewESTcInstanceRepository) 17 | } 18 | 19 | type ESTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *ESTcInstanceRepository) GetInstanceKey() string { 26 | return "InstanceId" 27 | } 28 | 29 | func (repo *ESTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeInstancesRequest() 31 | req.InstanceIds = []*string{&id} 32 | resp, err := repo.client.DescribeInstances(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.InstanceList) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.InstanceList[0] 40 | instance, err = NewESTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *ESTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *ESTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeInstancesRequest() 53 | var offset uint64 = 0 54 | var limit uint64 = 100 55 | var total uint64 = 0 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeInstances(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == 0 { 66 | total = *resp.Response.TotalCount 67 | } 68 | for _, meta := range resp.Response.InstanceList { 69 | ins, e := NewESTcInstance(*meta.InstanceId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create es instance fail", "id", *meta.InstanceId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < total { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | return 82 | } 83 | 84 | func NewESTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewESClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &ESTcInstanceRepository{ 90 | credential: cred, 91 | client: cli, 92 | logger: logger, 93 | } 94 | return 95 | } 96 | -------------------------------------------------------------------------------- /pkg/instance/repository_kafka.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/CKAFKA", NewKafkaTcInstanceRepository) 17 | } 18 | 19 | type KafkaTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *KafkaTcInstanceRepository) GetInstanceKey() string { 26 | return "InstanceId" 27 | } 28 | 29 | func (repo *KafkaTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeInstancesRequest() 31 | req.InstanceId = &id 32 | resp, err := repo.client.DescribeInstances(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.Result.InstanceList) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.Result.InstanceList[0] 40 | instance, err = NewKafkaTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *KafkaTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *KafkaTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeInstancesRequest() 53 | var offset int64 = 0 54 | var limit int64 = 100 55 | var total int64 = 0 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeInstances(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == 0 { 66 | total = *resp.Response.Result.TotalCount 67 | } 68 | for _, meta := range resp.Response.Result.InstanceList { 69 | ins, e := NewKafkaTcInstance(*meta.InstanceId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create kafka instance fail", "id", *meta.InstanceId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < total { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | return 82 | } 83 | 84 | func NewKafkaTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewKafkaClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &KafkaTcInstanceRepository{ 90 | credential: cred, 91 | client: cli, 92 | logger: logger, 93 | } 94 | return 95 | } 96 | -------------------------------------------------------------------------------- /pkg/instance/repository_lighthouse.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/LIGHTHOUSE", NewLighthouseTcInstanceRepository) 17 | } 18 | 19 | type LighthouseTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *LighthouseTcInstanceRepository) GetInstanceKey() string { 26 | return "InstanceId" 27 | } 28 | 29 | func (repo *LighthouseTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeInstancesRequest() 31 | req.InstanceIds = []*string{&id} 32 | resp, err := repo.client.DescribeInstances(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.InstanceSet) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.InstanceSet[0] 40 | instance, err = NewLighthouseTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *LighthouseTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *LighthouseTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeInstancesRequest() 53 | var offset int64 = 0 54 | var limit int64 = 100 55 | var total int64 = -1 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeInstances(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == -1 { 66 | total = *resp.Response.TotalCount 67 | } 68 | for _, meta := range resp.Response.InstanceSet { 69 | ins, e := NewLighthouseTcInstance(*meta.InstanceId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create lighthouse instance fail", "id", *meta.InstanceId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < total { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | return 82 | } 83 | 84 | func NewLighthouseTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewLighthouseClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &LighthouseTcInstanceRepository{ 90 | credential: cred, 91 | client: cli, 92 | logger: logger, 93 | } 94 | return 95 | } 96 | -------------------------------------------------------------------------------- /pkg/instance/repository_mariadb.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb/v20170312" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/MARIADB", NewMariaDBTcInstanceRepository) 17 | } 18 | 19 | type MariaDBTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *MariaDBTcInstanceRepository) GetInstanceKey() string { 26 | return "InstanceId" 27 | } 28 | 29 | func (repo *MariaDBTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeDBInstancesRequest() 31 | req.InstanceIds = []*string{&id} 32 | resp, err := repo.client.DescribeDBInstances(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.Instances) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.Instances[0] 40 | instance, err = NewMariaDBTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *MariaDBTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *MariaDBTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeDBInstancesRequest() 53 | var offset int64 = 0 54 | var limit int64 = 100 55 | var total int64 = 0 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeDBInstances(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == 0 { 66 | total = int64(*resp.Response.TotalCount) 67 | } 68 | for _, meta := range resp.Response.Instances { 69 | ins, e := NewMariaDBTcInstance(*meta.InstanceId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create mariadb instance fail", "id", *meta.InstanceId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < total { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | return 82 | } 83 | 84 | func NewMariaDBTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewMariaDBClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &MariaDBTcInstanceRepository{ 90 | credential: cred, 91 | client: cli, 92 | logger: logger, 93 | } 94 | return 95 | } 96 | -------------------------------------------------------------------------------- /pkg/instance/repository_memcached.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/memcached/v20190318" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/MEMCACHED", NewMemcachedTcInstanceRepository) 17 | } 18 | 19 | type MemcachedTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *MemcachedTcInstanceRepository) GetInstanceKey() string { 26 | return "InstanceId" 27 | } 28 | 29 | func (repo *MemcachedTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeInstancesRequest() 31 | req.InstanceIds = []*string{&id} 32 | resp, err := repo.client.DescribeInstances(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.InstanceList) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.InstanceList[0] 40 | instance, err = NewMemcachedTcInstance(fmt.Sprintf("%d", *meta.CmemId), meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *MemcachedTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *MemcachedTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeInstancesRequest() 53 | var offset uint64 = 0 54 | var limit uint64 = 100 55 | var total uint64 = 0 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeInstances(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == 0 { 66 | total = uint64(*resp.Response.TotalNum) 67 | } 68 | for _, meta := range resp.Response.InstanceList { 69 | ins, e := NewMemcachedTcInstance(fmt.Sprintf("%d", *meta.CmemId), meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create memcached instance fail", "id", *meta.InstanceId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < total { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | return 82 | } 83 | 84 | func NewMemcachedTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewMemcacheClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &MemcachedTcInstanceRepository{ 90 | credential: cred, 91 | client: cli, 92 | logger: logger, 93 | } 94 | return 95 | } 96 | -------------------------------------------------------------------------------- /pkg/instance/repository_mongo.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | 7 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 8 | 9 | "github.com/go-kit/log" 10 | "github.com/go-kit/log/level" 11 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 13 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 14 | ) 15 | 16 | func init() { 17 | registerRepository("QCE/CMONGO", NewMongoTcInstanceRepository) 18 | } 19 | 20 | type MongoTcInstanceRepository struct { 21 | credential common.CredentialIface 22 | client *sdk.Client 23 | logger log.Logger 24 | } 25 | 26 | func (repo *MongoTcInstanceRepository) GetInstanceKey() string { 27 | return "target" 28 | } 29 | 30 | func (repo *MongoTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 31 | req := sdk.NewDescribeDBInstancesRequest() 32 | req.InstanceIds = []*string{&id} 33 | resp, err := repo.client.DescribeDBInstances(req) 34 | if err != nil { 35 | return 36 | } 37 | if len(resp.Response.InstanceDetails) != 1 { 38 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 39 | } 40 | meta := resp.Response.InstanceDetails[0] 41 | instance, err = NewMongoTcInstance(id, meta) 42 | if err != nil { 43 | return 44 | } 45 | return 46 | } 47 | 48 | func (repo *MongoTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 49 | return 50 | } 51 | 52 | func (repo *MongoTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 53 | req := sdk.NewDescribeDBInstancesRequest() 54 | var offset uint64 = 0 55 | var limit uint64 = 100 56 | var total int64 = -1 57 | 58 | req.Offset = &offset 59 | req.Limit = &limit 60 | 61 | if v, ok := filters["ProjectId"]; ok { 62 | tv, e := strconv.ParseInt(v, 10, 64) 63 | utv := uint64(tv) 64 | if e == nil { 65 | req.ProjectIds = []*uint64{&utv} 66 | } 67 | } 68 | if v, ok := filters["InstanceId"]; ok { 69 | req.InstanceIds = []*string{&v} 70 | } 71 | 72 | getMoreInstances: 73 | resp, err := repo.client.DescribeDBInstances(req) 74 | if err != nil { 75 | return 76 | } 77 | if total == -1 { 78 | total = int64(*resp.Response.TotalCount) 79 | } 80 | for _, meta := range resp.Response.InstanceDetails { 81 | ins, e := NewMongoTcInstance(*meta.InstanceId, meta) 82 | if e != nil { 83 | level.Error(repo.logger).Log("msg", "Create mongo instance fail", "id", *meta.InstanceId) 84 | continue 85 | } 86 | instances = append(instances, ins) 87 | } 88 | offset += limit 89 | if offset < uint64(total) { 90 | req.Offset = &offset 91 | goto getMoreInstances 92 | } 93 | 94 | return 95 | } 96 | 97 | func NewMongoTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 98 | cli, err := client.NewMongodbClient(cred, c) 99 | if err != nil { 100 | return 101 | } 102 | repo = &MongoTcInstanceRepository{ 103 | credential: cred, 104 | client: cli, 105 | logger: logger, 106 | } 107 | return 108 | } 109 | -------------------------------------------------------------------------------- /pkg/instance/repository_nat.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/NAT_GATEWAY", NewNatTcInstanceRepository) 17 | } 18 | 19 | type NatTcInstanceRepository struct { 20 | credential common.CredentialIface 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *NatTcInstanceRepository) GetInstanceKey() string { 26 | return "instanceid" 27 | } 28 | 29 | func (repo *NatTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeNatGatewaysRequest() 31 | req.NatGatewayIds = []*string{&id} 32 | resp, err := repo.client.DescribeNatGateways(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.NatGatewaySet) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.NatGatewaySet[0] 40 | instance, err = NewNatTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *NatTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *NatTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeNatGatewaysRequest() 53 | var offset uint64 = 0 54 | var limit uint64 = 100 55 | var total int64 = -1 56 | req.Offset = &offset 57 | req.Limit = &limit 58 | 59 | getMoreInstances: 60 | resp, err := repo.client.DescribeNatGateways(req) 61 | if err != nil { 62 | return 63 | } 64 | if total == -1 { 65 | total = int64(*resp.Response.TotalCount) 66 | } 67 | for _, meta := range resp.Response.NatGatewaySet { 68 | ins, e := NewNatTcInstance(*meta.NatGatewayId, meta) 69 | if e != nil { 70 | level.Error(repo.logger).Log("msg", "Create redis instance fail", "id", *meta.NatGatewayId) 71 | continue 72 | } 73 | instances = append(instances, ins) 74 | } 75 | offset += limit 76 | if offset < uint64(total) { 77 | req.Offset = &offset 78 | goto getMoreInstances 79 | } 80 | return 81 | } 82 | 83 | func NewNatTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 84 | cli, err := client.NewVpvClient(cred, c) 85 | if err != nil { 86 | return 87 | } 88 | repo = &NatTcInstanceRepository{ 89 | credential: cred, 90 | client: cli, 91 | logger: logger, 92 | } 93 | return 94 | } 95 | -------------------------------------------------------------------------------- /pkg/instance/repository_pg.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | var idKey = "db-instance-id" 16 | 17 | func init() { 18 | registerRepository("QCE/POSTGRES", NewPGTcInstanceRepository) 19 | } 20 | 21 | type PGTcInstanceRepository struct { 22 | credential common.CredentialIface 23 | client *sdk.Client 24 | logger log.Logger 25 | } 26 | 27 | func (repo *PGTcInstanceRepository) GetInstanceKey() string { 28 | return "DBInstanceId" 29 | } 30 | 31 | func (repo *PGTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 32 | req := sdk.NewDescribeDBInstancesRequest() 33 | req.Filters = []*sdk.Filter{{ 34 | Name: &idKey, 35 | Values: []*string{&id}, 36 | }} 37 | resp, err := repo.client.DescribeDBInstances(req) 38 | if err != nil { 39 | return 40 | } 41 | if len(resp.Response.DBInstanceSet) != 1 { 42 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 43 | } 44 | meta := resp.Response.DBInstanceSet[0] 45 | instance, err = NewPGTcInstance(id, meta) 46 | if err != nil { 47 | return 48 | } 49 | return 50 | } 51 | 52 | func (repo *PGTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 53 | return 54 | } 55 | 56 | func (repo *PGTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 57 | req := sdk.NewDescribeDBInstancesRequest() 58 | var offset uint64 = 0 59 | var limit uint64 = 100 60 | var total uint64 = 0 61 | 62 | req.Offset = &offset 63 | req.Limit = &limit 64 | 65 | getMoreInstances: 66 | resp, err := repo.client.DescribeDBInstances(req) 67 | if err != nil { 68 | return 69 | } 70 | if total == 0 { 71 | total = *resp.Response.TotalCount 72 | } 73 | for _, meta := range resp.Response.DBInstanceSet { 74 | ins, e := NewPGTcInstance(*meta.DBInstanceId, meta) 75 | if e != nil { 76 | level.Error(repo.logger).Log("msg", "Create pg instance fail", "id", *meta.DBInstanceId) 77 | continue 78 | } 79 | instances = append(instances, ins) 80 | } 81 | offset += limit 82 | if offset < total { 83 | req.Offset = &offset 84 | goto getMoreInstances 85 | } 86 | return 87 | } 88 | 89 | func NewPGTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 90 | cli, err := client.NewPGClient(cred, c) 91 | if err != nil { 92 | return 93 | } 94 | repo = &PGTcInstanceRepository{ 95 | credential: cred, 96 | client: cli, 97 | logger: logger, 98 | } 99 | return 100 | } 101 | -------------------------------------------------------------------------------- /pkg/instance/repository_redis.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | mycommon "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" 11 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 13 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 14 | ) 15 | 16 | //go:generate mockgen -source=./repository_redis.go -destination=./repository_redis_mock.go -package=instance 17 | 18 | func init() { 19 | registerRepository("QCE/REDIS", NewRedisTcInstanceRepository) 20 | registerRepository("QCE/REDIS_MEM", NewRedisTcInstanceRepository) 21 | } 22 | 23 | type RedisTcInstanceRepository struct { 24 | credential mycommon.CredentialIface 25 | client *sdk.Client 26 | logger log.Logger 27 | } 28 | 29 | func NewRedisTcInstanceRepository(cred mycommon.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 30 | cli, err := client.NewRedisClient(cred, c) 31 | if err != nil { 32 | return 33 | } 34 | repo = &RedisTcInstanceRepository{ 35 | credential: cred, 36 | client: cli, 37 | logger: logger, 38 | } 39 | return 40 | } 41 | 42 | func (repo *RedisTcInstanceRepository) GetInstanceKey() string { 43 | return "instanceid" 44 | } 45 | 46 | func (repo *RedisTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 47 | req := sdk.NewDescribeInstancesRequest() 48 | req.InstanceId = &id 49 | resp, err := repo.client.DescribeInstances(req) 50 | if err != nil { 51 | return 52 | } 53 | if len(resp.Response.InstanceSet) != 1 { 54 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 55 | } 56 | meta := resp.Response.InstanceSet[0] 57 | instance, err = NewRedisTcInstance(id, meta) 58 | if err != nil { 59 | return 60 | } 61 | return 62 | } 63 | 64 | func (repo *RedisTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 65 | return 66 | } 67 | 68 | func (repo *RedisTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 69 | req := sdk.NewDescribeInstancesRequest() 70 | var offset uint64 = 0 71 | var limit uint64 = 100 72 | var total int64 = -1 73 | req.Offset = &offset 74 | req.Limit = &limit 75 | req.Status = []*int64{common.Int64Ptr(2)} 76 | 77 | getMoreInstances: 78 | resp, err := repo.client.DescribeInstances(req) 79 | if err != nil { 80 | return 81 | } 82 | if total == -1 { 83 | total = *resp.Response.TotalCount 84 | } 85 | for _, meta := range resp.Response.InstanceSet { 86 | ins, e := NewRedisTcInstance(*meta.InstanceId, meta) 87 | if e != nil { 88 | level.Error(repo.logger).Log("msg", "Create redis instance fail", "id", *meta.InstanceId) 89 | continue 90 | } 91 | instances = append(instances, ins) 92 | } 93 | offset += limit 94 | if offset < uint64(total) { 95 | req.Offset = &offset 96 | goto getMoreInstances 97 | } 98 | 99 | return 100 | } 101 | 102 | type RedisTcInstanceNodeRepository interface { 103 | GetNodeInfo(instanceId string) (*sdk.DescribeInstanceNodeInfoResponse, error) 104 | } 105 | 106 | type RedisTcInstanceNodeRepositoryImpl struct { 107 | credential mycommon.CredentialIface 108 | client *sdk.Client 109 | logger log.Logger 110 | } 111 | 112 | func (repo *RedisTcInstanceNodeRepositoryImpl) GetNodeInfo(instanceId string) (*sdk.DescribeInstanceNodeInfoResponse, error) { 113 | req := sdk.NewDescribeInstanceNodeInfoRequest() 114 | req.InstanceId = common.StringPtr(instanceId) 115 | return repo.client.DescribeInstanceNodeInfo(req) 116 | } 117 | 118 | func NewRedisTcInstanceNodeRepository(cred mycommon.CredentialIface, c *config.TencentConfig, logger log.Logger) (RedisTcInstanceNodeRepository, error) { 119 | cli, err := client.NewRedisClient(cred, c) 120 | if err != nil { 121 | return nil, err 122 | } 123 | repo := &RedisTcInstanceNodeRepositoryImpl{ 124 | credential: cred, 125 | client: cli, 126 | logger: logger, 127 | } 128 | return repo, nil 129 | } 130 | -------------------------------------------------------------------------------- /pkg/instance/repository_sqlserver.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328" 11 | 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 13 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 14 | ) 15 | 16 | func init() { 17 | registerRepository("QCE/SQLSERVER", NewSqlServerTcInstanceRepository) 18 | } 19 | 20 | type SqlServerTcInstanceRepository struct { 21 | credential common.CredentialIface 22 | client *sdk.Client 23 | logger log.Logger 24 | } 25 | 26 | func (repo *SqlServerTcInstanceRepository) GetInstanceKey() string { 27 | return "InstanceId" 28 | } 29 | 30 | func (repo *SqlServerTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 31 | req := sdk.NewDescribeDBInstancesRequest() 32 | req.InstanceIdSet = []*string{&id} 33 | resp, err := repo.client.DescribeDBInstances(req) 34 | if err != nil { 35 | return 36 | } 37 | if len(resp.Response.DBInstances) != 1 { 38 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 39 | } 40 | meta := resp.Response.DBInstances[0] 41 | instance, err = NewSqlServerTcInstance(id, meta) 42 | if err != nil { 43 | return 44 | } 45 | return 46 | } 47 | 48 | func (repo *SqlServerTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 49 | return 50 | } 51 | 52 | func (repo *SqlServerTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 53 | req := sdk.NewDescribeDBInstancesRequest() 54 | var offset int64 = 0 55 | var limit int64 = 100 56 | var total int64 = -1 57 | 58 | req.Offset = &offset 59 | req.Limit = &limit 60 | 61 | getMoreInstances: 62 | resp, err := repo.client.DescribeDBInstances(req) 63 | if err != nil { 64 | return 65 | } 66 | if total == -1 { 67 | total = *resp.Response.TotalCount 68 | } 69 | for _, meta := range resp.Response.DBInstances { 70 | ins, e := NewSqlServerTcInstance(*meta.InstanceId, meta) 71 | if e != nil { 72 | level.Error(repo.logger).Log("msg", "Create cdb instance fail", "id", *meta.InstanceId) 73 | continue 74 | } 75 | instances = append(instances, ins) 76 | } 77 | offset += limit 78 | if offset < total { 79 | req.Offset = &offset 80 | goto getMoreInstances 81 | } 82 | 83 | return 84 | } 85 | 86 | func NewSqlServerTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 87 | cli, err := client.NewSqlServerClient(cred, c) 88 | if err != nil { 89 | return 90 | } 91 | repo = &SqlServerTcInstanceRepository{ 92 | credential: cred, 93 | client: cli, 94 | logger: logger, 95 | } 96 | return 97 | } 98 | -------------------------------------------------------------------------------- /pkg/instance/repository_vbc.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/go-kit/log/level" 8 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 9 | 10 | tccommon "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 13 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 14 | ) 15 | 16 | func init() { 17 | registerRepository("QCE/VBC", NewVbcTcInstanceRepository) 18 | } 19 | 20 | type VbcTcInstanceRepository struct { 21 | client *sdk.Client 22 | logger log.Logger 23 | } 24 | 25 | func (repo *VbcTcInstanceRepository) GetInstanceKey() string { 26 | return "InstanceId" 27 | } 28 | 29 | func (repo *VbcTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 30 | req := sdk.NewDescribeCcnsRequest() 31 | req.CcnIds = []*string{&id} 32 | resp, err := repo.client.DescribeCcns(req) 33 | if err != nil { 34 | return 35 | } 36 | if len(resp.Response.CcnSet) != 1 { 37 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 38 | } 39 | meta := resp.Response.CcnSet[0] 40 | instance, err = NewVbcTcInstance(id, meta) 41 | if err != nil { 42 | return 43 | } 44 | return 45 | } 46 | 47 | func (repo *VbcTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 48 | return 49 | } 50 | 51 | func (repo *VbcTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 52 | req := sdk.NewDescribeCcnsRequest() 53 | var offset uint64 = 0 54 | var limit uint64 = 100 55 | var total int64 = -1 56 | 57 | req.Offset = &offset 58 | req.Limit = &limit 59 | 60 | getMoreInstances: 61 | resp, err := repo.client.DescribeCcns(req) 62 | if err != nil { 63 | return 64 | } 65 | if total == -1 { 66 | total = int64(*resp.Response.TotalCount) 67 | } 68 | for _, meta := range resp.Response.CcnSet { 69 | ins, e := NewVbcTcInstance(*meta.CcnId, meta) 70 | if e != nil { 71 | level.Error(repo.logger).Log("msg", "Create Vbc instance fail", "id", *meta.CcnId) 72 | continue 73 | } 74 | instances = append(instances, ins) 75 | } 76 | offset += limit 77 | if offset < uint64(total) { 78 | req.Offset = &offset 79 | goto getMoreInstances 80 | } 81 | 82 | return 83 | } 84 | 85 | func NewVbcTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 86 | cli, err := client.NewVpvClient(cred, c) 87 | if err != nil { 88 | return 89 | } 90 | repo = &VbcTcInstanceRepository{ 91 | client: cli, 92 | logger: logger, 93 | } 94 | return 95 | } 96 | 97 | // VbcDRegion 98 | type VbcTcInstanceDRegionRepository interface { 99 | GetVbcDRegionInfo(instanceId string) (*sdk.DescribeCcnRegionBandwidthLimitsResponse, error) 100 | } 101 | 102 | type VbcTcInstanceDRegionRepositoryImpl struct { 103 | client *sdk.Client 104 | logger log.Logger 105 | } 106 | 107 | func (repo *VbcTcInstanceDRegionRepositoryImpl) GetVbcDRegionInfo(instanceId string) (*sdk.DescribeCcnRegionBandwidthLimitsResponse, error) { 108 | req := sdk.NewDescribeCcnRegionBandwidthLimitsRequest() 109 | req.CcnId = tccommon.StringPtr(instanceId) 110 | return repo.client.DescribeCcnRegionBandwidthLimits(req) 111 | } 112 | func NewVbcTcInstanceDRegionRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (VbcTcInstanceDRegionRepository, error) { 113 | cli, err := client.NewVpvClient(cred, c) 114 | if err != nil { 115 | return nil, err 116 | } 117 | repo := &VbcTcInstanceDRegionRepositoryImpl{ 118 | client: cli, 119 | logger: logger, 120 | } 121 | return repo, nil 122 | } 123 | -------------------------------------------------------------------------------- /pkg/instance/repository_vpngw.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/go-kit/log/level" 8 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 9 | 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/VPNGW", NewVpngwTcInstanceRepository) 17 | } 18 | 19 | type VpngwTcInstanceRepository struct { 20 | client *sdk.Client 21 | logger log.Logger 22 | } 23 | 24 | func (repo *VpngwTcInstanceRepository) GetInstanceKey() string { 25 | return "InstanceId" 26 | } 27 | 28 | func (repo *VpngwTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 29 | req := sdk.NewDescribeVpnGatewaysRequest() 30 | // req.Filters.Name = []*string{&id} 31 | resp, err := repo.client.DescribeVpnGateways(req) 32 | if err != nil { 33 | return 34 | } 35 | if len(resp.Response.VpnGatewaySet) != 1 { 36 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 37 | } 38 | meta := resp.Response.VpnGatewaySet[0] 39 | instance, err = NewVpngwTcInstance(id, meta) 40 | if err != nil { 41 | return 42 | } 43 | return 44 | } 45 | 46 | func (repo *VpngwTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 47 | return 48 | } 49 | 50 | func (repo *VpngwTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 51 | req := sdk.NewDescribeVpnGatewaysRequest() 52 | var offset uint64 = 0 53 | var limit uint64 = 100 54 | var total int64 = -1 55 | 56 | req.Offset = &offset 57 | req.Limit = &limit 58 | 59 | getMoreInstances: 60 | resp, err := repo.client.DescribeVpnGateways(req) 61 | if err != nil { 62 | return 63 | } 64 | if total == -1 { 65 | total = int64(*resp.Response.TotalCount) 66 | } 67 | for _, meta := range resp.Response.VpnGatewaySet { 68 | ins, e := NewVpngwTcInstance(*meta.VpnGatewayId, meta) 69 | if e != nil { 70 | level.Error(repo.logger).Log("msg", "Create vpngw instance fail", "id", *meta.VpnGatewayId) 71 | continue 72 | } 73 | instances = append(instances, ins) 74 | } 75 | offset += limit 76 | if offset < uint64(total) { 77 | req.Offset = &offset 78 | goto getMoreInstances 79 | } 80 | 81 | return 82 | } 83 | 84 | func NewVpngwTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewVpvClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &VpngwTcInstanceRepository{ 90 | client: cli, 91 | logger: logger, 92 | } 93 | return 94 | } 95 | -------------------------------------------------------------------------------- /pkg/instance/repository_vpnx.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/go-kit/log/level" 8 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" 9 | 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/VPNX", NewVpnxTcInstanceRepository) 17 | } 18 | 19 | type VpnxTcInstanceRepository struct { 20 | client *sdk.Client 21 | logger log.Logger 22 | } 23 | 24 | func (repo *VpnxTcInstanceRepository) GetInstanceKey() string { 25 | return "InstanceId" 26 | } 27 | 28 | func (repo *VpnxTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 29 | req := sdk.NewDescribeVpnConnectionsRequest() 30 | // req.Filters.Name = []*string{&id} 31 | resp, err := repo.client.DescribeVpnConnections(req) 32 | if err != nil { 33 | return 34 | } 35 | if len(resp.Response.VpnConnectionSet) != 1 { 36 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 37 | } 38 | meta := resp.Response.VpnConnectionSet[0] 39 | instance, err = NewVpnxTcInstance(id, meta) 40 | if err != nil { 41 | return 42 | } 43 | return 44 | } 45 | 46 | func (repo *VpnxTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 47 | return 48 | } 49 | 50 | func (repo *VpnxTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 51 | req := sdk.NewDescribeVpnConnectionsRequest() 52 | var offset uint64 = 0 53 | var limit uint64 = 100 54 | var total int64 = -1 55 | 56 | req.Offset = &offset 57 | req.Limit = &limit 58 | 59 | getMoreInstances: 60 | resp, err := repo.client.DescribeVpnConnections(req) 61 | if err != nil { 62 | return 63 | } 64 | if total == -1 { 65 | total = int64(*resp.Response.TotalCount) 66 | } 67 | for _, meta := range resp.Response.VpnConnectionSet { 68 | ins, e := NewVpnxTcInstance(*meta.VpnConnectionId, meta) 69 | if e != nil { 70 | level.Error(repo.logger).Log("msg", "Create vpnx instance fail", "id", *meta.VpnConnectionId) 71 | continue 72 | } 73 | instances = append(instances, ins) 74 | } 75 | offset += limit 76 | if offset < uint64(total) { 77 | req.Offset = &offset 78 | goto getMoreInstances 79 | } 80 | 81 | return 82 | } 83 | 84 | func NewVpnxTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 85 | cli, err := client.NewVpvClient(cred, c) 86 | if err != nil { 87 | return 88 | } 89 | repo = &VpnxTcInstanceRepository{ 90 | client: cli, 91 | logger: logger, 92 | } 93 | return 94 | } 95 | -------------------------------------------------------------------------------- /pkg/instance/repository_waf.go: -------------------------------------------------------------------------------- 1 | package instance 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/go-kit/log" 7 | "github.com/go-kit/log/level" 8 | sdk "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125" 9 | 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/client" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/common" 12 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 13 | ) 14 | 15 | func init() { 16 | registerRepository("QCE/WAF", NewWafTcInstanceRepository) 17 | } 18 | 19 | type WafTcInstanceRepository struct { 20 | client *sdk.Client 21 | logger log.Logger 22 | } 23 | 24 | func (repo *WafTcInstanceRepository) GetInstanceKey() string { 25 | return "InstanceId" 26 | } 27 | 28 | var domain = "Domain" 29 | var exactMatchTrue = true 30 | 31 | func (repo *WafTcInstanceRepository) Get(id string) (instance TcInstance, err error) { 32 | req := sdk.NewDescribeDomainsRequest() 33 | var offset uint64 = 1 34 | var limit uint64 = 100 35 | 36 | req.Offset = &offset 37 | req.Limit = &limit 38 | req.Filters = []*sdk.FiltersItemNew{{ 39 | Name: &domain, 40 | Values: []*string{&id}, 41 | ExactMatch: &exactMatchTrue, 42 | }} 43 | resp, err := repo.client.DescribeDomains(req) 44 | if err != nil { 45 | return 46 | } 47 | if len(resp.Response.Domains) != 1 { 48 | return nil, fmt.Errorf("Response instanceDetails size != 1, id=%s ", id) 49 | } 50 | meta := resp.Response.Domains[0] 51 | instance, err = NewWafTcInstance(id, meta) 52 | if err != nil { 53 | return 54 | } 55 | return 56 | } 57 | 58 | func (repo *WafTcInstanceRepository) ListByIds(id []string) (instances []TcInstance, err error) { 59 | return 60 | } 61 | 62 | func (repo *WafTcInstanceRepository) ListByFilters(filters map[string]string) (instances []TcInstance, err error) { 63 | req := sdk.NewDescribeDomainsRequest() 64 | var offset uint64 = 1 65 | var limit uint64 = 100 66 | var total int64 = -1 67 | 68 | req.Offset = &offset 69 | req.Limit = &limit 70 | 71 | getMoreInstances: 72 | resp, err := repo.client.DescribeDomains(req) 73 | if err != nil { 74 | return 75 | } 76 | if total == -1 { 77 | total = int64(*resp.Response.Total) 78 | } 79 | for _, meta := range resp.Response.Domains { 80 | ins, e := NewWafTcInstance(*meta.Domain, meta) 81 | if e != nil { 82 | level.Error(repo.logger).Log("msg", "Create Waf instance fail", "id", *meta.InstanceId) 83 | continue 84 | } 85 | instances = append(instances, ins) 86 | } 87 | offset++ 88 | if (offset-1)*limit < uint64(total) { 89 | req.Offset = &offset 90 | goto getMoreInstances 91 | } 92 | 93 | return 94 | } 95 | 96 | func NewWafTcInstanceRepository(cred common.CredentialIface, c *config.TencentConfig, logger log.Logger) (repo TcInstanceRepository, err error) { 97 | cli, err := client.NewWafClient(cred, c) 98 | if err != nil { 99 | return 100 | } 101 | repo = &WafTcInstanceRepository{ 102 | client: cli, 103 | logger: logger, 104 | } 105 | return 106 | } 107 | -------------------------------------------------------------------------------- /pkg/metric/cache.go: -------------------------------------------------------------------------------- 1 | package metric 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "time" 7 | 8 | "github.com/go-kit/log" 9 | "github.com/go-kit/log/level" 10 | ) 11 | 12 | // 腾讯云监控指标缓存, 在TcmMetricRepository封装一层, 指标元数据使用缓存, 转发获取数据点请求 13 | type TcmMetricCache struct { 14 | Raw TcmMetricRepository 15 | metaCache map[string]map[string]*TcmMeta //k1=namespace, k2=metricname(小写) 16 | metaLastReloadTime map[string]int64 17 | logger log.Logger 18 | } 19 | 20 | func (c *TcmMetricCache) GetMeta(namespace string, name string) (*TcmMeta, error) { 21 | err := c.checkMetaNeedreload(namespace) 22 | if err != nil { 23 | return nil, err 24 | } 25 | np, exists := c.metaCache[namespace] 26 | if !exists { 27 | return nil, fmt.Errorf("namespace cache not exists") 28 | } 29 | m, exists := np[strings.ToLower(name)] 30 | if !exists { 31 | return nil, fmt.Errorf("metric cache not exists") 32 | } 33 | return m, nil 34 | } 35 | 36 | func (c *TcmMetricCache) ListMetaByNamespace(namespace string) ([]*TcmMeta, error) { 37 | err := c.checkMetaNeedreload(namespace) 38 | if err != nil { 39 | return nil, err 40 | } 41 | var metas []*TcmMeta 42 | for _, meta := range c.metaCache[namespace] { 43 | metas = append(metas, meta) 44 | } 45 | return metas, nil 46 | } 47 | 48 | func (c *TcmMetricCache) GetSamples(series *TcmSeries, startTime int64, endTime int64) (samples *TcmSamples, err error) { 49 | return c.Raw.GetSamples(series, startTime, endTime) 50 | } 51 | 52 | func (c *TcmMetricCache) ListSamples(metric *TcmMetric, startTime int64, endTime int64) (samplesList []*TcmSamples, err error) { 53 | return c.Raw.ListSamples(metric, startTime, endTime) 54 | } 55 | 56 | // 检测是否需要reload缓存的数据 57 | func (c *TcmMetricCache) checkMetaNeedreload(namespace string) (err error) { 58 | v, ok := c.metaLastReloadTime[namespace] 59 | if ok && v != 0 { 60 | return nil 61 | } 62 | metas, err := c.Raw.ListMetaByNamespace(namespace) 63 | if err != nil { 64 | return err 65 | } 66 | np, ok := c.metaCache[namespace] 67 | if !ok { 68 | np = map[string]*TcmMeta{} 69 | c.metaCache[namespace] = np 70 | } 71 | for _, meta := range metas { 72 | np[strings.ToLower(meta.MetricName)] = meta 73 | } 74 | c.metaLastReloadTime[namespace] = time.Now().Unix() 75 | 76 | level.Info(c.logger).Log("msg", "Reload metric meta cache", "namespace", namespace, "num", len(np)) 77 | return 78 | } 79 | 80 | func NewTcmMetricCache(repo TcmMetricRepository, logger log.Logger) TcmMetricRepository { 81 | cache := &TcmMetricCache{ 82 | Raw: repo, 83 | metaCache: map[string]map[string]*TcmMeta{}, 84 | metaLastReloadTime: map[string]int64{}, 85 | logger: logger, 86 | } 87 | return cache 88 | 89 | } 90 | -------------------------------------------------------------------------------- /pkg/metric/conf.go: -------------------------------------------------------------------------------- 1 | package metric 2 | 3 | import ( 4 | "strings" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 7 | ) 8 | 9 | type TcmMetricConfig struct { 10 | CustomNamespacePrefix string 11 | CustomProductName string 12 | CustomMetricName string 13 | MetricNameType int32 14 | CustomQueryDimensions []map[string]string 15 | InstanceLabelNames []string 16 | ConstLabels map[string]string 17 | StatTypes []string 18 | StatPeriodSeconds int64 19 | StatNumSamples int64 20 | StatDelaySeconds int64 21 | AllInstances bool 22 | InstanceFilters map[string]string 23 | OnlyIncludeInstances []string 24 | ExcludeInstances []string 25 | } 26 | 27 | func (c *TcmMetricConfig) IsIncludeOnlyInstance() bool { 28 | return len(c.OnlyIncludeInstances) > 0 29 | } 30 | 31 | func (c *TcmMetricConfig) IsIncludeAllInstance() bool { 32 | if c.IsIncludeOnlyInstance() { 33 | return false 34 | } 35 | return c.AllInstances 36 | } 37 | 38 | func (c *TcmMetricConfig) IsCustomQueryDimensions() bool { 39 | if c.IsIncludeOnlyInstance() { 40 | return false 41 | } 42 | if c.IsIncludeAllInstance() { 43 | return false 44 | } 45 | return len(c.CustomQueryDimensions) > 0 46 | } 47 | 48 | func NewTcmMetricConfigWithMetricYaml(c config.TencentMetric, meta *TcmMeta) (*TcmMetricConfig, error) { 49 | conf := &TcmMetricConfig{} 50 | 51 | npitems := strings.Split(c.Namespace, "/") 52 | conf.CustomNamespacePrefix = npitems[0] 53 | 54 | conf.CustomProductName = npitems[1] 55 | conf.CustomMetricName = c.MetricReName 56 | if c.MetricNameType != 0 { 57 | conf.MetricNameType = c.MetricNameType 58 | } else { 59 | conf.MetricNameType = 2 60 | } 61 | 62 | conf.CustomQueryDimensions = []map[string]string{c.Dimensions} 63 | conf.InstanceLabelNames = c.Labels 64 | if len(c.Statistics) != 0 { 65 | for _, st := range c.Statistics { 66 | conf.StatTypes = append(conf.StatTypes, strings.ToLower(st)) 67 | } 68 | } else { 69 | conf.StatTypes = []string{"last"} 70 | } 71 | // 自动获取支持的统计周期 72 | period, err := meta.GetPeriod(c.PeriodSeconds) 73 | if err != nil { 74 | return nil, err 75 | } 76 | conf.StatPeriodSeconds = period 77 | conf.StatNumSamples = (c.RangeSeconds / period) + 1 78 | // 至少采集4个点的数据 79 | if conf.StatNumSamples < 4 { 80 | conf.StatNumSamples = 4 81 | } 82 | conf.StatDelaySeconds = c.DelaySeconds 83 | 84 | if len(c.Dimensions) == 0 { 85 | conf.AllInstances = true 86 | } 87 | 88 | conf.InstanceFilters = c.Filters 89 | return conf, nil 90 | 91 | } 92 | 93 | func NewTcmMetricConfigWithProductYaml(c config.TencentProduct, meta *TcmMeta) (*TcmMetricConfig, error) { 94 | conf := &TcmMetricConfig{} 95 | 96 | npitems := strings.Split(c.Namespace, "/") 97 | conf.CustomNamespacePrefix = npitems[0] 98 | 99 | conf.CustomProductName = npitems[1] 100 | conf.CustomMetricName = "" 101 | if c.MetricNameType != 0 { 102 | conf.MetricNameType = c.MetricNameType 103 | } else { 104 | conf.MetricNameType = 2 105 | } 106 | 107 | conf.CustomQueryDimensions = c.CustomQueryDimensions 108 | conf.InstanceLabelNames = c.ExtraLabels 109 | if len(c.Statistics) != 0 { 110 | for _, st := range c.Statistics { 111 | conf.StatTypes = append(conf.StatTypes, strings.ToLower(st)) 112 | } 113 | } else { 114 | conf.StatTypes = []string{"last"} 115 | } 116 | 117 | period, err := meta.GetPeriod(c.PeriodSeconds) 118 | if err != nil { 119 | return nil, err 120 | } 121 | conf.StatPeriodSeconds = period 122 | conf.StatNumSamples = (c.RangeSeconds / period) + 1 123 | if conf.StatNumSamples < 4 { 124 | conf.StatNumSamples = 4 125 | } 126 | conf.StatDelaySeconds = c.DelaySeconds 127 | conf.AllInstances = c.AllInstances 128 | conf.InstanceFilters = c.InstanceFilters 129 | conf.OnlyIncludeInstances = c.OnlyIncludeInstances 130 | conf.ExcludeInstances = c.ExcludeInstances 131 | 132 | return conf, nil 133 | 134 | } 135 | -------------------------------------------------------------------------------- /pkg/metric/label.go: -------------------------------------------------------------------------------- 1 | package metric 2 | 3 | import ( 4 | "crypto/md5" 5 | "encoding/json" 6 | "fmt" 7 | "sort" 8 | "strings" 9 | 10 | "github.com/tencentyun/tencentcloud-exporter/pkg/instance" 11 | ) 12 | 13 | type Labels map[string]string 14 | 15 | func (l *Labels) Md5() (string, error) { 16 | h := md5.New() 17 | jb, err := json.Marshal(l) 18 | if err != nil { 19 | return "", err 20 | } 21 | return fmt.Sprintf("%x", h.Sum(jb)), nil 22 | } 23 | 24 | // 代表一个指标的labels 25 | type TcmLabels struct { 26 | queryLableNames []string // 用于查询数据的条件标签 27 | instanceLabelNames []string // 从获取实例对象动态获取字段值的标签 28 | constLabels Labels // 用户自定义的常量标签 29 | Names []string // 所有标签名列表 30 | } 31 | 32 | // 根据标签名, 获取所有标签的值 33 | func (l *TcmLabels) GetValues(filters map[string]string, ins instance.TcInstance) map[string]string { 34 | lowerKeyFilters := map[string]string{} 35 | for k, v := range filters { 36 | lowerKeyFilters[strings.ToLower(k)] = v 37 | } 38 | 39 | nameValues := map[string]string{} 40 | for _, name := range l.queryLableNames { 41 | v, ok := lowerKeyFilters[strings.ToLower(name)] 42 | if ok { 43 | nameValues[name] = v 44 | } 45 | } 46 | for _, name := range l.instanceLabelNames { 47 | if ins == nil { 48 | continue 49 | } 50 | vMap, e := ins.GetFieldValuesByName(name) 51 | if e == nil && vMap != nil { 52 | for vName, values := range vMap { 53 | for _, value := range values { 54 | nameValues[vName] = value 55 | } 56 | } 57 | } 58 | } 59 | for name, value := range l.constLabels { 60 | nameValues[name] = value 61 | } 62 | return nameValues 63 | } 64 | 65 | func NewTcmLabels(qln []string, iln []string, cl Labels) (*TcmLabels, error) { 66 | var labelNames []string 67 | labelNames = append(labelNames, qln...) 68 | labelNames = append(labelNames, iln...) 69 | for lname := range cl { 70 | labelNames = append(labelNames, lname) 71 | } 72 | var uniq = map[string]bool{} 73 | for _, name := range labelNames { 74 | uniq[name] = true 75 | } 76 | var uniqLabelNames []string 77 | for n := range uniq { 78 | uniqLabelNames = append(uniqLabelNames, n) 79 | } 80 | sort.Strings(uniqLabelNames) 81 | 82 | l := &TcmLabels{ 83 | queryLableNames: qln, 84 | instanceLabelNames: iln, 85 | constLabels: cl, 86 | Names: uniqLabelNames, 87 | } 88 | return l, nil 89 | } 90 | -------------------------------------------------------------------------------- /pkg/metric/meta.go: -------------------------------------------------------------------------------- 1 | package metric 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "sort" 7 | "strconv" 8 | "strings" 9 | 10 | monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724" 11 | "github.com/tencentyun/tencentcloud-exporter/pkg/config" 12 | ) 13 | 14 | // 代表一个云监控指标的元数据 15 | type TcmMeta struct { 16 | Id string 17 | Namespace string 18 | ProductName string 19 | MetricName string 20 | SupportDimensions []string 21 | m *monitor.MetricSet 22 | } 23 | 24 | func (meta *TcmMeta) GetPeriod(confPeriod int64) (int64, error) { 25 | if len(meta.m.Period) == 0 { 26 | return 0, errors.New("period is empty") 27 | } 28 | 29 | var allowPeriods []int 30 | for _, p := range meta.m.Period { 31 | allowPeriods = append(allowPeriods, int(*p)) 32 | } 33 | sort.Ints(allowPeriods) 34 | 35 | var period int64 36 | if confPeriod != 0 { 37 | period = confPeriod 38 | } else { 39 | period = config.DefaultPeriodSeconds 40 | } 41 | 42 | idx := sort.SearchInts(allowPeriods, int(period)) 43 | if idx<0 || idx>=len(allowPeriods){ 44 | return 0,errors.New("not support stats period") 45 | } 46 | return int64(allowPeriods[idx]), nil 47 | } 48 | 49 | func (meta *TcmMeta) GetStatType(period int64) (string, error) { 50 | var statType string 51 | var defaultStatType string 52 | for _, p := range meta.m.Periods { 53 | i, err := strconv.ParseInt(*p.Period, 10, 64) 54 | if err != nil { 55 | return "", err 56 | } 57 | if i == period { 58 | statType = *p.StatType[0] 59 | } 60 | if i == 300 { 61 | defaultStatType = *p.StatType[0] 62 | } 63 | } 64 | if statType != "" { 65 | return statType, nil 66 | } 67 | if defaultStatType != "" { 68 | return defaultStatType, nil 69 | } 70 | 71 | return "", fmt.Errorf("not found statType, period=%d", period) 72 | 73 | } 74 | 75 | func NewTcmMeta(m *monitor.MetricSet) (*TcmMeta, error) { 76 | id := fmt.Sprintf("%s-%s", *m.Namespace, *m.MetricName) 77 | 78 | var productName string 79 | nsItems := strings.Split(*m.Namespace, "/") 80 | productName = nsItems[len(nsItems)-1] 81 | 82 | var supportDimensions []string 83 | for _, dimension := range m.Dimensions { 84 | for _, d := range dimension.Dimensions { 85 | supportDimensions = append(supportDimensions, *d) 86 | } 87 | } 88 | 89 | meta := &TcmMeta{ 90 | Id: id, 91 | Namespace: *m.Namespace, 92 | ProductName: productName, 93 | MetricName: *m.MetricName, 94 | SupportDimensions: supportDimensions, 95 | m: m, 96 | } 97 | return meta, nil 98 | 99 | } 100 | -------------------------------------------------------------------------------- /pkg/metric/query.go: -------------------------------------------------------------------------------- 1 | package metric 2 | 3 | import ( 4 | "github.com/prometheus/client_golang/prometheus" 5 | ) 6 | 7 | // 负责一个指标的查询管理 8 | type TcmQuery struct { 9 | Metric *TcmMetric 10 | LatestQueryStatus int 11 | repo TcmMetricRepository 12 | } 13 | 14 | type TcmQuerySet []*TcmQuery 15 | 16 | func (q *TcmQuery) GetPromMetrics() (pms []prometheus.Metric, err error) { 17 | q.LatestQueryStatus = 2 18 | 19 | pms, err = q.Metric.GetLatestPromMetrics(q.repo) 20 | if err != nil { 21 | return 22 | } 23 | 24 | q.LatestQueryStatus = 1 25 | return 26 | } 27 | 28 | func (qs TcmQuerySet) SplitByBatch(batch int) (steps [][]*TcmQuery) { 29 | total := len(qs) 30 | for i := 0; i < total/batch+1; i++ { 31 | s := i * batch 32 | if s >= total { 33 | continue 34 | } 35 | e := i*batch + batch 36 | if e >= total { 37 | e = total 38 | } 39 | steps = append(steps, qs[s:e]) 40 | } 41 | return 42 | } 43 | 44 | func NewTcmQuery(m *TcmMetric, repo TcmMetricRepository) (query *TcmQuery, err error) { 45 | query = &TcmQuery{ 46 | Metric: m, 47 | repo: repo, 48 | } 49 | return 50 | } 51 | -------------------------------------------------------------------------------- /pkg/metric/sample.go: -------------------------------------------------------------------------------- 1 | package metric 2 | 3 | import ( 4 | "fmt" 5 | 6 | monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724" 7 | ) 8 | 9 | // 代表一个数据点 10 | type TcmSample struct { 11 | Timestamp float64 12 | Value float64 13 | Dimensions []*monitor.Dimension 14 | } 15 | 16 | // 代表一个时间线的多个数据点 17 | type TcmSamples struct { 18 | Series *TcmSeries 19 | Samples []*TcmSample 20 | } 21 | 22 | func (s *TcmSamples) GetLatestPoint() (point *TcmSample, err error) { 23 | if len(s.Samples) == 1 { 24 | return s.Samples[0], nil 25 | } else { 26 | return s.Samples[len(s.Samples)-1], nil 27 | } 28 | } 29 | 30 | func (s *TcmSamples) GetMaxPoint() (point *TcmSample, err error) { 31 | maxValue := s.Samples[0].Value 32 | var maxIdx int 33 | for idx, sample := range s.Samples { 34 | if sample.Value > maxValue { 35 | maxValue = sample.Value 36 | maxIdx = idx 37 | } 38 | } 39 | return s.Samples[maxIdx], nil 40 | } 41 | 42 | func (s *TcmSamples) GetMinPoint() (point *TcmSample, err error) { 43 | minValue := s.Samples[0].Value 44 | var minIdx int 45 | for idx, sample := range s.Samples { 46 | if sample.Value < minValue { 47 | minValue = sample.Value 48 | minIdx = idx 49 | } 50 | } 51 | return s.Samples[minIdx], nil 52 | } 53 | 54 | func (s *TcmSamples) GetAvgPoint() (point *TcmSample, err error) { 55 | var sum float64 56 | for _, sample := range s.Samples { 57 | sum = sum + sample.Value 58 | } 59 | avg := sum / float64(len(s.Samples)) 60 | sample := &TcmSample{ 61 | Timestamp: s.Samples[len(s.Samples)-1].Timestamp, 62 | Value: avg, 63 | Dimensions: s.Samples[len(s.Samples)-1].Dimensions, 64 | } 65 | return sample, nil 66 | } 67 | 68 | func NewTcmSamples(series *TcmSeries, p *monitor.DataPoint) (s *TcmSamples, err error) { 69 | s = &TcmSamples{ 70 | Series: series, 71 | Samples: []*TcmSample{}, 72 | } 73 | 74 | if len(p.Timestamps) == 0 { 75 | return nil, fmt.Errorf("Samples is empty ") 76 | } 77 | 78 | if len(p.Timestamps) != len(p.Values) { 79 | return nil, fmt.Errorf("Samples error, timestamps != values ") 80 | } 81 | 82 | for i := 0; i < len(p.Timestamps); i++ { 83 | s.Samples = append(s.Samples, &TcmSample{ 84 | Timestamp: *p.Timestamps[i], 85 | Value: *p.Values[i], 86 | Dimensions: p.Dimensions, 87 | }) 88 | } 89 | return 90 | } 91 | -------------------------------------------------------------------------------- /pkg/metric/series.go: -------------------------------------------------------------------------------- 1 | package metric 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/tencentyun/tencentcloud-exporter/pkg/instance" 7 | ) 8 | 9 | // 代表某个指标的一个时间线 10 | type TcmSeries struct { 11 | Id string 12 | Metric *TcmMetric 13 | QueryLabels Labels 14 | Instance instance.TcInstance 15 | } 16 | 17 | func GetTcmSeriesId(m *TcmMetric, ql Labels) (string, error) { 18 | m5, e := ql.Md5() 19 | if e != nil { 20 | return "", e 21 | } 22 | return fmt.Sprintf("%s-%s", m.Id, m5), nil 23 | } 24 | 25 | func NewTcmSeries(m *TcmMetric, ql Labels, ins instance.TcInstance) (*TcmSeries, error) { 26 | id, err := GetTcmSeriesId(m, ql) 27 | if err != nil { 28 | return nil, err 29 | } 30 | 31 | s := &TcmSeries{ 32 | Id: id, 33 | Metric: m, 34 | QueryLabels: ql, 35 | Instance: ins, 36 | } 37 | return s, nil 38 | 39 | } 40 | -------------------------------------------------------------------------------- /pkg/util/label.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "regexp" 5 | "unicode" 6 | ) 7 | 8 | // IsValidTagKey ref: https://cloud.tencent.com/document/product/1416/73370#.E8.87.AA.E5.AE.9A.E4.B9.89.E4.B8.8A.E6.8A.A5.E9.99.90.E5.88.B6 9 | func IsValidTagKey(str string) bool { 10 | for _, r := range str { 11 | if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) { 12 | return false 13 | } 14 | } 15 | if !regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`).MatchString(str) || len(str) > 1024 { 16 | return false 17 | } 18 | return true 19 | } 20 | -------------------------------------------------------------------------------- /pkg/util/label_test.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func Test_IsValidTagKey(t *testing.T) { 10 | res := IsValidTagKey("test-test") 11 | assert.False(t, res) 12 | res = IsValidTagKey("_4324hdj_fas213") 13 | assert.True(t, res) 14 | res = IsValidTagKey("4324hdj_fas213") 15 | assert.False(t, res) 16 | res = IsValidTagKey("是3213dsa") 17 | assert.False(t, res) 18 | res = IsValidTagKey("cls-xxxx-eks-eip-tag-pod-uid") 19 | assert.False(t, res) 20 | } 21 | -------------------------------------------------------------------------------- /pkg/util/list.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | func IsStrInList(l []string, i string) bool { 4 | for _, item := range l { 5 | if item == i { 6 | return true 7 | } 8 | } 9 | return false 10 | 11 | } 12 | 13 | func IsInt64InList(l []*int64, i int64) bool { 14 | for _, item := range l { 15 | if *item == i { 16 | return true 17 | } 18 | } 19 | return false 20 | 21 | } 22 | -------------------------------------------------------------------------------- /pkg/util/map.go: -------------------------------------------------------------------------------- 1 | package util 2 | -------------------------------------------------------------------------------- /pkg/util/set.go: -------------------------------------------------------------------------------- 1 | package util 2 | -------------------------------------------------------------------------------- /pkg/util/str.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | func ToUnderlineLower(s string) string { 4 | var interval byte = 'a' - 'A' 5 | b := make([]byte, 0, len(s)) 6 | for i := 0; i < len(s); i++ { 7 | c := s[i] 8 | if c >= 'A' && c <= 'Z' { 9 | c += interval 10 | if i != 0 { 11 | b = append(b, '_') 12 | } 13 | } 14 | b = append(b, c) 15 | } 16 | return string(b) 17 | } 18 | -------------------------------------------------------------------------------- /pkg/util/time.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import "time" 4 | 5 | // FormatTime formats time by zone. 6 | func FormatTime(t time.Time, format string) string { 7 | var local time.Time 8 | _, offset := t.Zone() 9 | local = t.Add(time.Duration(8*3600-offset) * time.Second) 10 | return local.Format(format) 11 | } 12 | --------------------------------------------------------------------------------