monit-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

source patch to alterate log level of start stop restart event


From: fusillator
Subject: source patch to alterate log level of start stop restart event
Date: Mon, 30 Nov 2020 17:17:54 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0

Hi all, I changed a bit the source of release 5.25.3 to log start/stop/restart execution stdout and succeeded message.. here's the patch

diff --git a/src/control.c b/src/control.c
index 33cbbe8..cef95ef 100644
--- a/src/control.c
+++ b/src/control.c
@@ -152,7 +152,7 @@ static int _commandExecute(Service_T S, command_t c, char *msg, int msglen, int6                                          n = _getOutput(Process_getInputStream(P), buf, sizeof(buf));
                                 if (n > 0) {
                                         buf[n] = 0;
-                                        DEBUG("%s", buf);
+                                        LogInfo("%s", buf);
                                         // Report the first message (override existing plain timeout message if some program output is available)
                                         if (! total)
                                                 snprintf(msg, msglen, "'%s': %s%s", Util_commandDescription(c, (char[STRLEN]){}), *timeout <= 0 ? "Program timed out -- " : "", buf);
diff --git a/src/event.c b/src/event.c
index c08bb5e..7a67127 100644
--- a/src/event.c
+++ b/src/event.c
@@ -429,7 +429,10 @@ void Event_post(Service_T service, long id, State_Type state, EventAction_T acti
         if (! e) {
                 /* Only first failed/changed event can initialize the queue for given event type, thus succeeded events are ignored until first error. */                  if (state == State_Succeeded || state == State_ChangedNot) {
-                        DEBUG("'%s' %s\n", service->name, message);
+                        if (id == Event_Exec)
+                                LogInfo("'%s' %s\n", service->name, message);
+                        else
+                                DEBUG("'%s' %s\n", service->name, message);
                         FREE(message);
                         return;
                 }

here's my test with a stupid daemon I wrote when I was learning glibc

$ cat daemon2.c
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <signal.h>

extern char **environ;

volatile sig_atomic_t usr_interrupt = 0;

void myhandler(int sig){
    FILE *flog;
    flog = fopen("/var/tmp/daemon2.log","a");
    if (flog == NULL){
      /* Log the failure */
      exit(EXIT_FAILURE);
    }
    if (sig == SIGUSR1){
        usr_interrupt = 1;
        fprintf(flog, "handled %u\n", sig);
    } else if (sig == SIGUSR2){
        char *s = *environ;
        for (int i=0; s; i++) {
            fprintf(flog, "%s\n", s);
            s = *(environ+i);
        }
        fprintf(flog, "handled %u\n", sig);
    }
    fclose(flog);
}

int main(void) {

  /* Our process ID and Session ID */
  pid_t pid, sid;

  /* Fork off the parent process */
  pid = fork();
  if (pid < 0) {
          exit(EXIT_FAILURE);
  }
  /* If we got a good PID, then
     we can exit the parent process. */
  if (pid > 0) {
          exit(EXIT_SUCCESS);
  }

  /* Change the file mode mask */
  umask(0);

  /* Open any logs here */

  /* Create a new SID for the child process */
  sid = setsid();
  if (sid < 0) {
          /* Log the failure */
          exit(EXIT_FAILURE);
  }



  /* Change the current working directory */
  if ((chdir("/")) < 0) {
          /* Log the failure */
          exit(EXIT_FAILURE);
  }

  /* Close out the standard file descriptors */
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);

  /* Daemon-specific initialization goes here */
  FILE *fpid;
  fpid = fopen("/var/tmp/daemon2.pid","w");
  if (fpid == NULL){
    /* Log the failure */
    exit(EXIT_FAILURE);
  }
  fprintf(fpid, "%d", getpid());
  fclose(fpid);

  sigset_t mask, oldmask;
  //printf("pid %u waits for SIGUSR1 signal\n", pid);

  /* Set up the mask of signals to temporarily block. */
  sigemptyset (&mask);
  sigaddset (&mask, SIGUSR1);
  sigaddset (&mask, SIGUSR2);
  struct sigaction sa;
  sa.sa_handler = myhandler;
  sa.sa_flags = 0;
  sigaction(SIGUSR1, &sa, NULL);
  sigaction(SIGUSR2, &sa, NULL);

  /* Wait for a signal to arrive. */
  sigprocmask (SIG_BLOCK, &mask, &oldmask);
  while (!usr_interrupt)
      sigsuspend (&oldmask);
  sigprocmask (SIG_UNBLOCK, &mask, NULL);

  remove("/var/tmp/daemon2.pid");
  exit(EXIT_SUCCESS);
}

$ cat daemon2.sh
#!/bin/bash
log (){
  echo "***** $(date +"%F %T") $1 *****" | tee -a /var/log/daemon2.log
}

case "$1" in
  start)
        log "starting daemon2"
        /home/fusillator/daemon2
    return=$?
    if [ $return -eq 0 ]; then log "started daemon2"
        else log "daemon2 exited with return code $return"
    fi
    exit $return
        ;;
  stop)
    log "stopping daemon2"
    if [ -f "/var/tmp/daemon2.pid" ]; then
          [[ $2 =~ ^[0-9]+$ ]] && sleep $2
      kill -USR1 $(cat /var/tmp/daemon2.pid)
      log "USR1 signal sent"
      exit 0
    else
      log "file /var/tmp/daemon2.pid does not exist"
      exit 1
    fi
    ;;
  *)
        echo "Usage: $0 {start|stop|stop seconds_to_sleep}"
    exit 1
esac

#tail -f /var/log/monit.log

[CET Nov 30 17:11:01] info     : 'daemon2' stop on user request
[CET Nov 30 17:11:01] info     : Monit daemon with PID 2433 awakened
[CET Nov 30 17:11:01] info     : Awakened by User defined signal 1
[CET Nov 30 17:11:01] info     : 'daemon2' stop: '/home/fusillator/daemon2.sh stop 15' [CET Nov 30 17:11:16] info     : ***** 2020-11-30 17:11:01 stopping daemon2 *****
***** 2020-11-30 17:11:16 USR1 signal sent *****
[CET Nov 30 17:11:16] info     : 'daemon2' stopped
[CET Nov 30 17:11:16] info     : 'daemon2' stop action done
[CET Nov 30 17:11:52] info     : 'daemon2' start on user request
[CET Nov 30 17:11:52] info     : Monit daemon with PID 2433 awakened
[CET Nov 30 17:11:52] info     : Awakened by User defined signal 1
[CET Nov 30 17:11:52] info     : 'daemon2' start: '/home/fusillator/daemon2.sh start' [CET Nov 30 17:11:52] info     : ***** 2020-11-30 17:11:52 starting daemon2 *****
***** 2020-11-30 17:11:52 started daemon2 *****
[CET Nov 30 17:11:52] info     : 'daemon2' started
[CET Nov 30 17:11:52] info     : 'daemon2' start action done


I'm wondering if it's the right way to alterate the log level of the process command, or maybe i miss something in the manual, is there some directive or a smarter way to do this without touching the source? I didn't tested new version, could it be done in a recent release?

I also add that I only glimpse on the source, I used a debugger to get the line to change and I'm not aware of all implications of my changes, if anyone would use them be aware of the risk.

Do you know if my changes could cause some trouble in a production environment?

Best regards

Luca Cazzaniga




reply via email to

[Prev in Thread] Current Thread [Next in Thread]