CMK version: Checkmk Enterprise Edition 2.1.0p6 OS version: Red Hat Enterprise Linux 8.6 (Ootpa)
**Error message: The “live_example.py” for Using LQL in Python that Check_mk uploaded in the check_mk docs get stucked in the ‘while’ loop and never ends.
This is where the code get stucked:
# receive the reply as a JSON string
chunks = []
while len(chunks) == 0 or chunks[-1] != "":
chunks.append(sock.recv(4096))
sock.close()
reply = "".join(chunks)
Please, Any help about what is going wrong? Could someone share a running example?
After I changed the socket type from blocking to non blocking (sock.setblocking(False)) and added a sleep timer (sleep(5)). I was finaly able to get the script working.
#!/usr/bin/env python
# Sample program for accessing Livestatus from Python
import json, os, socket
import time
# for local site only: file path to socket
address = "%s/tmp/run/live" % os.getenv("OMD_ROOT")
# for local/remote sites: TCP address/port for Livestatus socket
# address = ("localhost", 6557)
# connect to Livestatus
family = socket.AF_INET if type(address) == tuple else socket.AF_UNIX
sock = socket.socket(family, socket.SOCK_STREAM)
sock.connect(address)
sock.setblocking(False)
time.sleep(5)
# send our request. sock.sendall expects byte encoded data
sock.sendall("GET status\nOutputFormat: json\n".encode())
sock.shutdown(socket.SHUT_WR)
# you ned the decode the recived data from bytes into str, and convert as a JSON
chunks = []
while len(chunks) == 0 or chunks[-1] != "":
chunks.append(sock.recv(4096).decode())
sock.close()
reply = "".join(chunks)
# print the parsed reply
print(json.loads(reply))
Forums say that is because of the “non-blocking” state.
Anyway, This is so frustrating and complicated. I tried differenta approach using “lq” and os.popen() and it worked
This slipped through the cracks. Sorry about that.
The example is now fixed in the user guide as well. Please do check out the fixed example in the user guide, because it works without having to use sleep(n).
Your example works for me. I have remove the sock.setblocking(False) and sleep(1) from my scrpit. Works now without them to. No idea why it was needed before.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.