查看正常运行时间检查

本页面介绍如何执行以下操作:

  • 列出所有正常运行时间检查。
  • 获取正常运行时间检查使用的 IP 地址列表。

要创建、更新和删除正常运行时间检查,请参阅管理正常运行时间检查页面。要查看正常运行时间检查的定价和限制,请参阅定价和限制

列出正常运行时间检查

控制台

要列出您当前的正常运行时间检查,请执行以下操作:

  1. 在 Cloud Console 中,选择 Monitoring

    转到“监控”

  2. 选择正常运行时间检查

要查看正常运行时间检查的详细信息,请点击其名称。

要限制列出的正常运行时间检查,请添加过滤条件。每个过滤条件都由名称和值组成。您可以将该值设置为与正常运行时间检查名称完全匹配或部分匹配。匹配项不区分大小写。例如,如需列出名称包含 default 的所有正常运行时间检查,请执行以下操作:

  • 点击过滤表格,然后选择显示名称
  • 输入 default,然后点击回车键。

如果您有多个过滤条件,则除非您插入 OR 过滤条件,否则这些过滤条件会由逻辑 AND 自动合并。在以下示例中,使用 OR 过滤条件,以便在名称与 defaulttesting check 匹配时列出正常运行时间检查:

包含过滤条件的正常运行时间检查概览示例

API

要获取正常运行时间配置列表,请调用 projects.uptimeCheckConfigs.list 方法。指定以下参数:

  • parent:您想要列出其正常运行时间检查的项目。格式为:

    projects/[PROJECT_ID]
        

要获取特定的正常运行时间检查,请调用 projects.uptimeCheckConfigs.get 方法。指定以下参数:

  • name:正常运行时间检查配置的完整名称。

    projects/[PROJECT_ID]/uptimeCheckConfigs/[UPTIME_CHECK_ID]
        

    您可以从 createlist 方法的响应中获取 [UPTIME_CHECK_ID]。该 ID 未显示在 Google Cloud Console 中。

C#

public static object ListUptimeCheckConfigs(string projectId)
    {
        var client = UptimeCheckServiceClient.Create();
        var configs = client.ListUptimeCheckConfigs(
            new ProjectName(projectId).ToString());
        foreach (UptimeCheckConfig config in configs)
        {
            Console.WriteLine(config.Name);
        }
        return 0;
    }

Java

private static void listUptimeChecks(String projectId) throws IOException {
      ListUptimeCheckConfigsRequest request =
          ListUptimeCheckConfigsRequest.newBuilder().setParent(ProjectName.format(projectId)).build();
      try (UptimeCheckServiceClient client = UptimeCheckServiceClient.create()) {
        ListUptimeCheckConfigsPagedResponse response = client.listUptimeCheckConfigs(request);
        for (UptimeCheckConfig config : response.iterateAll()) {
          System.out.println(config.getDisplayName());
        }
      } catch (Exception e) {
        usage("Exception listing uptime checks: " + e.toString());
        throw e;
      }
    }

Go


    // list is an example of listing the uptime checks in projectID.
    func list(w io.Writer, projectID string) error {
    	ctx := context.Background()
    	client, err := monitoring.NewUptimeCheckClient(ctx)
    	if err != nil {
    		return fmt.Errorf("NewUptimeCheckClient: %v", err)
    	}
    	defer client.Close()
    	req := &monitoringpb.ListUptimeCheckConfigsRequest{
    		Parent: "projects/" + projectID,
    	}
    	it := client.ListUptimeCheckConfigs(ctx, req)
    	for {
    		config, err := it.Next()
    		if err == iterator.Done {
    			break
    		}
    		if err != nil {
    			return fmt.Errorf("ListUptimeCheckConfigs: %v", err)
    		}
    		fmt.Fprintln(w, config)
    	}
    	fmt.Fprintln(w, "Done listing uptime checks")
    	return nil
    }
    

