check_systemd_timer: handle running timer
This commit is contained in:
parent
57a8b24fc3
commit
02164d257a
|
@ -18,12 +18,12 @@ from checker.result import quit_check
|
|||
sys.path.insert(0, "/usr/lib/python3/dist-packages")
|
||||
import dbus
|
||||
|
||||
SYSTEMCTL_STATUS_RE = r'Loaded:\s.*\.timer;\s(.*?);.*?\)\s*Active:\s(.*?) since.*?\s*Trigger:\s(.*?);'
|
||||
SYSTEMCTL_STATUS_RE = r'Loaded:\s.*\.timer;\s(.*?);.*?\)\s*Active:\s(.*?) since.*?\s*Trigger:\s(.*?;|n\/a)'
|
||||
|
||||
|
||||
class TimerInfo(BaseModel):
|
||||
next: datetime
|
||||
time_left: timedelta
|
||||
next: Union[datetime, None]
|
||||
time_left: Union[timedelta, None]
|
||||
last: Union[datetime, None]
|
||||
since_last: Union[timedelta, None]
|
||||
unit: str
|
||||
|
@ -49,18 +49,24 @@ def get_last_trigger(timer_name: str):
|
|||
|
||||
|
||||
def get_next_elapse(timer_name: str) -> Tuple[TimerInfo | None, None | str]:
|
||||
now = datetime.now()
|
||||
try:
|
||||
output = subprocess.check_output(["systemctl", "status", timer_name], universal_newlines=True)
|
||||
if timer_name in output.split('\n')[0]:
|
||||
try:
|
||||
parts = re.search(SYSTEMCTL_STATUS_RE, output)
|
||||
|
||||
next_trigger = parse(parts.group(3))
|
||||
now = datetime.now(tz=next_trigger.tzinfo)
|
||||
time_left = next_trigger - now
|
||||
next_trigger_str = parts.group(3)
|
||||
if next_trigger_str.lower() != 'n/a':
|
||||
next_trigger = parse(next_trigger_str)
|
||||
time_left = next_trigger - now.replace(tzinfo=next_trigger.tzinfo)
|
||||
else:
|
||||
next_trigger = None
|
||||
time_left = None
|
||||
|
||||
last_trigger = get_last_trigger(timer_name)
|
||||
if last_trigger is not None:
|
||||
since_last = now - last_trigger
|
||||
since_last = now.replace(tzinfo=last_trigger.tzinfo) - last_trigger
|
||||
else:
|
||||
since_last = None
|
||||
|
||||
|
@ -104,18 +110,18 @@ def check_timer(timer_name: str, expected_interval: int = None):
|
|||
# if (next_elapse.left is not None and next_elapse.passed is not None) and (next_elapse.left < 0 or next_elapse.passed < 0):
|
||||
# quit_check(f'Timer is negative??? Left: {next_elapse["left"]}. Passed: {next_elapse["passed"]}', nagios.STATE_UNKNOWN)
|
||||
|
||||
next_elapse_human = next_elapse.next.replace(tzinfo=tz.tzlocal()).strftime('%a %Y-%m-%d %H:%M %Z')
|
||||
remaining_time_human = humanfriendly.format_timespan(next_elapse.time_left)
|
||||
next_elapse_human = next_elapse.next.replace(tzinfo=tz.tzlocal()).strftime('%a %Y-%m-%d %H:%M %Z') if next_elapse.next else 'N/A'
|
||||
remaining_time_human = humanfriendly.format_timespan(next_elapse.time_left) if next_elapse.time_left else 'N/A'
|
||||
since_last_human = humanfriendly.format_timespan(next_elapse.since_last) if next_elapse.since_last else 'N/A'
|
||||
|
||||
perfdata_dict = {
|
||||
'remaining_time': {
|
||||
'value': next_elapse.time_left.seconds,
|
||||
'value': next_elapse.time_left.seconds if next_elapse.time_left else 0,
|
||||
'unit': 's',
|
||||
'min': 0
|
||||
},
|
||||
'time_since_last': {
|
||||
'value': next_elapse.since_last.seconds if next_elapse.since_last is not None else 0,
|
||||
'value': next_elapse.since_last.seconds if next_elapse.since_last else 0,
|
||||
'unit': 's',
|
||||
'min': 0
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue