4

I would like to make Monit's "check process" work conditionally based on an env variable.

Here's the pseudo-code of what I would like to achieve(not a valid monit config syntax):

[ -n "$run_foo" ] && check process foo ....

My use case: I have a docker image that starts monit process like this:

CMD ["/bin/monit", "-c","/config/monit.conf"]

Monit then starts various daemons.

Now, I would like to be able to tell at run time which daemons I want to start in a particular container created from this image, like this:

docker run --env="run_foo=1;run_bar=1" ...

I am aware of monit unmonitor, and similar args, however I believe they don't serve my purpose.

Thought about something like this:

start program="/bin/bash -c '[ -n \"$run_foo\" ] && /bin/foo .."

However, seems like it will cause monit to spin continuously trying to start and monitor things that shouldn't be started.

I am aware of possibility of using different monit config files, or using different docker images - these don't qualify as an answer.

Other ideas appreciated.

Dmitry z
  • 143
  • 4

1 Answers1

1

Sadly that is not how Monit is working.

Monit is there to react to states you define. If you use it as init system (what is totally fine and working) you have to have a config file that is built for your needs.

You could, however, try to hack it with a little sed or something.

The idea is to have a monitrc with all configs, but all inactive and activate them on first start. Like:

#check process foo with pidfile /run/foo.pid # proc-foo
#  onreboot nostart # proc-foo
#  mode passive # proc-foo

#check process bar with pidfile /run/bar.pid # proc-bar
#  onreboot nostart # proc-bar
#  mode passive # proc-bar

check program firststart path "/bin/bash -c /tmp/firststart.sh" # first-start

While /tmp/firststart.sh is something like:

#!/usr/bin/env bash

# Uncomment Lines Per Config:
[[ -n "${run_foo}" ]] && sed -i 's/^#\(.*\) # proc-foo$/\1/' /etc/monitrc
[[ -n "${run_bar}" ]] && sed -i 's/^#\(.*\) # proc-bar$/\1/' /etc/monitrc

# Delete FirstStart Line
sed -i '/^.*# first-start$/d' /etc/monitrc

# Force reload of monit config
kill -SIGHUP $(cat /run/monit.pid)
boppy
  • 476
  • 2
  • 5