Zum Hauptinhalt springen

๐Ÿ”ง Extending Monitron

Monitron SaaS is designed to be extensible. Here's how to add new monitor types, notification channels, and AI features.


๐Ÿ“ก Adding a New Monitor Typeโ€‹

Step 1: Add to Enumโ€‹

// app/Enums/MonitorType.php
case MY_CUSTOM = 'my_custom';

Add label, color, icon, category, and default port in the respective methods.

Step 2: Create Handlerโ€‹

// app/Monitors/MyCustomMonitor.php
<?php

namespace App\Monitors;

use App\DTOs\MonitorCheckResult;
use App\Enums\MonitorStatus;
use App\Enums\MonitorType;
use App\Models\Monitor;
use App\Monitors\Contracts\MonitorHandler;

class MyCustomMonitor implements MonitorHandler
{
public function check(Monitor $monitor): MonitorCheckResult
{
$start = microtime(true);

try {
// Your check logic here
$success = true;
$responseTime = (microtime(true) - $start) * 1000;

return new MonitorCheckResult(
status: $success ? MonitorStatus::UP : MonitorStatus::DOWN,
responseTimeMs: (int) $responseTime,
);
} catch (\Throwable $e) {
return new MonitorCheckResult(
status: MonitorStatus::DOWN,
errorMessage: $e->getMessage(),
);
}
}

public function type(): MonitorType
{
return MonitorType::MY_CUSTOM;
}
}

Step 3: Register Handlerโ€‹

// app/Services/MonitorRegistry.php
// Add to the constructor or register method:
$this->register('my_custom', MyCustomMonitor::class);

Step 4: Add Form Fieldsโ€‹

In MonitorResource.php, add a section for your type's specific configuration fields.


๐Ÿ”” Adding a New Notification Channelโ€‹

Step 1: Add to Enumโ€‹

// app/Enums/NotificationChannel.php
case MY_CHANNEL = 'my_channel';

Step 2: Add Dispatch Methodโ€‹

// app/Services/NotificationDispatcher.php
protected function sendMyChannel(array $config, string $message, array $context): bool
{
// Your notification logic
return true;
}

Step 3: Wire into Dispatchโ€‹

Add a case for your channel in the main dispatch() method of NotificationDispatcher.


๐Ÿค– Adding a New AI Featureโ€‹

Step 1: Create Serviceโ€‹

// app/Services/AI/MyFeature.php
<?php

namespace App\Services\AI;

class MyFeature
{
public function __construct(protected AiService $ai) {}

public function analyze(): ?array
{
if (!$this->ai->isConfigured()) return null;

return $this->ai->json(
'Your system prompt here',
'Your user message with context'
);
}
}

Step 2: Register as Singletonโ€‹

// app/Providers/MonitorServiceProvider.php
$this->app->singleton(MyFeature::class);

Step 3: Add Toggleโ€‹

Add a toggle in AiSettings.php and check it before using the feature.