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

general question #9

Open michabbb opened 2 years ago

michabbb commented 2 years ago

hi, is it possible to update checks in zabbix with this client? I would like to replace Nagios passive checks, where the client pushes the new state to the server.

thanks!

intellitrend-team commented 2 years ago

It's generally not possible to write any metrics via the Zabbix API, which is what this library is using. For that purpose, a PHP library that implements the Zabbix trapper/sender protocol should be used instead, like https://github.com/zarplata/zabbix-sender-php, for example. Alternatively, PHP can also send data by calling the zabbix_sender process.

azzus78 commented 1 year ago

hello, before testing implemented your code I testing with POSTMAN my Zabbix API was result success with ouput json

Then i tried your code same as like simple.php file to access my Zabbix APPI ,here my code :

`require_once("ZabbixApi.php");

use IntelliTrend\Zabbix\ZabbixApi; use IntelliTrend\Zabbix\ZabbixApiException;

print "Zabbix API Example\n"; print " Connect to API, check certificate/hostname and get number of hosts\n"; print "=====================================================\n";

$zabUrl ='https://my.zabbixurl.com/api_jsonrpc.php'; $zabUser = 'myuser'; $zabPassword = 'mypassword';

$zbx = new ZabbixApi(); try {

$zbx->login($zabUrl, $zabUser, $zabPassword);
//this is similar to: $result = $zbx->call('apiinfo.version');
$result = $zbx->getApiVersion();
print "Remote Zabbix API Version:$result\n";
// Get number of host available to this useraccount
// $result = $zbx->call('host.get',array("countOutput" => true));
// print "Number of Hosts:$result\n";

} catch (ZabbixApiException $e) { print "==== Zabbix API Exception ===\n"; print 'Errorcode: '.$e->getCode()."\n"; print 'ErrorMessage: '.$e->getMessage()."\n"; exit; } catch (Exception $e) { print "==== Exception ===\n"; print 'Errorcode: '.$e->getCode()."\n"; print 'ErrorMessage: '.$e->getMessage()."\n"; exit; }`

Result :

Zabbix API Example Connect to API, check certificate/hostname and get number of hosts ===================================================== ==== Zabbix API Exception === Errorcode: 404 ErrorMessage: Request failed with HTTP-Code:404, sslVerifyResult:0.

Whats wrong with my code ?

But I expriment with code using curl :

`$url = "https://my.zabbixurl.com/api_jsonrpc.php";

$headers = array( "Content-Type: application/json-rpc", );

$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$data = <<<DATA { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "myuser", "password": "mypassword" }, "id": 1 }

DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$resp = curl_exec($curl); curl_close($curl);

echo $resp;`

Thats code successfull output json same as like i tried with POSTMAN

What is different between your class code because I want use your class ? Please give me some suggestion to handle the issues ?

azzus78 commented 1 year ago

hello, before testing implemented your code I testing with POSTMAN my Zabbix API was result success with ouput json

Then i tried your code same as like simple.php file to access my Zabbix APPI ,here my code :

`require_once("ZabbixApi.php");

use IntelliTrend\Zabbix\ZabbixApi; use IntelliTrend\Zabbix\ZabbixApiException;

print "Zabbix API Example\n"; print " Connect to API, check certificate/hostname and get number of hosts\n"; print "=====================================================\n";

$zabUrl ='https://my.zabbixurl.com/api_jsonrpc.php'; $zabUser = 'myuser'; $zabPassword = 'mypassword';

$zbx = new ZabbixApi(); try {

$zbx->login($zabUrl, $zabUser, $zabPassword);
//this is similar to: $result = $zbx->call('apiinfo.version');
$result = $zbx->getApiVersion();
print "Remote Zabbix API Version:$result\n";
// Get number of host available to this useraccount
// $result = $zbx->call('host.get',array("countOutput" => true));
// print "Number of Hosts:$result\n";

} catch (ZabbixApiException $e) { print "==== Zabbix API Exception ===\n"; print 'Errorcode: '.$e->getCode()."\n"; print 'ErrorMessage: '.$e->getMessage()."\n"; exit; } catch (Exception $e) { print "==== Exception ===\n"; print 'Errorcode: '.$e->getCode()."\n"; print 'ErrorMessage: '.$e->getMessage()."\n"; exit; }`

Result :

Zabbix API Example Connect to API, check certificate/hostname and get number of hosts ===================================================== ==== Zabbix API Exception === Errorcode: 404 ErrorMessage: Request failed with HTTP-Code:404, sslVerifyResult:0.

Whats wrong with my code ?

But I expriment with code using curl :

`$url = "https://my.zabbixurl.com/api_jsonrpc.php";

$headers = array( "Content-Type: application/json-rpc", );

$curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$data = <<<DATA { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "myuser", "password": "mypassword" }, "id": 1 }

DATA;

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$resp = curl_exec($curl); curl_close($curl);

echo $resp;`

Thats code successfull output json same as like i tried with POSTMAN

What is different between your class code because I want use your class ? Please give me some suggestion to handle the issues ?

Yey..solved. I wrong write url endpoint. Remove "api_jsonrpc.php" from url so the actual is : https://my.zabbixurl.com

thanks