113 lines
2.8 KiB
PHP
113 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Carbon\Carbon;
|
|
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)
|
|
{
|
|
$this->token = $this->request([
|
|
"jsonrpc" => "2.0",
|
|
"method" => "user.login",
|
|
"params" => [
|
|
"username" => $username,
|
|
"password" => $password,
|
|
],
|
|
"id" => $this->id,
|
|
"auth" => null,
|
|
]);
|
|
return $this->token;
|
|
}
|
|
|
|
public function hosts($params = [])
|
|
{
|
|
return $this->request([
|
|
"jsonrpc" => "2.0",
|
|
"method" => "host.get",
|
|
"params" => $params,
|
|
"id" => $this->id,
|
|
"auth" => $this->token,
|
|
]);
|
|
}
|
|
|
|
public function hostGroups($params = [])
|
|
{
|
|
return $this->request([
|
|
"jsonrpc" => "2.0",
|
|
"method" => "hostgroup.get",
|
|
"params" => $params,
|
|
"id" => $this->id,
|
|
"auth" => $this->token,
|
|
]);
|
|
}
|
|
|
|
public function maintenances($params = [])
|
|
{
|
|
return $this->request([
|
|
"jsonrpc" => "2.0",
|
|
"method" => "maintenance.get",
|
|
"params" => $params,
|
|
"id" => $this->id,
|
|
"auth" => $this->token,
|
|
]);
|
|
}
|
|
|
|
public function maintenancesCreate($params = [])
|
|
{
|
|
return $this->request([
|
|
"jsonrpc" => "2.0",
|
|
"method" => "maintenance.create",
|
|
"params" => $params,
|
|
"id" => $this->id,
|
|
"auth" => $this->token,
|
|
]);
|
|
}
|
|
|
|
public function maintenancesUpdate( $params = [])
|
|
{
|
|
return $this->request([
|
|
"jsonrpc" => "2.0",
|
|
"method" => "maintenance.update",
|
|
"params" => $params,
|
|
"id" => $this->id,
|
|
"auth" => $this->token,
|
|
]);
|
|
}
|
|
|
|
/*Helpers*/
|
|
|
|
private function request($body = [])
|
|
{
|
|
if (empty($this->token) && $body['auth'] != null) {
|
|
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 is_array($responseObject["result"]) ? collect($responseObject["result"]) : $responseObject["result"];
|
|
}
|
|
}
|