[comment]: # ({c7f202d4-c7f202d4})
# action.create

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

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

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

此方法允许create新动作.

::: noteclassic
此方法仅适用于*Admin*和*Super admin*
用户类型. 调用该方法的权限可在用户角色
设置中撤销. 更多信息请参阅[User
roles](/manual/web_interface/frontend_sections/administration/user_roles)

:::

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

[comment]: # ({52238aaf-5a9be3c9})
### 参数

`(object/array)` 用于create的操作.

除了[动作](object#动作)之外，
该方法还接受以下参数.

| 参数 | [数据类型](/manual/api/reference_commentary#数据类型) | 描述 |
|--|--|------|
| filter | object | 操作的[操作过滤器](/manual/api/reference/action/object#操作过滤器) object. |
| operations | array | 操作的[action-操作](/manual/api/reference/action/object#action-操作)用于create. |
| recovery\_operations | array | 操作的[action-恢复操作](/manual/api/reference/action/object#action-恢复操作)用于create. |
| update\_operations | array | 操作的[update operations](/manual/api/reference/action/object#action_update_operation)用于create. |

[comment]: # ({/52238aaf-5a9be3c9})

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

`(object)` 返回一个包含所创建动作ID的object，这些ID位于`actionids`属性下。返回的ID顺序与传入的动作顺序相匹配。

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

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

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

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

创建一个触发器动作，当来自主机 "10084"的触发器（名称中包含"memory"字样）进入PROBLEM状态时触发。
该动作将配置4个操作。
第一个即时操作将通过媒介类型"1"向用户组"7"中的所有用户发送消息。
如果事件在30分钟内未解决，第二个操作将在组"2"中的所有主机上run[配置全局脚本](/manual/web_interface/frontend_sections/administration/scripts#配置全局脚本)"5"（作用域为"Action operation"的脚本）。
如果事件解决，恢复操作将通知所有收到过该问题相关消息的用户。
如果事件更新，acknowledge/update操作将以自定义主题和消息通知所有收到过该问题相关消息的用户。

请求:

```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"
                }
            }
        ]
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```
响应:

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

[comment]: # ({/34096b6e-afc96865})

[comment]: # ({4e47d40a-53bdbb4b})
#### 创建发现动作

创建一个发现动作，将发现的主机关联到模板"10001"。

请求:

```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"
                    }
                ]
            }
        ]
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```
响应:

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

[comment]: # ({/4e47d40a-53bdbb4b})

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

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

请求:

```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"
                }
            }
        ]
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```
响应:

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

[comment]: # ({/56bf3fe7-de84bbc4})

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

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

请求:

```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"
                    }
                ]
            }
        ]
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
```
响应:

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

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

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

-   [操作过滤器](object#操作过滤器)
-   [action-操作](object#action-操作)
-   [Script](/manual/api/reference/script/object)

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

[comment]: # ({32335876-32335876})
### 来源

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

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