关于 huawei-cloud-eip-cost-optimizer
此技能使用 hcloud CLI 查询和分析 Huawei Cloud 弹性公网 IP(EIP),帮助云资源运维人员识别闲置资源并估算费用。它提供跨区域报告、闲置监控和审计日志;所有脚本均为只读,不会调整带宽或释放 EIP。
主要功能
- 01跨区域查询和筛选 EIP
- 02识别闲置 EIP 并估算费用
- 03生成文本、HTML 或 JSON 报告
- 04通过 Webhook 或邮件监控告警
- 05记录并导出操作审计日志
适用场景
- 01查找超过指定天数未绑定的 EIP
- 02汇总多个区域的 EIP 状态
- 03生成费用分析和节省建议报告
- 04设置闲置资源告警并查看操作记录
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ huaweicloud/ huaweicloud-skills --skill huawei-cloud-eip-cost-optimizer安装后在 Claude Code 中输入/huawei-cloud-eip-cost-optimizer确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/acceptance-criteria.mdcli-installation-guide.mdeip-api-guide.mdiam-policies.mdtest-vars.jsonverification-method.mdscripts/analyze_idle_eips.shcheck_env.shconfig.sheip_audit_log.sheip_cost_report.shlist_eips.shmonitor_idle_eips.sh详细介绍
Huawei Cloud EIP 成本优化器
概述
此技能提供 Huawei Cloud 弹性公网 IP(EIP)的批量管理和成本优化功能。
架构:Shell + hcloud CLI(KooCLI)→ EIP 服务 API → VPC/带宽资源
相关技能:如需跨所有资源类型(ECS、EVS、OBS 等)进行更全面的成本优化,请参阅已归档的 huaweicloud-cost-optimizer 技能。此技能专注于 EIP 优化,提供更深入的功能,并完全遵循 hcloud CLI。
- 定期清理闲置 EIP,降低持有成本
- 成本分析和优化建议
- 多区域统一管理
- 自动监控并提醒闲置资源
- 记录操作审计日志以满足合规要求
典型使用场景:
- “帮我识别闲置 EIP 并生成优化报告”
- “生成 EIP 成本分析报告,识别高成本资源”
- “设置闲置 EIP 监控,并通过 webhook 或电子邮件自动发送提醒”
- “查看所有区域的 EIP 分布和状态摘要”
- “显示过去 30 天的 EIP 操作审计日志”
- “列出 cn-north-4 中的所有 EIP,并显示详细信息”
前置条件
1. CLI 环境要求(必需)
- hcloud CLI(KooCLI) v7.0+ — Huawei Cloud 命令行工具
- jq — 用于解析 API 响应的 JSON 处理器
- bc — 用于成本估算的任意精度计算器
- curl — 用于 webhook 通知的 HTTP 客户端
安装 hcloud CLI:
# Linux/macOS one-click install
curl -sSL https://hwcloudcli.obs.cn-north-1.myhuaweicloud.com/cli/latest/hcloud_install.sh | bash
# Verify installation
hcloud --version
安装 jq、bc、curl:
# Ubuntu/Debian
sudo apt install -y jq bc curl
# CentOS/RHEL
sudo yum install -y jq bc curl
# macOS
brew install jq bc curl
可用 Shell 脚本:
scripts/config.sh- 共享配置(凭据、区域、代理)scripts/list_eips.sh- 列出某个区域中的所有 EIP(支持筛选和摘要)scripts/analyze_idle_eips.sh- 分析闲置 EIP 并生成优化报告(只读)scripts/eip_cost_report.sh- 生成 EIP 成本分析报告(文本/HTML/JSON)scripts/monitor_idle_eips.sh- 监控闲置 EIP,并通过 webhook/电子邮件发送提醒,支持 cronscripts/check_env.sh- 检查并验证环境(hcloud CLI、工具和 API)scripts/eip_audit_log.sh- 操作审计日志(JSONL + CSV/JSON 导出)
注意:所有脚本均为只读。此技能不会执行带宽调整、标签管理或 EIP 释放/删除操作。
2. 身份验证配置
此技能仅支持通过环境变量进行一种身份验证方式:
环境变量
export HW_ACCESS_KEY=<your-ak>
export HW_SECRET_KEY=<your-sk>
export HW_REGION_NAME=cn-north-4
注意:如果你已通过交互方式配置
hcloud configure(通过提示输入凭据,而非使用命令行参数),此技能也会检测并使用这些凭据。
环境变量:
| 变量 | 必需 | 说明 |
|---|---|---|
HW_ACCESS_KEY | 可选 | Huawei Cloud Access Key ID(仅当未配置 hcloud configure 时需要) |
HW_SECRET_KEY | 可选 | Huawei Cloud Secret Access Key(仅当未配置 hcloud configure 时需要) |
HW_REGION_NAME | 可选 | 默认区域(默认值:cn-north-4) |
HW_SECURITY_TOKEN | 可选 | 临时凭据的安全令牌 |
安全注意事项:
- 切勿将凭据提交到版本控制系统
- 切勿在代码、对话或命令中暴露 AK/SK 值
- 切勿将 AK/SK 值作为命令行参数传递(这会导致凭据暴露在 shell 历史记录和
ps aux中) - 使用仅具有所需最小权限的 IAM 用户
- 为敏感操作启用 MFA
- 定期轮换 AK/SK
- 运行脚本前,使用
./scripts/check_env.sh验证凭据
3. 快速入门
# Step 1: Configure authentication via environment variables
export HW_ACCESS_KEY=<your-ak>
export HW_SECRET_KEY=<your-sk>
export HW_REGION_NAME=cn-north-4
# Step 2: Run environment check
bash scripts/check_env.sh
# Step 3: Run scripts
bash scripts/list_eips.sh --region cn-north-4
bash scripts/analyze_idle_eips.sh --idle-days 7
bash scripts/eip_cost_report.sh --format html
4. IAM 权限要求
注意:此技能对 EIP 资源为只读。不会执行任何写入操作。
| API 操作 | 权限 | 用途 |
|---|---|---|
vpc:publicIps:list | 列出 EIP | 查询所有 EIP 及其状态 |
vpc:publicIps:get | 获取 EIP 详情 | 查看单个 EIP 的信息 |
工作流
主要步骤
- 环境检查 → 验证 hcloud CLI、jq 和凭据
- EIP 查询 → 通过 hcloud CLI 列出各区域的 EIP
- 闲置分析 → 识别闲置时间超过阈值且未绑定的 EIP
- 成本报告 → 生成 HTML/JSON 成本分析报告
- 监控设置 → 配置闲置 EIP 告警(webhook/电子邮件)
- 审计日志 → 记录操作以满足合规要求
EIP 查询工作流
列出各区域的 EIP,按状态筛选,并以 JSON/表格格式输出。
闲置 EIP 分析工作流
检测闲置时间超过阈值且未绑定的 EIP,计算持有成本,并生成优化报告。
成本报告工作流
汇总 EIP 成本数据,生成 HTML 或 JSON 报告,并提供节省成本的建议。
闲置 EIP 监控工作流
定期检查闲置 EIP,检测到闲置 EIP 时通过 webhook 或电子邮件发送告警。
审计日志工作流
所有 EIP 操作(列表/分析/报告/监控)都会自动写入审计日志文件,以满足合规要求。每个脚本都会在退出时记录自己的操作。使用 --action query-log --days N 查询历史记录。
所有 EIP 操作均使用 hcloud CLI 命令:
| Python SDK 方法 | hcloud CLI 命令 | 描述 |
|---|---|---|
EipClient.list_publicips() | hcloud EIP ListPublicips/v2 --cli-region=<region> | 列出所有 EIP |
EipClient.show_publicip() | hcloud EIP ShowPublicip/v2 --publicip_id=<id> | 获取 EIP 详情 |
IamClient.keystone_list_projects() | hcloud IAM KeystoneListProjects | 列出项目 |
输出格式:所有命令均使用 --cli-output=json 输出机器可读的数据,并由 jq 解析。
核心命令
| 命令 | 描述 | 后端 |
|---|---|---|
list_eips.sh | 列出并查询各区域的 EIP | hcloud CLI |
analyze_idle_eips.sh | 识别闲置/未绑定的 EIP 并分析成本 | hcloud CLI |
eip_cost_report.sh | 生成 HTML/JSON 成本分析报告 | hcloud CLI |
monitor_idle_eips.sh | 设置闲置 EIP 监控和告警 | hcloud CLI |
eip_audit_log.sh | 维护操作审计日志 | Shell |
check_env.sh | 验证环境先决条件 | Shell |
config.sh | 加载配置和凭据 | Shell |
EIP 查询
# List all EIPs in a region
bash scripts/list_eips.sh --region cn-north-4
# List EIPs with status filter
bash scripts/list_eips.sh --region cn-north-4 --status DOWN
# List EIPs across multiple regions
bash scripts/list_eips.sh --region cn-north-4,cn-east-3,cn-south-1
闲置 EIP 分析
# Analyze idle EIPs (default threshold: 0 days = all unbound)
bash scripts/analyze_idle_eips.sh
# Custom idle threshold (14 days)
bash scripts/analyze_idle_eips.sh --idle-days 14
# Analyze specific region with JSON output
bash scripts/analyze_idle_eips.sh --region cn-north-4 --idle-days 7 --json
成本报告
# Generate text cost report (default)
bash scripts/eip_cost_report.sh
# Generate HTML report
bash scripts/eip_cost_report.sh --format html
# Generate JSON report
bash scripts/eip_cost_report.sh --format json
# Custom region
bash scripts/eip_cost_report.sh --region cn-east-3 --format html
成本模型:按带宽计费(cn-north-4 按需约为 3 CNY/Mbps/月)+ IP 保留费(未绑定 EIP 约为 0.02 CNY/小时)。API 不会返回 charge_mode,因此所有估算均采用按带宽计费的模型。
闲置 EIP 监控
# Monitor idle EIPs (default threshold: 7 days)
bash scripts/monitor_idle_eips.sh
# Custom threshold
bash scripts/monitor_idle_eips.sh --idle-days 14
# Monitor with webhook alert (only DingTalk/WeCom/Slack HTTPS URLs are allowed)
bash scripts/monitor_idle_eips.sh --idle-days 7 --webhook "https://oapi.dingtalk.com/robot/send?access_token=YOUR_ACCESS_TOKEN"
# Monitor with email alert
bash scripts/monitor_idle_eips.sh --idle-days 7 --email [email protected]
# Set up daily cron job (9:00 AM) - REQUIRES webhook or email, otherwise it's rejected.
# The cron line embeds the alert parameters so scheduled runs actually notify.
bash scripts/monitor_idle_eips.sh --region cn-north-4 --idle-days 7 --setup-cron \
--webhook "https://oapi.dingtalk.com/robot/send?access_token=YOUR_ACCESS_TOKEN"
# Remove cron job
bash scripts/monitor_idle_eips.sh --remove-cron
环境检查
# Full environment validation (CLI + tools + API)
bash scripts/check_env.sh
# Verbose mode (show versions)
bash scripts/check_env.sh --verbose
# Auto-fix missing dependencies
bash scripts/check_env.sh --fix
操作审计日志
# Log an EIP list operation
bash scripts/eip_audit_log.sh --action list --detail "Queried all EIPs"
# Log an analyze operation
bash scripts/eip_audit_log.sh --action analyze --detail "Idle EIP analysis for cn-north-4"
# Export audit logs to CSV
bash scripts/eip_audit_log.sh --action list --export csv
# Export audit logs to JSON
bash scripts/eip_audit_log.sh --action list --export json
# Custom log directory (must NOT be a system dir; will be created if missing)
bash scripts/eip_audit_log.sh --action list --log-dir ~/eip_audit_logs
# Query audit history for the last 30 days
bash scripts/eip_audit_log.sh --action query-log --days 30
# Query audit history for a specific action type
bash scripts/eip_audit_log.sh --action query-log --days 7
审计日志条目格式(JSONL,带时区的时间戳):
{
"timestamp": "2026-07-16T10:30:00+08:00",
"region": "cn-north-4",
"action": "list",
"detail": "Queried all EIPs",
"user": "root"
}
KooCLI 命令格式
# General format
hcloud <Service> <Operation> --cli-region=<region> --param1=value1 --param2=value2
# EIP list example
hcloud EIP ListPublicips/v2 --cli-region=cn-north-4
# EIP show detail
hcloud EIP ShowPublicip/v2 --cli-region=cn-north-4 --publicip_id=<id>
| 特性 | 描述 | 示例 |
|---------|-------------|---------||
| 服务名称 | 大驼峰命名法 | EIP、VPC、IAM |
| 操作名称 | 带版本号的大驼峰命名法 | ListPublicips/v2、ShowPublicip/v2 |
| 区域参数 | --cli-region=<value> | --cli-region=cn-north-4 |
| 简单参数 | --key=value | --publicip_id=xxx |
| 输出格式 | --cli-output=json | 用于程序化解析的 JSON 输出 |
参数
Shell 脚本参数
| 脚本 | 参数 | 必填/可选 | 说明 | 默认值 |
|---|---|---|---|---|
list_eips.sh | --region | 可选 | 区域(多个区域以逗号分隔) | HW_REGION_NAME 或 cn-north-4 |
list_eips.sh | --status | 可选 | 按状态筛选(ACTIVE/DOWN/ERROR) | 全部 |
analyze_idle_eips.sh | --region | 可选 | 目标区域 | HW_REGION_NAME 或 cn-north-4 |
analyze_idle_eips.sh | --idle-days | 可选 | 空闲天数阈值 | 0(所有未绑定的 EIP) |
analyze_idle_eips.sh | --json | 可选 | 以 JSON 格式输出报告 | false |
eip_cost_report.sh | --region | 可选 | 目标区域 | HW_REGION_NAME 或 cn-north-4 |
eip_cost_report.sh | --format | 可选 | 输出格式:text/html/json | text |
monitor_idle_eips.sh | --region | 可选 | 目标区域 | HW_REGION_NAME 或 cn-north-4 |
monitor_idle_eips.sh | --idle-days | 可选 | 空闲天数阈值 | 7 |
monitor_idle_eips.sh | --webhook | 可选 | Webhook 告警 URL | - |
monitor_idle_eips.sh | --email | 可选 | 告警邮箱地址 | - |
monitor_idle_eips.sh | --setup-cron | 可选 | 设置 cron 监控 | - |
monitor_idle_eips.sh | --remove-cron | 可选 | 移除 cron 监控 | - |
check_env.sh | --verbose | 可选 | 显示详细检查信息 | false |
check_env.sh | --fix | 可选 | 自动修复缺失的依赖项 | false |
eip_audit_log.sh | --action | 必填 | 操作类型(list/query/analyze/monitor/report) | - |
eip_audit_log.sh | --detail | 可选 | 操作详情说明 | - |
eip_audit_log.sh | --export | 可选 | 导出格式:csv/json | - |
eip_audit_log.sh | --log-dir | 可选 | 日志目录路径 | ./eip_audit_logs |
环境变量
| 变量 | 必填 | 说明 | 默认值 |
|---|---|---|---|
HW_ACCESS_KEY | 可选* | Huawei Cloud AK(仅在未设置 hcloud configure 时需要) | - |
HW_SECRET_KEY | 可选* | Huawei Cloud SK(仅在未设置 hcloud configure 时需要) | - |
HW_REGION_NAME | 可选 | 默认区域 | cn-north-4 |
HW_SECURITY_TOKEN | 可选 | 临时凭证令牌 | - |
*如果已设置 hcloud configure,则不需要 HW_ACCESS_KEY 和 HW_SECRET_KEY。同时配置时,环境变量优先。
输出格式
EIP 列表输出
========================================
Huawei Cloud EIP List (Region: cn-north-4)
========================================
EIP ID: eip-xxx1, IP: 123.45.67.89, BW: 5 Mbps, Status: BOUND (ECS: ecs-xxx)
EIP ID: eip-xxx2, IP: 98.76.54.32, BW: 10 Mbps, Status: UNBOUND ⚠️
========================================
Total: 2 EIPs, Idle: 1
成本报告输出
由 scripts/eip_cost_report.sh 生成,包含:
- 汇总统计(EIP 总数、空闲数、活跃数、总带宽和成本)
- 每个 EIP 的详情表及成本估算
- 提供 text、HTML 和 JSON 格式
定价模型:按带宽计费(约 3 CNY/Mbps/月 + 未绑定 EIP 的 0.02 CNY/小时 IP 保留费)
验证
环境合规性检查
# Run full environment check (CLI + tools + API)
bash scripts/check_env.sh
# Exit codes:
# 0 - All checks passed
# 1 - Missing dependencies or API errors
最佳实践
- 仅使用 Shell 脚本:所有脚本均使用 Shell + hcloud CLI,不依赖 Python SDK。
- 定期监控:使用
monitor_idle_eips.sh --setup-cron设置每日 cron 任务,以便尽早发现空闲 EIP - 成本报告:使用
eip_cost_report.sh --format html每周生成成本报告,跟踪优化进度 - 审计日志:使用
eip_audit_log.sh为所有 EIP 操作启用审计日志 - 多区域列表:
list_eips.sh支持使用逗号分隔的区域--region cn-north-4,cn-east-3跨区域列出资源。注意:analyze_idle_eips.sh、eip_cost_report.sh和monitor_idle_eips.sh每次只处理一个区域(多区域分析时,每个区域运行一次)。 - Webhook 告警:配置 Webhook,以实时接收空闲 EIP 通知
- 环境验证:始终先运行
check_env.sh,验证 hcloud CLI 和依赖项 - 空闲天数一致性:
analyze_idle_eips.sh和monitor_idle_eips.sh均使用--idle-days参数,并采用一致的时区处理方式
参考资料
| 文档 | 描述 |
|---|---|
| IAM 权限策略 | 所需权限和策略 JSON |
| EIP API 指南 | EIP API 参考(hcloud CLI) |
| CLI 安装指南 | hcloud CLI 安装、配置和故障排查 |
| 验证方法 | 逐步验证 |
| 验收标准 | 生产就绪验收测试 |
注意事项
- 成本估算仅供参考——基于 cn-north-4 按需定价(带宽计费模式:约 3 CNY/Mbps/月 + 0.02 CNY/小时的 IP 保留费)。实际成本可能因区域和计费模式而异。
- API 不会返回 charge_mode——脚本无法区分带宽计费和流量计费;所有估算均采用带宽计费模式。
- 此技能为只读——它会分析并报告闲置 EIP,但不会释放或删除任何资源。释放 EIP 需要在控制台中手动操作。
- EIP 释放后无法撤销——如果根据分析报告选择释放闲置 EIP,公网 IP 地址将被回收且无法恢复。
- 绝不能将 AK/SK 硬编码——只能通过环境变量(
HW_ACCESS_KEY、HW_SECRET_KEY)或hcloud configure交互模式(通过提示输入,而非命令行参数)获取凭证。 - 仅支持使用 hcloud CLI——所有脚本均原生使用 hcloud CLI(KooCLI)。
- 身份验证:优先使用环境变量(
HW_ACCESS_KEY、HW_SECRET_KEY)。如果已通过交互方式配置hcloud configure,该技能会检测并使用这些凭证。绝不要将凭证作为命令行参数传递。 - 支持临时凭证:此技能支持通过 IAM STS 获取的临时 AK/SK + Token。使用临时凭证时,请设置
HW_SECURITY_TOKEN。 - 环境变量规范:使用
HW_*前缀,以与其他华为云技能保持一致。 - 所有用于解析 hcloud CLI JSON 输出的脚本都需要 jq。
- 闲置天数的计算时区一致——分析和监控脚本均使用
date命令计算纪元时间,从而避免 UTC 时差问题。 - 审计日志时间戳会注明时区——格式为
YYYY-MM-DDTHH:MM:SS+HH:MM(例如+08:00),而非容易引起误解的Z后缀。
常见问题
| 问题 | 症状 | 快速解决方法 |
|---|---|---|
| 未安装 hcloud | command not found: hcloud | 安装 KooCLI |
| 未安装 jq | JSON 解析错误 | sudo apt install jq |
| 未安装 bc | 成本计算错误 | sudo apt install bc |
| 未设置 AK/SK | API 401 / 凭证错误 | 导出 HW_ACCESS_KEY/HW_SECRET_KEY,或以交互方式配置 hcloud |
| 区域错误 | ❌ API 返回异常 | 使用有效的区域 ID(例如 cn-north-4) |
| 格式无效 | ❌ 不支持的格式 | --format 使用 text/html/json |
| API 速率限制 | 429 Too Many Requests | 在调用之间增加延迟 |