intellitrend / zabbixapi-php

Zabbix API client for PHP with session cache and SSL management
GNU Lesser General Public License v3.0
56 stars 10 forks source link

Documentation for trigger.create #8

Closed michaelbarkdoll closed 2 years ago

michaelbarkdoll commented 2 years ago

Could you please provide an example of trigger.create? I'm unable to get it to work with the current version of zabbixapi-php and instead had to use python-pip3 with requests to create a trigger over the api.

intellitrend-team commented 2 years ago

Hello, here's an example on how to create a trigger on the host "Zabbix server" that triggers if the last value of system.cpu.util is greater than 75:

<?php
require_once("../src/ZabbixApi.php");

use IntelliTrend\Zabbix\ZabbixApi;

$zabUrl ='https://my.zabbixurl.com/zabbix';
$zabUser = 'myusername';
$zabPassword = 'mypassword';

$zbx = new ZabbixApi();
try {
    $zbx->login($zabUrl, $zabUser, $zabPassword);
    $result = $zbx->call('trigger.create', array(
        "description" => "High CPU usage on {HOST.NAME}",
        "expression" => "{Zabbix server:system.cpu.util.last()}>75", // before Zabbix 5.4
        //"expression" => "last(/Zabbix server/system.cpu.util)>75", // after Zabbix 5.4
    ));
    print_r($result);
} catch (Exception $e) {
    print "==== Exception ===\n";
    print 'Errorcode: '.$e->getCode()."\n";
    print 'ErrorMessage: '.$e->getMessage()."\n";
    exit;
}

?>

$result will contain the ID of the created trigger.

Note the different trigger expression syntax introduced in Zabbix 5.4 and above.

michaelbarkdoll commented 2 years ago

Thanks, I'm having issues passing in additional arrays for some functions in the API. E.g., the tags with the trigger.create:

$params = array(
        "description" => "{HOST.NAME} has been up for too long (uptime > 14d) and updates are required",
        "expression" => "last(/Linux by Zabbix agent/system.uptime)>14d and last(/Linux by Zabbix agent/dnf.needs.reboot)>0", // after Zabbix 5.4
        "comments" => "The host uptime is more than 14 days and updates are required.",
        "recovery_mode" => 0,
        "type" => 0,
        "correlation_mode" => 0,
        "status" => 0,
        "priority" => 2,
        "manual_close" => 1, 
        "tags" => array(
            "tag" => "scope", 
            "value" => "notice"
        )
    );
    $result = $zbx->call('trigger.create', $params);

==== Exception === Errorcode: -32602 ErrorMessage: Invalid params. [Invalid parameter "/1/tags/1": an array is expected.]

Also, with autoregistration:

$params = array(
        'name' => "Autoregistration",
        'eventsource' => 2,
                 'status' => 0,
        'filter' => array("evaltype" => 0, "conditions" => array("conditiontype" => 24, "value" => "Linux", "operator" => 2)),
        'operations' => array(
                            array("operationtype" => 4, "opgroup" => array("groupid" => 2)),
                            array("operationtype" => 6, "optemplate" => array("templateid" => 10001))
                            )
    );
    $result = $zbx->call('action.create', $params);

I'm curious if I'm doing something wrong?

The pip3 requests payload below works with the API:

payload = {
    "jsonrpc": "2.0",
    "method": "action.create",
    "params": {
        "name": "Autoregistration",
        "eventsource": 2,
        "status": 0,
        "filter": {
            "evaltype": 0,
            "conditions": [{
                "conditiontype": 24,
                "value": "Linux",
                "operator": 2
            }]
        },
        "operations": [{
            "operationtype": 4, 
            "opgroup": [{
                "groupid": 2
            }]
        },
        {
            "operationtype": 6,
            "optemplate": [{
                "templateid": 10001
            }]
        }]
    },
    "id": 2,
    "auth": api_token
    }
intellitrend-team commented 2 years ago

The problem is that an array of objects in PHP requires two calls of array(); one for the actual array and one for the associative array (the object). So the $params for trigger.create should look like this:

$params = array(
    "description" => "{HOST.NAME} has been up for too long (uptime > 14d) and updates are required",
    "expression" => "last(/Linux by Zabbix agent/system.uptime)>14d and last(/Linux by Zabbix agent/dnf.needs.reboot)>0", // after Zabbix 5.4
    "comments" => "The host uptime is more than 14 days and updates are required.",
    "recovery_mode" => 0,
    "type" => 0,
    "correlation_mode" => 0,
    "status" => 0,
    "priority" => 2,
    "manual_close" => 1, 
    "tags" => array(array(
        "tag" => "scope", 
        "value" => "notice"
    ))
);

Same for the registration:

$params = array(
    'name' => "Autoregistration",
    'eventsource' => 2,
    'status' => 0,
    'filter' => array("evaltype" => 0, "conditions" => array(array("conditiontype" => 24, "value" => "Linux", "operator" => 2))),
    'operations' => array(array(
                        array("operationtype" => 4, "opgroup" => array(array("groupid" => 2))),
                        array("operationtype" => 6, "optemplate" => array(array("templateid" => 10001)))
                    ))
);

Where in Python or JSON, you can just write [{"a":"b"}], you need to use array(array("a" => "b")) in PHP.

Starting with PHP 7.1, you can shorten the code a bit, though: [array("a" => "b")].

Also, if you're not sure about the parameters for a create/update API call, you can always use the get call on a known object and use the result as a template.

intellitrend-team commented 2 years ago

I'll close this for now. If there any more questions, feel free to reopen this issue.