LAR_Maintenance/app/Http/Controllers/MaintenanceController.php

171 lines
6.0 KiB
PHP
Raw Normal View History

2024-07-30 16:13:21 +00:00
<?php
namespace App\Http\Controllers;
2024-07-31 16:45:14 +00:00
use App\Models\MaintenanceHistory;
2024-08-16 16:20:45 +00:00
use App\Services\ZabbixService;
use Carbon\Carbon;
2024-08-07 05:52:07 +00:00
use Illuminate\Http\Request;
2024-07-31 16:45:14 +00:00
2024-07-30 16:13:21 +00:00
class MaintenanceController extends BaseController
{
public function index()
{
return view('maintenance.index');
}
2024-07-31 15:04:33 +00:00
public function planned()
{
return view('maintenance.planned');
}
2024-07-31 16:45:14 +00:00
public function plannedDetail(MaintenanceHistory $maintenance_history)
{
2024-08-16 21:37:42 +00:00
2024-08-09 06:37:00 +00:00
if (!empty($maintenance_history->finished_at)) {
abort(404);
}
2024-07-31 16:45:14 +00:00
return view('maintenance.planned-detail', [
2024-08-16 11:28:14 +00:00
'maintenance_history' => $maintenance_history ?? [],
2024-07-31 16:45:14 +00:00
]);
}
2024-08-07 05:52:07 +00:00
public function plannedDetailPut(Request $request, MaintenanceHistory $maintenance_history)
{
2024-08-16 16:20:45 +00:00
2024-08-09 06:37:00 +00:00
if (!empty($maintenance_history->finished_at)) {
abort(404);
}
2024-08-07 05:52:07 +00:00
return view('maintenance.planned-detail-done', [
'maintenance_history' => $maintenance_history,
2024-08-16 11:28:14 +00:00
'maintenance_task_status' => $request->input('maintenance_task_status') ?? [],
'maintenance_host_skipped' => $request->input('maintenance_host_skipped') ?? [],
2024-08-07 05:52:07 +00:00
]);
}
public function plannedDetailFinishPost(Request $request, MaintenanceHistory $maintenance_history)
{
2024-08-09 06:37:00 +00:00
if (!empty($maintenance_history->finished_at)) {
abort(404);
}
2024-08-16 11:28:14 +00:00
$request->validate([
"skippedHostsSummary.*.*" => 'required|string'
]);
// if ($request->has('skippedHostsSummary') || $request->has('skippedHostTasksSummary'))
// {
// return redirect()->back()->with('error', __('boilerplate::ui.incorect.old.password'));
// }
2024-08-09 06:37:00 +00:00
foreach ($request->input('skippedHostsSummary') as $history_host_id => $summary) {
$maintenance_history->historyHosts->find($history_host_id)->update([
'summary' => $summary,
]);
}
2024-08-16 11:28:14 +00:00
if ($request->has('skippedHostTasksSummary')) {
2024-08-09 06:37:00 +00:00
foreach ($maintenance_history->historyHosts as $history_host) {
2024-08-16 11:28:14 +00:00
if (!isset($request->input('skippedHostTasksSummary')[$history_host->id]))
2024-08-09 06:37:00 +00:00
continue;
2024-08-16 11:28:14 +00:00
foreach ($request->input('skippedHostTasksSummary')[$history_host->id] as $history_task_id => $task_summary) {
$history_host->historyTasks->find($history_task_id)->update([
2024-08-09 06:37:00 +00:00
'summary' => $task_summary,
]);
}
}
}
$maintenance_history->finished_at = Carbon::now();
$maintenance_history->save();
return redirect()->route('maintenance.history.detail', $maintenance_history->id);
}
2024-08-09 06:37:00 +00:00
public function history(Request $request)
{
return view('maintenance.history');
}
2024-08-09 06:37:00 +00:00
public function historyDetail(Request $request, MaintenanceHistory $maintenance_history)
{
2024-08-16 11:28:14 +00:00
return view('maintenance.history-detail-done', [
'maintenance_history' => $maintenance_history ?? []
]);
2024-08-09 06:37:00 +00:00
}
2024-08-16 21:37:42 +00:00
public function start(Request $request, MaintenanceHistory $maintenance_history)
{
$hostsForMaintenance = [];
foreach ($maintenance_history->historyHosts as $historyHost) {
if (empty($historyHost->host->external_id)) {
continue;
}
$hostsForMaintenance[] = [
"hostid" => (int) $historyHost->host->external_id,
];
}
$zabbix = new ZabbixService(config('zabbix.url'));
$zabbix->connect(config('zabbix.username'), config('zabbix.password'));
if (empty($maintenance_history->maintenance->external_id)) {
$maintenance_history->maintenance->external_id = $zabbix->maintenancesCreate([
"name" => $maintenance_history->maintenance->name . ' - ' . $maintenance_history->start_at->format('Y'),
"description" => strip_tags($maintenance_history->maintenance->description),
"active_since" => $maintenance_history->start_at->startofyear()->timestamp,
"active_till" => $maintenance_history->start_at->endofyear()->timestamp,
"tags_evaltype" => 0,
"hosts" => $hostsForMaintenance,
"timeperiods" => [[
"timeperiod_type" => 0,
"start_date" => $maintenance_history->start_at->timestamp,
"period" => (($maintenance_history->maintenance->duration * 60) * 60),
]],
])['maintenanceids'][0];
$maintenance_history->maintenance->save();
} else {
$createPeriode = true;
$timePeriodes = $zabbix->maintenances([
"selectTimeperiods" => "extend",
'maintenanceids' => $maintenance_history->maintenance->external_id,
])[0]['timeperiods'];
foreach ($timePeriodes as $periode) {
if ((int) $periode['start_date'] === $maintenance_history->start_at->timestamp) {
$createPeriode = false;
}
}
if ($createPeriode) {
for ($i=0; $i < count($timePeriodes); $i++) {
unset($timePeriodes[$i]['every']);
unset($timePeriodes[$i]['month']);
unset($timePeriodes[$i]['dayofweek']);
unset($timePeriodes[$i]['day']);
unset($timePeriodes[$i]['start_time']);
}
$timePeriodes[] = [
"timeperiod_type" => 0,
"start_date" => $maintenance_history->start_at->timestamp,
"period" => (($maintenance_history->maintenance->duration * 60) * 60),
];
$zabbix->maintenancesUpdate([
"maintenanceid" => $maintenance_history->maintenance->external_id,
"timeperiods" => $timePeriodes,
]);
}
}
return redirect()->back();
}
2024-07-30 16:13:21 +00:00
}