Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions docs/monitors/zpool.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
zpool - zpool status
^^^^^^^^^^^^^^^^^^^^

Check zpools' statuses to make sure they're ``ONLINE``.

.. confval:: pools

:type: comma-separated list of string
:required: false
:default: none (all pools)

the pools to have their status checked. If empty, all pools are checked.
14 changes: 7 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ring-doorbell = ">=0.6.0"
paramiko = ">=2.7.2,<4.0.0"
pyaarlo = ">=0.7.1.3,<0.9.0.0"
icmplib = "^3.0.3"
packaging = "^25.0"

[tool.poetry.group.dev.dependencies]
boto3-stubs = {extras = ["sns"], version = "^1.40.47"}
Expand Down
2 changes: 2 additions & 0 deletions simplemonitor/Monitors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
MonitorPortAudit,
MonitorSwap,
MonitorZap,
MonitorZpool,
)
from .network import (
MonitorDNS,
Expand Down Expand Up @@ -75,5 +76,6 @@
"MonitorUnixService",
"MonitorWindowsDHCPScope",
"MonitorZap",
"MonitorZpool",
"RemoteHostsMonitor",
]
106 changes: 105 additions & 1 deletion simplemonitor/Monitors/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import shlex
import subprocess # nosec
import time
from typing import Tuple, cast
from typing import Optional, Tuple, cast

from markupsafe import escape
from packaging.version import parse

from ..util import bytes_to_size_string, size_string_to_bytes
from .monitor import Monitor, register
Expand Down Expand Up @@ -599,3 +600,106 @@ def get_params(self) -> Tuple:
self.result_max,
self.show_output,
)


@register
class MonitorZpool(Monitor):
"""Check zpool status is healthy"""

monitor_type = "zpool"

def __init__(self, name: str, config_options: dict) -> None:
super().__init__(name, config_options)
self.pools = cast(
list[str],
map(
str.strip,
self.get_config_option("pools", required_type="str").split(","),
),
)
self.use_json = False
try:
zpool_output = subprocess.run(
["zpool", "--version"], capture_output=True
).stdout.decode() # nosec
zpool_version = parse(zpool_output.splitlines()[0].split("-")[1])
if zpool_version >= parse("2.4.0"):
self.use_json = True
except Exception:
self.monitor_logger.warning(
"Failed to divine zpool version; using text parsing"
)

def describe(self) -> str:
if not self.pools:
pools = "all zpools"
else:
pools = "zpools: " + ", ".join(self.pools)
return f"Checking zpool status for {pools}"

def get_params(self) -> Tuple:
return (self.pools,)

def _run_test_json(self) -> Optional[str]:
"""Return error information, or None if all ok."""
messages: list[str] = []
cmd = ["zpool", "status", "-j"]
if self.pools:
cmd.extend(self.pools)
try:
_output = subprocess.run(cmd, capture_output=True)
except subprocess.SubprocessError:
return "Failed to run zpool status"
try:
zpool_info = json.loads(_output.stdout.decode())
except Exception:
return "Failed to parse zpool JSON output"
for pool in zpool_info["pools"].values():
if pool["state"] != "ONLINE":
messages.append(f"pool {pool['name']} is {pool['state']}")
if len(messages) == 0:
return None
return ", ".join(messages)

def _run_test_text(self) -> Optional[str]:
"""Return error information, or None if all ok."""
messages: list[str] = []
cmd = ["zpool", "status"]
if self.pools:
cmd.extend(self.pools)
try:
_output = subprocess.run(cmd, capture_output=True)
except subprocess.SubprocessError:
return "Failed to run zpool status"
zpool_info = _output.stdout.decode()
current_pool = None
for line in zpool_info.splitlines():
matches = re.match(r" *pool: (.+)", line)
if matches:
if current_pool:
return "Failed to parse zpool status output"
current_pool = matches.group(1)
continue
matches = re.match(r" *state: ([A-Z_]+)", line)
if matches:
if not current_pool:
return "Failed to parse zpool status output"
status = matches.group(1)
if status != "ONLINE":
messages.append(f"pool {current_pool} is {matches.group(1)}")
current_pool = None
if current_pool:
messages.append(f"Failed to find status for pool {current_pool}")
if len(messages) == 0:
return None
return ", ".join(messages)

def run_test(self) -> bool:
if self.use_json:
message = self._run_test_json()
else:
message = self._run_test_text()
if message:
return self.record_fail(message)
else:
return self.record_success("all pools ONLINE")
Loading