source patch to alterate log level of start stop restart event

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

source patch to alterate log level of start stop restart event

fusillator
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 | Threaded
Open this post in threaded view
|

RE: source patch to alterate log level of start stop restart event

Lutz Mader

Hello,

are you do some tests with the monit options -v or -vv to get the additional information you are interrested in?

These options enables a verbose mode inside monit and report some more information to the log file.

 

A sugestion/question only,

Lutz



Reply | Threaded
Open this post in threaded view
|

Re: source patch to alterate log level of start stop restart event

fusillator

Hi Luts, thanks for your hint and time. I tried the verbose command line options but they are too noisy for my aim, I'm only interested to log the executions outcome of the process events.

That's because I noticed that some services provide startup scripts which rely on the service manager capacities to collects their outputs
we're too lazy to change every scripts or redirect the commands output in the monit configurations to collect them so I thought I'd rather exploit the hard work of monit programmers. 

In my opinion it could be a good option to make customizable so I'm wondering if there's some cons or work in progress.


Regars


Luca



 


On 02/12/20 16:41, [hidden email] wrote:

Hello,

are you do some tests with the monit options -v or -vv to get the additional information you are interrested in?

These options enables a verbose mode inside monit and report some more information to the log file.

 

A sugestion/question only,

Lutz



Reply | Threaded
Open this post in threaded view
|

Re: source patch to alterate log level of start stop restart event

Lutz Mader
Hello Luca,
yes, sometimes I missed some script/program output too.

For some scripts I am interresting in the output I use "check program".
This works well and support "start program" and "stop program" and you
can use the script status check to test the application as long as you
get a useful status code from the script.

A suggestion only,
Lutz

Reply | Threaded
Open this post in threaded view
|

Re: source patch to alterate log level of start stop restart event

fusillator
Hi Lutz thanks for your help

Yeah i had evaluated that option too, you're right the output of the
program is collected in the monit log, anyway not those of start/stop
scripts.
So it's not enough to implement the check process logic in the program
check script, you need to embed also the startup logic.

And you need to set up two check program if you want to start stop your
service using monit, maybe too machinary.. do you mean that?


On 03/12/20 09:48, Lutz Mader wrote:

> Hello Luca,
> yes, sometimes I missed some script/program output too.
>
> For some scripts I am interresting in the output I use "check program".
> This works well and support "start program" and "stop program" and you
> can use the script status check to test the application as long as you
> get a useful status code from the script.
>
> A suggestion only,
> Lutz
>

Reply | Threaded
Open this post in threaded view
|

Re: source patch to alterate log level of start stop restart event

Lutz Mader
Hello Luca,
I use one script for all.

> And you need to set up two check program if you want to start stop your> service using monit, maybe too machinary.. do you mean that?
But my scripts deal with start/stop/status and restart in general and I
get useful status codes. But you are right (sorry), monit collect the
output from the status check only.

check program Manager with path "/usr/local/etc/monit/scripts/em.sh status"
  start program "/usr/local/etc/monit/scripts/em.sh start" with timeout
240 seconds
  stop program "/usr/local/etc/monit/scripts/em.sh stop" with timeout
300 seconds
  if status > 1 for 3 times within 5 cycles then alert

The output of the status check of my script looks similar to the
start/stop output.

With regards,
Lutz


Reply | Threaded
Open this post in threaded view
|

Re: source patch to alterate log level of start stop restart event

fusillator
hi Lutz, a good workaround, it gathers messages regarding the status,
but my aim was collecting messages/status of the actual execution of
start/stop program, although similar the flow could not be the same.


On Fri, 4 Dec 2020 at 23:07, Lutz Mader <[hidden email]> wrote:

>
> Hello Luca,
> I use one script for all.
>
> > And you need to set up two check program if you want to start stop your> service using monit, maybe too machinary.. do you mean that?
> But my scripts deal with start/stop/status and restart in general and I
> get useful status codes. But you are right (sorry), monit collect the
> output from the status check only.
>
> check program Manager with path "/usr/local/etc/monit/scripts/em.sh status"
>   start program "/usr/local/etc/monit/scripts/em.sh start" with timeout
> 240 seconds
>   stop program "/usr/local/etc/monit/scripts/em.sh stop" with timeout
> 300 seconds
>   if status > 1 for 3 times within 5 cycles then alert
>
> The output of the status check of my script looks similar to the
> start/stop output.
>
> With regards,
> Lutz
>
>

Reply | Threaded
Open this post in threaded view
|

Re: source patch to alterate log level of start stop restart event

Lutz Mader
Hello Luca,
unfortunatly, my way does not collect the start/stop messages with
monit. The status information are collected only.

> hi Lutz, a good workaround, it gathers messages regarding the status,
> but my aim was collecting messages/status of the actual execution of
> start/stop program, although similar the flow could not be the same.

But it is a nice idea to collect the output from the last command in
monit, you should describe your problem and add an issue via Bitbucket.
Proposals or enhancement requests are welcome too, in general. And if
these information became available via M/Monit (or the web interface)
your suggestion saves some time to find problems, I think.

I use an additional file check to find useful messages, but the used
scripts write all the output to a log file too, also it is not a problem
for me and the monitored applications.

But I use the status output (from check program) to get some more
information about the application status for some applications.

With regards,
Lutz

p.s.
This is my way to handle more complex application, see
https://mmonit.com/wiki/Monit/HowTo
for example https://mmonit.com/wiki/Monit/LibertyProfileApplicationServer

Reply | Threaded
Open this post in threaded view
|

Re: source patch to alterate log level of start stop restart event

fusillator
hi Lutz, thanks again for all your suggestions and time.
I'll try to submit the proposal request.
I need sometime to refresh my c skill and study better the source.
the wiki is great. thanks for sharing your know-how.


Regards

Luca



On 05/12/20 10:26, Lutz Mader wrote:

> Hello Luca,
> unfortunatly, my way does not collect the start/stop messages with
> monit. The status information are collected only.
>
>> hi Lutz, a good workaround, it gathers messages regarding the status,
>> but my aim was collecting messages/status of the actual execution of
>> start/stop program, although similar the flow could not be the same.
> But it is a nice idea to collect the output from the last command in
> monit, you should describe your problem and add an issue via Bitbucket.
> Proposals or enhancement requests are welcome too, in general. And if
> these information became available via M/Monit (or the web interface)
> your suggestion saves some time to find problems, I think.
>
> I use an additional file check to find useful messages, but the used
> scripts write all the output to a log file too, also it is not a problem
> for me and the monitored applications.
>
> But I use the status output (from check program) to get some more
> information about the application status for some applications.
>
> With regards,
> Lutz
>
> p.s.
> This is my way to handle more complex application, see
> https://mmonit.com/wiki/Monit/HowTo
> for example https://mmonit.com/wiki/Monit/LibertyProfileApplicationServer
>

Reply | Threaded
Open this post in threaded view
|

Re: source patch to alterate log level of start stop restart event

Lutz Mader
Hello Luca,
no problem.

> hi Lutz, thanks again for all your suggestions and time.
> I'll try to submit the proposal request.
> I need sometime to refresh my c skill and study better the source.
> the wiki is great. thanks for sharing your know-how.

You can explain your problem and someone will try to help you,
there is no reason to add the solution too.

With regards,
Lutz