82 lines
2.5 KiB
PHP
82 lines
2.5 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Notifications;
|
||
|
|
|
||
|
|
use App\Models\TcpPort;
|
||
|
|
use Illuminate\Bus\Queueable;
|
||
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
||
|
|
use Illuminate\Notifications\Notification;
|
||
|
|
|
||
|
|
class TcpPortAlert extends Notification implements ShouldQueue
|
||
|
|
{
|
||
|
|
use Queueable;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a new notification instance.
|
||
|
|
*/
|
||
|
|
public function __construct(
|
||
|
|
public TcpPort $tcpPort,
|
||
|
|
public string $status
|
||
|
|
) {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the notification's delivery channels.
|
||
|
|
*
|
||
|
|
* @return array<int, string>
|
||
|
|
*/
|
||
|
|
public function via(object $notifiable): array
|
||
|
|
{
|
||
|
|
return ['mail', 'database'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the mail representation of the notification.
|
||
|
|
*/
|
||
|
|
public function toMail(object $notifiable): MailMessage
|
||
|
|
{
|
||
|
|
$subject = $this->status === 'closed'
|
||
|
|
? "TCP Port {$this->tcpPort->port} is Closed"
|
||
|
|
: "TCP Port {$this->tcpPort->port} is Now Open";
|
||
|
|
|
||
|
|
$message = (new MailMessage)
|
||
|
|
->subject($subject)
|
||
|
|
->line("Port monitoring alert for {$this->tcpPort->site->name}");
|
||
|
|
|
||
|
|
if ($this->status === 'closed') {
|
||
|
|
$message->error()
|
||
|
|
->line("Port {$this->tcpPort->display_name} ({$this->tcpPort->port}) is currently closed.")
|
||
|
|
->line("This could indicate a service outage or firewall issue.");
|
||
|
|
} else {
|
||
|
|
$message->success()
|
||
|
|
->line("Port {$this->tcpPort->display_name} ({$this->tcpPort->port}) is now accessible.")
|
||
|
|
->line("The service has been restored.");
|
||
|
|
}
|
||
|
|
|
||
|
|
return $message
|
||
|
|
->line("Site: {$this->tcpPort->site->url}")
|
||
|
|
->line("Checked at: {$this->tcpPort->last_checked_at->format('Y-m-d H:i:s')}")
|
||
|
|
->action('View Details', url("/sites/{$this->tcpPort->site_id}"));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the array representation of the notification.
|
||
|
|
*
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
public function toArray(object $notifiable): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'type' => 'tcp_port',
|
||
|
|
'tcp_port_id' => $this->tcpPort->id,
|
||
|
|
'site_id' => $this->tcpPort->site_id,
|
||
|
|
'site_name' => $this->tcpPort->site->name,
|
||
|
|
'port' => $this->tcpPort->port,
|
||
|
|
'port_name' => $this->tcpPort->display_name,
|
||
|
|
'status' => $this->status,
|
||
|
|
'is_open' => $this->tcpPort->is_open,
|
||
|
|
'checked_at' => $this->tcpPort->last_checked_at,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|