List uptime check IP addresses

Demonstrates how to list uptime check IP addresses.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C#

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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;
}

Go

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


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

Java

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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;
  }
}

Node.js

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To authenticate to Monitoring, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.