View a markdown version of this page

在 Amazon OpenSearch 无服务器上配置异常检测 - 亚马逊 OpenSearch 服务
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在 Amazon OpenSearch 无服务器上配置异常检测

异常检测使用随机剪切森林 (RCF) 算法,近乎实时地自动检测 OpenSearch 数据中的异常。RCF 是一种无监督的机器学习算法,可对传入数据流的草图进行建模。对于每个传入的数据点,RCF 会计算 a anomaly grade 和 a。confidence score有关异常检测的概念性概述,请参阅网站上的异常检测。 OpenSearch

无服务器异常检测使用 Amazon OpenSearch Serverless 集合作为检测器的数据源。您可以通过 OpenSearch UI 创建和管理探测器。每个探测器都是您在其中创建它的 OpenSearch UI 工作区的资源。本页介绍无服务器集合特有的先决条件、权限和注意事项。它还记录了您用来以编程方式管理探测器的工作空间范围的 API 操作。

重要

探测器是 OpenSearch UI 工作区资源。因此,无服务器集合的异常检测 API 操作与下的开源异常检测 API 操作不同。_plugins/_anomaly_detection使用中的工作空间范围的操作。无服务器集合的异常检测 API 操作

先决条件

为异常检测选择采集类型

我们建议您将异常检测与时间序列集合一起使用。无服务器集合上的检测器监视单个索引。时间序列集合会自动管理该索引的数据保留期,因此您不必构建自定义保留流程。

请注意以下事项:

  • 每个探测器对应一个索引 — 探测器监视单个索引。不支持索引模式、别名和以逗号分隔的多个索引列表。如果您需要检测多个索引的异常,请为每个索引创建一个单独的检测器。

  • 自动数据保留-时间序列集合为您管理受监控索引中数据的生命周期。这与单一索引要求非常吻合,因为否则单个长寿命索引将需要您自己管理留存率。

  • 搜索集合-搜索集合也被接受为探测器数据源,但它们不为您管理数据保留。

有关集合类型的更多信息,请参阅选择集合类型。

配置异常检测权限

探测器作为后台作业对你的馆藏运行。以下集合数据访问策略授予异常检测所需的权限。placeholder values用您的具体信息替换。ARN 分区取决于。 Amazon Web Services 区域标准 Amazon Web Services 区域 使用aws,中国地区aws-cn使用。有关更多信息,请参阅 受支持的策略权限。

{ "Rules": [ { "Resource": [ "collection/collection_name" ], "Permission": [ "aoss:CreateCollectionItems", "aoss:DeleteCollectionItems", "aoss:UpdateCollectionItems", "aoss:DescribeCollectionItems" ], "ResourceType": "collection" }, { "Resource": [ "index/collection_name/*" ], "Permission": [ "aoss:CreateIndex", "aoss:DeleteIndex", "aoss:UpdateIndex", "aoss:DescribeIndex", "aoss:ReadDocument", "aoss:WriteDocument", "aoss:DelegateAccess" ], "ResourceType": "index" } ], "Principal": [ "arn:aws:iam::account_id:role/role_name" ], "Description": "Anomaly detection access policy for collection_name" }
  • 集合权限 (aoss:CreateCollectionItemsaoss:DeleteCollectionItems、、aoss:UpdateCollectionItems、aoss:DescribeCollectionItems)-允许异常检测作业解析集合并对其进行操作。

  • 索引权限 (aoss:CreateIndex、aoss:DeleteIndex、aoss:UpdateIndex、aoss:DescribeIndex、aoss:ReadDocument、aoss:WriteDocument)-允许检测器读取受监控的索引。它们还允许检测器创建自定义结果索引并将结果写入其中。

  • 委托权限 (aoss:DelegateAccess)-允许异常检测后台作业代表您访问集合。

重要

