Hello everyone! I wanted to discuss an issue I'm facing. I've been assigned tasks involving the Zabbix API (I'm using version 5.4.0, 2001–2021, Zabbix SIA), and I've encountered a problem. While inspecting the Zabbix files on Linux, I noticed that I'm missing the "API_jsonrpc.php" file. I downloaded the file from the Zabbix GitHub for version 5.4, uploaded it to Zabbix, but I'm unable to get it to work. Should I install this file using commands? If anyone has encountered this issue and can provide a solution, I would appreciate it. Thanks!
Ad Widget
Collapse
Problem with missing api_jsonrpc.php
Collapse
X
-
If your web frontend is installed properly you should also have this file. Whatever version you might have, it has always been there...
Code::~]$ ls -l /usr/share/zabbix/ total 692 ... -rw-r--r--. 1 root root 2350 Dec 13 09:17 api_jsonrpc.php ....
Comment
-
Thank you, anyway I remember checking if it was there because I was getting 'api_jsonrpc not found.' I will search again on the server. If, in a hypothetical case, it is not there, I have been looking in the Zabbix git for the file for version 5.4.0, and when I upload it to Zabbix, I get a 500 http error. Do you know what could be causing this issue? I emphasize that I will search on the server again when they reopen my permissions
This is the code i was putting inside the server as the api_jsonrpc.php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, Authorization'); // Añade Authorization aquí
header('Access-Control-Allow-Methods: POST, GET');
header('Access-Control-Max-Age: 1000');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
return;
}
require_once dirname(__FILE__).'/include/func.inc.php';
require_once dirname(__FILE__).'/include/classes/core/CHttpRequest.php';
$allowed_content = [
'application/json-rpc' => 'json-rpc',
'application/json' => 'json-rpc',
'application/jsonrequest' => 'json-rpc'
];
// Establecer el token directamente (reemplaza 'tu_token_aqui' con tu propio token conocido)
$apiToken = '**********************************';
// Establecer el token en la instancia de la clase API
API::getWrapper()->setAuthToken($apiToken);
$http_request = new CHttpRequest();
$content_type = $http_request->header('Content-Type');
$content_type = explode(';', $content_type);
$content_type = $content_type[0];
if (!isset($allowed_content[$content_type])) {
header('HTTP/1.0 412 Precondition Failed');
return;
}
require_once dirname(__FILE__).'/include/classes/core/APP.php';
header('Content-Type: application/json');
$data = $http_request->body();
try {
APP::getInstance()->run(APP::EXEC_MODE_API);
$apiClient = API::getWrapper()->getClient();
// unset wrappers so that calls between methods would be made directly to the services
API::setWrapper();
$jsonRpc = new CJsonRpc($apiClient, $data);
echo $jsonRpc->execute();
}
catch (Exception $e) {
// decode input json request to get request's id
$jsonData = json_decode($data, true);
$response = [
'jsonrpc' => '2.0',
'error' => [
'code' => 1,
'message' => $e->getMessage(),
'data' => ''
],
'id' => (isset($jsonData['id']) ? $jsonData['id'] : null)
];
echo json_encode($response);
}
session_write_close();
?>Last edited by jlenoci; 06-02-2024, 09:34.
-
-
Please... do not use "comments", it makes impossible to quote you properly...
That code does not look like something from Zabbix git... All those spanish(?) comments...? This is somekind of edited version compared to https://git.zabbix.com/projects/ZBX/...Frelease%2F5.4Comment
-
I'm sorry, it's my first time on this forum. The comments are my edits in Spanish to make it easier for me to understand. The code is edited to use API-token authentication. Have I made a mistake with these modifications? I apologize for insisting, I have never worked on this before, and I have been stuck for a couple of weeks alreadyComment
-
Why should you edit this for tokens, if that is supported out-of-the-box..? https://www.zabbix.com/documentation...540#api-tokensI'm sorry, it's my first time on this forum. The comments are my edits in Spanish to make it easier for me to understand. The code is edited to use API-token authentication. Have I made a mistake with these modifications? I apologize for insisting, I have never worked on this before, and I have been stuck for a couple of weeks alreadyComment
-
Hello again, I have checked in the directory /var/www/html/zabbix, and the file api_jsonrpc.php is there; it is the same as the code from the page you provided. I have deleted the one I had uploaded with the API token issue. Now, the error when accessing the URL has changed from HTTP Error 500 to HTTP Error 412.
Comment
-
Now I'm getting responses that I didn't have before, but I can't access the website through the URL with api_jsonrpc.php. I'm entering the URL like this: https://miserverexample/api_jsonrpc.phpComment
-
ok .. you get already some answers (api version), now authenticate yourself to API and then perform your requests... https://www.zabbix.com/documentation/5.4/en/manual/api plenty of examples...Comment
Comment