Environment variable in local check not available when using cache

CMK version: 2.2.0p7 raw
OS version: Debian 11

Error message:
I have a local check, that reads an environment variable. The check is written in python, and I access the variable using

myvar = os.environ["myvar"]

This works perfectly fine, when the check is located in /usr/lib/check_mk_agent/local/. But if I move it to a folder, to have the result cached (/usr/lib/check_mk_agent/local/43200), the environment variable is not available anymore. Nothing else has changed; I literally did:

$ sudo mkdir 43200
$ sudo mv check* 43200/

I copied the check back to the parent directory, where it works again. cmk-agent-ctl dump now gives me both results - working and not working.

Further info about the setup: we use the “new” agent. The check-mk-agent@-Servicetemplate has the environment variable available (set via a loaded env-File, double-checked using strings /proc/<servicepid>/environ).

Does anybody have an explanation or better yet, a fix for this? It seems like a very weird thing to be broken.

Create your own, working reproduction:
(assumes you are not working as root, have sudo and the “new” check_mk agent set up)

sudo mkdir /etc/systemd/system/check-mk-agent@.service.d/
cat > /tmp/cmk-service-override.conf << EOF
[Service]
EnvironmentFile=/etc/check_mk/.env
EOF
sudo mv /tmp/cmk-service-override.conf /etc/systemd/system/check-mk-agent@.service.d/override.conf
sudo systemctl daemon-reload
cat > /tmp/cmk.env << EOF
myvar="TEST"
EOF
sudo mv /tmp/cmk.env /etc/check_mk/.env
sudo chmod 0600 /etc/check_mk/.env
sudo systemctl restart cmk-agent-ctl-daemon.service
cat > /tmp/env_var_test.py << EOF
#!/usr/bin/env python3
import os

try:
    myvar = os.environ["myvar"]
except KeyError:
   myvar = "Error on loading var"

print("0 'env variable test' - Output: '{}'".format(myvar))
EOF
sudo chmod 0755 /tmp/env_var_test.py
sudo mv /tmp/env_var_test.py /usr/lib/check_mk_agent/local/env_var_test.py
sudo mkdir /usr/lib/check_mk_agent/local/43200
sudo cp /usr/lib/check_mk_agent/local/{,43200/}env_var_test.py
sudo cmk-agent-ctl dump | grep "env variable test" |grep -v grep

This should print two line of output:

0 'env variable test' - Output: 'TEST'
cached(1704270481,43200) 0 'env variable test' - Output: 'Error on loading var'

The uncached check is working fine, the cached version does not get the env var on the first run.

To reset the cache time, use something like

sudo touch -t 2401010000 /var/lib/check_mk_agent/cache/local_env_var_test.py.cache

Output of “cmk --debug -vvn hostname”: (If it is a problem with checks or plugins)

Checkmk version 2.2.0p7
Updating IPv4 DNS cache for hostname: IP
Trying to acquire lock on /omd/sites/site/var/check_mk/ipaddresses.cache
Got lock on /omd/sites/site/var/check_mk/ipaddresses.cache
Releasing lock on /omd/sites/site/var/check_mk/ipaddresses.cache
Released lock on /omd/sites/site/var/check_mk/ipaddresses.cache
+ FETCHING DATA
  Source: SourceInfo(hostname='hostname', ipaddress='IP', ident='piggyback', fetcher_type=<FetcherType.PIGGYBACK: 4>, source_type=<SourceType.HOST: 1>)
[cpu_tracking] Start [7fbef7182c90]
Read from cache: NoCache(hostname, path_template=/dev/null, max_age=MaxAge(checking=0.0, discovery=0.0, inventory=0.0), simulation=False, use_only_cache=False, file_cache_mode=1)
[PiggybackFetcher] Execute data source
No piggyback files for 'hostname'. Skip processing.
No piggyback files for 'IP'. Skip processing.
[cpu_tracking] Stop [7fbef7182c90 - Snapshot(process=posix.times_result(user=0.0, system=0.0, children_user=0.0, children_system=0.0, elapsed=0.0))]
+ PARSE FETCHER RESULTS
No persisted sections
  HostKey(hostname='hostname', source_type=<SourceType.HOST: 1>)  -> Add sections: []
Received no piggyback data
[cpu_tracking] Start [7fbef6271f50]
value store: synchronizing
Trying to acquire lock on /omd/sites/site/tmp/check_mk/counters/hostname
Got lock on /omd/sites/site/tmp/check_mk/counters/hostname
value store: loading from disk
Releasing lock on /omd/sites/site/tmp/check_mk/counters/hostname
Released lock on /omd/sites/site/tmp/check_mk/counters/hostname
No piggyback files for 'hostname'. Skip processing.
No piggyback files for 'IP'. Skip processing.
[cpu_tracking] Stop [7fbef6271f50 - Snapshot(process=posix.times_result(user=0.0, system=0.0, children_user=0.0, children_system=0.0, elapsed=0.0))]
[piggyback] Success (but no data found for this host), execution time 0.0 sec | execution_time=0.000 user_time=0.000 system_time=0.000 children_user_time=0.000 children_system_time=0.000 cmk_time_agent=0.000

Async Plugins and Local Checks are executed via the check-mk-agent-async.service systemd unit.
You will need to set your environment variable there.

2 Likes

Ah, thanks!

It’s funny how things work when you actually do them correctly.

1 Like