Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-130271 / 24.10 / Check SnapshotCountAlert on SMB shares only #14161

Merged
merged 9 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions src/middlewared/middlewared/alert/source/snapshot_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@ class SnapshotCountAlertClass(AlertClass):
level = AlertLevel.WARNING
title = "Too Many Snapshots Exist For Dataset"
text = (
"Dataset %(dataset)s has more snapshots (%(count)d) than recommended (%(max)d). Performance or functionality "
"might degrade."
"SMB share %(dataset)s has more snapshots (%(count)d) than recommended (%(max)d). File Explorer may not "
"display all snapshots in the Previous Versions tab."
)


class SnapshotCountAlertSource(AlertSource):
schedule = CrontabSchedule(hour=1)
run_on_backup_node = False

async def check(self):
max_ = await self.middleware.call("pool.snapshottask.max_count")
async def _check_total(self) -> Alert | None:
"""Return an `Alert` if the total number of snapshots exceeds the limit."""
max_total = await self.middleware.call("pool.snapshottask.max_total_count")

total = 0
datasets = await self.middleware.call("zfs.snapshot.count")
total = 0

for cnt in datasets.values():
yocalebo marked this conversation as resolved.
Show resolved Hide resolved
total += cnt
Expand All @@ -43,11 +42,28 @@ async def check(self):
key=None,
)

for dataset in sorted(datasets.keys()):
count = datasets[dataset]
if count > max_:
return Alert(
SnapshotCountAlertClass,
{"dataset": dataset, "count": count, "max": max_},
key=None,
)
async def _check_smb(self) -> list[Alert]:
"""Return an `Alert` for every dataset shared over smb whose number of snapshots exceeds the limit."""
max_ = await self.middleware.call("pool.snapshottask.max_count")
datasets = await self.middleware.call("zfs.snapshot.count")
smb_shares = await self.middleware.call("sharing.smb.query")
creatorcary marked this conversation as resolved.
Show resolved Hide resolved
smb_paths = [share["path"].removeprefix("/mnt/") for share in smb_shares]
to_alert = list()

for path in sorted(smb_paths):
if path in datasets:
count = datasets[path]
if count > max_:
to_alert.append(Alert(
SnapshotCountAlertClass,
{"dataset": path, "count": count, "max": max_},
key=path,
))

return to_alert

async def check(self):
creatorcary marked this conversation as resolved.
Show resolved Hide resolved
alerts = await self._check_smb()
if total_alert := await self._check_total():
creatorcary marked this conversation as resolved.
Show resolved Hide resolved
alerts.append(total_alert)
return alerts or None
4 changes: 2 additions & 2 deletions tests/api2/test_snapshot_count_alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ def test_snapshot_count_alert(request):

alert = call("alert.run_source", "SnapshotCount")[0]
assert alert["text"] % alert["args"] == (
"Dataset tank/snapshot_count has more snapshots (11) than recommended (10). Performance or "
"functionality might degrade."
"SMB share tank/snapshot_count has more snapshots (11) than recommended (10). File Explorer may not "
"display all snapshots in the Previous Versions tab."
)
Loading