Debugging Monit Start and Stop Actions

I was recently trying to do some tricky start and stop commands with Monit http://mmonit.com/monit/.  Unfortunately while Monit itself can log to syslog, it doesn't output anything from attempts to start and stop the applications.

The stripped down environment that Monit spawns can be problematic and getting it to cough up whats wrong can be frustrating.

Based off of an answer to a similar question on stack overflow http://stackoverflow.com/questions/3356476/debugging-monit by billitch I think I came up with a good way to keep tabs on whats happening inside those Monit start and stop commands.  A wrapper script pipes both standard output and error to syslog.

I created two shell scripts in /etc/monit, one for full debugging, and one for minimal extra output and ongoing use just in case a problem crops up, you can check your syslog and see what bad thing went down.

My original scripts also preserved the exit code of the command under test but apparently Monit doesn't give a whit about what the command exited as; so I've presented the simpler script here.

/etc/monit/modebug

#!/bin/sh
    {
     echo "MONIT-WRAPPER date"
     date
     echo "MONIT-WRAPPER env"
     env
     echo "MONIT-WRAPPER $@"
     $@
     R=$?
     echo "MONIT-WRAPPER exit code $R"
    } 2>&1 | logger

/etc/monit/morun

#!/bin/sh
    {
     echo "MONIT-WRAPPER $@"
     $@
     R=$?
     echo "MONIT-WRAPPER exit code $R"
    } 2>&1 | logger 

This is an example Monit script, showing execution of both scripts.

/etc/monit/conf.d/dk-filter.monit

check process dk-filter with pidfile /var/run/dk-filter/dk-filter.pid
      group mail
      start program = "/etc/monit/modebug /etc/init.d/dk-filter start"
      stop  program = "/etc/monit/morun /etc/init.d/dk-filter stop"
      if 5 restarts within 5 cycles then timeout
      if failed unixsocket /var/run/dk-filter/dk-filter.sock then restarter.sock then restart 
4 responses
Thanks! Your post helped me just tonight.
Really nice! Thanks! It has taken me 2 days to figure out how to debug the monit log until i see this post.
Sigh...it seems when I run my application with the debug script then Monit starts it just fine but if I remove your script as a prefix, the mysterious problem re-appears. I guess debugging fixed it in a way...
Thanks very much. This saved me from losing my mind. Why Monit does not do this itself is totally beyond me. Even in debug mode, start script errors are completely swallowed...