文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
DescribeAnomalyDetectors
搭配 AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 DescribeAnomalyDetectors
。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- .NET
-
- AWS SDK for .NET
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /// <summary> /// Describe anomaly detectors for a metric and namespace. /// </summary> /// <param name="metricNamespace">The namespace of the metric.</param> /// <param name="metricName">The metric of the anomaly detectors.</param> /// <returns>The list of detectors.</returns> public async Task<List<AnomalyDetector>> DescribeAnomalyDetectors(string metricNamespace, string metricName) { List<AnomalyDetector> detectors = new List<AnomalyDetector>(); var paginatedDescribeAnomalyDetectors = _amazonCloudWatch.Paginators.DescribeAnomalyDetectors( new DescribeAnomalyDetectorsRequest() { MetricName = metricName, Namespace = metricNamespace }); await foreach (var data in paginatedDescribeAnomalyDetectors.AnomalyDetectors) { detectors.Add(data); } return detectors; }
-
如需 API 詳細資訊,請參閱AWS SDK for .NET API 參考中的 DescribeAnomalyDetectors。
-
- CLI
-
- AWS CLI
-
擷取異常偵測模型的清單
下列
describe-anomaly-detectors
範例顯示與指定帳戶中AWS/Logs
的命名空間相關聯的異常偵測器模型相關資訊。aws cloudwatch describe-anomaly-detectors \ --namespace
AWS/Logs
輸出:
{ "AnomalyDetectors": [ { "Namespace": "AWS/Logs", "MetricName": "IncomingBytes", "Dimensions": [], "Stat": "SampleCount", "Configuration": { "ExcludedTimeRanges": [] }, "StateValue": "TRAINED", "SingleMetricAnomalyDetector": { "AccountId": "123456789012", "Namespace": "AWS/Logs", "MetricName": "IncomingBytes", "Dimensions": [], "Stat": "SampleCount" } }, { "Namespace": "AWS/Logs", "MetricName": "IncomingBytes", "Dimensions": [ { "Name": "LogGroupName", "Value": "demo" } ], "Stat": "Average", "Configuration": { "ExcludedTimeRanges": [] }, "StateValue": "PENDING_TRAINING", "SingleMetricAnomalyDetector": { "AccountId": "123456789012", "Namespace": "AWS/Logs", "MetricName": "IncomingBytes", "Dimensions": [ { "Name": "LogGroupName", "Value": "demo" } ], "Stat": "Average" } } ] }
如需詳細資訊,請參閱《Amazon CloudWatch 使用者指南》中的使用 CloudWatch 異常偵測。 Amazon CloudWatch
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeAnomalyDetectors
。
-
- Java
-
- SDK for Java 2.x
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** * Describes the anomaly detectors based on the specified JSON file. * * @param fileName the name of the JSON file containing the custom metric namespace and name * @return a {@link CompletableFuture} that completes when the anomaly detectors have been described * @throws RuntimeException if there is a failure during the operation, such as when reading or parsing the JSON file, * or when describing the anomaly detectors */ public CompletableFuture<Void> describeAnomalyDetectorsAsync(String fileName) { CompletableFuture<JsonNode> readFileFuture = CompletableFuture.supplyAsync(() -> { try { JsonParser parser = new JsonFactory().createParser(new File(fileName)); return new ObjectMapper().readTree(parser); } catch (IOException e) { throw new RuntimeException("Failed to read or parse the file", e); } }); return readFileFuture.thenCompose(rootNode -> { try { String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText(); String customMetricName = rootNode.findValue("customMetricName").asText(); DescribeAnomalyDetectorsRequest detectorsRequest = DescribeAnomalyDetectorsRequest.builder() .maxResults(10) .metricName(customMetricName) .namespace(customMetricNamespace) .build(); return getAsyncClient().describeAnomalyDetectors(detectorsRequest).thenAccept(response -> { List<AnomalyDetector> anomalyDetectorList = response.anomalyDetectors(); for (AnomalyDetector detector : anomalyDetectorList) { logger.info("Metric name: {} ", detector.singleMetricAnomalyDetector().metricName()); logger.info("State: {} ", detector.stateValue()); } }); } catch (RuntimeException e) { throw new RuntimeException("Failed to describe anomaly detectors", e); } }).whenComplete((result, exception) -> { if (exception != null) { throw new RuntimeException("Error describing anomaly detectors", exception); } }); }
-
如需 API 詳細資訊,請參閱AWS SDK for Java 2.x API 參考中的 DescribeAnomalyDetectors。
-
- Kotlin
-
- SDK for Kotlin
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 suspend fun describeAnomalyDetectors(fileName: String) { // Read values from the JSON file. val parser = JsonFactory().createParser(File(fileName)) val rootNode = ObjectMapper().readTree<JsonNode>(parser) val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText() val customMetricName = rootNode.findValue("customMetricName").asText() val detectorsRequest = DescribeAnomalyDetectorsRequest { maxResults = 10 metricName = customMetricName namespace = customMetricNamespace } CloudWatchClient { region = "us-east-1" }.use { cwClient -> val response = cwClient.describeAnomalyDetectors(detectorsRequest) response.anomalyDetectors?.forEach { detector -> println("Metric name: ${detector.singleMetricAnomalyDetector?.metricName}") println("State: ${detector.stateValue}") } } }
-
如需 API 詳細資訊,請參閱《適用於 Kotlin 的AWS SDK API 參考》中的 DescribeAnomalyDetectors
。
-
DescribeAlarmsForMetric
DisableAlarmActions