-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDB.cpp
More file actions
175 lines (138 loc) · 5.15 KB
/
DB.cpp
File metadata and controls
175 lines (138 loc) · 5.15 KB
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
/*
*
* (C) 2013-14 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* 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, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "ntop_includes.h"
/* ******************************************* */
DB::DB(NetworkInterface *_iface, u_int32_t _dir_duration, u_int8_t _db_id) {
if((m = new(std::nothrow) Mutex()) == NULL)
ntop->getTrace()->traceEvent(TRACE_WARNING, "Internal error: NULL mutex. Are you running out of memory?");
dir_duration = max_val(_dir_duration, 300); /* 5 min is the minimum duration */
db_path[0] = '\0';
// sqlite3_config(SQLITE_CONFIG_SERIALIZED);
db = NULL, end_dump = 0, iface = _iface, db_id = _db_id;
contacts_cache_idx = 0;
for(int i=0; i<CONST_NUM_OPEN_DB_CACHE; i++) {
contacts_cache[i].db = NULL,
contacts_cache[i].last_open_contacts_db_path[0] = '\0',
contacts_cache[i].num_contacts_db_insert = 0,
contacts_cache[i].last_insert = 0;
}
}
/* ******************************************* */
DB::~DB() {
termDB();
if(m) delete m;
}
/* ******************************************* */
void DB::termDB() {
if(!ntop->getPrefs()->do_dump_flows_on_db()) return;
if(db) {
execSQL(db, (char*)"COMMIT;");
sqlite3_close(db);
db = NULL;
}
for(int i=0; i<CONST_NUM_OPEN_DB_CACHE; i++) {
if(contacts_cache[i].db != NULL) {
char *zErrMsg = NULL;
if(sqlite3_exec(contacts_cache[i].db, "COMMIT;", NULL, 0, &zErrMsg) != SQLITE_OK)
sqlite3_free(zErrMsg);
}
}
end_dump = 0;
}
/* ******************************************* */
void DB::initDB(time_t when, const char *create_sql_string) {
char path[MAX_PATH];
if(!ntop->getPrefs()->do_dump_flows_on_db()) return;
if(db != NULL) {
if(when < end_dump)
return;
else
termDB(); /* Too old: we first close it */
}
when -= when % dir_duration;
strftime(path, sizeof(path), "%Y/%m/%d/%H", localtime(&when));
snprintf(db_path, sizeof(db_path), "%s/%u/flows/%s",
ntop->get_working_dir(), iface->get_id(), path);
ntop->fixPath(db_path);
if(Utils::mkdir_tree(db_path)) {
strftime(path, sizeof(path), "%Y/%m/%d/%H/%M", localtime(&when));
snprintf(db_path, sizeof(db_path), "%s/%u/flows/%s.sqlite",
ntop->get_working_dir(), iface->get_id(), path);
end_dump = when + dir_duration;
if(sqlite3_open(db_path, &db) != 0) {
ntop->getTrace()->traceEvent(TRACE_ERROR,
"[DB] Unable to open/create DB %s [%s]",
db_path, sqlite3_errmsg(db));
end_dump = 0, db = NULL;
} else {
execSQL(db, (char*)create_sql_string);
ntop->getTrace()->traceEvent(TRACE_INFO,
"[DB] Created %s", db_path);
}
} else
ntop->getTrace()->traceEvent(TRACE_ERROR,
"[DB] Unable to create directory tree %s", db_path);
}
/* ******************************************* */
bool DB::dumpFlow(time_t when, Flow *f, char *json) {
const char *create_flows_db = "BEGIN; CREATE TABLE IF NOT EXISTS flows (ID INTEGER PRIMARY KEY AUTOINCREMENT, vlan_id number, cli_ip string KEY, cli_port number, "
"srv_ip string KEY, srv_port number, proto number, bytes number, first_seen number, last_seen number, duration number, json string);";
char sql[4096], cli_str[64], srv_str[64];
snprintf(sql, sizeof(sql),
"INSERT INTO flows VALUES (NULL, %u, '%s', %u, '%s', %u, %u, %lu, %u, %u, %u, '%s');",
f->get_vlan_id(),
f->get_cli_host()->get_ip()->print(cli_str, sizeof(cli_str)),
f->get_cli_port(),
f->get_srv_host()->get_ip()->print(srv_str, sizeof(srv_str)),
f->get_srv_port(),
f->get_protocol(),
(unsigned long)f->get_bytes(),
(unsigned) f->get_first_seen(),
(unsigned) f->get_last_seen(),
f->get_duration(), json ? json : "");
ntop->getTrace()->traceEvent(TRACE_DEBUG, "Dump Flow: %s", json);
if(m) m->lock(__FILE__, __LINE__);
initDB(when, create_flows_db);
execSQL(db, sql);
if(m) m->unlock(__FILE__, __LINE__);
return(true);
}
/* ******************************************* */
bool DB::execSQL(sqlite3 *_db, char* sql) {
if(ntop->getPrefs()->do_dump_flows_on_db()) {
int rc;
char *zErrMsg = 0;
if(_db == NULL) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "[DB] NULL DB handler [%s]", sql);
return(false);
}
ntop->getTrace()->traceEvent(TRACE_INFO, "[DB] %s", sql);
rc = sqlite3_exec(_db, sql, NULL, 0, &zErrMsg);
if(rc != SQLITE_OK) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "[DB] SQL error: %s [%s]", sql, zErrMsg);
sqlite3_free(zErrMsg);
return(false);
} else
return(true);
} else
return(false);
}
/* ******************************************* */