Project

General

Profile

manager-failover.diff

Implement a second failover for failure with a specific message. - Yoann VANDOORSELAERE, 07/12/2007 10:52 AM

Download (6.91 KB)

View differences:

plugins/reports/db/db.c
88 88
        if ( ret < 0 )
89 89
                prelude_log(PRELUDE_LOG_WARN, "could not insert message into database: %s.\n", preludedb_strerror(ret));
90 90

  
91
        if ( prelude_error_get_code(ret) == PRELUDEDB_ERROR_CONNECTION )
92
                ret = MANAGER_REPORT_PLUGIN_FAILURE_GLOBAL;
93
        else
94
                ret = MANAGER_REPORT_PLUGIN_FAILURE_SINGLE;
95

  
91 96
        return ret;
92 97
}
93 98

  
src/include/prelude-manager.h
6 6
* This file is part of the Prelude-Manager program.
7 7
*
8 8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by 
9
* it under the terms of the GNU General Public License as published by
10 10
* the Free Software Foundation; either version 2, or (at your option)
11 11
* any later version.
12 12
*
......
28 28
/*
29 29
 * Report plugin entry structure.
30 30
 */
31
#define MANAGER_REPORT_PLUGIN_FAILURE_GLOBAL  -1
32
#define MANAGER_REPORT_PLUGIN_FAILURE_SINGLE  -2
33

  
31 34
typedef struct {
32 35
        PRELUDE_PLUGIN_GENERIC;
33 36
        int (*run)(prelude_plugin_instance_t *pi, idmef_message_t *message);
src/report-plugins.c
54 54

  
55 55

  
56 56
typedef struct {
57
        int failover_enabled;
57
        prelude_bool_t failover_enabled;
58 58
        prelude_timer_t timer;
59

  
59 60
        prelude_failover_t *failover;
61
        prelude_failover_t *failed_failover;
60 62
} plugin_failover_t;
61 63

  
62 64

  
63 65

  
66
static int report_plugin_run_single(prelude_plugin_instance_t *pi, plugin_failover_t *pf, idmef_message_t *idmef);
67

  
68

  
69

  
64 70
static void get_failover_filename(prelude_plugin_instance_t *pi, char *buf, size_t size)
65 71
{
66 72
        prelude_plugin_generic_t *plugin = prelude_plugin_instance_get_plugin(pi);
......
94 100
                if ( ret < 0 )
95 101
                        break;
96 102

  
97
                ret = prelude_plugin_run(pi, manager_report_plugin_t, run, pi, idmef);
98
                if ( ret < 0 && pf )
103
                ret = report_plugin_run_single(pi, pf, idmef);
104
                if ( ret < 0 && ret != MANAGER_REPORT_PLUGIN_FAILURE_SINGLE )
99 105
                        break;
100 106

  
101 107
                prelude_msg_destroy(msg);
......
153 159
                text = "failed recovering";
154 160
        else {
155 161
                text = "recovered";
156
                pf->failover_enabled = 0;
162
                pf->failover_enabled = FALSE;
157 163
        }
158 164

  
159 165
        prelude_log(PRELUDE_LOG_WARN, "Plugin %s[%s]: %s from failover: %u/%u message flushed (%" PRELUDE_PRIu64 " bytes).\n",
......
192 198
        get_failover_filename(pi, filename, sizeof(filename));
193 199

  
194 200
        if ( ! prelude_plugin_instance_has_commit_func(pi) ) {
195
                prelude_log(PRELUDE_LOG_WARN, "plugin %s doesn't support failover.\n", plugin->name);
201
                prelude_log(PRELUDE_LOG_WARN, "plugin %s does not support failover.\n", plugin->name);
196 202
                return -1;
197 203
        }
198 204

  
......
209 215
                return -1;
210 216
        }
211 217

  
218
        snprintf(filename + strlen(filename), sizeof(filename) - strlen(filename), "/invalid");
219

  
220
        ret = prelude_failover_new(&pf->failed_failover, filename);
221
        if ( ret < 0 ) {
222
                prelude_perror(ret, "could not create failover object in %s", filename);
223
                free(pf);
224
                return -1;
225
        }
226

  
212 227
        prelude_plugin_instance_set_data(pi, pf);
213 228

  
214 229
        try_recovering_from_failover(pi, pf);
......
251 266

  
252 267

  
253 268

  
254
static void failover_init(prelude_plugin_generic_t *pg, prelude_plugin_instance_t *pi, plugin_failover_t *pf)
269
static void failover_init(prelude_plugin_instance_t *pi, plugin_failover_t *pf)
255 270
{
256
        pf->failover_enabled = 1;
271
        prelude_plugin_generic_t *pg = prelude_plugin_instance_get_plugin(pi);
272

  
273
        pf->failover_enabled = TRUE;
257 274

  
258 275
        prelude_log(PRELUDE_LOG_WARN, "Plugin %s[%s]: failure. Enabling failover.\n",
259 276
                    pg->name, prelude_plugin_instance_get_name(pi));
......
271 288
static int save_msgbuf(prelude_msgbuf_t *msgbuf, prelude_msg_t *msg)
272 289
{
273 290
        int ret;
274
        plugin_failover_t *pf = prelude_msgbuf_get_data(msgbuf);
291
        prelude_failover_t *pf = prelude_msgbuf_get_data(msgbuf);
275 292

  
276
        ret = prelude_failover_save_msg(pf->failover, msg);
293
        ret = prelude_failover_save_msg(pf, msg);
277 294
        if ( ret < 0 )
278 295
                prelude_perror(ret, "error saving message to disk");
279 296

  
......
283 300

  
284 301

  
285 302

  
286
static void save_idmef_message(plugin_failover_t *pf, idmef_message_t *msg)
303
static void save_idmef_message(prelude_failover_t *pf, idmef_message_t *msg)
287 304
{
288 305
        /*
289 306
         * this is a message we generated ourself...
......
295 312

  
296 313

  
297 314

  
315
static int report_plugin_run_single(prelude_plugin_instance_t *pi, plugin_failover_t *pf, idmef_message_t *idmef)
316
{
317
        int ret;
318

  
319
        ret = prelude_plugin_run(pi, manager_report_plugin_t, run, pi, idmef);
320
        if ( ret < 0 && pf ) {
321
                if ( ret == MANAGER_REPORT_PLUGIN_FAILURE_SINGLE )
322
                        save_idmef_message(pf->failed_failover, idmef);
323
                else {
324
                        failover_init(pi, pf);
325
                        save_idmef_message(pf->failover, idmef);
326
                }
327
        }
328

  
329
        return ret;
330
}
331

  
332

  
298 333

  
299 334
/*
300 335
 * Start all plugins of kind 'list'.
......
322 357
                        continue;
323 358

  
324 359
                if ( pf && pf->failover_enabled ) {
325
                        save_idmef_message(pf, idmef);
360
                        save_idmef_message(pf->failover, idmef);
326 361
                        continue;
327 362
                }
328 363

  
329
                ret = prelude_plugin_run(pi, manager_report_plugin_t, run, pi, idmef);
330
                if ( ret < 0 && pf ) {
331
                        failover_init(pg, pi, pf);
332
                        save_idmef_message(pf, idmef);
333
                }
334
        }
364
                report_plugin_run_single(pi, pf, idmef);
365
         }
335 366
}
336 367

  
337 368