[comment]: # translation:outdated

[comment]: # ({c7f202d4-c7f202d4})
# 创建

[comment]: # ({/c7f202d4-c7f202d4})

[comment]: # ({b15ccd83-71c93aa0})
### 描述

`object action.create(object/array actions)`

此方法允许创建新的动作。

::: noteclassic
此方法仅对*Admin*和*Super admin*用户类型可用。可以在用户角色设置中撤销调用此方法的权限。更多信息请参见[用户角色](/manual/web_interface/frontend_sections/users/user_roles)。
:::

[comment]: # ({/b15ccd83-71c93aa0})

[comment]: # ({4625852a-5a9be3c9})
### 参数

`(object/array)` 要创建的动作。

除了[标准动作属性](object#动作)外，该方法还接受以下参数。

| 参数 | [类型](/manual/api/reference_commentary#数据类型) | 描述 |
| --- | --- | --- |
| filter | object | [动作过滤](/manual/api/reference/action/object#动作过滤) 对象，用于指定动作筛选条件。 |
| operations | array | [动作操作](/manual/api/reference/action/object#动作操作) 数组，为动作创建的操作。 |
| recovery\_operations | array | [动作恢复操作](/manual/api/reference/action/object#动作恢复操作) 数组，为动作创建的恢复操作。 |
| update\_operations | array | [动作更新操作](/manual/api/reference/action/object#动作更新操作) 数组，为动作创建的更新操作。 |

[comment]: # ({/4625852a-5a9be3c9})

[comment]: # ({2830affd-2830affd})
### 返回值

`(object)` 返回一个对象，该对象在`actionids`属性下包含已创建动作的ID。返回的ID顺序与传递的动作顺序相匹配。

[comment]: # ({/2830affd-2830affd})

[comment]: # ({b41637d2-b41637d2})
### 示例

[comment]: # ({/b41637d2-b41637d2})

[comment]: # ({9ca78970-afc96865})
#### 创建触发器动作

创建一个触发器动作，该动作将在主机ID为"10084"上的触发器（名称中包含"memory"）进入PROBLEM状态时开始执行。该动作将配置4个操作。
- 第一个也是立即执行的操作，将通过媒体类型"1"向用户组ID为"7"中的所有用户发送消息。
- 如果事件在30分钟内未解决，第二个操作将在组"2"中的所有主机上运行[脚本](/manual/web_interface/frontend_sections/alerts/scripts#配置全局脚本)"5"（来自"动作操作"按钮创建的脚本）。
- 如果事件得到解决，所有与该问题相关的用户将收到一个恢复操作通知。
- 如果事件被更新，所有与该问题相关的用户将收到一个确认/更新操作通知。

[请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "action.create",
    "params": {
        "name": "Trigger action",
        "eventsource": 0,
        "esc_period": "30m",
        "filter": {
            "evaltype": 0,
            "conditions": [
                {
                    "conditiontype": 1,
                    "operator": 0,
                    "value": "10084"
                },
                {
                    "conditiontype": 3,
                    "operator": 2,
                    "value": "memory"
                }
            ]
        },
        "operations": [
            {
                "operationtype": 0,
                "esc_step_from": 1,
                "esc_step_to": 1,
                "opmessage_grp": [
                    {
                        "usrgrpid": "7"
                    }
                ],
                "opmessage": {
                    "default_msg": 1,
                    "mediatypeid": "1"
                }
            },
            {
                "operationtype": 1,
                "esc_step_from": 2,
                "esc_step_to": 2,
                "opconditions": [
                    {
                        "conditiontype": 14,
                        "operator": 0,
                        "value": "0"
                    }
                ],
                "opcommand_grp": [
                    {
                        "groupid": "2"
                    }
                ],
                "opcommand": {
                    "scriptid": "5"
                }
            }
        ],
        "recovery_operations": [
            {
                "operationtype": "11",
                "opmessage": {
                    "default_msg": 1
                }
            }
        ],
        "update_operations": [
            {
                "operationtype": "12",
                "opmessage": {
                    "default_msg": 0,
                    "message": "Custom update operation message body",
                    "subject": "Custom update operation message subject"
                }
            }
        ]
    },
    "id": 1
}
```

响应:


```json
{
    "jsonrpc": "2.0",
    "result": {
        "actionids": [
            "17"
        ]
    },
    "id": 1
}
```

[comment]: # ({/9ca78970-afc96865})

[comment]: # ({0f8305f5-53bdbb4b})
#### 创建一个发现动作

创建一个发现动作，该动作将模板"10001"链接到已发现的主机。

[请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "action.create",
    "params": {
        "name": "Discovery action",
        "eventsource": 1,
        "filter": {
            "evaltype": 0,
            "conditions": [
                {
                    "conditiontype": 21,
                    "operator": 0,
                    "value": "1"
                },
                {
                    "conditiontype": 10,
                    "operator": 0,
                    "value": "2"
                }
            ]
        },
        "operations": [
            {
                "operationtype": 6,
                "optemplate": [
                    {
                        "templateid": "10001"
                    }
                ]
            }
        ]
    },
    "id": 1
}
```

响应:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "actionids": [
            "18"
        ]
    },
    "id": 1
}
```

[comment]: # ({/0f8305f5-53bdbb4b})

[comment]: # ({2311dde2-de84bbc4})
#### 使用自定义表达式过滤

创建一个触发器动作，该动作使用自定义表达式 - "A and (B or C)" - 来评估动作条件。
一旦来自主机"10084"或主机"10106"的告警级别大于等于"Warning"触发器将进入PROBLEM状态，
该动作将通过媒体类型"1"向用户组"7"中的所有用户发送消息。
公式ID "A"、"B" 和 "C" 是任意选择的。

[请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "action.create",
    "params": {
        "name": "Trigger action",
        "eventsource": 0,
        "esc_period": "15m",
        "filter": {
            "evaltype": 3,
            "formula": "A and (B or C)",
            "conditions": [
                {
                    "conditiontype": 4,
                    "operator": 5,
                    "value": "2",
                    "formulaid": "A"
                },
                {
                    "conditiontype": 1,
                    "operator": 0,
                    "value": "10084",
                    "formulaid": "B"
                },
                {
                    "conditiontype": 1,
                    "operator": 0,
                    "value": "10106",
                    "formulaid": "C"
                }
            ]
        },
        "operations": [
            {
                "operationtype": 0,
                "esc_step_from": 1,
                "esc_step_to": 1,
                "opmessage_grp": [
                    {
                        "usrgrpid": "7"
                    }
                ],
                "opmessage": {
                    "default_msg": 1,
                    "mediatypeid": "1"
                }
            }
        ]
    },
    "id": 1
}
```

响应:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "actionids": [
            "18"
        ]
    },
    "id": 1
}
```

[comment]: # ({/2311dde2-de84bbc4})

[comment]: # ({a0b5931f-b81a306b})
#### 创建 agent 自动注册规则

创建一个自动注册动作，当主机名包含“SRV”或元数据包含“AlmaLinux”时，将该主机添加到主机组“2”中。

[请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "action.create",
    "params": {
        "name": "Register Linux servers",
        "eventsource": "2",
        "filter": {
            "evaltype": "2",
            "conditions": [
                {
                    "conditiontype": "22",
                    "operator": "2",
                    "value": "SRV"
                },
                {
                    "conditiontype": "24",
                    "operator": "2",
                    "value": "AlmaLinux"
                }
            ]
        },
        "operations": [
            {
                "operationtype": "4",
                "opgroup": [
                    {
                        "groupid": "2"
                    }
                ]
            }
        ]
    },
    "id": 1
}
```

响应:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "actionids": [
            19
        ]
    },
    "id": 1
}
```

[comment]: # ({/a0b5931f-b81a306b})

[comment]: # ({7ff1c370-35a2b594})
#### 创建带有主机标签的代理自动注册规则

创建一个自动注册动作，将主机添加到主机组"2"并添加两个主机标签。

[请求](/manual/api#执行请求):

```json
{
    "jsonrpc": "2.0",
    "method": "action.create",
    "params": {
        "name": "Register Linux servers with tags",
        "eventsource": "2",
        "operations": [
            {
                "operationtype": "4",
                "opgroup": [
                    {
                        "groupid": "2"
                    }
                ]
            },
            {
                "operationtype": "13",
                "optag": [
                    {
                        "tag": "Location",
                        "value": "Office"
                    },
                    {
                        "tag": "City",
                        "value": "Riga"
                    }
                ]
            }
        ]
    },
    "id": 1
}
```

响应:

```json
{
    "jsonrpc": "2.0",
    "result": {
        "actionids": [
            20
        ]
    },
    "id": 1
}
```

[comment]: # ({/7ff1c370-35a2b594})

[comment]: # ({0f0e8283-755496f7})
### 另请参阅

-   [动作过滤](object#动作过滤)
-   [动作操作](object#动作操作)
-   [脚本](/manual/api/reference/script/object)

[comment]: # ({/0f0e8283-755496f7})

[comment]: # ({32335876-32335876})
### 源码位置

`CAction::create()` 在 *ui/include/classes/api/services/CAction.php* 文件中。

[comment]: # ({/32335876-32335876})
