puppet: Factor out pattern of writing a nagios state file atomically.

This commit is contained in:
Alex Vandiver
2024-05-22 04:22:22 +00:00
committed by Tim Abbott
parent 230040caa9
commit f246b82f67
10 changed files with 138 additions and 159 deletions

View File

@@ -16,7 +16,7 @@ import sys
import time
import uuid
from datetime import datetime, timedelta
from typing import IO, Any, Dict, List, Optional, Sequence, Set, Union, overload
from typing import IO, Any, Dict, List, Literal, Optional, Sequence, Set, Union, overload
from urllib.parse import SplitResult
import zoneinfo
@@ -723,6 +723,32 @@ def listening_publicly(port: int) -> List[str]:
return [line.split()[4] for line in lines]
def atomic_nagios_write(
name: str,
status: Literal["ok", "warning", "critical", "unknown"],
message: Optional[str] = None,
event_time: Optional[int] = None,
) -> int:
if message is None:
message = status
if event_time is None:
event_time = int(time.time())
if status == "ok":
status_int = 0
elif status == "warning":
status_int = 1
elif status == "critical":
status_int = 2
elif status == "unknown":
status_int = 3
path = "/var/lib/nagios_state/" + name
with open(path + ".tmp", "w") as fh:
fh.write("|".join([str(event_time), str(status_int), status, message]) + "\n")
os.rename(path + ".tmp", path)
return status_int
if __name__ == "__main__":
cmd = sys.argv[1]
if cmd == "make_deploy_path":