check_systemd_timer: handle when 'n/a' is in status

This commit is contained in:
Cyberes 2024-03-07 15:10:27 -07:00
parent a7ad8635e2
commit 41f524e259
1 changed files with 38 additions and 21 deletions

View File

@ -20,7 +20,7 @@ from datetime import datetime
import humanfriendly
SYSTEMCTL_TIMERS_RE = re.compile(
r'^([A-Za-z]*\s[0-9]{4}-[0-9]{2}-[0-9]{2}\s*[0-9]{2}:[0-9]{2}:[0-9]{2}\s[A-Z]*)\s*(([0-9]*[a-z]*\s)*left)\s*([A-Za-z]*\s[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[A-Z]*)\s*([0-9A-Za-z\s]*\sago)\s*([A-Za-z\-_]*.timer)\s*([A-Za-z\-_]*.service)')
r'^(([A-Za-z]*\s[0-9]{4}-[0-9]{2}-[0-9]{2}\s*[0-9]{2}:[0-9]{2}:[0-9]{2}\s[A-Z]*)|n\/a)\s*((([0-9]*[a-z]*\s)*left)|n\/a)\s*(([A-Za-z]*\s[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[A-Z]*)|n\/a)\s*(([0-9A-Za-z\s]*\sago)|n\/a)\s*([A-Za-z\-_]*.timer)\s*(([A-Za-z\-_]*.service)|\s*)')
def get_next_elapse(timer_name):
@ -31,26 +31,32 @@ def get_next_elapse(timer_name):
if timer_name in line:
parts = re.search(SYSTEMCTL_TIMERS_RE, line)
try:
datetime_object = datetime.strptime(parts.group(1), '%a %Y-%m-%d %H:%M:%S %Z')
except ValueError as e:
return None, e
datetime_object = None
if parts.group(2):
try:
datetime_object = datetime.strptime(parts.group(2), '%a %Y-%m-%d %H:%M:%S %Z')
except ValueError as e:
return None, e
time_left = parse_systemctl_time_delta(parts.group(2))
if isinstance(time_left, humanfriendly.InvalidTimespan):
return None, humanfriendly.InvalidTimespan
time_left = 'n/a'
if parts.group(4):
time_left = parse_systemctl_time_delta(parts.group(4))
if isinstance(time_left, humanfriendly.InvalidTimespan):
return None, humanfriendly.InvalidTimespan
time_passed = parse_systemctl_time_delta(parts.group(5))
if isinstance(time_passed, humanfriendly.InvalidTimespan):
return None, humanfriendly.InvalidTimespan
time_passed = 'n/a'
if parts.group(9):
time_passed = parse_systemctl_time_delta(parts.group(9))
if isinstance(time_passed, humanfriendly.InvalidTimespan):
return None, humanfriendly.InvalidTimespan
timer_info = {
'next': datetime_object,
'left': time_left,
'last': parts.group(4),
'last': parts.group(7),
'passed': time_passed,
'unit': parts.group(6),
'activates': parts.group(7)
'unit': parts.group(10),
'activates': parts.group(12)
}
return timer_info, None
return None, ValueError('Timer not found')
@ -75,21 +81,32 @@ def check_timer(timer_name):
if err:
quit_check(f'{err}', nagios.STATE_UNKNOWN)
if next_elapse['left'] < 0 or next_elapse['passed'] < 0:
if (next_elapse['left'] != 'n/a' and next_elapse['passed'] != 'n/a') 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)
local_tz = tz.tzlocal()
next_elapse_str = next_elapse['next'].replace(tzinfo=local_tz).strftime('%a %Y-%m-%d %H:%M %Z')
remaining_time_human = humanfriendly.format_timespan(next_elapse['left'])
passed_time_human = humanfriendly.format_timespan(next_elapse['passed'])
if next_elapse['next']:
local_tz = tz.tzlocal()
next_elapse_str = next_elapse['next'].replace(tzinfo=local_tz).strftime('%a %Y-%m-%d %H:%M %Z')
else:
next_elapse_str = 'n/a'
if next_elapse['left'] != 'n/a':
remaining_time_human = humanfriendly.format_timespan(next_elapse['left'])
else:
remaining_time_human = 'n/a'
if next_elapse['passed'] != 'n/a':
passed_time_human = humanfriendly.format_timespan(next_elapse['passed'])
else:
passed_time_human = 'n/a'
perfdata_dict = {
'remaining_time': {
'value': int(next_elapse['left']),
'value': int(next_elapse['left']) if next_elapse['left'] != 'n/a' else -1,
'unit': 's',
'min': 0
},
'passed_time': {
'value': int(next_elapse['passed']),
'value': int(next_elapse['passed']) if next_elapse['passed'] != 'n/a' else -1,
'unit': 's',
'min': 0
}