131 lines
3.6 KiB
PHP
131 lines
3.6 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Services;
|
||
|
|
||
|
use Exception;
|
||
|
use Illuminate\Support\Facades\Http;
|
||
|
|
||
|
class ZabbixService
|
||
|
{
|
||
|
public string $token = "";
|
||
|
public string $url = "";
|
||
|
public int $id = 1;
|
||
|
|
||
|
function __construct($url) {
|
||
|
$this->url = $url;
|
||
|
}
|
||
|
|
||
|
public function connect( $username, $password)
|
||
|
{
|
||
|
$response = Http::withoutVerifying()->post($this->url . "/api_jsonrpc.php", [
|
||
|
"jsonrpc" => "2.0",
|
||
|
"method" => "user.login",
|
||
|
"params" => [
|
||
|
"username" => $username,
|
||
|
"password" => $password,
|
||
|
],
|
||
|
"id" => $this->id,
|
||
|
"auth" => null,
|
||
|
]);
|
||
|
|
||
|
if (!$response->successful()) {
|
||
|
throw new Exception("Unable To Authenticated", 1);
|
||
|
}
|
||
|
|
||
|
$responseObject = $response->json();
|
||
|
if (isset($responseObject['error'])) {
|
||
|
throw new Exception($responseObject['error']['data'], $responseObject['error']['code']);
|
||
|
}
|
||
|
|
||
|
$this->token = $response['result'];
|
||
|
$this->id++;
|
||
|
}
|
||
|
|
||
|
public function hosts($params = [])
|
||
|
{
|
||
|
if (empty($this->token)) {
|
||
|
throw new Exception("you need to connect first", 1);
|
||
|
}
|
||
|
|
||
|
$response = Http::withoutVerifying()->post($this->url . "/api_jsonrpc.php", [
|
||
|
"jsonrpc" => "2.0",
|
||
|
"method" => "host.get",
|
||
|
"params" => $params,
|
||
|
"id" => $this->id,
|
||
|
"auth" => $this->token,
|
||
|
]);
|
||
|
|
||
|
if (!$response->successful()) {
|
||
|
throw new Exception("Unable To Request", 1);
|
||
|
}
|
||
|
|
||
|
$responseObject = $response->json();
|
||
|
if (isset($responseObject['error'])) {
|
||
|
throw new Exception($responseObject['error']['data'], $responseObject['error']['code']);
|
||
|
}
|
||
|
|
||
|
$this->id++;
|
||
|
return collect($responseObject["result"]);
|
||
|
}
|
||
|
|
||
|
public function hostGroups($params = [])
|
||
|
{
|
||
|
if (empty($this->token)) {
|
||
|
throw new Exception("you need to connect first", 1);
|
||
|
}
|
||
|
|
||
|
$response = Http::withoutVerifying()->post($this->url . "/api_jsonrpc.php", [
|
||
|
"jsonrpc" => "2.0",
|
||
|
"method" => "hostgroup.get",
|
||
|
"params" => $params,
|
||
|
"id" => $this->id,
|
||
|
"auth" => $this->token,
|
||
|
]);
|
||
|
|
||
|
if (!$response->successful()) {
|
||
|
throw new Exception("Unable To Request", 1);
|
||
|
}
|
||
|
|
||
|
$responseObject = $response->json();
|
||
|
if (isset($responseObject['error'])) {
|
||
|
throw new Exception($responseObject['error']['data'], $responseObject['error']['code']);
|
||
|
}
|
||
|
|
||
|
$this->id++;
|
||
|
return collect($responseObject["result"]);
|
||
|
}
|
||
|
|
||
|
public function maintenances($params = []){
|
||
|
return $this->request( [
|
||
|
"jsonrpc" => "2.0",
|
||
|
"method" => "maintenance.get",
|
||
|
"params" => $params,
|
||
|
"id" => $this->id,
|
||
|
"auth" => $this->token,
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
|
||
|
/*Helpers*/
|
||
|
|
||
|
private function request($body = []){
|
||
|
if (empty($this->token)) {
|
||
|
throw new Exception("you need to connect first", 1);
|
||
|
}
|
||
|
|
||
|
$response = Http::withoutVerifying()->post($this->url . "/api_jsonrpc.php", $body);
|
||
|
|
||
|
if (!$response->successful()) {
|
||
|
throw new Exception("Unable To Request", 1);
|
||
|
}
|
||
|
|
||
|
$responseObject = $response->json();
|
||
|
if (isset($responseObject['error'])) {
|
||
|
throw new Exception($responseObject['error']['data'], $responseObject['error']['code']);
|
||
|
}
|
||
|
|
||
|
$this->id++;
|
||
|
return collect($responseObject["result"]);
|
||
|
}
|
||
|
}
|