? monitor.h Index: alert.c =================================================================== RCS file: /cvsroot/monit/monit/alert.c,v retrieving revision 1.4 diff -u -r1.4 alert.c --- alert.c 9 Aug 2002 00:03:11 -0000 1.4 +++ alert.c 13 Aug 2002 14:54:07 -0000 @@ -21,6 +21,7 @@ #include #include #include +#include #include "monitor.h" #include "net.h" @@ -35,13 +36,14 @@ static void free_mail(Mail_T); static int count(char *, char *); static void copy_mail(Mail_T, Mail_T); -static void smtp_alert(Process_T, int); static char *replace(char *, char *, char *); +static char *format(const char *s, va_list ap); +static void smtp_alert(Process_T, int, char *, va_list); static void substitute(Mail_T, char *name, char *event); /* Private Variables */ -static char desc[][STRLEN]= {"timed out", "restarted", "checksum error"}; static char desclog[][STRLEN]= {"Timeout", "Restart", "Checksum error"}; +static char desc[][STRLEN]= {"timed out", "restarted", "checksum error"}; /** @@ -65,10 +67,16 @@ * Send an alert timeout message to the email address for * this process * @param p A Process_T object + * @param m An optional message string. May be NULL. */ -void smtp_alert_timeout(Process_T p) { +void smtp_alert_timeout(Process_T p, char *m, ...) { - smtp_alert(p, DO_TIMEOUT); + va_list ap; + + va_start(ap, m); + smtp_alert(p, DO_TIMEOUT, m, ap); + va_end(ap); + } @@ -77,10 +85,15 @@ * Send an alert checksum message to the email address for * this process * @param p A process_t object + * @param m An optional message string. May be NULL. */ -void smtp_alert_checksum(Process_T p) { +void smtp_alert_checksum(Process_T p, char *m, ...) { - smtp_alert(p, DO_CHECKSUM); + va_list ap; + + va_start(ap, m); + smtp_alert(p, DO_CHECKSUM, m, ap); + va_end(ap); } @@ -88,10 +101,15 @@ /** * Send an alert restart message to the email address for this process * @param p A process_t object + * @param m An optional message string. May be NULL. */ -void smtp_alert_restart(Process_T p) { +void smtp_alert_restart(Process_T p, char *m, ...) { - smtp_alert(p, DO_RESTART); + va_list ap; + + va_start(ap, m); + smtp_alert(p, DO_RESTART, m, ap); + va_end(ap); } @@ -102,7 +120,7 @@ /** * Send a smtp notification */ -static void smtp_alert(Process_T p, int event) { +static void smtp_alert(Process_T p, int event, char *optmsg, va_list ap) { if(p->maillist) { @@ -110,6 +128,7 @@ sigset_t ns, os; Mail_T list= NULL; + /* Set a SIGUSR1 block here */ sigemptyset(&ns); sigaddset(&ns, SIGUSR1); @@ -130,6 +149,8 @@ Mail_T tmp= NEW(tmp); copy_mail(tmp, m); + + if(optmsg) tmp->opt_message= format(optmsg, ap); substitute(tmp, p->name, desc[event]); @@ -258,6 +279,7 @@ Run.MailFormat.message? xstrdup(Run.MailFormat.message): xstrdup(ALERT_MESSAGE); + n->opt_message= NULL; } @@ -271,7 +293,36 @@ free(m->from); free(m->subject); free(m->message); + free(m->opt_message); free(m); m= NULL; } + + +static char *format(const char *s, va_list ap) { + + int n; + int size= STRLEN; + char *buf= xmalloc(size); + + while(TRUE) { + + n= vsnprintf(buf, size, s, ap); + + if(n > -1 && n < size) + break; + + if(n > -1) + size= n+1; + else + size*= 2; + + buf= xresize(buf, size); + + } + + return buf; + +} + Index: monitor.h.in =================================================================== RCS file: /cvsroot/monit/monit/monitor.h.in,v retrieving revision 1.18 diff -u -r1.18 monitor.h.in --- monitor.h.in 9 Aug 2002 00:19:05 -0000 1.18 +++ monitor.h.in 13 Aug 2002 14:54:08 -0000 @@ -155,6 +155,7 @@ char *from; /**< The mail from address */ char *subject; /**< The mail subject */ char *message; /**< The mail message */ + char *opt_message; /**< An optional message used in alerts */ int alert_on_timeout; /**< If TRUE, alert user when process timeout */ int alert_on_restart; /**< If TRUE, alert user when the process restarts */ int alert_on_checksum; /**< If TRUE, alert user when the checksum fail */ @@ -264,9 +265,10 @@ void *xcalloc(long, long); void *xrealloc (void *, int); char *xstrdup(const char *); -void smtp_alert_timeout(Process_T); -void smtp_alert_restart(Process_T); -void smtp_alert_checksum(Process_T); +void *xresize(void *, long); +void smtp_alert_timeout(Process_T, char *, ...); +void smtp_alert_restart(Process_T, char *, ...); +void smtp_alert_checksum(Process_T, char *, ...); void set_alarm_handler(void *); void monit_http(int); int check_httpd(); Index: sendmail.c =================================================================== RCS file: /cvsroot/monit/monit/sendmail.c,v retrieving revision 1.7 diff -u -r1.7 sendmail.c --- sendmail.c 9 Aug 2002 00:03:11 -0000 1.7 +++ sendmail.c 13 Aug 2002 14:54:08 -0000 @@ -102,6 +102,7 @@ do_send("Content-Transfer-Encoding: quoted-printable\r\n"); do_send("\r\n"); do_send("%s\r\n", p->message); + if(p->opt_message) do_send("%s\r\n", p->opt_message); do_send(".\r\n"); do_status(); Index: validate.c =================================================================== RCS file: /cvsroot/monit/monit/validate.c,v retrieving revision 1.10 diff -u -r1.10 validate.c --- validate.c 13 Aug 2002 00:41:45 -0000 1.10 +++ validate.c 13 Aug 2002 14:54:09 -0000 @@ -191,7 +191,7 @@ sleep(3); /* In case we just started our own smtp server */ - smtp_alert_restart(p); + smtp_alert_restart(p, NULL); } @@ -329,7 +329,7 @@ log("*Alert* process '%s' timed out and will not be checked anymore.\n", p->name); - smtp_alert_timeout(p); + smtp_alert_timeout(p, NULL); return TRUE; @@ -434,7 +434,7 @@ log("'%s' **Alert** checksum error for %s\n", p->name, s); - smtp_alert_checksum(p); + smtp_alert_checksum(p, NULL); LOCK(Run.mutex) p->do_validate= FALSE; Index: xmalloc.c =================================================================== RCS file: /cvsroot/monit/monit/xmalloc.c,v retrieving revision 1.2 diff -u -r1.2 xmalloc.c --- xmalloc.c 28 Jun 2002 20:59:03 -0000 1.2 +++ xmalloc.c 13 Aug 2002 14:54:09 -0000 @@ -1,5 +1,6 @@ /* * Copyright (C), 1998 by Eric S. Raymond. + * Copyright (C), 2000-2002 by Contributors to the monit codebase. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -54,7 +55,7 @@ } - return(p); + return p; } @@ -70,7 +71,7 @@ } - return(p); + return p; } @@ -107,3 +108,18 @@ } + +void *xresize(void *p, long nbytes) { + + p= realloc(p, nbytes); + if(p == NULL) { + + error("%s: realloc failed -- %s\n", prog, STRERROR); + exit(1); + + } + + return p; + +} +