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
| /**
* Post a new Event
* @param service The Service the event belongs to
* @param id The event identification
* @param state The event state
* @param action Description of the event action
* @param s Optional message describing the event
*/
void Event_post(Service_T service, long id, short state, EventAction_T action, char *s, ...) {
ASSERT(service);
ASSERT(action);
ASSERT(s);
ASSERT(state == STATE_FAILED || state == STATE_SUCCEEDED || state == STATE_CHANGED || state == STATE_CHANGEDNOT);
va_list ap;
va_start(ap, s);
char *message = Str_vcat(s, ap);
va_end(ap);
Event_T e = service->eventlist;
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);
free(message);
return;
}
/* Initialize event list and add first event. The manadatory informations
* are cloned so the event is as standalone as possible and may be saved
* to the queue without the dependency on the original service, thus
* persistent and managable across monit restarts */
NEW(e);
e->id = id;
gettimeofday(&e->collected, NULL);
e->source = Str_dup(service->name);
e->mode = service->mode;
e->type = service->type;
e->state = STATE_INIT;
e->state_map = 1;
e->action = action;
e->message = message;
service->eventlist = e;
} else {
/* Try to find the event with the same origin and type identification. Each service and each test have its own custom actions object, so we share actions object address to identify event source. */
do {
if (e->action == action && e->id == id) {
gettimeofday(&e->collected, NULL);
/* Shift the existing event flags to the left and set the first bit based on actual state */
e->state_map <<= 1;
e->state_map |= ((state == STATE_SUCCEEDED || state == STATE_CHANGEDNOT) ? 0 : 1);
/* Update the message */
FREE(e->message);
e->message = message;
break;
}
e = e->next;
} while (e);
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);
free(message);
return;
}
/* Event was not found in the pending events list, we will add it.
* The manadatory informations are cloned so the event is as standalone
* as possible and may be saved to the queue without the dependency on
* the original service, thus persistent and managable across monit
* restarts */
NEW(e);
e->id = id;
gettimeofday(&e->collected, NULL);
e->source = Str_dup(service->name);
e->mode = service->mode;
e->type = service->type;
e->state = STATE_INIT;
e->state_map = 1;
e->action = action;
e->message = message;
e->next = service->eventlist;
service->eventlist = e;
}
}
e->state_changed = Event_check_state(e, state);
/* In the case that the state changed, update it and reset the counter */
if (e->state_changed) {
e->state = state;
e->count = 1;
} else
e->count++;
handle_event(service, e);
}
|