Skip to content

Commit

Permalink
Avoid raising exceptions for unmounted volumes in Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Jan 3, 2025
1 parent cd2a20c commit 64c1b38
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pyudisk start
> _By default, `PyUdisk` will look for a `.env` file in the current working directory._
</details>
- **SMART_LIB**: Path to the `udisksctl` command-line tool. Default: `/usr/bin/udisksctl`
- **SMART_LIB**: Path to the S.M.A.R.T CLI library. Uses `udisksctl` for Linux and `smartctl` for macOS.
- **DISK_LIB**: Path to disk util library. Uses `lsblk` for Linux and `diskutil` for macOS.
- **METRICS**: List of metrics to monitor. Default: `[]`
- **GMAIL_USER**: Gmail username to authenticate SMTP library.
- **GMAIL_PASS**: Gmail password to authenticate SMTP library.
Expand Down
2 changes: 1 addition & 1 deletion pyudisk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .main import EnvConfig, generate_report, monitor, smart_metrics # noqa: F401

version = "0.3.2a0"
version = "1.1.0a0"


@click.command()
Expand Down
26 changes: 15 additions & 11 deletions pyudisk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,22 @@ def smart_metrics(env: EnvConfig) -> Generator[linux.Disk | darwin.Disk]:
smart_dump = get_smart_metrics(env)
block_devices = parse_block_devices(env, smart_dump)
drives = {k: v for k, v in sorted(parse_drives(smart_dump).items())}
diff = set()
# Enable mount warning by default (log warning messages if disk is not mounted)
mount_warning = os.environ.get("MOUNT_WARNING", "1") == "1"
# A drive can have multiple partitions, but any mounted drive should have at least one partition
if len(block_devices) < len(drives):
LOGGER.warning(
LOGGER.debug(
f"Number of block devices [{len(block_devices)}] is less than the number of drives [{len(drives)}]"
)
device_names = set(block_devices.keys())
drive_names = set(drives.keys())
diff = (
drive_names - device_names
if len(drive_names) > len(device_names)
else device_names - drive_names
)
LOGGER.warning("UNmounted drive(s) found - '%s'", ", ".join(diff))
device_names = set(sorted(block_devices.keys()))
drive_names = set(sorted(drives.keys()))
if len(drive_names) > len(device_names):
diff = drive_names - device_names
else:
diff = device_names - drive_names
if diff and mount_warning:
LOGGER.warning("UNmounted drive(s) found - '%s'", ", ".join(diff))
optional_fields = [
k
for k, v in linux.Disk.model_json_schema().get("properties").items()
Expand All @@ -350,8 +353,9 @@ def smart_metrics(env: EnvConfig) -> Generator[linux.Disk | darwin.Disk]:
yield linux.Disk(
id=drive, model=data.get("Info", {}).get("Model", ""), **data
)
else:
raise ValueError(f"{drive} not found in {drives.keys()}")
elif drive not in diff:
# Check if this issue has been caught in unmounted warnings already - if so, skip the warning
LOGGER.warning(f"{drive} not found in {block_devices.keys()}")


def generate_html(
Expand Down

0 comments on commit 64c1b38

Please sign in to comment.