Created
April 29, 2026 07:47
-
-
Save ensean/d380b00bc3baf213ba3ebaac723c24ec to your computer and use it in GitHub Desktop.
query-hostzone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # 批量查询所有 HostedZone 的 DNSQueries 指标 | |
| # 绕过 Metrics Insights 500 条时间序列限制 | |
| set -e | |
| PROFILE="default" | |
| REGION="us-east-1" | |
| PERIOD=3600 | |
| START_TIME=$(date -u -v-1d +%Y-%m-%dT%H:%M:%SZ) | |
| END_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ) | |
| BATCH_SIZE=500 | |
| TMP_DIR=$(mktemp -d) | |
| echo "时间范围: $START_TIME ~ $END_TIME" | |
| echo "临时目录: $TMP_DIR" | |
| # 1. 获取所有 HostedZone ID | |
| echo "正在获取所有 HostedZone..." | |
| ZONE_IDS=$(aws route53 list-hosted-zones --profile "$PROFILE" --output json | \ | |
| jq -r '.HostedZones[].Id | sub("/hostedzone/";"")') | |
| TOTAL=$(echo "$ZONE_IDS" | wc -l | tr -d ' ') | |
| echo "共 $TOTAL 个 HostedZone" | |
| # 2. 构造所有 MetricDataQueries | |
| echo "$ZONE_IDS" | jq -R -s 'split("\n") | map(select(length > 0)) | to_entries | map({ | |
| Id: ("q" + (.key | tostring)), | |
| MetricStat: { | |
| Metric: { | |
| Namespace: "AWS/Route53", | |
| MetricName: "DNSQueries", | |
| Dimensions: [{Name: "HostedZoneId", Value: .value}] | |
| }, | |
| Period: '"$PERIOD"', | |
| Stat: "Sum" | |
| } | |
| })' > "$TMP_DIR/all_queries.json" | |
| # 3. 分批查询 | |
| BATCH=0 | |
| OFFSET=0 | |
| while [ $OFFSET -lt $TOTAL ]; do | |
| BATCH=$((BATCH + 1)) | |
| END=$((OFFSET + BATCH_SIZE)) | |
| echo "查询批次 $BATCH: zone $((OFFSET+1)) ~ $([ $END -lt $TOTAL ] && echo $END || echo $TOTAL)" | |
| jq ".[$OFFSET:$END]" "$TMP_DIR/all_queries.json" > "$TMP_DIR/batch_${BATCH}.json" | |
| aws cloudwatch get-metric-data \ | |
| --profile "$PROFILE" \ | |
| --region "$REGION" \ | |
| --start-time "$START_TIME" \ | |
| --end-time "$END_TIME" \ | |
| --metric-data-queries "file://$TMP_DIR/batch_${BATCH}.json" \ | |
| --output json > "$TMP_DIR/result_${BATCH}.json" | |
| OFFSET=$END | |
| done | |
| # 4. 汇总结果 | |
| echo "" | |
| echo "=== DNSQueries 汇总 ===" | |
| for f in "$TMP_DIR"/result_*.json; do | |
| jq -r '.MetricDataResults[] | select(.Values | length > 0) | "\(.Label)\t\(.Values | add | floor)"' "$f" | |
| done | sort -t$'\t' -k2 -rn | column -t -s$'\t' | |
| echo "" | |
| echo "详细结果保存在: $TMP_DIR/result_*.json" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment