bytes-commerce/zabbix-api
Symfony bundle for the Zabbix JSON-RPC API with persistent auth/token caching, type-safe action factory, and history/metrics retrieval. PHP 8.3+ strict typing, zero-config via env vars, broad API coverage, optional async monitoring via Messenger.
Installation
composer require bytes-commerce/zabbix-api
The bundle auto-registers via Symfony Flex.
Configuration
Add to .env.local:
ZABBIX_API_BASE_URI=https://zabbix.example.com/api_jsonrpc.php
ZABBIX_USERNAME=monitoring_user
ZABBIX_PASSWORD=secure_password
ZABBIX_AUTH_TTL=3600 # Optional: cache TTL in seconds
APP_NAME=MyApplication # Used for host identification
First Use Case
Inject ZabbixServiceInterface and use the factory pattern:
use BytesCommerce\ZabbixApi\ZabbixServiceInterface;
use BytesCommerce\ZabbixApi\Actions\History;
class MonitoringController {
public function __construct(private readonly ZabbixServiceInterface $zabbix) {}
public function getCpuMetrics(): array {
$history = $this->zabbix->action(History::class);
return $history->getLast24Hours(
itemIds: ['12345'],
historyType: HistoryTypeEnum::NUMERIC_FLOAT
);
}
}
Dto classes under each action (e.g., GetHostGroupDto) for type-safe parameters.HistoryTypeEnum, ItemTypeEnum, etc., for strict typing.$zabbix->action(ActionClass::class).
$hostAction = $zabbix->action(Host::class);
$history = $zabbix->action(History::class);
HostGroup:
$dto = new GetHostGroupDto(
groupids: ['15'],
output: 'extend',
selectHosts: true
);
$groups = $hostGroup->get($dto)->hostGroups;
MassAddHostGroupDto, MassUpdateHostGroupDto, etc., for bulk actions.$bus->dispatch(new PushMetricMessage(
key: 'app.user.login',
value: 1,
tags: ['env' => 'prod']
));
config/packages/zabbix_api.yaml:
zabbix_api:
messenger_transport: async # Options: async, sync, false
zabbix_api:
setup_enabled: true
app_name: 'MyApp'
host_group: 'AppServers'
dashboard_config_path: '%kernel.project_dir%/config/zabbix'
EnsureZabbixSetupMessage manually if needed.ZabbixApiException for API errors:
try {
$history->getLatest(['12345']);
} catch (ZabbixApiException $e) {
$this->handleZabbixError($e);
}
ZabbixClientInterface for raw API access:
$client->call(ZabbixAction::HOST_GET, ['output' => 'extend']);
ActionServiceInterface for runtime method calls:
$this->actionService->call(History::class, [
'method' => 'get',
'params' => ['itemids' => ['10084']]
]);
Token Cache Invalidation
ZABBIX_AUTH_TTL is too short, frequent re-authentication may occur.7200 for 2 hours) or disable caching (ZABBIX_AUTH_TTL=0).DTO Validation
hostid in CreateItemDto) cause silent failures.if (empty($dto->hostid)) {
throw new \InvalidArgumentException('hostid is required');
}
Messenger Transport
zabbix_api_async queue or switch to sync for debugging:
zabbix_api:
messenger_transport: sync
History Data Types
HistoryTypeEnum (e.g., using NUMERIC_FLOAT for text data) returns empty results.Auto-Setup Conflicts
setup_enabled: true may overwrite existing hosts/items.dashboard_config_path or overriding the EnsureZabbixSetupMessage handler.Enable API Logging
Add to config/packages/monolog.yaml:
handlers:
zabbix_api:
type: stream
path: "%kernel.logs_dir%/zabbix_api.log"
level: debug
channels: ["zabbix_api"]
Then enable the channel in ZabbixService:
$this->logger->debug('Zabbix API call', ['params' => $params]);
Raw API Responses
Access raw responses via ZabbixClientInterface:
$response = $client->call(ZabbixAction::HOST_GET, ['output' => 'extend']);
$this->logger->debug('Raw response', ['data' => $response]);
Token Debugging Check cached token location:
$token = $this->tokenStorage->getToken();
$this->logger->debug('Current token', ['token' => $token]);
Custom Actions
Extend the factory by implementing ActionInterface:
class CustomAction implements ActionInterface {
public function __invoke(ZabbixClientInterface $client): mixed {
return $client->call(ZabbixAction::CUSTOM_ACTION, ['param' => 'value']);
}
}
Register in services.yaml:
BytesCommerce\ZabbixApi\ZabbixService:
arguments:
$actions:
- BytesCommerce\ZabbixApi\Actions\CustomAction
Override DTOs Create custom DTO classes extending the bundle’s DTOs to add validation or defaults:
class CustomGetHostGroupDto extends GetHostGroupDto {
public function __construct() {
parent::__construct(output: 'extend', selectHosts: true);
}
}
Messenger Handlers
Extend PushMetricMessageHandler or PushEventMessageHandler to customize payload formatting:
class CustomMetricHandler implements PushMetricMessageHandlerInterface {
public function __invoke(PushMetricMessage $message): void {
// Custom logic before sending to Zabbix
$client->sendMetric($message->key, $message->value);
}
}
Environment Variables
.env.local > .env > config/packages/zabbix_api.yaml.ZABBIX_API_BASE_URI as the default; override in config if needed.Auto-Registration
config/bundles.php:
return [
BytesCommerce\ZabbixApiBundle\ZabbixApiBundle::class => ['all' => true],
];
PHP 8.3+ Features
How can I help you explore Laravel packages today?