Cloudflare から traceroute を実行する

# traceroute とは

[What is My Traceroute (MTR)? | Cloudflare](https://www.cloudflare.com/learning/network-layer/what-is-mtr)

# Cloudflare Traceroute API

Cloudflare とオリジンサーバーの間で問題がある可能性がある場合に、問題切り分けや情報収集ツールとして活用できます。

- Enterprise アカウントで利用できます。

> [Introducing Cloudflare traceroute, a tool that helps customers debug network issues](https://www.cloudflare.com/whats-new/#introducing-cloudflare-traceroute-a-tool-that-helps-customers-debug-network-issues)
> The traceroute tool is generally available to all Cloudflare ENT customers and is available via our API at the link below.
> https://api.cloudflare.com/#diagnostics-traceroute

# curl で実行

`colos` は [cloudflare-gcp/colos.json](https://github.com/cloudflare/cloudflare-gcp/blob/master/security-events/static/colos.json) を参考にしてください。

```shell
export EMAIL='YOUR_EMAIL'
export APIKEY='YOUR_APIKEY'
export ACCOUNT_ID='YOUR_ACCOUNT_ID'

curl -X POST \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $APIKEY" \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/diagnostics/traceroute" \
--data '{"targets":["google.co.jp"],"colos":["nrt01","kix01"],"options":{"packets_per_ttl":10,"packet_type":"icmp","max_ttl":5,"wait_time":1,"port":0}}'
```

```json:result.json
{
  "result": [
    {
      "target": "google.co.jp",
      "colos": [
        {
          "colo": {
            "name": "kix01",
            "city": "Osaka, JP"
          },
          "traceroute_time_ms": 155,
          "target_summary": {
            "asn": "",
            "ip": "142.250.206.227",
            "name": "google.co.jp",
            "packet_count": 0,
            "mean_rtt_ms": 0,
            "std_dev_rtt_ms": 0,
            "min_rtt_ms": 0,
            "max_rtt_ms": 0
          },
          "hops": [
            {
              "packets_ttl": 1,
              "packets_sent": 10,
              "packets_lost": 0,
              "nodes": [
                {
                  "asn": "",
                  "ip": "162.158.5.1",
                  "name": "_gateway",
                  "packet_count": 10,
                  "mean_rtt_ms": 1.0074,
                  "std_dev_rtt_ms": 0.41863885257927136,
                  "min_rtt_ms": 0.65,
                  "max_rtt_ms": 1.587
                }
              ]
            },
            {
              "packets_ttl": 2,
              "packets_sent": 10,
              "packets_lost": 0,
              "nodes": [
                {
                  "asn": "",
                  "ip": "162.158.4.2",
                  "name": "162.158.4.2",
                  "packet_count": 10,
                  "mean_rtt_ms": 1.2268000000000001,
                  "std_dev_rtt_ms": 0.44843327262815813,
                  "min_rtt_ms": 0.766,
                  "max_rtt_ms": 1.727
                }
              ]
            },
            {
              "packets_ttl": 3,
              "packets_sent": 10,
              "packets_lost": 0,
              "nodes": [
                {
                  "asn": "",
                  "ip": "142.250.162.128",
                  "name": "142.250.162.128",
                  "packet_count": 10,
                  "mean_rtt_ms": 1.2922,
                  "std_dev_rtt_ms": 0.05607891265390624,
                  "min_rtt_ms": 1.216,
                  "max_rtt_ms": 1.379
                }
              ]
            },
            {
              "packets_ttl": 4,
              "packets_sent": 10,
              "packets_lost": 0,
              "nodes": [
                {
                  "asn": "",
                  "ip": "142.250.239.93",
                  "name": "142.250.239.93",
                  "packet_count": 10,
                  "mean_rtt_ms": 0.20860000000000004,
                  "std_dev_rtt_ms": 0.037506147644234426,
                  "min_rtt_ms": 0.16,
                  "max_rtt_ms": 0.27
                }
              ]
            },
            {
              "packets_ttl": 5,
              "packets_sent": 10,
              "packets_lost": 0,
              "nodes": [
                {
                  "asn": "",
                  "ip": "142.250.58.193",
                  "name": "142.250.58.193",
                  "packet_count": 10,
                  "mean_rtt_ms": 0.1767,
                  "std_dev_rtt_ms": 0.02430843475010269,
                  "min_rtt_ms": 0.146,
                  "max_rtt_ms": 0.207
                }
              ]
            }
          ]
        },
        {
          "colo": {
            "name": "nrt01",
            "city": "Tokyo, JP"
          },
          "traceroute_time_ms": 1582,
          "target_summary": {
            "asn": "",
            "ip": "142.250.196.131",
            "name": "google.co.jp",
            "packet_count": 0,
            "mean_rtt_ms": 0,
            "std_dev_rtt_ms": 0,
            "min_rtt_ms": 0,
            "max_rtt_ms": 0
          },
          "hops": [
            {
              "packets_ttl": 1,
              "packets_sent": 10,
              "packets_lost": 10,
              "nodes": [
                {
                  "asn": "",
                  "ip": "",
                  "name": "NO RESPONSE",
                  "packet_count": 0,
                  "mean_rtt_ms": 0,
                  "std_dev_rtt_ms": 0,
                  "min_rtt_ms": 0,
                  "max_rtt_ms": 0
                }
              ]
            },
            {
              "packets_ttl": 2,
              "packets_sent": 10,
              "packets_lost": 0,
              "nodes": [
                {
                  "asn": "",
                  "ip": "103.22.201.37",
                  "name": "103.22.201.37",
                  "packet_count": 10,
                  "mean_rtt_ms": 18.5475,
                  "std_dev_rtt_ms": 5.805427427081585,
                  "min_rtt_ms": 10.679,
                  "max_rtt_ms": 24.214
                }
              ]
            },
            {
              "packets_ttl": 3,
              "packets_sent": 10,
              "packets_lost": 0,
              "nodes": [
                {
                  "asn": "",
                  "ip": "142.250.160.170",
                  "name": "142.250.160.170",
                  "packet_count": 10,
                  "mean_rtt_ms": 0.5307999999999999,
                  "std_dev_rtt_ms": 0.03322248234755092,
                  "min_rtt_ms": 0.5,
                  "max_rtt_ms": 0.591
                }
              ]
            },
            {
              "packets_ttl": 4,
              "packets_sent": 10,
              "packets_lost": 0,
              "nodes": [
                {
                  "asn": "",
                  "ip": "74.125.37.209",
                  "name": "74.125.37.209",
                  "packet_count": 10,
                  "mean_rtt_ms": 2.4455,
                  "std_dev_rtt_ms": 0.15879354171026253,
                  "min_rtt_ms": 2.205,
                  "max_rtt_ms": 2.638
                }
              ]
            },
            {
              "packets_ttl": 5,
              "packets_sent": 10,
              "packets_lost": 0,
              "nodes": [
                {
                  "asn": "",
                  "ip": "142.251.60.197",
                  "name": "142.251.60.197",
                  "packet_count": 10,
                  "mean_rtt_ms": 0.5263,
                  "std_dev_rtt_ms": 0.02790877998050076,
                  "min_rtt_ms": 0.489,
                  "max_rtt_ms": 0.576
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "success": true,
  "errors": [],
  "messages": []
}
```

# CLI ライクに実行

curl 実行だと少し見にくいので、[jacobbednarz/cf-traceroute](https://github.com/jacobbednarz/cf-traceroute/releases) をダウンロードして使います。

```shell
export EMAIL='YOUR_EMAIL'
export APIKEY='YOUR_APIKEY'
export ACCOUNT_ID='YOUR_ACCOUNT_ID'

./cf-traceroute \
-email=$EMAIL \
-api-key=$APIKEY \
-account-id=$ACCOUNT_ID \
-targets "google.co.jp" \
-colos "nrt01,kix01"
```

```shell:result.txt
google.co.jp
  KIX01
    1	_gateway (162.158.5.1 - )		1.55ms
    2	162.158.4.4 (162.158.4.4 - )		0.97ms
    3	142.250.47.156 (142.250.47.156 - )	1.10ms
    4	142.250.229.5 (142.250.229.5 - )	0.24ms
    5	64.233.175.247 (64.233.175.247 - )	1.34ms
  NRT01
    1	NO RESPONSE ( - )			0.00ms
    2	103.22.201.37 (103.22.201.37 - )	1.20ms
    3	142.250.160.170 (142.250.160.170 - )	0.58ms
    4	108.170.226.179 (108.170.226.179 - )	0.60ms
    5	142.251.60.193 (142.251.60.193 - )	0.56ms
```

# 他の便利ツール

一般的に世界中から同様の確認をしたい場合は、以下のようなツールも便利です。 

- Ping, mtr, dig and TCP port check from multiple locations
  - https://ping.pe/
- MTR.sh Looking Glass
  - https://mtr.sh/
- Test Your Website Speed From Around The World | PingBear
  - https://pingbear.com/