Skip to content

Commit

Permalink
Adding set exec for alert
Browse files Browse the repository at this point in the history
  • Loading branch information
aforward committed Jul 26, 2011
1 parent 9615705 commit cb4e29c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/alert.c
Expand Up @@ -71,7 +71,7 @@
static void copy_mail(Mail_T, Mail_T);
static void replace_bare_linefeed(Mail_T *);
static void substitute(Mail_T *, Event_T);

static void substitute_exec(char *, Event_T);

/* ------------------------------------------------------------------ Public */

Expand Down Expand Up @@ -182,6 +182,13 @@ int handle_alert(Event_T E) {
gc_mail_list(&list);

}

if (Run.alert_exec)
{
char* exec_command = Util_getString("%s",Run.alert_exec);
substitute_exec(exec_command,E);
system(exec_command);
}

}

Expand Down Expand Up @@ -216,6 +223,18 @@ static void substitute(Mail_T *m, Event_T e) {
Util_replaceString(&(*m)->message, "$ACTION", Event_get_action_description(e));
}

static void substitute_exec(char *exec_command, Event_T e) {
char timestamp[STRLEN];
Util_getRFC822Date((time_t *)&e->collected.tv_sec, timestamp, STRLEN);

Util_replaceString(&exec_command, "$HOST", Run.localhostname);
Util_replaceString(&exec_command, "$DATE", timestamp);
Util_replaceString(&exec_command, "$SERVICE", Event_get_source_name(e));
Util_replaceString(&exec_command, "$EVENT", Event_get_description(e));
Util_replaceString(&exec_command, "$DESCRIPTION", NVLSTR(Event_get_message(e)));
Util_replaceString(&exec_command, "$ACTION", Event_get_action_description(e));
}


static void copy_mail(Mail_T n, Mail_T o) {
ASSERT(n && o);
Expand Down
1 change: 1 addition & 0 deletions src/monit.h
Expand Up @@ -852,6 +852,7 @@ struct myrun {
char *mail_hostname; /**< Used in HELO/EHLO/MessageID when sending mail */
int mailserver_timeout; /**< Connect and read timeout for a SMTP server */
Mail_T maillist; /**< Global alert notification mailinglist */
char *alert_exec; /* a script to run on exec */
MailServer_T mailservers; /**< List of MTAs used for alert notification */
Mmonit_T mmonits; /**< Event notification and status receivers list */
Auth_T credentials; /** A list holding Basic Authentication information */
Expand Down
25 changes: 25 additions & 0 deletions src/p.y
Expand Up @@ -181,6 +181,7 @@
static struct myrate rate1 = {1, 1};
static struct myrate rate2 = {1, 1};
static char * htpasswd_file = NULL;
static char * exec = NULL;
static int digesttype = DIGEST_CLEARTEXT;
static int hassystem = FALSE;

Expand All @@ -207,6 +208,7 @@
static void addgeneric(Port_T, char*, char*);
static void addcommand(int, unsigned);
static void addargument(char *);
static void addexec(char *);
static void addmmonit(URL_T, int, int, char *);
static void addmailserver(MailServer_T);
static int addcredentials(char *, char *, int, int);
Expand Down Expand Up @@ -312,6 +314,7 @@ statement_list : statement
;

statement : setalert
| setexec
| setdaemon
| setlog
| seteventqueue
Expand Down Expand Up @@ -490,6 +493,9 @@ setalert : SET alertmail '{' eventoptionlist '}' formatlist reminder {
}
;

setexec : SET EXEC execlist { Run.alert_exec = exec; exec = NULL; }
;

setdaemon : SET DAEMON NUMBER startdelay {
if (!Run.isdaemon || ihp.daemon) {
ihp.daemon = TRUE;
Expand Down Expand Up @@ -875,6 +881,14 @@ argumentlist : argument
| argumentlist argument
;

execlist : exec
| execlist exec
;

exec : STRING { addexec($1); }
| PATH { addexec($1); }
;

useroptionlist : useroption
| useroptionlist useroption
;
Expand Down Expand Up @@ -1894,6 +1908,7 @@ static void preparse() {
Run.expectbuffer = STRLEN;
Run.mmonits = NULL;
Run.maillist = NULL;
Run.alert_exec = NULL;
Run.mailservers = NULL;
Run.MailFormat.from = NULL;
Run.MailFormat.replyto = NULL;
Expand Down Expand Up @@ -2725,6 +2740,16 @@ static void addargument(char *argument) {

}

static void addexec(char *more_exec) {
if (! exec)
{
exec = more_exec;
}
else
{
exec = Util_getString("%s '%s'",exec, more_exec);
}
}

/*
* Setup a url request for the current port object
Expand Down

0 comments on commit cb4e29c

Please sign in to comment.