TencentCloud / tencentcloud-sdk-nodejs

Tencent Cloud API 3.0 SDK for NodeJS
Apache License 2.0
370 stars 99 forks source link

GetMonitorData Failing #24

Closed rouralberto closed 5 years ago

rouralberto commented 5 years ago

Hi, I'm using this SDK to get monitor data of CVM instances and I've been unable to form a working query. So far I'm getting an error when following instructions here.

I opened the source code of the Models for monitor and then formed an Instances array with a Dimensions array inside. No luck either. I get a Error: Param dimensions error

This is where I am now:

        const tencent = require('tencentcloud-sdk-nodejs');
        const MonitorApi = tencent.monitor.v20180724;
        const MonitorApiClient = MonitorApi.Client;
        const MonitorApiModels = MonitorApi.Models;
        const Credential = tencent.common.Credential;
        const cred = new Credential(credentials.key, credentials.secret);

        const monitorRequest = new MonitorApiModels.GetMonitorDataRequest();
        const monitorClient = new MonitorApiClient(cred, regionId);

        console.log('Tencent Cloud CVM initialised');

        monitorRequest.Period = Period;
        monitorRequest.StartTime = StartTime;
        monitorRequest.EndTime = EndTime;
        monitorRequest.Instances = [
          {
            Dimensions: [
              {
                Name: 'unInstanceId',
                Value: instanceId,
              },
            ],
          },
        ];

        monitorClient.GetMonitorData(monitorRequest, callback);

I'd love to get an example of how to query it.

zqfan commented 5 years ago

@roura356a tencentcloud-sdk-nodejs is a SDK for Tencent Cloud API 3.0 (with endpoint .tencentcloudapi.com), the document you referred is Tencent Cloud API 2.0 (with endpoint .api.qcloud.com/v2/index.php). Since they are not compatible, you cannot get what you want with API 2.0 parameters while calling API 3.0 interface.

There is a tool, named API Explorer, which can be used to get familiar with API 3.0, while generating simple SDK source code, for this particular interface, please refer to: https://console.cloud.tencent.com/api/explorer?Product=monitor&Version=2018-07-24&Action=GetMonitorData&SignVersion=

here is the source code I generated with the tool, you can adjust the parameters according to you purpose:

const tencentcloud = require("../../../../tencentcloud-sdk-nodejs");

const MonitorClient = tencentcloud.monitor.v20180724.Client;
const models = tencentcloud.monitor.v20180724.Models;

const Credential = tencentcloud.common.Credential;
const ClientProfile = tencentcloud.common.ClientProfile;
const HttpProfile = tencentcloud.common.HttpProfile;

let cred = new Credential("masked-secret-id", "masked-secret-key");
let httpProfile = new HttpProfile();
httpProfile.endpoint = "monitor.tencentcloudapi.com";
let clientProfile = new ClientProfile();
clientProfile.httpProfile = httpProfile;
let client = new MonitorClient(cred, "ap-guangzhou", clientProfile);

let req = new models.GetMonitorDataRequest();

let params = '{"Namespace":"QCE/CVM","MetricName":"CPUUsage","Instances":[{"Dimensions":[{"Name":"InstanceId","Value":"ins-oxbabxrc"}]}]}'
req.from_json_string(params);

client.GetMonitorData(req, function(errMsg, response) {

    if (errMsg) {
        console.log(errMsg);
        return;
    }

    console.log(response.to_json_string());
});

The document you need to read is https://cloud.tencent.com/document/product/248/30385 , unfortunately it is Chinese simplified only.

rouralberto commented 5 years ago

Ohh got it, @zqfan! Thank you so much for the clarification. I'm still quite new with Tencent Cloud. I'll be more aware from now on about this differences 👍

rouralberto commented 5 years ago

Sadly the Api Explorer is locked to the Chinese portal and I can't access with my international login.

zqfan commented 5 years ago

I thought it just doesn't support English. International account is different, I think the support will not be soon.