Node.js

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

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

    /**
     * TODO(developer): Uncomment and edit the following lines of code.
     */
    // const projectId = 'YOUR_PROJECT_ID';

    const request = {
      parent: client.projectPath(projectId),
    };

    // Retrieves an uptime check config
    const [uptimeCheckConfigs] = await client.listUptimeCheckConfigs(request);

    uptimeCheckConfigs.forEach(uptimeCheckConfig => {
      console.log(`ID: ${uptimeCheckConfig.name}`);
      console.log(`  Display Name: ${uptimeCheckConfig.displayName}`);
      console.log('  Resource: %j', uptimeCheckConfig.monitoredResource);
      console.log('  Period: %j', uptimeCheckConfig.period);
      console.log('  Timeout: %j', uptimeCheckConfig.timeout);
      console.log(`  Check type: ${uptimeCheckConfig.check_request_type}`);
      console.log(
        '  Check: %j',
        uptimeCheckConfig.httpCheck || uptimeCheckConfig.tcpCheck
      );
      console.log(
        `  Content matchers: ${uptimeCheckConfig.contentMatchers
          .map(matcher => matcher.content)
          .join(', ')}`
      );
      console.log(`  Regions: ${uptimeCheckConfig.selectedRegions.join(', ')}`);
    });
    

PHP

use Google\Cloud\Monitoring\V3\UptimeCheckServiceClient;

    /**
     * Example:
     * ```
     * list_uptime_checks($projectId);
     * ```
     */
    function list_uptime_checks($projectId)
    {
        $uptimeCheckClient = new UptimeCheckServiceClient([
            'projectId' => $projectId,
        ]);

        $pages = $uptimeCheckClient->listUptimeCheckConfigs(
            $uptimeCheckClient->projectName($projectId)
        );

        foreach ($pages->iteratePages() as $page) {
            foreach ($page as $uptimeCheck) {
                print($uptimeCheck->getName() . PHP_EOL);
            }
        }
    }

Python

def list_uptime_check_configs(project_name):
        client = monitoring_v3.UptimeCheckServiceClient()
        configs = client.list_uptime_check_configs(project_name)

        for config in configs:
            pprint.pprint(config)

Ruby

def list_uptime_check_configs project_id
      require "google/cloud/monitoring/v3"

      client = Google::Cloud::Monitoring::V3::UptimeCheck.new
      project_name = Google::Cloud::Monitoring::V3::UptimeCheckServiceClient.project_path project_id
      configs = client.list_uptime_check_configs project_name

      configs.each { |config| puts config.name }
    end

获取正常运行时间检查 IP 地址

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

控制台

  1. 在 Cloud Console 中,选择 Monitoring

    转到“监控”

  2. 选择正常运行时间检查

  3. 点击已列入白名单的检查工具 IP 地址

API

  1. 调用 Monitoring API 的 uptimeCheckIps.list 方法。

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

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

C#

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

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


    // 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: %v", 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: %v", err)
    		}
    		fmt.Fprintln(w, config)
    	}
    	fmt.Fprintln(w, "Done listing uptime check IPs")
    	return nil
    }
    

Node.js

// 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

use Google\Cloud\Monitoring\V3\UptimeCheckServiceClient;

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

        $pages = $uptimeCheckClient->listUptimeCheckIps();

        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

def list_uptime_check_ips():
        client = monitoring_v3.UptimeCheckServiceClient()
        ips = client.list_uptime_check_ips()
        print(tabulate.tabulate(
            [(ip.region, ip.location, ip.ip_address) for ip in ips],
            ('region', 'location', 'ip_address')
        ))

Ruby

def list_ips
      require "google/cloud/monitoring/v3"
      client = Google::Cloud::Monitoring::V3::UptimeCheck.new

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

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

正常运行时间检查所使用的 IP 地址可能会改变,但通常情况下每一刻钟改变不会超过一次,并且会给出通知。

识别正常运行时间检查流量

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

  • ipip 字段包含正常运行时间检查服务器使用的地址之一。请参阅获取 IP 地址
  • 用户代理User-Agent 标头值始终如下所示:

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

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