Python SDK example

Updated at:

This topic shows how to call the GetInstanceInspections API using the Python SDK for Database Autonomy Service (DAS). This API retrieves the inspection and scoring results for a database instance.

Review the API documentation

Before calling an API, review its documentation to understand the required parameters and permissions. For more information, see API overview.

Create a RAM user and grant permissions

If you have already created a RAM user and granted the required permissions, you can skip this step.

  1. Create a RAM user:

    1. Go to the Users page and click Create User.

    2. Set Logon Name to das-openapi-operator. For Access Method, select Programmatic access.

    3. Click OK to create the RAM user, and save the AccessKey ID and AccessKey Secret.

  2. Grant permissions:

    1. Go to the Users page. In the Actions column for the target RAM user, click Add Permissions.

    2. In the search box, enter DAS and select the AliyunHDMReadOnlyAccess policy.

      Note

      The AliyunHDMReadOnlyAccess policy grants read-only permissions on DAS. You can also select the AliyunHDMFullAccess policy for full management permissions or create a custom policy. For more information, see Create a custom policy.

    3. Click OK to complete the authorization.

Call the API

This topic uses the Python SDK as an example. The process is similar for SDKs in other languages. For more information, see Database Autonomy Service (DAS) SDK.

Prepare the Python environment

Download and install Python 3. If you have Python installed, run the following command to check the version: python --version.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

  • For Linux and macOS, replace <ACCESS_KEY_ID> and <ACCESS_KEY_SECRET> with your AccessKey ID and AccessKey Secret, and run the following commands in your terminal:

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>
  • For Windows, create environment variables named ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET, and set their values to your AccessKey ID and AccessKey Secret. Restart your system.

Install dependencies

Run the following command in your terminal to install the required dependencies:

pip install alibabacloud_das20200116==2.4.2

Download the sample code

  1. Go to the OpenAPI Explorer page for GetInstanceInspections.

  2. On the Parameter Configuration tab, configure the parameters as follows. This example uses an ApsaraDB RDS for MySQL instance.

    Parameter

    Value

    Engine

    MySQL

    StartTime

    1718776527000

    Important

    The start time of the task. Specify a UNIX timestamp in milliseconds. Replace the example value with your actual start time.

    EndTime

    1718776827000

    Important
    • The end time of the task. Specify a UNIX timestamp in milliseconds. Replace the example value with your actual end time.

    • The query returns completed inspection and scoring tasks within the specified time range. If no tasks are found, manually run an inspection on an instance and try again after the task completes. To learn how, see Inspection and scoring.

    InstanceArea

    RDS

    PageNo

    1

    PageSize

    10

  1. On the SDK Sample Code tab on the right, select Python and click Download Project.

  2. Extract the package and go to the alibabacloud_sample directory.

Run the sample code

Run the following command:

python sample.py

The following is a sample response:

{
  "headers": {
    "date": "Wed, 19 Jun 2024 06:24:10 GMT",
    "content-type": "application/json;charset=utf-8",
    "content-length": "1428",
    "connection": "keep-alive",
    "keep-alive": "timeout=25",
    "vary": "Accept-Encoding",
    "access-control-allow-origin": "*",
    "access-control-expose-headers": "*",
    "x-acs-request-id": "B576DACC-D08A-5058-8843-E613CBF474CF",
    "x-acs-trace-id": "2fb5b5b76565ce67233288b9369f6d96",
    "etag": "1rcNeDQg43APurk7PohefuQ8"
  },
  "statusCode": 200,
  "body": {
    "Code": 200,
    "Data": {
      "List": [
        {
          "AutoFunction": {
            "AutoIndex": 1,
            "AutoLimitedSql": 0,
            "AutoResourceOptimize": 2,
            "AutoScale": 0,
            "EventSubscription": 0
          },
          "Data": {
            "hasDeadLock": false,
            "slowSqlCount": 0,
            "exceptionTableMap": {},
            "spaceItem": {
              "valid": true,
              "current": 0.16416015625,
              "avg": 0.16416015625,
              "min": 0.16416015625,
              "max": 0.16416015625
            },
            "maxIops": 3100,
            "bigTransactionCount": 0,
            "cpu": 2,
            "isRds": true,
            "storage": 20,
            "lastHourRate": 0.0,
            "lastDayCount": 0,
            "lastHourCount": 0,
            "slowSqlList": [],
            "lastDayRate": 0.0,
            "activeSessions": [],
            "enable": false,
            "bigTransactionList": [],
            "bigSessionList": [
              {
                "User": "system user",
                "Command": "Query",
                "State": "Replica has read all relay log; waiting for more updates",
                "active": false,
                "Host": "",
                "Time": 24,
                "Id": 61,
                "Info": "",
                "db": ""
              }
            ],
            "tableStats": [],
            "bigSessionCount": 1,
            "spaceUsage": 0.16416015625,
            "availableDays": 76
          },
          "EnableDasPro": 0,
          "EndTime": 1718726400000,
          "GmtCreate": 1718776692000,
          "Instance": {
            "AccountId": "1648821913965368",
            "Category": "standard",
            "Cpu": "2",
            "Engine": "MySQL",
            "EngineVersion": "8.0",
            "InstanceArea": "RDS",
            "InstanceClass": "mysql.n2m.medium.2c",
            "InstanceId": "rm-bp1s6jtqtr41m****",
            "Memory": 4096,
            "NetworkType": "VPC",
            "Region": "cn-hangzhou",
            "Storage": 20,
            "Uuid": "hdm_6d00b701adcb484d5fa4d2800dc0****",
            "VpcId": "vpc-bp1ov7as4yvz4kxei****"
          },
          "Score": 100,
          "StartTime": 1718640000000,
          "State": 2,
          "TaskType": 1
        }
      ],
      "PageNo": 1,
      "PageSize": 10,
      "Total": 1
    },
    "Message": "Successful",
    "RequestId": "B576DACC-D08A-5058-8843-E613CBF474CF",
    "Success": true
  }
}