mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Apply service templates on a per-device basis (#15024)
* Created and use a function to apply service templates on a per-device basis * formatting fix
This commit is contained in:
@@ -309,6 +309,21 @@ class ServiceTemplateController extends Controller
|
|||||||
return response($msg, 200);
|
return response($msg, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply all Service Templates for a device
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\View\View
|
||||||
|
*/
|
||||||
|
public function applyDeviceAll(int $device_id)
|
||||||
|
{
|
||||||
|
foreach (ServiceTemplate::all() as $template) {
|
||||||
|
$this->applyDevice($template, $device_id);
|
||||||
|
}
|
||||||
|
$msg = __('All Service Templates have been applied to device ' . $device_id);
|
||||||
|
|
||||||
|
return response($msg, 200);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply specified Service Template.
|
* Apply specified Service Template.
|
||||||
*
|
*
|
||||||
@@ -329,6 +344,66 @@ class ServiceTemplateController extends Controller
|
|||||||
return response($msg, 200);
|
return response($msg, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply specified Service Template to a device.
|
||||||
|
*
|
||||||
|
* @param \App\Models\ServiceTemplate $template
|
||||||
|
* @param int $device_id
|
||||||
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\View\View
|
||||||
|
*/
|
||||||
|
public function applyDevice(ServiceTemplate $template, int $device_id)
|
||||||
|
{
|
||||||
|
// Check if the device needs to be added
|
||||||
|
foreach (Device::inServiceTemplate($template->id)->where('device_id', $device_id)->get() as $device) {
|
||||||
|
$device->services()->updateOrCreate(
|
||||||
|
[
|
||||||
|
'service_template_id' => $template->id,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'service_name' => $template->name,
|
||||||
|
'service_type' => $template->check,
|
||||||
|
'service_template_id' => $template->id,
|
||||||
|
'service_param' => $template->param,
|
||||||
|
'service_ip' => $template->ip,
|
||||||
|
'service_desc' => $template->desc,
|
||||||
|
'service_disabled' => $template->disabled,
|
||||||
|
'service_ignore' => $template->ignore,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return response('Service template ' . $template->id . ' applied to device ID ' . $device_id, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (DeviceGroup::inServiceTemplate($template->id)->get() as $device_group) {
|
||||||
|
foreach (Device::inDeviceGroup($device_group->id)->where('device_id', $device_id)->get() as $device) {
|
||||||
|
$device->services()->updateOrCreate(
|
||||||
|
[
|
||||||
|
'service_template_id' => $template->id,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'service_name' => $template->name,
|
||||||
|
'service_type' => $template->check,
|
||||||
|
'service_template_id' => $template->id,
|
||||||
|
'service_param' => $template->param,
|
||||||
|
'service_ip' => $template->ip,
|
||||||
|
'service_desc' => $template->desc,
|
||||||
|
'service_disabled' => $template->disabled,
|
||||||
|
'service_ignore' => $template->ignore,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return response('Service template ' . $template->id . ' applied to device ID ' . $device_id, 200);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove if this template no longer applies
|
||||||
|
foreach (Device::notInServiceTemplate($template->id)->notInDeviceGroup($template->groups->pluck('id'))->where('device_id', $device_id)->pluck('device_id') as $device_id) {
|
||||||
|
Service::where('device_id', $device_id)->where('service_template_id', $template->id)->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
return response('Service template ' . $template->id . ' applied to device ID ' . $device_id, 200);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove specified Service Template.
|
* Remove specified Service Template.
|
||||||
*
|
*
|
||||||
|
@@ -4,7 +4,7 @@ use App\Http\Controllers\ServiceTemplateController;
|
|||||||
use LibreNMS\Config;
|
use LibreNMS\Config;
|
||||||
|
|
||||||
if (Config::get('discover_services_templates')) {
|
if (Config::get('discover_services_templates')) {
|
||||||
(new ServiceTemplateController())->applyAll(); // FIXME applyAll() should not be on a controller
|
(new ServiceTemplateController())->applyDeviceAll($device['device_id']); // FIXME applyAll() should not be on a controller
|
||||||
}
|
}
|
||||||
if (Config::get('discover_services')) {
|
if (Config::get('discover_services')) {
|
||||||
// FIXME: use /etc/services?
|
// FIXME: use /etc/services?
|
||||||
|
Reference in New Issue
Block a user