2021-10-06 07:29:47 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\AlertTransport;
|
|
|
|
|
use App\Models\Device;
|
|
|
|
|
use Illuminate\Http\Request;
|
2023-05-23 09:25:17 -05:00
|
|
|
use LibreNMS\Alert\AlertData;
|
2022-09-05 16:20:10 -05:00
|
|
|
use LibreNMS\Exceptions\AlertTransportDeliveryException;
|
2021-10-06 07:29:47 -05:00
|
|
|
|
|
|
|
|
class AlertTransportController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function test(Request $request, AlertTransport $transport): \Illuminate\Http\JsonResponse
|
|
|
|
|
{
|
2023-05-23 09:25:17 -05:00
|
|
|
/** @var Device $device */
|
2021-10-06 07:29:47 -05:00
|
|
|
$device = Device::with('location')->first();
|
2023-05-23 09:25:17 -05:00
|
|
|
$alert_data = AlertData::testData($device);
|
2021-10-06 07:29:47 -05:00
|
|
|
|
|
|
|
|
try {
|
2023-05-23 09:25:17 -05:00
|
|
|
$result = $transport->instance()->deliverAlert($alert_data);
|
2021-10-06 07:29:47 -05:00
|
|
|
|
|
|
|
|
if ($result === true) {
|
|
|
|
|
return response()->json(['status' => 'ok']);
|
|
|
|
|
}
|
2022-09-05 16:20:10 -05:00
|
|
|
} catch (AlertTransportDeliveryException $e) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'status' => 'error',
|
2023-05-23 09:25:17 -05:00
|
|
|
'message' => strip_tags($e->getMessage()),
|
2022-09-05 16:20:10 -05:00
|
|
|
]);
|
2021-10-06 07:29:47 -05:00
|
|
|
} catch (\Exception $e) {
|
2021-10-29 22:12:20 -05:00
|
|
|
\Log::error($e);
|
|
|
|
|
$result = basename($e->getFile(), '.php') . ':' . $e->getLine() . ' ' . $e->getMessage();
|
2021-10-06 07:29:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'status' => 'error',
|
2023-05-23 09:25:17 -05:00
|
|
|
'message' => strip_tags($result),
|
2021-10-06 07:29:47 -05:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|