From ce588ffc69d55b62ad9259dc4ce8c26926e3fd65 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Fri, 20 Sep 2024 09:25:44 +0200 Subject: [PATCH 1/7] Add new alerttype to db --- python/nav/models/sql/changes/sc.05.10.0003.sql | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 python/nav/models/sql/changes/sc.05.10.0003.sql diff --git a/python/nav/models/sql/changes/sc.05.10.0003.sql b/python/nav/models/sql/changes/sc.05.10.0003.sql new file mode 100644 index 0000000000..386b239182 --- /dev/null +++ b/python/nav/models/sql/changes/sc.05.10.0003.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.'); From 86e088653981e6b3d301b9bad9a1c1473e4cd805 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Fri, 20 Sep 2024 09:27:06 +0200 Subject: [PATCH 2/7] Add new alerttypes to docs --- doc/reference/alerttypes.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 - - - - From 7712fa845452e60ae2cea7a0aba90a8d2116e1b2 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Fri, 20 Sep 2024 09:30:52 +0200 Subject: [PATCH 3/7] Add alertsmg templates for new alerttypes --- .../nav/etc/alertmsg/deviceState/deviceDeletedChassis-email.txt | 2 ++ .../nav/etc/alertmsg/deviceState/deviceDeletedChassis-sms.txt | 1 + .../nav/etc/alertmsg/deviceState/deviceDeletedModule-email.txt | 2 ++ python/nav/etc/alertmsg/deviceState/deviceDeletedModule-sms.txt | 1 + 4 files changed, 6 insertions(+) create mode 100644 python/nav/etc/alertmsg/deviceState/deviceDeletedChassis-email.txt create mode 100644 python/nav/etc/alertmsg/deviceState/deviceDeletedChassis-sms.txt create mode 100644 python/nav/etc/alertmsg/deviceState/deviceDeletedModule-email.txt create mode 100644 python/nav/etc/alertmsg/deviceState/deviceDeletedModule-sms.txt 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 From 203c504744fa29ea83b08f89e3f17f69a9805116 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Fri, 20 Sep 2024 09:53:04 +0200 Subject: [PATCH 4/7] Create events for deleted chassis/module in status2 --- python/nav/web/status2/views.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) 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) From 2d7a626416b24242488dacef5c538eac2349c233 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Fri, 20 Sep 2024 10:26:59 +0200 Subject: [PATCH 5/7] Create events for modules deleted via devicehistory --- python/nav/web/devicehistory/views.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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')) From eb576eb8cb07ed3f85a8f6297c781cd82da91b35 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Mon, 23 Sep 2024 15:25:58 +0200 Subject: [PATCH 6/7] Add newsfragment --- changelog.d/2983.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/2983.added.md 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 From 73739eec0cb1d9ca13d40c4ebef50b8575c5b7f7 Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Tue, 24 Sep 2024 15:56:15 +0200 Subject: [PATCH 7/7] Fix sql changefile name to correct version --- .../models/sql/changes/{sc.05.10.0003.sql => sc.05.11.0002.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename python/nav/models/sql/changes/{sc.05.10.0003.sql => sc.05.11.0002.sql} (100%) diff --git a/python/nav/models/sql/changes/sc.05.10.0003.sql b/python/nav/models/sql/changes/sc.05.11.0002.sql similarity index 100% rename from python/nav/models/sql/changes/sc.05.10.0003.sql rename to python/nav/models/sql/changes/sc.05.11.0002.sql