diff --git a/changelog.d/2983.added.md b/changelog.d/2983.added.md new file mode 100644 index 0000000000..32873336e5 --- /dev/null +++ b/changelog.d/2983.added.md @@ -0,0 +1 @@ +Post event when a module or chassis is deleted via the status page or device history \ No newline at end of file diff --git a/doc/reference/alerttypes.rst b/doc/reference/alerttypes.rst index 8dde9d04e4..3a9f7a0be1 100644 --- a/doc/reference/alerttypes.rst +++ b/doc/reference/alerttypes.rst @@ -217,6 +217,10 @@ Registers the state of a device - The device has been removed as a fan. * - ``deviceDeletedPsu`` - The device has been removed as a power supply. + * - ``deviceDeletedChassis`` + - The device has been removed as a chassis. + * - ``deviceDeletedModule`` + - The device has been removed as a module. @@ -567,7 +571,3 @@ UPS running on battery or utility power - Ups running on battery power * - ``upsOnUtilityPower`` - Ups running on utility power - - - - diff --git a/python/nav/etc/alertmsg/deviceState/deviceDeletedChassis-email.txt b/python/nav/etc/alertmsg/deviceState/deviceDeletedChassis-email.txt new file mode 100644 index 0000000000..b22c296021 --- /dev/null +++ b/python/nav/etc/alertmsg/deviceState/deviceDeletedChassis-email.txt @@ -0,0 +1,2 @@ +Subject: Device has been removed as a chassis +Device {{device}} has been removed from {{netbox}} as a chassis diff --git a/python/nav/etc/alertmsg/deviceState/deviceDeletedChassis-sms.txt b/python/nav/etc/alertmsg/deviceState/deviceDeletedChassis-sms.txt new file mode 100644 index 0000000000..f6ba3ccc5d --- /dev/null +++ b/python/nav/etc/alertmsg/deviceState/deviceDeletedChassis-sms.txt @@ -0,0 +1 @@ +Device {{device}} has been removed from {{netbox}} as a chassis diff --git a/python/nav/etc/alertmsg/deviceState/deviceDeletedModule-email.txt b/python/nav/etc/alertmsg/deviceState/deviceDeletedModule-email.txt new file mode 100644 index 0000000000..7a01dcc629 --- /dev/null +++ b/python/nav/etc/alertmsg/deviceState/deviceDeletedModule-email.txt @@ -0,0 +1,2 @@ +Subject: Device has been removed as a module +Device {{device}} has been removed from {{netbox}} as a module diff --git a/python/nav/etc/alertmsg/deviceState/deviceDeletedModule-sms.txt b/python/nav/etc/alertmsg/deviceState/deviceDeletedModule-sms.txt new file mode 100644 index 0000000000..093290710a --- /dev/null +++ b/python/nav/etc/alertmsg/deviceState/deviceDeletedModule-sms.txt @@ -0,0 +1 @@ +Device {{device}} has been removed from {{netbox}} as a module diff --git a/python/nav/models/sql/changes/sc.05.11.0002.sql b/python/nav/models/sql/changes/sc.05.11.0002.sql new file mode 100644 index 0000000000..386b239182 --- /dev/null +++ b/python/nav/models/sql/changes/sc.05.11.0002.sql @@ -0,0 +1,5 @@ +-- Add new deviceState alert +INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES + ('deviceState','deviceDeletedChassis','The device has been removed as a chassis.'); + INSERT INTO alerttype (eventtypeid,alerttype,alerttypedesc) VALUES + ('deviceState','deviceDeletedModule','The device has been removed as a module.'); diff --git a/python/nav/web/devicehistory/views.py b/python/nav/web/devicehistory/views.py index dcd1f68a14..1641308440 100644 --- a/python/nav/web/devicehistory/views.py +++ b/python/nav/web/devicehistory/views.py @@ -22,6 +22,7 @@ from django.shortcuts import render, redirect from django.urls import reverse +from nav.event2 import EventFactory from nav.models.fields import INFINITY from nav.models.manage import Netbox, Module from nav.models.event import AlertHistory @@ -37,6 +38,8 @@ from nav.web.devicehistory.utils.error import register_error_events from nav.web.devicehistory.forms import DeviceHistoryViewFilter +device_event = EventFactory('ipdevpoll', 'eventEngine', 'deviceState') + DEVICEQUICKSELECT_VIEW_HISTORY_KWARGS = { 'button': 'View %s history', 'module': True, @@ -289,12 +292,18 @@ def do_delete_module(request): # AlertHistory entries will be closed by a database trigger. cursor.execute("DELETE FROM module WHERE moduleid IN %s", (module_ids,)) - # Delete the entities representing these modules for hist in history: + # Delete the entity representing the module cursor.execute( "DELETE FROM netboxentity WHERE netboxid = %s and deviceid = %s", [hist.module.netbox.id, hist.module.device.id], ) + # Create event for deleted module + device_event.notify( + device=hist.module.device, + netbox=hist.module.netbox, + alert_type="deviceDeletedModule", + ).save() return HttpResponseRedirect(reverse('devicehistory-module')) diff --git a/python/nav/web/status2/views.py b/python/nav/web/status2/views.py index 128e9c64ca..2a4789d25e 100644 --- a/python/nav/web/status2/views.py +++ b/python/nav/web/status2/views.py @@ -25,6 +25,7 @@ from django.urls import reverse from nav.maintengine import check_devices_on_maintenance +from nav.event2 import EventFactory from nav.models.event import AlertHistory from nav.models.manage import Netbox, NetboxEntity, Module from nav.models.msgmaint import MaintenanceTask, MaintenanceComponent @@ -33,6 +34,8 @@ _logger = logging.getLogger(__name__) +device_event = EventFactory('ipdevpoll', 'eventEngine', 'deviceState') + class StatusView(View): """Generic Status view""" @@ -190,12 +193,33 @@ def delete_module_or_chassis(request): alerts = get_alerts_from_request( request, event_type_filter=accepted_event_types ) - module_ids = [a.subid for a in alerts if a.event_type.pk == 'moduleState'] - entity_ids = [a.subid for a in alerts if a.event_type.pk == 'chassisState'] + module_ids = [] + entity_ids = [] + notify_events = [] + + for alert in alerts: + if alert.event_type.pk == 'moduleState': + module_ids.append(alert.subid) + notify_events.append( + (alert.device, alert.netbox, "deviceDeletedModule") + ) + elif alert.event_type.pk == 'chassisState': + entity_ids.append(alert.subid) + notify_events.append( + (alert.device, alert.netbox, "deviceDeletedChassis") + ) Module.objects.filter(pk__in=module_ids).delete() NetboxEntity.objects.filter(pk__in=entity_ids).delete() resolve_alerts(alerts) + + for device, netbox, alert_type in notify_events: + device_event.notify( + device=device, + netbox=netbox, + alert_type=alert_type, + ).save() + return HttpResponse() return HttpResponse('Wrong request type', status=400)