-
Notifications
You must be signed in to change notification settings - Fork 4
/
dblayer.c
353 lines (264 loc) · 7.41 KB
/
dblayer.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Copyright (C) 2012 Ricardo Maraschini
*
* 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, version 2 of the License.
*
* 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.
*
*/
#include "baseline.h"
int max_entries;
int sazonality;
MYSQL *conn = NULL;
char *db_server = NULL;
char *db_user = NULL;
char *db_password = NULL;
char *db_database = "nada";
int db_set_dbserver(char *srv) {
db_server = malloc(strlen(srv) + 1);
if (db_server == NULL)
return ERROR;
strcpy(db_server,srv);
return OK;
}
int db_set_dbname(char *dbname) {
db_database = malloc(strlen(dbname) + 1);
if (!db_database)
return ERROR;
strcpy(db_database, dbname);
return OK;
}
void db_set_sazonality(int saz) {
sazonality = saz;
}
int db_set_dbuser(char *usr) {
db_user = malloc(strlen(usr) + 1);
if (db_user == NULL)
return ERROR;
strcpy(db_user,usr);
return OK;
}
int db_set_dbpassword(char *pass) {
db_password = malloc(strlen(pass) + 1);
if (db_password == NULL)
return ERROR;
strcpy(db_password,pass);
return OK;
}
void db_set_max_entries(int entries) {
max_entries = entries;
}
int db_get_max_entries() {
return max_entries;
}
int db_insert_command_line(char *command_line) {
char *query;
char *prot_host_name;
char *prot_service_description;
MYSQL_RES *result = NULL;
MYSQL_ROW row;
int id = 0;
prot_host_name = escape_string(host_name);
prot_service_description = escape_string(service_description);
asprintf(&query,"insert into commands values ('','%s','%s','%s')", command_line,
prot_host_name,
prot_service_description
);
do_query(query, FALSE, NULL);
free(query);
query = NULL;
asprintf(&query,"select LAST_INSERT_ID()");
do_query(query, TRUE, &result);
while ( (row = mysql_fetch_row(result)) ) {
id = atoi(row[0]);
}
mysql_free_result(result);
free(query);
return id;
}
int db_get_command_line_id(char *command_line) {
int id;
char *query = NULL;
char *prot_host_name;
char *prot_service_description;
MYSQL_RES *result = NULL;
MYSQL_ROW row;
prot_host_name = escape_string(host_name);
prot_service_description = escape_string(service_description);
asprintf(&query, "select id from commands where command_line='%s' and host_name='%s' and service_description = '%s'", command_line,
prot_host_name,
prot_service_description
);
id = 0;
do_query(query, TRUE, &result);
while ( (row = mysql_fetch_row(result)) ) {
id = atoi(row[0]);
}
mysql_free_result(result);
free(query);
return id;
}
int db_insert_metric(char *command_line, struct metric_t *mt) {
char *query = NULL;
char *prot_cmd_line = NULL;
char *prot_metric_name = NULL;
int ret;
int command_line_id;
prot_cmd_line = escape_string(command_line);
prot_metric_name = escape_string(mt->name);
command_line_id = db_get_command_line_id(prot_cmd_line);
if (command_line_id == 0) {
// a new command line
command_line_id = db_insert_command_line(prot_cmd_line);
}
asprintf( &query,
"insert into history values( %d, now(), %f, '%s');",
command_line_id,
mt->value,
prot_metric_name
);
ret = do_query(query, FALSE, NULL);
free(prot_cmd_line);
free(prot_metric_name);
free(query);
if (ret != OK)
return ret;
return OK;
}
int db_retrieve_last_values(char *command_line, struct metric_t *mt, float *last_values) {
int i = 0;
char *prot_cmd_line = NULL;
char *prot_metric_name = NULL;
char *query = NULL;
char *time_gaps = NULL;
MYSQL_RES *result = NULL;
MYSQL_ROW row;
int command_line_id = 0;
prot_cmd_line = escape_string(command_line);
prot_metric_name = escape_string(mt->name);
command_line_id = db_get_command_line_id(prot_cmd_line);
time_gaps = db_create_time_gaps();
asprintf( &query,
"select value from history where command_line_id=%d and metric='%s' and (%s) order by entry_time asc limit %d",
command_line_id,
prot_metric_name,
time_gaps,
max_entries
);
do_query(query, TRUE, &result);
while ( (row = mysql_fetch_row(result)) ) {
*(last_values + i) = atof(row[0]);
i++;
}
mysql_free_result(result);
free(query);
free(prot_cmd_line);
free(prot_metric_name);
free(time_gaps);
return OK;
}
char *db_create_time_gaps() {
time_t now;
time_t previous_week;
time_t before;
time_t after;
struct tm *time_begin;
struct tm *time_end;
char *time_gaps = NULL;
char *tmp_time_gap = NULL;
char *str_time_begin = NULL;
char *str_time_end = NULL;
int time_tolerance = 300;
int i;
now = time(NULL);
if ((str_time_begin = malloc(20)) == NULL)
return NULL;
if ((str_time_end = malloc(20)) == NULL)
return NULL;
asprintf(&time_gaps," ");
// date format: 2012-06-27 22:02:55
for(i=0; i<max_entries; i++) {
previous_week = now - (i * 60 * 60 * 24 * sazonality);
before = previous_week - time_tolerance;
after = previous_week + time_tolerance;
if ((time_begin = malloc(sizeof(struct tm))) == NULL) {
free(str_time_begin);
free(str_time_end);
return NULL;
}
if ((time_end = malloc(sizeof(struct tm))) == NULL) {
free(str_time_begin);
free(str_time_end);
return NULL;
}
localtime_r(&before, time_begin);
localtime_r(&after, time_end);
strftime(str_time_begin, 20, "%Y-%m-%d %H:%M:%S", time_begin);
strftime(str_time_end, 20, "%Y-%m-%d %H:%M:%S", time_end);
asprintf(&tmp_time_gap," (entry_time between '%s' and '%s') ", str_time_begin, str_time_end);
if (i > 0) {
time_gaps = realloc(time_gaps, strlen(time_gaps) + 3);
strcat(time_gaps,"or");
}
time_gaps = realloc(time_gaps, strlen(time_gaps) + strlen(tmp_time_gap) + 1);
strcat(time_gaps,tmp_time_gap);
free(time_begin);
free(time_end);
free(tmp_time_gap);
tmp_time_gap = NULL;
}
free(str_time_begin);
free(str_time_end);
return time_gaps;
}
int db_purge_old_data() {
struct timeval now;
time_t time_boundary;
char *query;
if (gettimeofday(&now,NULL) < 0)
return ERROR;
time_boundary = now.tv_sec - max_entries * sazonality * 24 * 60 * 60;
asprintf(&query, "delete from history where entry_time < from_unixtime(%lu)", time_boundary);
if (do_query(query, FALSE, NULL) != OK)
return ERROR;
free(query);
return OK;
}
int db_open_conn() {
conn = mysql_init(NULL);
if ( !mysql_real_connect(conn, db_server, db_user, db_password, db_database, 0, NULL, 0)) {
return ERROR;
}
return OK;
}
void db_close_conn() {
mysql_close(conn);
free(db_server);
free(db_user);
free(db_password);
}
char *escape_string(char *from) {
char *to = NULL;
to = malloc( strlen(from) * 2 + 1);
if (to == NULL)
return NULL;
mysql_real_escape_string(conn, to, from, strlen(from));
return to;
}
int do_query(char *q, int return_values, MYSQL_RES **result) {
MYSQL_RES *res = NULL;
if (mysql_query(conn, q)) {
return ERROR;
}
if (return_values) {
res = mysql_store_result(conn);
*result = res;
}
return OK;
}