2022 Zabbix中国峰会
2022 Zabbix中国峰会

创建

描述

object action.create(object/array actions)

此方法用于创建新动作。

此方法只有 Admin(管理员)Super admin(超级管理员) 用户可用。可以在用户角色设置中撤销调用该方法的权限。更多信息请查看用户角色

参数

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

此外,还有 标准动作参数,方法接受如下参数。

参数 类型 描述
filter object 动作的动作过滤 对象。
operations array 为这个动作创建的动作操作
recovery_operations array 为这个动作创建的动作恢复操作
update_operations array 为这个动作创建的动作更新操作

返回值

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

示例

创建触发器动作

创建一个触发器动作,当主机 "10084" 的触发器(其名称中包含 "memory" 一词)变为问题状态时将开始执行。 该动作将配置有4个操作。

第一个立即执行的操作将通过媒体类型 "1" 向用户组 "7" 中的所有用户发送消息。 如果事件在30分钟内未解决,第二个操作将在组 "2" 中的所有主机上运行脚本 "5"(作用域为 "动作操作" 的脚本)。 如果事件得到解决,恢复操作将通知所有收到有关问题消息的用户。 如果事件被更新,确认/更新操作将使用自定义主题和消息通知(所有收到有关问题消息的用户)。

请求:

{
           "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
       }

响应:

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

创建发现动作

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

请求:

{
           "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
       }

响应:

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

使用自定义表达式过滤器

创建一个触发器动作,该动作使用自定义表达式 - "A and (B or C)" - 来评估动作条件。 一旦主机 "10084" 或主机 "10106" 上的触发器达到或超过 "Warning" 严重性级别并变为问题状态, 该动作将通过媒体类型 "1" 向用户组 "7" 中的所有用户发送消息。 公式 ID "A"、"B" 和 "C" 是任意选择的。

请求:

{
           "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
       }

响应:

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

创建代理自动注册规则

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

请求:

{
           "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
       }

响应:

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

另请参阅

来源

ui/include/classes/api/services/CAction.php 中的 CAction::create() 。