orphan disk search update
This commit is contained in:
+20
-5
@@ -3,6 +3,7 @@
|
||||
import argparse
|
||||
import csv
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
@@ -146,7 +147,7 @@ def scan(root=Path('/etc/pve/nodes'), get=api):
|
||||
for volume in disks:
|
||||
result = classify(volume, name, shared, refs, bool(errors))
|
||||
if result:
|
||||
rows.append(dict(zip(FIELDS, [label, sid, volume['volid'], volume.get('size', 0), *result])))
|
||||
rows.append(dict(zip(FIELDS, [label, sid, volume['volid'], volume.get('size'), *result])))
|
||||
# Detect migrations/config changes during this non-atomic inventory.
|
||||
_, after_errors, after = references(root, nodes, guests)
|
||||
if after != before or after_errors:
|
||||
@@ -156,6 +157,19 @@ def scan(root=Path('/etc/pve/nodes'), get=api):
|
||||
return rows, warnings, coverage
|
||||
|
||||
|
||||
def format_size(size_bytes):
|
||||
try:
|
||||
size = float(size_bytes)
|
||||
except (TypeError, ValueError):
|
||||
return 'Unknown'
|
||||
if not math.isfinite(size) or size < 0:
|
||||
return 'Unknown'
|
||||
for unit in ('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'):
|
||||
if size < 1024 or unit == 'PiB':
|
||||
return f'{size:.2f} {unit}'
|
||||
size /= 1024
|
||||
|
||||
|
||||
def render(rows, warnings, coverage):
|
||||
print('\nCluster orphaned / unused disk report (read-only)')
|
||||
print('Coverage: ' + ('INCOMPLETE / NEEDS REVIEW' if warnings else 'all discovered disk storage scanned'))
|
||||
@@ -169,8 +183,8 @@ def render(rows, warnings, coverage):
|
||||
if group != (row['node'], row['storage']):
|
||||
group = (row['node'], row['storage'])
|
||||
print(f'\n{group[0]} / {group[1]}')
|
||||
size = float(row['size_bytes'] or 0) / (1024 ** 3)
|
||||
print(f" {row['volume']} {size:.2f} GiB {row['status']}\n {row['detail']}")
|
||||
size = format_size(row['size_bytes'])
|
||||
print(f" {row['volume']} Disk size: {size} {row['status']}\n {row['detail']}")
|
||||
print(f'\n{len(rows)} volume(s) reported.')
|
||||
if not rows:
|
||||
print('No unreferenced or unused volumes found in the successfully scanned storage.')
|
||||
@@ -179,10 +193,11 @@ def render(rows, warnings, coverage):
|
||||
def export_csv(path, rows, warnings):
|
||||
# Exclusive creation avoids overwriting existing files, including symlink targets.
|
||||
with open(path, 'x', newline='') as handle:
|
||||
writer = csv.DictWriter(handle, fieldnames=FIELDS + ['coverage_warnings'])
|
||||
writer = csv.DictWriter(handle, fieldnames=FIELDS + ['disk_size', 'coverage_warnings'])
|
||||
writer.writeheader()
|
||||
for row in rows:
|
||||
writer.writerow(dict(row, coverage_warnings='; '.join(warnings)))
|
||||
writer.writerow(dict(row, disk_size=format_size(row['size_bytes']),
|
||||
coverage_warnings='; '.join(warnings)))
|
||||
if warnings and not rows:
|
||||
writer.writerow({'status': 'Incomplete scan', 'coverage_warnings': '; '.join(warnings)})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user