CMK version: 2.2.0p23cre
OS version: Alma Linux 8.9
Client agent: 2.2.0p23
Client OS: Alma Linux 8.9
Got a python script which runs a command to check battery levels in some home automation gear.
From command line the script outputs this:
P "Batt-Doorbell_Battery_Level" pct=75;20:;10: Battery charge remaining.
Do I have the syntax wrong there? It looks okay to me but I cannot get the server to inventory the services. Running check_mk_agent on the client side the python script doesn’t seem to get called and the output never shows up.
I have others but the output only changes in the Name section and the actual value. I’m only worried about the battery getting below 20%, and go critical if it’s below 10%.
Running the script as root on the client system it works great. I’ve put it in /usr/lib/check_mk_agent/local/ and permissions are 755.
A test local script works great, only real difference is it happens to be a bash script.
ADDED: Tried using sys.stdout.write in the python instead of a basic print() statement, didn’t change the behavior.
If you call the script “manually” as root, how exactly are you doing that? Like
/usr/lib/check_mk_agent/local/my-script.py
?
Has the script a proper shebang line? That’s the first line in the script. It should look similar to
#!/usr/bin/env python
or
#!/usr/bin/env python3
The full path to the python binary is also ok, but it must be correct (e.g. #!/usr/bin/python3).
If that doesn’t help, then maybe the script crashes before it can output anything. Try running the agent in debug mode (issue check_mk_agent -d as root and better redirect the output):
check_mk_agent -d 1>out.txt 2>err.txt
If that doesn’t help either, then it’s probably an environment thing. The agent is called via a systemd unit and may have a different environment compared to the one from your shell.
I think I just figured out the problem, if not the solution yet. My python script runs a command that relies on an ssh key to authenticate into the OpenHAB console and karaf instance. While it works great when run as root the check_mk_agent isn’t able to run as root.
The agent is set to run as root in the /etc/systemd/check_mk@.service file but it doesn’t appear to actually have full root rights.
There’s a cmk-agent user on the client, is that what user the system will see when the agent is run in a normal check process?
The agent controller runs everything with root privileges unless specified otherwise. BUT: It uses a different environment than the root user. So you might want to either emulate a different environment in your script using os.environ or just by specifying full paths to key files.
What’s the difference between the root users environment and what the agent is running in?
I started this script with the thought of expanding it later and some ideas I had would probably be easier to accomplish in python. But I can always backtrack to a bash off it’ll be an easier way to get it done.
P “Batt-Doorbell_Battery_Level” pct=75;20:;10: Battery charge remaining.
When you use option P, the watn and crit values need to be integers. From my point of view the representaion is a string “20:” instead of 20. Did you check this?
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.