Search code examples
monit

monit: use hostname in logfile path


I am new to monit and want to use different logfile path for monit (not the default one)

set logfile /x/home/xxxx/yyyy/monit/monit-5.20.0/logs/monit_$HOST.log

In place of $HOST, I want the hostname where the monit is running.
Any idea how can we achieve this? Similarly, I wan to use the hostname for idfile and statefile as well.

Note: /x/home/xxxx/yyyy/monit/monit-5.20.0 is common mount for all machines and want to run monit on them. But don't want the same log file.


Solution

  • Finally I found the way to have hostname in the logfile, idfile and statsfile.
    I created a wrapper script start.sh as follows and started the monit by passing control file, logfile, idfile and statsfile.

    #!/bin/bash
    
    BASEDIR=`dirname $0`
    HOST=`hostname`
    MONIT_BIN=$BASEDIR/bin/monit
    CTRL_FILE=$BASEDIR/conf/monitrc
    LOG_FILE=$BASEDIR/logs/monit_$HOST.log
    PID_FILE=$BASEDIR/run/monit_$HOST.pid
    STATS_FILE=$BASEDIR/run/.monit_$HOST.state
    
    mkdir -p $BASEDIR/run
    mkdir -p $BASEDIR/logs
    
    touch $PID_FILE
    touch $STATS_FILE
    touch $LOG_FILE
    
    nohup $MONIT_BIN -c $CTRL_FILE -l $LOG_FILE -p $PID_FILE -s $STATS_FILE &> /dev/null &