Installation
composer require crifi/zabbix-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Crifi\ZabbixBundle\CrifiZabbixBundle::class => ['all' => true],
];
Configuration
Define Zabbix API credentials in config/packages/crifi_zabbix.yaml:
crifi_zabbix:
url: 'http://your-zabbix-server/api_jsonrpc.php'
username: 'Admin'
password: 'zabbix'
First Use Case: Fetch Hosts
Inject the ZabbixClient service and call a method:
use Crifi\ZabbixBundle\Client\ZabbixClient;
class MyService {
public function __construct(private ZabbixClient $zabbix) {}
public function getHosts() {
return $this->zabbix->host->get(['output' => 'extend']);
}
}
Host Management
$zabbix->host->get()$zabbix->host->create() or $zabbix->host->update()$hosts = collect($csvData)->map(fn($row) => [
'host' => $row['name'],
'interfaces' => [['type' => 1, 'main' => 1, 'useip' => 1, 'ip' => $row['ip']]],
]);
$this->zabbix->host->create($hosts->toArray());
Item/Trigger Operations
$item = [
'hostid' => $hostId,
'name' => 'CPU Load',
'key_' => 'system.cpu.load[percpu,avg1]',
'type' => 0,
];
$this->zabbix->item->create($item);
Authentication & Rate Limiting
public function __construct(private ZabbixClient $zabbix) {}
ZabbixClient or wrap calls in a retry loop).Event-Driven Integrations
$alerts = $this->zabbix->alert->get(['output' => 'extend', 'selectHosts' => 'extend']);
foreach ($alerts as $alert) {
$this->dispatch(new ZabbixAlertEvent($alert));
}
Authentication Failures
config/packages/crifi_zabbix.yaml:
debug: true
Or wrap calls in a try-catch:
try {
$this->zabbix->host->get();
} catch (\RuntimeException $e) {
// Log or retry
}
API Version Mismatch
api_version: '6.0'
Large Data Responses
output=extend return nested arrays, which can be memory-intensive.output=shortenKeys or limit fields:
$this->zabbix->host->get(['output' => 'shortenKeys', 'selectInterfaces' => 'extend']);
Idempotency in Updates
update() requires the hostid (or similar) to be included in the payload.create() with updateExisting=true (if supported).Custom API Methods Extend the bundle by creating a custom client:
use Crifi\ZabbixBundle\Client\ZabbixClient;
class CustomZabbixClient extends ZabbixClient {
public function getHostWithItems(int $hostId) {
return $this->call('host.get', [
'output' => 'extend',
'filter' => ['hostid' => $hostId],
'selectItems' => ['output' => 'extend'],
]);
}
}
Register as a service in services.yaml:
services:
App\Service\CustomZabbixClient:
arguments:
$client: '@crifi_zabbix.client'
Caching Responses Cache frequent API calls (e.g., host lists) using Symfony’s cache system:
$cache = $this->container->get('cache.app');
$hosts = $cache->get('zabbix_hosts', function() {
return $this->zabbix->host->get();
});
Logging API Calls Add a subscriber to log all API requests:
use Crifi\ZabbixBundle\Event\ZabbixEvent;
class ZabbixLoggerSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [ZabbixEvent::API_CALL => 'onApiCall'];
}
public function onApiCall(ZabbixEvent $event) {
$this->logger->info('Zabbix API Call', [
'method' => $event->getMethod(),
'params' => $event->getParams(),
]);
}
}
Testing
Mock the ZabbixClient in tests:
$mock = $this->createMock(ZabbixClient::class);
$mock->method('host->get')->willReturn([['hostid' => 1, 'host' => 'Test']]);
$this->container->set('crifi_zabbix.client', $mock);
Environment-Specific Config Use Symfony’s parameter bag for environment-specific settings:
# config/packages/dev/crifi_zabbix.yaml
crifi_zabbix:
debug: true
timeout: 30
How can I help you explore Laravel packages today?