43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
from dataclasses import dataclass
|
|
|
|
from ..models import STATE_NAMES
|
|
from .validation import PromptError
|
|
|
|
LABEL_MAX = 200
|
|
OUTPUT_MAX = 2000
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class SampleAlert:
|
|
host: str
|
|
service: str
|
|
state: int
|
|
output: str
|
|
|
|
|
|
ZPOOL_SAMPLE = SampleAlert(
|
|
host="kosext100.krs.local",
|
|
service="zpool status",
|
|
state=1,
|
|
output=("RaidZ: One or more devices has experienced an unrecoverable error. An attempt was made to "
|
|
"correct the error. Applications are unaffected. Determine if the device needs to be replaced, "
|
|
"and clear the errors using 'zpool clear' or replace the device with 'zpool replace'."),
|
|
)
|
|
|
|
STATE_CHOICES = tuple(STATE_NAMES.items())
|
|
|
|
|
|
def sample_from_row(row: dict) -> SampleAlert:
|
|
return SampleAlert(str(row["host"]), str(row["service"]), int(row["state"]), str(row["output"]))
|
|
|
|
|
|
def parse_sample(host: str, service: str, state: str, output: str) -> SampleAlert:
|
|
host, service, output = host.strip(), service.strip(), output.strip()
|
|
if not host or not service:
|
|
raise PromptError("Host und Service sind Pflicht.")
|
|
if max(len(host), len(service)) > LABEL_MAX or len(output) > OUTPUT_MAX:
|
|
raise PromptError("Host, Service oder Plugin-Output ist zu lang.")
|
|
if not state.isdecimal() or int(state) not in STATE_NAMES:
|
|
raise PromptError("Ungültiger Status.")
|
|
return SampleAlert(host, service, int(state), output)
|