This guide shows how to use the Database Autonomy Service (DAS) SDK for Python to call the GetInstanceInspections API operation, which retrieves inspection and scoring results for a database instance.
Prerequisites
Before you begin, ensure that you have:
Python 3 installed. Run
python --versionto check.A RAM user with DAS permissions. Follow the steps in Set up a RAM user if you haven't done this yet.
Your AccessKey ID and AccessKey secret stored as environment variables. Follow the steps in Configure environment variables.
How it works
The example in this guide does the following:
Authenticates using credentials from environment variables.
Calls
GetInstanceInspectionsto query completed inspection and scoring tasks within a specified time range.Returns a paginated list of inspection results, including scores, instance details, and diagnostic data.
For a full list of DAS API operations, see the DAS API reference. For SDKs in other languages, see DAS SDK.
Set up a RAM user
Skip this section if you already have a Resource Access Management (RAM) user with DAS permissions.
Create a RAM user.
Log on to the RAM console, go to the Users page, and then click Create User.
Set Logon Name to
das-openapi-operatorand set Access Mode to Using permanent AccessKey to access.Click OK. Save the AccessKey ID and AccessKey secret displayed after creation.
Grant permissions to the RAM user.
On the Users page, find the RAM user and click Add Permissions in the Actions column.
Search for
DASand select AliyunHDMReadOnlyAccess.NoteAliyunHDMReadOnlyAccess grants read-only access to DAS. For full read and write access, select AliyunHDMFullAccess instead. To create a custom policy with specific permissions, see Create custom policies.
Click Grant permissions.
Configure environment variables
Store your credentials as environment variables to avoid hardcoding sensitive values in your code.
Linux or macOS:
export ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>Windows (Command Prompt):
set ALIBABA_CLOUD_ACCESS_KEY_ID=<ACCESS_KEY_ID>
set ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ACCESS_KEY_SECRET>Windows (PowerShell):
$env:ALIBABA_CLOUD_ACCESS_KEY_ID = "<ACCESS_KEY_ID>"
$env:ALIBABA_CLOUD_ACCESS_KEY_SECRET = "<ACCESS_KEY_SECRET>"The set and $env: commands are session-scoped — the variables are lost when you close the terminal or restart Windows. To set persistent environment variables on Windows, use the Windows System Properties dialog (Control Panel > System > Advanced system settings > Environment Variables) or create a file that sets these variables and restart Windows.
Replace <ACCESS_KEY_ID> and <ACCESS_KEY_SECRET> with the values you saved when creating the RAM user.
Install the SDK
We recommend using a virtual environment to avoid dependency conflicts with other packages on your system:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activateThen install the DAS Python SDK:
pip install alibabacloud_das20200116==2.4.2Call the API
Get the sample code
The sample code is auto-generated by OpenAPI Explorer based on the parameters you configure.
Go to the GetInstanceInspections page in OpenAPI Explorer.
On the Parameters tab, set the following parameters. This example queries inspections on an ApsaraDB RDS for MySQL instance.
Parameter Example value Notes Engine MySQLStartTime 1718776527000UNIX timestamp in milliseconds. Set this to the actual start of your query range. EndTime 1718776827000UNIX timestamp in milliseconds. Set this to the actual end of your query range. If no inspections completed within the range, run an inspection manually and retry. InstanceArea RDSPageNo 1PageSize 10On the SDK Sample Code tab on the right, select Python for the Languages parameter and click Download Project.
Extract the downloaded package and go to the
alibabacloud_sampledirectory.
Run the sample code
python sample.pyUnderstand the response
A successful call returns HTTP status code 200. The response body has the following structure:
| Field | Description |
|---|---|
Data.List | Array of inspection records. Each record contains instance metadata, diagnostic data, and a score. |
Data.List[].Score | Inspection score for the instance. |
Data.List[].State | Task state. |
Data.List[].TaskType | Task type. |
Data.List[].Instance | Instance details: engine, version, region, CPU, memory, storage, and network type. |
Data.List[].Data | Diagnostic data: slow SQL count, deadlock detection, active sessions, big transactions, and storage usage. |
Data.PageNo | Current page number. |
Data.PageSize | Number of records per page. |
Data.Total | Total number of matching records. |
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
}
}What's next
Inspection and scoring — Learn how to view and interpret inspection results in the DAS console.
DAS API reference — Explore all available API operations.
DAS SDK — Download SDKs for Java, Go, Node.js, and other languages.