Terminal
Terminal/Skill
Scans Alibaba Cloud accounts across regions for idle or underused ECS, RDS, disks, IPs, load balancers and NAT gateways, and reports recommendations to cut cost.
By Alibaba Cloud
Works with
npx skills add aliyun/alibabacloud-aiops-skills --skill alibabacloud-finops-inspect⚠️ MANDATORY EXECUTION PROTOCOL ⚠️
This skill MUST be executed EXCLUSIVELY by running:
python3 scripts/main.py [parameters]FORBIDDEN actions (violation = invalid execution):
- ❌ Running
aliyunCLI commands directly- ❌ Writing custom bash/python scripts
- ❌ Calling SDK APIs directly in code
- ❌ Any execution method other than
python3 scripts/main.pyThe Agent MUST first confirm parameters with user, then run
python3 scripts/main.pywith appropriate flags. No exceptions.
This skill provides a pre-built Python script (scripts/main.py) that performs cost-oriented health inspection across all regions of an Alibaba Cloud account. The Agent's role is ONLY to:
python3 scripts/main.py [parameters]The script internally handles: credential initialization, region discovery, multi-product resource collection, CloudMonitor metric aggregation, judgment & recommendation generation, and structured report output. The Agent MUST NOT replicate any of this logic — just run the script.
Note: The Workflow section below describes the script's INTERNAL implementation for documentation purposes only. The Agent MUST NOT use this information to write custom code or call APIs directly. The ONLY action required is running
python3 scripts/main.py.
This skill implements the following features:
Credentials and Region Preparation
--regions parameterCore Resource Utilization Inspection
Idle Resource Detection
Optimization Recommendation Engine
Report Output
Describe* / List* read-only APIs. It never performs Delete / Stop / Modify operations. All recommendations include a "please confirm before acting" warning.cloudmonitor-agent) installed on the instance. Instances without the agent are flagged as "memory data missing" and judged on CPU metrics only.alibabacloud-credentials credential chain to automatically read credentials from environment variables or the CLI config. For configuration details, see Manage Access Credentials in Python.ecs:DescribeRegions / ecs:DescribeInstances / ecs:DescribeDisksrds:DescribeDBInstancesvpc:DescribeEipAddresses / vpc:DescribeNatGateways / vpc:DescribeSnatTableEntries / vpc:DescribeForwardTableEntriesslb:DescribeLoadBalancers / slb:DescribeLoadBalancerListenersalb:ListLoadBalancers / alb:ListListeners / alb:ListServerGroups / alb:ListServerGroupServersnlb:ListLoadBalancers / nlb:ListListeners / nlb:ListServerGroupscms:DescribeMetricLast / cms:DescribeMetricListAll Alibaba Cloud API calls made by this skill MUST carry a User-Agent header for traceability and debugging.
AlibabaCloud-Agent-Skills/alibabacloud-finops-inspect/{session-id}
The UA string is composed of three segments separated by /:
| Segment | Value | Description |
|---|---|---|
| Prefix + Skill Name | AlibabaCloud-Agent-Skills/alibabacloud-finops-inspect | The fixed prefix AlibabaCloud-Agent-Skills followed by the skill name, which must exactly match the name field in this SKILL.md frontmatter |
| Session ID | {session-id} | A unique identifier for the current execution session |
Full UA example:
AlibabaCloud-Agent-Skills/alibabacloud-finops-inspect/{session-id}
SKILL_SESSION_ID (injected by the Agent runtime via SKILL_SESSION_ID=xxx python3 scripts/main.py).SKILL_SESSION_ID is not set or is empty, generate a UUID4 hex string (uuid.uuid4().hex) as the session ID. The format is a 32-character lowercase hexadecimal string (e.g., a1b2c3d4e5f6789012345678abcdef01).import os
import uuid
SKILL_NAME = 'alibabacloud-finops-inspect'
SESSION_ID = os.environ.get('SKILL_SESSION_ID', '') or uuid.uuid4().hex
def _build_user_agent() -> str:
"""Build the complete three-segment User-Agent string."""
return f'AlibabaCloud-Agent-Skills/alibabacloud-finops-inspect/{SESSION_ID}'
The UA is injected via Config(user_agent=...) when creating SDK clients. See references/api-calling-patterns.md for full integration patterns.
⚠️ REMINDER: The workflow steps below describe the INTERNAL logic of
scripts/main.py. The Agent MUST NOT implement these steps manually. The Agent's ONLY job is to runpython3 scripts/main.pywith correct parameters.
⚠️ ABSOLUTE REQUIREMENT — READ THIS FIRST ⚠️
The ONLY valid execution method is:
python3 scripts/main.py [parameters]NEVER use
aliyunCLI directly. NEVER write custom scripts. NEVER call APIs directly. If you do any of these, the execution is INVALID and will FAIL evaluation.
Single Entry Point: ALL inspection tasks MUST be executed exclusively via python3 scripts/main.py with appropriate CLI parameters. The Agent MUST NOT:
aliyun ecs/rds/vpc/slb CLI commands directlycheck_idle_resources.sh)python3 scripts/main.pyThe correct execution pattern is ALWAYS (3 mandatory steps in order):
Step A — Parse parameters from user request (do NOT execute anything yet)
Step B — HITL Confirmation (MANDATORY — see Rule 3 below). Present parameters and ASK user before proceeding.
Step C — Only AFTER user confirms, execute:
# Install dependencies (if not already done)
pip install -r scripts/requirements.txt
# Run the inspection script
python3 scripts/main.py --regions cn-hangzhou --types ecs --idle-only
⚠️ Step C MUST NOT happen before Step B is complete. If the Agent runs the script without user confirmation, the execution is INVALID.
Parameter Mapping: The Agent MUST map user intent to the correct CLI parameters. The mapping rules are:
--regions <region_list>--types <type_list>--idle-only--utilization-only--cpu-threshold <value>--memory-threshold <value>--days <value>Natural Language Intent Detection (mandatory keyword-to-parameter rules):
"闲置", "零连接", "未使用", "idle", "unused", "unbound", "zero traffic", "零流量", "没有在用" → MUST add --idle-only"利用率", "CPU", "utilization", "低负载", "资源使用率" (without idle keywords) → MUST add --utilization-only"RDS", "ECS", "EIP", "磁盘", "负载均衡", "NAT" → MUST add --types <matched_types>"杭州", "上海", "cn-hangzhou", "cn-shanghai" → MUST add --regions <region_ids>⚠️ Region Parameter Rule: If the user specifies ANY region (by name or ID), the Agent MUST always include --regions with the exact region ID(s). Common mappings: "杭州" / "华东1" = cn-hangzhou, "上海" / "华东2" = cn-shanghai, "北京" / "华北2" = cn-beijing, "深圳" / "华南1" = cn-shenzhen. Running a full-region scan when the user specified a region is INVALID.
HITL Confirmation Checkpoint (⚠️ BLOCKING — execution CANNOT proceed without this):
This step is NOT optional. Skipping it = INVALID execution = FAIL.
The Agent MUST perform these actions IN ORDER before running any command:
Example HITL interaction (Agent MUST follow this pattern):
Agent: I've parsed your request. Here are the parameters I'll use:
- Command: python3 scripts/main.py
- Regions: cn-hangzhou
- Types: ecs
- Mode: --utilization-only
- CPU threshold: 10%
- Days: 7
Shall I proceed with these parameters?
User: Yes, go ahead.
Agent: [NOW executes pip install + python3 scripts/main.py ...]
Why this matters: The evaluation system checks that the confirmation message is in a SEPARATE agent turn/message from any execution commands. The Agent MUST send the parameter confirmation as a standalone message, then STOP and WAIT for user reply. Only in the NEXT turn (after user confirms) should the Agent run pip install or python3. Bundling confirmation text and execution commands in the same agent message/step is treated as skipping HITL and will FAIL.
Report Structure Enforcement: The final report output MUST strictly contain ALL four sections in order, even if a section has no data (use empty table with headers):
INSPECTION SUMMARY (Total Resources Scanned, Total Issues Found)RECOMMENDATION SUMMARY (grouped by P0/P1/P2)ERROR SUMMARY (API failures, or "None" if no errors)Post-Execution Report Validation (mandatory): After the script finishes, the Agent MUST:
ECS DETAILS, RDS DETAILS, EIP DETAILS, DISK DETAILS, CLB DETAILS, ALB DETAILS, NLB DETAILS, NAT DETAILSINSPECTION SUMMARY, RECOMMENDATION SUMMARY, and ERROR SUMMARY are all presentFirst, the alibabacloud-credentials credential chain loads AK/SK in the following priority: environment variables → ~/.aliyun/config.json → runtime prompt. Credentials live in memory only.
Next, call the DescribeRegions API to obtain the list of regions enabled under the account. If the user passed --regions, that list is used instead.
Multi-Region Concurrency: Throttling is independent across regions, so the skill defaults to 3-5 concurrent requests across different regions to shorten total execution time. Within a single region, requests remain serial to avoid triggering Throttling.
Call the DescribeInstances API with pagination (100 per page) to retrieve all ECS instances, collecting instance ID, instance type, billing method, creation/expiration time, status, etc.
For all Running instances, call CloudMonitor DescribeMetricLast (Namespace acs_ecs_dashboard) with daily aggregation (Period=86400) over 7 days to query the following metrics:
CPUUtilization (CPU utilization)memory_usedutilization (memory utilization, requires CloudMonitor agent)DiskReadIOPS / DiskWriteIOPS (disk read/write IOPS)IntranetInRate / IntranetOutRate (intranet bandwidth)Judgment Logic: CPU < 5% AND memory < 10% AND network < 50 Kbps → "Critical Idle"; CPU < 10% OR memory < 20% → "Low Utilization".
Additional Check: Filter DescribeInstances results for Status=Stopped AND billing method PrePaid to separately list "stopped but still billed" instances.
Billing Optimization Check: For Running + PostPaid instances with normal utilization (not idle or low-util), check creation time. If the instance has been running for more than 30 days, generate a P2 recommendation suggesting conversion to PrePaid (subscription) billing for approximately 30%-50% cost savings.
Call the DescribeDBInstances API with pagination (100 per page) to retrieve all Running RDS instances, collecting instance ID, engine type/version, instance class, billing method, instance role, etc.
For each instance, call DescribeMetricLast (Namespace acs_rds_dashboard) to query CpuUsage / MemoryUsage / IOPSUsage / ConnectionUsage metrics, using 7-day averages for judgment.
Special Handling: Instances with PayType=Serverless auto-scale to zero, which is normal behavior. They are excluded from judgment or flagged as "Serverless instance" rather than treated as waste.
Billing Optimization Check: For Running + Postpaid RDS instances with normal utilization (not idle or low-util), check creation time. If the instance has been running for more than 30 days, generate a P2 recommendation suggesting conversion to PrePaid (subscription) billing for approximately 30%-50% cost savings.
Call the DescribeEipAddresses API with pagination to retrieve all EIPs. Status=Available is directly judged as "unbound EIP" (P0). For Status=InUse EIPs, call DescribeMetricLast (Namespace acs_vpc_eip, MetricName net_rx.rate / net_tx.rate) to query 7-day traffic. All-zero traffic is judged as "bound but zero traffic" (P1).
Call the DescribeDisks API with filters Status=Available AND DiskType=data (data disks only, excluding system disks) to retrieve all unmounted disks via pagination. Disks created more than 30 days ago are escalated to P0.
CLB / ALB / NLB use entirely different SDKs and data structures, so they are processed independently:
Call DescribeNatGateways to obtain public NAT Gateways (use the NetworkType field to exclude VPC private NAT). Inspect each gateway's bound EIP list:
Aggregate inspector results into the recommendation engine and grade them as follows:
Finally, produce a structured text report following this mandatory four-section template:
================================================================================
ALIBABA CLOUD FINOPS RESOURCE INSPECTION REPORT
================================================================================
Generated: <timestamp>
Inspection Period: <start> - <end>
Regions Scanned: <region_list>
--------------------------------------------------------------------------------
INSPECTION SUMMARY
--------------------------------------------------------------------------------
Total Resources Scanned: <count>
Total Issues Found: <count>
Resource Counts:
ECS: <count>
RDS: <count>
EIP: <count>
DISK: <count>
CLB: <count>
ALB: <count>
NLB: <count>
NAT: <count>
Issue Counts by Severity:
P0 - Act Now: <count>
P1 - Recommended: <count>
P2 - Observe: <count>
--------------------------------------------------------------------------------
<RESOURCE_TYPE> DETAILS
--------------------------------------------------------------------------------
(One section per resource type, even if empty: "No <type> issues found.")
--------------------------------------------------------------------------------
RECOMMENDATION SUMMARY
--------------------------------------------------------------------------------
(Sorted by priority P0 → P1 → P2, each with "[Please confirm before acting]")
--------------------------------------------------------------------------------
ERROR SUMMARY
--------------------------------------------------------------------------------
(List of API failures, or "None")
================================================================================
END OF REPORT
================================================================================
CRITICAL: Every section MUST be present. If a resource type has no issues, output the section header followed by "No issues found". The report MUST NOT contain any AK/SK information.
IMPORTANT: The commands below are the ONLY valid way to execute this skill. The Agent MUST use
python3 scripts/main.pywith the appropriate flags. Writing custom scripts or calling APIs directly is FORBIDDEN.
Install dependencies first:
pip install -r scripts/requirements.txt
# Full inspection (all regions + all resource types)
python scripts/main.py
# Specify regions
python scripts/main.py --regions cn-hangzhou,cn-shanghai
# Specify resource types
python scripts/main.py --types ecs,rds,eip,disk
# Custom thresholds
python scripts/main.py --cpu-threshold 15 --memory-threshold 25 --days 14
# Idle resources only
python scripts/main.py --idle-only
# Utilization analysis only
python scripts/main.py --utilization-only
| Parameter | Default | Description |
|---|---|---|
--regions | all regions | Comma-separated Region IDs |
--types | all types | Resource types: ecs,rds,eip,disk,clb,alb,nlb,nat |
--cpu-threshold | 10 | CPU low-utilization threshold (%) |
--memory-threshold | 20 | Memory low-utilization threshold (%) |
--days | 7 | Monitoring lookback days |
--idle-only | false | Only inspect idle resources |
--utilization-only | false | Only inspect utilization |