Imported Upstream version 2.5.1
[ossec-hids.git] / src / os_dbd / dbd.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation.
10  *
11  * License details at the LICENSE file included with OSSEC or 
12  * online at: http://www.ossec.net/en/licensing.html
13  */
14
15
16 #ifndef DBD
17    #define DBD
18 #endif
19
20 #ifndef ARGV0
21    #define ARGV0 "ossec-dbd"
22 #endif
23
24 #include "shared.h"
25 #include "dbd.h"
26
27
28
29 /* OS_DBD: Monitor the alerts and insert them into the database.
30  * Only return in case of error.
31  */
32 void OS_DBD(DBConfig *db_config)
33 {
34     time_t tm;     
35     struct tm *p;       
36
37     file_queue *fileq;
38     alert_data *al_data;
39
40
41     /* Getting currently time before starting */
42     tm = time(NULL);
43     p = localtime(&tm); 
44
45
46     /* Initating file queue - to read the alerts */
47     os_calloc(1, sizeof(file_queue), fileq);
48     Init_FileQueue(fileq, p, 0);
49
50
51     /* Creating location hash */
52     db_config->location_hash = OSHash_Create();
53     if(!db_config->location_hash)
54     {
55         ErrorExit(MEM_ERROR, ARGV0);
56     }
57
58
59     /* Getting maximum ID */
60     db_config->alert_id = OS_SelectMaxID(db_config);
61     db_config->alert_id++;
62
63
64     /* Infinite loop reading the alerts and inserting them. */
65     while(1)
66     {
67         tm = time(NULL);
68         p = localtime(&tm);
69
70
71         /* Get message if available (timeout of 5 seconds) */
72         al_data = Read_FileMon(fileq, p, 5);
73         if(!al_data)
74         {
75             continue;
76         }
77
78
79         /* Inserting into the db */
80         OS_Alert_InsertDB(al_data, db_config);
81
82
83         /* Clearing the memory */
84         FreeAlertData(al_data);
85     }
86 }
87
88 /* EOF */