sunetdrive/templates/application/check_nextcloud_mounts.py

42 lines
1.4 KiB
Python
Raw Normal View History

2025-03-05 12:49:32 +01:00
#!/usr/bin/env python3
from collections import Counter
import json
import shlex
import subprocess
import sys
exit = 0
base_message = "OK: no duplicate mounts"
long_message = ""
2025-03-05 12:49:32 +01:00
get_containers = subprocess.Popen('/usr/local/bin/get_containers', stdout=subprocess.PIPE).stdout.read()
containers = get_containers.decode().splitlines()
for i, container in enumerate(containers, start=1):
2025-03-06 15:42:32 +01:00
buckets = []
2025-03-05 12:49:32 +01:00
list_command = f"/usr/local/bin/nocc {container} files_external:list --all --show-password --output json"
command = shlex.split(list_command)
mount_data_byte = subprocess.Popen(command, stdout=subprocess.PIPE).stdout.read()
try:
mount_data = json.loads(mount_data_byte.decode())
except json.decoder.JSONDecodeError as err:
if i == 1 or i != len(containers):
base_message = "WARNING: invalid json"
long_message += f"\ncontainer: {container} - json decode error: {err}"
# lets do exit 0 for now
# exit = 1
continue
2025-03-05 12:49:32 +01:00
for items in mount_data:
buckets.append(items["configuration"]["bucket"])
bucket_count = dict(Counter(buckets))
for k, v in bucket_count.items():
2025-03-05 12:49:32 +01:00
if v > 1:
base_message = "WARNING: buckets with multiple mounts"
long_message += f"\ncontainer: {container} - bucket: {k} - {v}"
2025-03-05 12:49:32 +01:00
# lets do exit 0 for now
# exit = 1
print(base_message)
print(long_message.lstrip())
2025-03-05 12:49:32 +01:00
sys.exit(exit)