您可以将索引资源的范围限定为特定的索引,而不是index/collection_name/*。在这种情况下,请同时涵盖监控索引和自定义结果索引。

在 OpenSearch 用户界面工作区中,异常检测功能的访问权限遵循工作区协作者权限(只读或管理员)。 read/write Read-only协作者可以查看探测器和结果,管理员协作者可以创建、更新、启动、停止和删除探测器。 read/write

自定义结果索引

无服务器集合上的检测器必须将其结果写入自定义结果索引。默认.opendistro-anomaly-results*索引在无服务器集合上不可用。

创建探测器时,在resultIndex字段中指定自定义结果索引。索引名称必须以开头opensearch-ad-plugin-result-。

"resultIndex": "opensearch-ad-plugin-result-my-detector"
重要

OpenSearch Serverless 不支持异常检测在已配置域上提供的结果索引生命周期设置,例如、resultIndexMinAgeresultIndexMinSize、resultIndexTtl和。flattenCustomResultIndex您负责管理和滚动结果索引。配置涵盖结果索引的数据生命周期策略。有关更多信息,请参阅 在 Amazon OpenSearch Serverless 中使用数据生命周期策略。

每个探测器都会写入自定义结果索引。确保中的数据访问策略配置异常检测权限涵盖结果索引和受监控的索引。

异常时发出警报

要在检测器发现异常时收到通知,请创建一个查询探测器自定义结果索引的警报监视器。探测器将其结果写入您集合中的常规索引。因此,您可以使用每个查询或每个存储桶的标准监视器,并启用anomaly_grade和触发器confidence。

完成以下步骤:

  1. 为您的收藏设置提醒先决条件和权限。

  2. 在 OpenSearch 用户界面中根据探测器的自定义结果索引创建监视器 (opensearch-ad-plugin-result-my-detector)。有关用于异常结果的触发条件的指导,请参阅网站上的管理异常检测。 OpenSearch

  3. 向触发器添加通知渠道,以便警报到达您选择的目的地。

注意

开源警报插件中的异常检测器监视器类型在无服务器集合中不可用,因为它从默认的异常结果索引中读取。改为查询自定义结果索引。

无服务器集合的异常检测 API 操作

无服务器集合支持网站上开源异常检测 API 中的所有操作。 OpenSearch 但是,你可以用工作空间风格的路径来称呼它们。探测器是 OpenSearch UI 工作空间资源,因此您可以向 OpenSearch UI 应用程序端点而不是集合端点发送请求。每条路径都包含一个工作空间,用于标识代表您的集合的数据源。

使用 SigV4-signing HTTP 客户端(例如awscurl在 GitHub网站上)来调用操作。以下各节描述了请求格式,列出了操作路径,并显示了一些代表性示例。

请求格式

请求使用以下格式:

https://application_endpoint/w/workspace_id/api/anomaly_detectors/operation_path

下表描述了请求的每个部分。

组件 说明

application_endpoint

您的 OpenSearch UI 应用程序的端点,格式为application-application_name-application_id.region.opensearch.amazonaws.com。

workspace_id

包含探测器的 OpenSearch 用户界面工作区的 ID。你可以在 OpenSearch 界面控制台的工作区 URL /w/ 部分中找到它。

data_source_id

工作空间中代表您的集合的数据源的 ID。根据操作的不同,您可以将此值作为路径的最后一段或作为dataSourceId查询参数传递。请参阅操作路径。

detector_id

探测器的 ID,在您创建或搜索探测器时在id字段中返回。

使用签名版本 4 (Sigv4) 对每个请求进行签名,使用opensearch作为服务名称。有关更多信息,请参阅 与其他客户端签署 HTTP 请求。本节中的示例在awscurl网站上使用,该 GitHub 网站使用您的 Amazon 个人资料中的凭据对请求进行签名。在每个请求中包含以下标头:

  • Content-Type: application/json— 发送正文的请求必填项。

  • osd-xsrf: osd-fetch— 必需的跨站请求伪造标头。

  • osd-version: 3.6.0— 您的 OpenSearch 用户界面应用程序的 OpenSearch 版本。

每个响应都使用信封。返回成功的响应{"ok": true, "response": {...}},返回失败的响应{"ok": false, "error": "message"}。

操作路径

下表列出了异常检测操作及其工作空间式路径。所有路径都是相对于的https://application_endpoint/w/workspace_id/api/anomaly_detectors。

操作 方法和路径

创建探测器

POST /detectors/data_source_id

去找个探测器

GET /detectors/detector_id/data_source_id

更新探测器

PUT /detectors/detector_id/data_source_id

删除探测器

DELETE /detectors/detector_id/data_source_id

验证探测器

POST /detectors/_validate/validation_type/data_source_id

预览探测器

POST /detectors/preview/data_source_id

建议配置值

POST /detectors/_suggest/suggest_type/data_source_id

启动探测器

POST /detectors/detector_id/start/data_source_id

停止探测器

POST /detectors/detector_id/stop/is_historical/data_source_id

对探测器进行性能分析

GET /detectors/detector_id/_profile/data_source_id

计数探测器

GET /detectors/_count/data_source_id

列出探测器

GET /detectors/_list?dataSourceId=data_source_id

匹配探测器名称

GET /detectors/detector_name/_match?dataSourceId=data_source_id

搜索探测器

POST /detectors/_search?dataSourceId=data_source_id

搜索探测器任务

POST /detectors/tasks/_search/data_source_id

搜索异常结果

POST /detectors/results/_search/data_source_id

获取探测器的结果

GET /detectors/detector_id/results/is_historical?dataSourceId=data_source_id

获取最常见的异常情况

POST /detectors/detector_id/_topAnomalies/is_historical?dataSourceId=data_source_id

获取统计数据

GET /stats/stats/data_source_id

请注意以下有关路径参数的信息:

  • is_historicalfalse用于实时检测或true历史分析。

  • validation_typedetector就是验证配置本身。还model用于检查配置是否有可能根据您的数据生成可用的模型。

  • stats是以逗号分隔的统计信息列表,例如。ad_execute_request_count,ad_execute_failure_count,anomaly_results_index_status

有关每个操作的请求和响应正文,请参阅 OpenSearch 网站上的异常检测 API。

注意

在更新或删除探测器之前,必须将其停止。启动探测器后,可能需要几分钟才能显示异常结果和任务记录。

示例:创建探测器

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0" \ -d '{ "name": "deny-anomalies", "description": "Detects anomalies in denied requests", "indices": ["server-metrics"], "resultIndex": "opensearch-ad-plugin-result-deny-anomalies", "filterQuery": {"match_all": {}}, "timeField": "@timestamp", "featureAttributes": [ { "featureName": "deny_sum", "featureEnabled": true, "importance": 1, "aggregationQuery": {"deny_sum": {"sum": {"field": "deny"}}} } ], "detectionInterval": {"period": {"interval": 2, "unit": "Minutes"}}, "windowDelay": {"period": {"interval": 1, "unit": "Minutes"}}, "shingleSize": 8, "history": 1000 }'

响应返回id字段中的探测器 ID。

{"ok": true, "response": {"id": "2d67c887-9420-4078-8b2c-fb5ce4830487", "name": "deny-anomalies"}}

请注意以下特定于无服务器的要求:

示例:启动和停止探测器

发送不带正文的启动请求以开始实时检测。探测器在每个检测间隔评估新数据。

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/detector_id/start/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"
{"ok": true, "response": {"_id": "2d67c887-9420-4078-8b2c-fb5ce4830487"}}

要改为对过去的时间范围运行历史分析,请在请求正文endTime中以纪元毫秒为单位包含startTime和。响应返回历史分析任务的 ID。

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/detector_id/start/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0" \ -d '{"startTime": 1778363544000, "endTime": 1781041944000}'
{"ok": true, "response": {"_id": "e3d68a89-d50f-4d9f-8284-21865795f886"}}

要停止实时检测,请发送is_historical设置为的停止请求false。true用于停止历史分析。

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/detector_id/stop/false/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"

示例:对探测器进行性能分析

性能分析返回检测器的运行时状态,例如检测器是在初始化还是在运行,以及模型初始化的进展情况。

awscurl --service opensearch --region us-west-2 \ -X GET \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/detector_id/_profile/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"
{"ok": true, "response": {"state": "RUNNING", "error": "", "init_progress": {"percentage": "100%"}}}

使用type查询参数以逗号分隔的列表形式请求特定的配置文件类型。以下示例请求高基数检测器的实体计数。

awscurl --service opensearch --region us-west-2 \ -X GET \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/detector_id/_profile/data_source_id?type=total_entities,active_entities' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"
{"ok": true, "response": {"total_entities": 10000, "active_entities": 28721}}

示例:列出、搜索和计数探测器

列表操作返回工作区中的探测器及其异常计数,这是 OpenSearch UI 检测器列表使用的视图。

awscurl --service opensearch --region us-west-2 \ -X GET \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/_list?dataSourceId=data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"

搜索操作对探测器配置进行查询。

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/_search?dataSourceId=data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0" \ -d '{"query": {"match_all": {}}, "size": 1}'

以下示例显示了被截断的搜索响应。

{ "ok": true, "response": { "totalDetectors": 11, "detectors": [ { "id": "bf1e824f-2867-4a2d-8c6a-5388e5452061", "name": "deny-anomalies", "timeField": "@timestamp", "indices": ["server-metrics"], "resultIndex": "opensearch-ad-plugin-result-deny-anomalies", "detectionInterval": {"period": {"unit": "Minutes", "interval": 2}}, "windowDelay": {"period": {"unit": "Minutes", "interval": 1}}, "shingleSize": 8, "detectorType": "SINGLE_ENTITY", "history": 1000, "dataSourceId": "ef581a74-e4e6-3fab-a5b4-9bffd8d4af64", "lastUpdateTime": 1781030925933, "filterQuery": {"match_all": {"boost": 1}}, "featureAttributes": [ { "featureId": "QQe3rZ4BN4hgnuM8MSjG", "featureEnabled": true, "featureName": "deny_sum", "aggregationQuery": {"deny_sum": {"sum": {"field": "deny"}}} } ], "enabled": false } ] } }

计数操作返回工作区中探测器的数量。使用它可以根据检测器配额检查您的使用情况。

awscurl --service opensearch --region us-west-2 \ -X GET \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/_count/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"
{"ok": true, "response": {"count": 303}}

示例:搜索异常结果和任务

以下示例查询检测器写入其自定义结果索引的异常结果。

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/results/_search/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0" \ -d '{ "size": 5, "sort": [{"data_end_time": {"order": "desc"}}], "query": { "bool": { "filter": [ {"term": {"detector_id": "detector_id"}}, {"range": {"anomaly_grade": {"gt": 0}}} ] } } }'

探测器任务跟踪个人的实时运行和历史运行情况。任务文档包括任务状态、进度、执行时间和任务运行时使用的检测器配置。

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/tasks/_search/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0" \ -d '{"query": {"match_all": {}}, "size": 1}'

以下示例显示了被截断的响应。

{ "ok": true, "response": { "hits": { "total": {"value": 26, "relation": "eq"}, "hits": [ { "_index": "opendistro-anomaly-detection-state", "_id": "916eed3b-8785-4cf0-8cb4-ab7191d2845e", "_source": { "detector_id": "c6eb3d92-371c-4e1b-8480-09dad082f3a0", "task_type": "HISTORICAL_HC_ENTITY", "state": "FINISHED", "task_progress": 1, "init_progress": 1, "is_latest": true, "detection_date_range": { "start_time": 1778426808849, "end_time": 1781018808849 }, "execution_start_time": 1781018874002, "execution_end_time": 1781018913066, "entity": [{"name": "service", "value": "app_3"}] } } ] }, "took": 12, "timed_out": false } }

示例:预览探测器

Preview 会在过去的时间范围内运行探测器配置,并在不创建探测器的情况下返回样本异常结果。预览请求正文使用 camelCase 字段名称,并将探测器配置嵌套在字段下。detector

awscurl --service opensearch --region us-west-2 \ -X POST \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/preview/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0" \ -d '{ "periodStart": 1778363544000, "periodEnd": 1781041944000, "detector": { "name": "preview-test", "description": "preview", "indices": ["server-metrics"], "resultIndex": "opensearch-ad-plugin-result-preview", "filterQuery": {"match_all": {}}, "timeField": "@timestamp", "featureAttributes": [ { "featureName": "deny_sum", "featureEnabled": true, "importance": 1, "aggregationQuery": {"deny_sum": {"sum": {"field": "deny"}}} } ], "detectionInterval": {"period": {"interval": 2, "unit": "Minutes"}}, "windowDelay": {"period": {"interval": 1, "unit": "Minutes"}}, "shingleSize": 8 } }'

示例:获取异常检测统计信息

awscurl --service opensearch --region us-west-2 \ -X GET \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/stats/ad_execute_request_count,ad_execute_failure_count,anomaly_results_index_status,anomaly_detectors_index_status,models_checkpoint_index_status/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"
{ "ok": true, "response": { "anomaly_detectors_index_status": "green", "models_checkpoint_index_status": "green", "anomaly_results_index_status": "green", "nodes": { "node-1": {"ad_execute_request_count": 341, "ad_execute_failure_count": 0}, "node-2": {"ad_execute_request_count": 60, "ad_execute_failure_count": 0}, "node-3": {"ad_execute_request_count": 744, "ad_execute_failure_count": 0}, "node-4": {"ad_execute_request_count": 1336, "ad_execute_failure_count": 78} } } }

示例:删除探测器

在删除探测器之前将其停止。删除探测器不会删除其自定义结果索引或其中的结果。

awscurl --service opensearch --region us-west-2 \ -X DELETE \ 'https://application_endpoint/w/workspace_id/api/anomaly_detectors/detectors/detector_id/data_source_id' \ -H "Content-Type: application/json" \ -H "osd-xsrf: osd-fetch" \ -H "osd-version: 3.6.0"