Skip to content

Commit

Permalink
Merge pull request #2998 from stveit/feature/create-event-when-chassi…
Browse files Browse the repository at this point in the history
…s-or-module-is-removed-manually

Create event when chassis or module is removed manually
  • Loading branch information
stveit authored Sep 24, 2024
2 parents 6c1beb8 + 73739ee commit d54881a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/2983.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Post event when a module or chassis is deleted via the status page or device history
8 changes: 4 additions & 4 deletions doc/reference/alerttypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.



Expand Down Expand Up @@ -567,7 +571,3 @@ UPS running on battery or utility power
- Ups running on battery power
* - ``upsOnUtilityPower``
- Ups running on utility power




Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Subject: Device has been removed as a chassis
Device {{device}} has been removed from {{netbox}} as a chassis
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Device {{device}} has been removed from {{netbox}} as a chassis
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Subject: Device has been removed as a module
Device {{device}} has been removed from {{netbox}} as a module
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Device {{device}} has been removed from {{netbox}} as a module
5 changes: 5 additions & 0 deletions python/nav/models/sql/changes/sc.05.11.0002.sql
Original file line number Diff line number Diff line change
@@ -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.');
11 changes: 10 additions & 1 deletion python/nav/web/devicehistory/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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'))

Expand Down
28 changes: 26 additions & 2 deletions python/nav/web/status2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,6 +34,8 @@

_logger = logging.getLogger(__name__)

device_event = EventFactory('ipdevpoll', 'eventEngine', 'deviceState')


class StatusView(View):
"""Generic Status view"""
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d54881a

Please sign in to comment.