icinga2-checks/checker/humanfriendly.py

22 lines
730 B
Python
Raw Normal View History

import re
import humanfriendly
SYSTEMCTL_TIMESPAN_RE = re.compile(r'([0-9]+((\s[a-z]*)|([a-z]+)))')
def parse_systemctl_time_delta(time_str: str) -> float:
"""
https://humanfriendly.readthedocs.io/en/latest/api.html?highlight=parse_timespan#humanfriendly.parse_timespan
"""
time_str = time_str.replace(' left', '').replace(' ago', '')
spans = []
for part in re.findall(SYSTEMCTL_TIMESPAN_RE, time_str):
delta = part[0]
if 'month' in delta:
# humanfriendly does not support "months" so we convert it to weeks
num = int(delta.split(' ')[0])
delta = f'{num * 4} weeks'
spans.append(humanfriendly.parse_timespan(delta))
return sum(spans)