列出拨测服务器 IP 地址

本文档介绍如何获取拨测服务器使用的 IP 地址列表,以及如何识别日志中来自拨测服务器的流量。

列出 IP 地址

如果您要检查受防火墙保护的服务,则可以对服务防火墙进行配置,令其接受来自用于拨测的当前一组 IP 地址的流量。要获取这些 IP 地址,请使用以下说明:

控制台

  1. 在 Google Cloud 控制台的导航面板中,选择 Monitoring,然后选择  拨测

    前往拨测

  2. 拨测菜单中,点击 下载。系统将下载 uptime-source-ips.txt 文件,其中包含 IP 地址。

gcloud

运行 gcloud monitoring uptime list-ips 命令:

gcloud monitoring uptime list-ips

该方法将为每个 IP 地址返回以下信息:

  • IPv4 或 IPv6 格式的 IP 地址,而不是范围。
  • 区域:USAEUROPESOUTH_AMERICAASIA_PACIFIC
  • 区域内的位置。

API

调用 Monitoring API 的 uptimeCheckIps.list 方法。

该方法将为每个 IP 地址返回以下信息:

  • 区域:USAEUROPESOUTH_AMERICAASIA_PACIFIC
  • 区域内更具体的位置。
  • IPv4 或 IPv6 格式的 IP 地址,而非范围。

C#

如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

public static object ListUptimeCheckIps()
{
    var client = UptimeCheckServiceClient.Create();
    var ips = client.ListUptimeCheckIps(new ListUptimeCheckIpsRequest());
    foreach (UptimeCheckIp ip in ips)
    {
        Console.WriteLine("{0,20} {1}", ip.IpAddress, ip.Location);
    }
    return 0;
}

Java

如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

private static void listUptimeCheckIps() throws IOException {
  try (UptimeCheckServiceClient client = UptimeCheckServiceClient.create()) {
    ListUptimeCheckIpsPagedResponse response =
        client.listUptimeCheckIps(ListUptimeCheckIpsRequest.newBuilder().build());
    for (UptimeCheckIp config : response.iterateAll()) {
      System.out.println(config.getRegion() + " - " + config.getIpAddress());
    }
  } catch (Exception e) {
    usage("Exception listing uptime IPs: " + e.toString());
    throw e;
  }
}

Go

如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


// listIPs is an example of listing uptime check IPs.
func listIPs(w io.Writer) error {
	ctx := context.Background()
	client, err := monitoring.NewUptimeCheckClient(ctx)
	if err != nil {
		return fmt.Errorf("NewUptimeCheckClient: %w", err)
	}
	defer client.Close()
	req := &monitoringpb.ListUptimeCheckIpsRequest{}
	it := client.ListUptimeCheckIps(ctx, req)
	for {
		config, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("ListUptimeCheckIps: %w", err)
		}
		fmt.Fprintln(w, config)
	}
	fmt.Fprintln(w, "Done listing uptime check IPs")
	return nil
}

Node.js

如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');

// Creates a client
const client = new monitoring.UptimeCheckServiceClient();

// List uptime check IPs
const [uptimeCheckIps] = await client.listUptimeCheckIps();
uptimeCheckIps.forEach(uptimeCheckIp => {
  console.log(
    uptimeCheckIp.region,
    uptimeCheckIp.location,
    uptimeCheckIp.ipAddress
  );
});

PHP

如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

use Google\Cloud\Monitoring\V3\Client\UptimeCheckServiceClient;
use Google\Cloud\Monitoring\V3\ListUptimeCheckIpsRequest;

/**
 * Example:
 * ```
 * list_uptime_check_ips($projectId);
 * ```
 */
function list_uptime_check_ips(string $projectId): void
{
    $uptimeCheckClient = new UptimeCheckServiceClient([
        'projectId' => $projectId,
    ]);
    $listUptimeCheckIpsRequest = new ListUptimeCheckIpsRequest();

    $pages = $uptimeCheckClient->listUptimeCheckIps($listUptimeCheckIpsRequest);

    foreach ($pages->iteratePages() as $page) {
        $ips = $page->getResponseObject()->getUptimeCheckIps();
        foreach ($ips as $ip) {
            printf(
                'ip address: %s, region: %s, location: %s' . PHP_EOL,
                $ip->getIpAddress(),
                $ip->getRegion(),
                $ip->getLocation()
            );
        }
    }
}

Python

如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

def list_uptime_check_ips() -> pagers.ListUptimeCheckIpsPager:
    """Gets all locations and IP addresses used by uptime check servers

    Returns:
        A list of locations and IP addresses of uptime check servers.
        Iterating over this object will yield results and resolve additional pages automatically.
    """
    client = monitoring_v3.UptimeCheckServiceClient()
    ips = client.list_uptime_check_ips(request={})
    print(
        tabulate.tabulate(
            [(ip.region, ip.location, ip.ip_address) for ip in ips],
            ("region", "location", "ip_address"),
        )
    )
    return ips

Ruby

如需向 Monitoring 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

def list_ips
  require "google/cloud/monitoring"
  client = Google::Cloud::Monitoring.uptime_check_service

  # Iterate over all results.
  client.list_uptime_check_ips({}).each do |element|
    puts "#{element.location} #{element.ip_address}"
  end
end

正常运行时间检查可能来自其中的任一个 IP 地址,但对于每个时间间隔,每个地理位置上仅会用到一个地址。拨测信息中心列出了地理位置(如上一部分中所示)。您还可以使用基于网络的免费服务来确定您下载的 IP 地址的注册位置。

确定日志中的拨测流量

要识别来自正常运行时间检查服务器的请求,您可以使用您的服务的请求日志中的以下信息:

  • ipip 字段包含正常运行时间检查服务器使用的一个地址。如需了解如何列出所有 IP 地址,请参阅列出 IP 地址
  • 用户代理User-Agent 标头值始终如下所示:

    GoogleStackdriverMonitoring-UptimeChecks(https://cloud.google.com/monitoring)
    

    指定 User-Agent 自定义标头会导致表单验证错误,以及无法保存检查配置。

后续步骤