limit output lines

after all, there is no point in cluttering the whole screen with output
from just one service.
This commit is contained in:
Franzi 2021-05-26 08:53:22 +02:00
parent ac32f02604
commit 5e843e5ea7
Signed by: kunsi
GPG key ID: 12E3D2136B818350

25
service
View file

@ -23,9 +23,28 @@ def current_time():
now = now.replace(tzinfo=None)
return now
def to_unixtimestamp(dt):
return int(time.mktime(dt.timetuple()))
def limit_output_lines(output, num_lines=10):
actual = len(output)
if actual <= num_lines:
return output
more = actual-(num_lines-1)
result = output[:(num_lines-1)]
result.append('... and {} more line{s}'.format(
more,
's' if more > 1 else '',
))
return result
def regenerate():
now = current_time()
@ -69,7 +88,7 @@ def regenerate():
'service': '-- HOST --',
'state': 2,
'type': int(host['attrs']['state_type']),
'output': host['attrs']['last_check_result']['output'].splitlines(),
'output': limit_output_lines(host['attrs']['last_check_result']['output'].splitlines(), 3),
'ack': ack,
'sort': '{}{}{}{}'.format(
int(host['attrs']['state_type'])*-1,
@ -100,7 +119,7 @@ def regenerate():
'service': svc['attrs']['display_name'],
'state': int(svc['attrs']['state']),
'type': int(svc['attrs']['state_type']),
'output': svc['attrs']['last_check_result']['output'].splitlines(),
'output': limit_output_lines(svc['attrs']['last_check_result']['output'].splitlines()),
'ack': ack,
'sort': '{}{}{}{}'.format(
int(svc['attrs']['state_type'])*-1,
@ -136,6 +155,7 @@ def regenerate():
with file("services.json", "wb") as f:
f.write(json.dumps(services, ensure_ascii=False).encode("utf8"))
def main():
while 1:
try:
@ -145,5 +165,6 @@ def main():
time.sleep(20)
if __name__ == "__main__":
main()