PHP code example of niladam / laravel-zabbix

1. Go to this page and download the library: Download niladam/laravel-zabbix library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

niladam / laravel-zabbix example snippets


return [
    /**
     * Your Zabbix server hostname.
     *
     * Just the hostname of the server.
     */
    'server' => env('ZABBIX_SERVER'),

    /**
     * Your zabbix port.
     *
     * If different from the default 10051.
     */
    'port' => env('ZABBIX_PORT', 10051),

    /**
     * Available hosts.
     *
     * Eeach key in the hosts array must contain the host_name and key keys.
     */
    'hosts' => [
        'default' => [
            'host_name' => env('ZABBIX_DEFAULT_HOSTNAME'),
            'key' => env('ZABBIX_DEFAULT_KEY'),
        ],
    ],
];


return [
    'server' => env('ZABBIX_SERVER', '127.0.0.1'),
    'port' => env('ZABBIX_PORT', 10051),
    'hosts' => [
        'default' => [
            'host_name' => 'default-host',
            'key' => 'default-key',
        ],
        'critical' => [
            'host_name' => 'critical-host',
            'key' => 'critical-key',
        ],
    ],
];

use Niladam\LaravelZabbix\ZabbixManager;
use Niladam\LaravelZabbix\Communication\Message;
use Niladam\LaravelZabbix\Communication\Response;

$manager = app(ZabbixManager::class);

// Create a message with the 'critical' configuration
$criticalMessage = Message::make('critical')->usingValue('Service is down');
$someOtherMessage = Message::make('default')->usingValue('Just another message');

// Send the message
$manager
    ->add($criticalMessage)
    ->add($someOtherMessage);

$response = $manager->send(); // Returns a Response class.

// Some useful methods:
$response->getSummary();            // Get a summary
$response->getDuration();           // Get duration
$response->getTotalCount();         // Get total count
$response->getProcessedCount();     // Get processed count
$response->getFailedCount();        // Get failed count

// The summary looks like:
$summary = [
    "success" => true,
    "humanDuration" => "66 µs",
    "processed" => 1,
    "failed" => 0,
    "total" => 1,
    "duration" => 6.6E-5,
];

// You can also use the message directly:
$response = Message::make('critical')->usingValue('Service is down')->send();

namespace App\Alerts\Zabbix;

use Niladam\LaravelZabbix\Notifications\ZabbixAlert;

class MyZabbixAlert extends ZabbixAlert
{
    /**
     * @return string|array
     */
    public function getHostConfiguration()
    {
        // Returning a string assumes you have a named configuration.
        return 'my-host-configuration-key-from-config';
        
        // OR
        
        // Returning an array, will use the host details
        return [
            'host_name' => 'your-hostname',
            'key' => 'your-key',
        ];
    }
    
    // If you have a getMessage method on your alert, this will be used.
    public function getMessage(): string
    {
        return 'Oh noes, something happened!';
    }
}

use Illuminate\Support\Facades\Notification;

Notification::sendNow(User::system(), MyZabbixAlert::make('Oh yes, i have changed the message'));
Notification::route('zabbix','',)->notify(MyZabbixAlert::make('Oh yes, i have changed the message'));

// Or simply send it out.
MyZabbixAlert::make()->send();

use Illuminate\Notifications\Notification;
use Niladam\LaravelZabbix\Communication\Message;
use Niladam\LaravelZabbix\Notifications\ZabbixChannel;

class ServerDownNotification extends Notification
{
    public function via($notifiable): array
    {
        return [ZabbixChannel::class];
    }

    public function toZabbix($notifiable): Message
    {
        return Message::make('default')->usingValue('Server is down');
    }
}

use Niladam\LaravelZabbix\ZabbixManager;

$zabbix = app(ZabbixManager::class);

// List the configured hosts
$zabbix->availableHostNames();
$zabbix->getConfig(); // Get the configuration

use Niladam\LaravelZabbix\ZabbixManager;
use Niladam\LaravelZabbix\Communication\Message;

$zabbix = app(ZabbixManager::class); // This uses the default server which will be overwritten below.

$zabbix->usingServer([
    'server' => 'temporary-server',
    'port' => 10052,
])
    ->add(
        Message::make()->usingValue('Temporary alert')
    )
    ->add(
        Message::make()
            ->usingHost('another-host')
            ->usingKey('different_key')
            ->usingValue('Something else')
   );

$zabbix->send();



use Niladam\LaravelZabbix\ZabbixManager;
use Niladam\LaravelZabbix\Communication\Message;

$configuration = [
    'server' => 'my-zabbix-server',
    'port' => 10051,
    'hosts' => [
        'default' => [
            'host_name' => 'my default host',
            'key' => 'the host key',
        ],
    ],
];

$manager = new ZabbixManager($configuration);

Message::setDefaultManager($manager);

$response = Message::make()->usingValue('test using single class')->send();

$response->getSummary();

// OR, multiple.

$manager->add(Message::make()->usingHost('another-host')->usingKey('some-key')->usingValue('test using multiple classes'));

$manager->add(Message::make()->usingConfigurationKey('default')->usingValue('test using configuration key'));

$manager->send();

bash
php artisan vendor:publish --provider="Niladam\LaravelZabbix\LaravelZabbixServiceProvider"