演示如何列出正常运行时间检查配置。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
public static object ListUptimeCheckConfigs(string projectId)
{
var client = UptimeCheckServiceClient.Create();
var configs = client.ListUptimeCheckConfigs(new ProjectName(projectId));
foreach (UptimeCheckConfig config in configs)
{
Console.WriteLine(config.Name);
}
return 0;
}
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
}
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;
}
}
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(request={"parent": project_name})
for config in configs:
pprint.pprint(config)
Ruby
def list_uptime_check_configs project_id
require "google/cloud/monitoring"
client = Google::Cloud::Monitoring.uptime_check_service
project_name = client.project_path project: project_id
configs = client.list_uptime_check_configs parent: project_name
configs.each { |config| puts config.name }
end
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。