This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
alert.c
259 lines (205 loc) · 6.81 KB
/
alert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (C) 2011 Tildeslash Ltd. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
#include <config.h>
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#include "monitor.h"
#include "event.h"
#include "net.h"
#include "alert.h"
/**
* Implementation of the alert module
*
* @author Jan-Henrik Haukeland, <[email protected]>
* @author Martin Pala <[email protected]>
* @author Christian Hopp <[email protected]>
*
* @file
*/
/* -------------------------------------------------------------- Prototypes */
static void copy_mail(Mail_T, Mail_T);
static void replace_bare_linefeed(Mail_T *);
static void substitute(Mail_T *, Event_T);
/* ------------------------------------------------------------------ Public */
/**
* Notify registred users about the event
* @param E An Event object
* @return If failed, return HANDLER_ALERT flag or HANDLER_SUCCEEDED if succeeded
*/
int handle_alert(Event_T E) {
Service_T s;
int rv = HANDLER_SUCCEEDED;
ASSERT(E);
s= Event_get_source(E);
if(!s) {
LogError("Aborting alert\n");
return rv;
}
if(s->maillist || Run.maillist) {
Mail_T m;
Mail_T n;
Mail_T list= NULL;
/*
* Build a mail-list with local recipients that has registered interest
* for this event.
*/
for(m= s->maillist; m; m= m->next) {
if(
/* particular event notification type is allowed for given recipient */
IS_EVENT_SET(m->events, Event_get_id(E)) &&
(
/* state change notification is sent always */
E->state_changed ||
/* in the case that the state is failed for more cycles we check
* whether we should send the reminder */
(E->state && m->reminder && E->count % m->reminder == 0)
)
)
{
Mail_T tmp= NULL;
NEW(tmp);
copy_mail(tmp, m);
substitute(&tmp, E);
replace_bare_linefeed(&tmp);
tmp->next= list;
list= tmp;
DEBUG("%s notification is sent to %s\n", Event_get_description(E), m->to);
}
}
/*
* Build a mail-list with global recipients that has registered interest
* for this event. Recipients which are defined in the service localy
* overrides the same recipient events which are registered globaly.
*/
for(m= Run.maillist; m; m= m->next) {
int skip= FALSE;
for(n= s->maillist; n; n= n->next) {
if(IS(m->to, n->to)) {
skip= TRUE;
break;
}
}
if(
/* the local service alert definition has not overrided the global one */
!skip &&
/* particular event notification type is allowed for given recipient */
IS_EVENT_SET(m->events, Event_get_id(E)) &&
(
/* state change notification is sent always */
E->state_changed ||
/* in the case that the state is failed for more cycles we check
* whether we should send the reminder */
(E->state && m->reminder && E->count % m->reminder == 0)
)
)
{
Mail_T tmp= NULL;
NEW(tmp);
copy_mail(tmp, m);
substitute(&tmp, E);
replace_bare_linefeed(&tmp);
tmp->next= list;
list= tmp;
DEBUG("%s notification is sent to %s\n", Event_get_description(E), m->to);
}
}
if(list) {
if(!sendmail(list))
rv = HANDLER_ALERT;
gc_mail_list(&list);
}
}
return rv;
}
static void substitute(Mail_T *m, Event_T e) {
char timestamp[STRLEN];
ASSERT(m && e);
Util_replaceString(&(*m)->from, "$HOST", Run.localhostname);
Util_replaceString(&(*m)->subject, "$HOST", Run.localhostname);
Util_replaceString(&(*m)->message, "$HOST", Run.localhostname);
Util_getRFC822Date((time_t *)&e->collected.tv_sec, timestamp, STRLEN);
Util_replaceString(&(*m)->subject, "$DATE", timestamp);
Util_replaceString(&(*m)->message, "$DATE", timestamp);
Util_replaceString(&(*m)->subject, "$SERVICE", Event_get_source_name(e));
Util_replaceString(&(*m)->message, "$SERVICE", Event_get_source_name(e));
Util_replaceString(&(*m)->subject, "$EVENT", Event_get_description(e));
Util_replaceString(&(*m)->message, "$EVENT", Event_get_description(e));
Util_replaceString(&(*m)->subject, "$DESCRIPTION", NVLSTR(Event_get_message(e)));
Util_replaceString(&(*m)->message, "$DESCRIPTION", NVLSTR(Event_get_message(e)));
Util_replaceString(&(*m)->subject, "$ACTION", Event_get_action_description(e));
Util_replaceString(&(*m)->message, "$ACTION", Event_get_action_description(e));
}
static void copy_mail(Mail_T n, Mail_T o) {
ASSERT(n && o);
n->to= xstrdup(o->to);
n->from=
o->from?
xstrdup(o->from):
Run.MailFormat.from?
xstrdup(Run.MailFormat.from):
xstrdup(ALERT_FROM);
n->replyto =
o->replyto?
xstrdup(o->replyto):
Run.MailFormat.replyto?
xstrdup(Run.MailFormat.replyto):
NULL;
n->subject=
o->subject?
xstrdup(o->subject):
Run.MailFormat.subject?
xstrdup(Run.MailFormat.subject):
xstrdup(ALERT_SUBJECT);
n->message=
o->message?
xstrdup(o->message):
Run.MailFormat.message?
xstrdup(Run.MailFormat.message):
xstrdup(ALERT_MESSAGE);
}
static void replace_bare_linefeed(Mail_T *m) {
Util_replaceString(&(*m)->message, "\r\n", "\n");
Util_replaceString(&(*m)->message, "\n", "\r\n");
}