-
Notifications
You must be signed in to change notification settings - Fork 45
/
sql.c
604 lines (511 loc) · 18.9 KB
/
sql.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
ex: set tabstop=4 shiftwidth=4 autoindent:
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU Lesser General Public |
| License as published by the Free Software Foundation; either |
| version 2.1 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 Lesser General Public License for more details. |
| |
| You should have received a copy of the GNU Lesser General Public |
| License along with this library; if not, write to the Free Software |
| Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 02110-1301, USA |
| |
+-------------------------------------------------------------------------+
| spine: a backend data gatherer for cacti |
+-------------------------------------------------------------------------+
| This poller would not have been possible without: |
| - Larry Adams (current development and enhancements) |
| - Rivo Nurges (rrd support, mysql poller cache, misc functions) |
| - RTG (core poller code, pthreads, snmp, autoconf examples) |
| - Brady Alleman/Doug Warner (threading ideas, implimentation details) |
+-------------------------------------------------------------------------+
| - Cacti - http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
#include "common.h"
#include "spine.h"
/*! \fn int db_insert(MYSQL *mysql, int type, const char *query)
* \brief inserts a row or rows in a database table.
* \param mysql the database connection object
* \param type the database to connect to local or remote
* \param query the database query to execute
*
* Unless the SQL_readonly boolean is set to TRUE, the function will execute
* the SQL statement specified in the query variable.
*
* \return TRUE if successful, or FALSE if not.
*
*/
int db_insert(MYSQL *mysql, int type, const char *query) {
int error;
int error_count = 0;
char query_frag[LRG_BUFSIZE];
/* save a fragment just in case */
memset(query_frag, 0, LRG_BUFSIZE);
snprintf(query_frag, LRG_BUFSIZE, "%s", query);
/* show the sql query */
SPINE_LOG_DEVDBG(("DEVDBG: SQL:%s", query_frag));
while(1) {
if (set.SQL_readonly == FALSE) {
if (mysql_query(mysql, query)) {
error = mysql_errno(mysql);
if (error == 2013 || error == 2006) {
if (errno != EINTR) {
db_reconnect(mysql, error, "db_insert");
error_count++;
if (error_count > 30) {
die("FATAL: Too many Reconnect Attempts!");
}
continue;
} else {
usleep(50000);
continue;
}
}
if ((error == 1213) || (error == 1205)) {
usleep(50000);
error_count++;
if (error_count > 30) {
SPINE_LOG(("ERROR: Too many Lock/Deadlock errors occurred!, SQL Fragment:'%s'", query_frag));
return FALSE;
}
continue;
} else {
SPINE_LOG(("ERROR: SQL Failed! Error:'%i', Message:'%s', SQL Fragment:'%s'", error, mysql_error(mysql), query_frag));
return FALSE;
}
} else {
return TRUE;
}
} else {
return TRUE;
}
}
}
int db_reconnect(MYSQL *mysql, int error, char *function) {
unsigned long mysql_thread = 0;
char query[100];
mysql_thread = mysql_thread_id(mysql);
mysql_ping(mysql);
if (mysql_thread_id(mysql) != mysql_thread) {
SPINE_LOG(("WARNING: Connection Broken in Function %s with Error %i. Reconnect successful.", function, error));
snprintf(query, 100, "KILL %lu;", mysql_thread);
mysql_query(mysql, query);
mysql_query(mysql, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_DATE', ''))");
mysql_query(mysql, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_IN_DATE', ''))");
mysql_query(mysql, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY', ''))");
mysql_query(mysql, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_AUTO_VALUE_ON_ZERO', ''))");
mysql_query(mysql, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'TRADITIONAL', ''))");
mysql_query(mysql, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'STRICT_ALL_TABLES', ''))");
sleep(1);
return TRUE;
} else {
SPINE_LOG(("WARNING: Connection Broken with Error %i. Reconnect failed.", error));
return FALSE;
}
}
/*! \fn MYSQL_RES *db_query(MYSQL *mysql, int type, const char *query)
* \brief executes a query and returns a pointer to the result set.
* \param mysql the database connection object
* \param query the database query to execute
*
* This function will execute the SQL statement specified in the query variable.
*
* \return MYSQL_RES a MySQL result structure
*
*/
MYSQL_RES *db_query(MYSQL *mysql, int type, const char *query) {
MYSQL_RES *mysql_res = 0;
int error = 0;
int error_count = 0;
char query_frag[LRG_BUFSIZE];
/* save a fragment just in case */
memset(query_frag, 0, LRG_BUFSIZE);
snprintf(query_frag, LRG_BUFSIZE, "%s", query);
/* show the sql query */
SPINE_LOG_DEVDBG(("DEVDBG: SQL:%s", query_frag));
while (1) {
if (mysql_query(mysql, query)) {
error = mysql_errno(mysql);
if (error == 2013 || error == 2006) {
if (errno != EINTR) {
db_reconnect(mysql, error, "db_query");
error_count++;
if (error_count > 30) {
die("FATAL: Too many Reconnect Attempts!");
}
continue;
} else {
usleep(50000);
continue;
}
}
if (error == 1213 || error == 1205) {
usleep(50000);
error_count++;
if (error_count > 30) {
SPINE_LOG(("FATAL: Too many Lock/Deadlock errors occured!, SQL Fragment:'%s'", query_frag));
exit(1);
}
continue;
} else {
SPINE_LOG(("FATAL: Database Error:'%i', Message:'%s'", error, mysql_error(mysql)));
SPINE_LOG(("ERROR: The Query Was:'%s'", query));
exit(1);
}
} else {
mysql_res = mysql_store_result(mysql);
break;
}
}
return mysql_res;
}
/*! \fn void db_connect(char *database, MYSQL *mysql)
* \brief opens a connection to a MySQL databse.
* \param database a string pointer to the database name
* \param mysql a pointer to a mysql database connection object
*
* This function will attempt to open a connection to a MySQL database and then
* return the connection object to the calling function. If the database connection
* fails more than 20 times, the function will fail and Spine will terminate.
*
*/
void db_connect(int type, MYSQL *mysql) {
int tries;
int attempts;
int timeout;
int rtimeout;
int wtimeout;
int options_error;
int success;
int error = 0;
bool reconnect;
MYSQL *connect_error;
char *hostname = NULL;
char *socket = NULL;
struct stat socket_stat;
static int connections = 0;
/* see if the hostname variable is a file reference. If so,
* and if it is a socket file, setup mysql to use it.
*/
if (set.poller_id > 1) {
if (type == LOCAL) {
STRDUP_OR_DIE(hostname, set.db_host, "db_host")
if (stat(hostname, &socket_stat) == 0) {
if (socket_stat.st_mode & S_IFSOCK) {
socket = strdup (set.db_host);
hostname = NULL;
}
} else if ((socket = strstr(hostname,":"))) {
*socket++ = 0x0;
}
} else {
STRDUP_OR_DIE(hostname, set.rdb_host, "rdb_host")
}
} else {
STRDUP_OR_DIE(hostname, set.db_host, "db_host")
if (stat(hostname, &socket_stat) == 0) {
if (socket_stat.st_mode & S_IFSOCK) {
socket = strdup (set.db_host);
hostname = NULL;
}
} else if ((socket = strstr(hostname,":"))) {
*socket++ = 0x0;
}
}
/* initialalize variables */
tries = 2;
success = FALSE;
timeout = 5;
rtimeout = 30;
wtimeout = 30;
reconnect = 1;
attempts = 1;
mysql_init(mysql);
if (mysql == NULL) {
printf("FATAL: Database unable to allocate memory and therefore can not connect\n");
exit(1);
}
MYSQL_SET_OPTION(MYSQL_OPT_READ_TIMEOUT, (int *)&rtimeout, "read timeout");
MYSQL_SET_OPTION(MYSQL_OPT_WRITE_TIMEOUT, (int *)&wtimeout, "write timeout");
MYSQL_SET_OPTION(MYSQL_OPT_CONNECT_TIMEOUT, (int *)&timeout, "general timeout");
#if defined(MARIADB_BASE_VERSION) || (MYSQL_VERSION_ID < 80034 && MYSQL_VERSION_ID >= 50013)
MYSQL_SET_OPTION(MYSQL_OPT_RECONNECT, &reconnect, "reconnect");
#endif
#ifdef HAS_MYSQL_OPT_RETRY_COUNT
MYSQL_SET_OPTION(MYSQL_OPT_RETRY_COUNT, &tries, "retry count");
#endif
/* set SSL options if available */
#ifdef HAS_MYSQL_OPT_SSL_KEY
char *ssl_key = NULL;
char *ssl_ca = NULL;
char *ssl_cert = NULL;
if (type == REMOTE) {
STRDUP_OR_DIE(ssl_key, set.rdb_ssl_key, "rdb_ssl_key");
STRDUP_OR_DIE(ssl_ca, set.rdb_ssl_ca, "rdb_ssl_ca");
STRDUP_OR_DIE(ssl_cert, set.rdb_ssl_cert, "rdb_ssl_cert");
} else {
STRDUP_OR_DIE(ssl_key, set.db_ssl_key, "db_ssl_key");
STRDUP_OR_DIE(ssl_ca, set.db_ssl_ca, "db_ssl_ca");
STRDUP_OR_DIE(ssl_cert, set.db_ssl_cert, "db_ssl_cert");
}
if (strlen(ssl_key)) MYSQL_SET_OPTION(MYSQL_OPT_SSL_KEY, ssl_key, "ssl key");
if (strlen(ssl_ca)) MYSQL_SET_OPTION(MYSQL_OPT_SSL_CA, ssl_ca, "ssl ca");
if (strlen(ssl_cert)) MYSQL_SET_OPTION(MYSQL_OPT_SSL_CERT, ssl_cert, "ssl cert");
#endif
while (tries > 0) {
tries--;
if (set.poller_id > 1) {
if (type == LOCAL) {
connect_error = mysql_real_connect(mysql, hostname, set.db_user, set.db_pass, set.db_db, set.db_port, socket, 0);
} else {
connect_error = mysql_real_connect(mysql, hostname, set.rdb_user, set.rdb_pass, set.rdb_db, set.rdb_port, socket, 0);
}
} else {
connect_error = mysql_real_connect(mysql, hostname, set.db_user, set.db_pass, set.db_db, set.db_port, socket, 0);
}
if (!connect_error) {
error = mysql_errno(mysql);
if ((error == 2002 || error == 2003 || error == 2006 || error == 2013) && errno == EINTR) {
usleep(5000);
tries++;
success = FALSE;
} else if (error == 2002) {
printf("Database: Connection Failed: Attempt:'%u', Error:'%u', Message:'%s'\n", attempts, mysql_errno(mysql), mysql_error(mysql));
sleep(1);
success = FALSE;
} else if (error != 1049 && error != 2005 && error != 1045) {
printf("Database: Connection Failed: Error:'%u', Message:'%s'\n", error, mysql_error(mysql));
success = FALSE;
usleep(50000);
} else {
tries = 0;
success = FALSE;
}
} else {
tries = 0;
success = TRUE;
break;
}
attempts++;
}
if (hostname != NULL) {
free(hostname);
}
#ifdef HAS_MYSQL_OPT_SSL_KEY
if (ssl_key != NULL) {
free(ssl_key);
}
if (ssl_ca != NULL) {
free(ssl_ca);
}
if (ssl_cert != NULL) {
free(ssl_cert);
}
#endif
if (!success){
printf("FATAL: Connection Failed, Error:'%i', Message:'%s'\n", error, mysql_error(mysql));
exit(1);
}
SPINE_LOG_DEBUG(("DEBUG: Total Connections made %i", connections));
connections++;
}
/*! \fn void db_disconnect(MYSQL *mysql)
* \brief closes connection to MySQL database
* \param mysql the database connection object
*
*/
void db_disconnect(MYSQL *mysql) {
if (mysql != NULL) {
mysql_close(mysql);
}
}
/*! \fn void db_create_connection_pool(int type)
* \brief Creates a connection pool for spine
* \param type the connection type, LOCAL or REMOTE
*
*/
void db_create_connection_pool(int type) {
int id;
if (type == LOCAL) {
SPINE_LOG_DEBUG(("DEBUG: Creating Local Connection Pool of %i threads.", set.threads));
for(id = 0; id < set.threads; id++) {
SPINE_LOG_DEBUG(("DEBUG: Creating Local Connection %i.", id));
db_connect(type, &db_pool_local[id].mysql);
db_insert(&db_pool_local[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_DATE', ''))");
db_insert(&db_pool_local[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_IN_DATE', ''))");
db_insert(&db_pool_local[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY', ''))");
db_insert(&db_pool_local[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_AUTO_VALUE_ON_ZERO', ''))");
db_insert(&db_pool_local[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'TRADITIONAL', ''))");
db_insert(&db_pool_local[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'STRICT_ALL_TABLES', ''))");
db_insert(&db_pool_local[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'STRICT_TRANS_TABLES', ''))");
db_pool_local[id].free = TRUE;
db_pool_local[id].id = id;
}
} else {
SPINE_LOG_DEBUG(("DEBUG: Creating Remote Connection Pool of %i threads.", set.threads));
for(id = 0; id < set.threads; id++) {
SPINE_LOG_DEBUG(("DEBUG: Creating Remote Connection %i.", id));
db_connect(type, &db_pool_remote[id].mysql);
db_insert(&db_pool_remote[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_DATE', ''))");
db_insert(&db_pool_remote[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_ZERO_IN_DATE', ''))");
db_insert(&db_pool_remote[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY', ''))");
db_insert(&db_pool_remote[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'NO_AUTO_VALUE_ON_ZERO', ''))");
db_insert(&db_pool_remote[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'TRADITIONAL', ''))");
db_insert(&db_pool_remote[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'STRICT_ALL_TABLES', ''))");
db_insert(&db_pool_remote[id].mysql, LOCAL, "SET SESSION sql_mode = (SELECT REPLACE(@@sql_mode,'STRICT_TRANS_TABLES', ''))");
db_pool_remote[id].free = TRUE;
db_pool_remote[id].id = id;
}
}
}
/*! \fn void db_close_connection_pool(int type)
* \brief Closes a connection pool for spine
* \param type the connection type, LOCAL or REMOTE
*
*/
void db_close_connection_pool(int type) {
int id;
if (type == LOCAL) {
for(id = 0; id < set.threads; id++) {
SPINE_LOG_DEBUG(("DEBUG: Closing Local Connection Pool ID %i", id));
db_disconnect(&db_pool_local[id].mysql);
}
free(db_pool_local);
} else {
for(id = 0; id < set.threads; id++) {
SPINE_LOG_DEBUG(("DEBUG: Closing Remote Connection Pool ID %i", id));
db_disconnect(&db_pool_remote[id].mysql);
}
free(db_pool_remote);
}
}
/*! \fn pool_t db_get_connection(int type)
* \brief returns a free mysql connection from the pool
* \param type the connection type, LOCAL or REMOTE
*
*/
pool_t *db_get_connection(int type) {
int id;
thread_mutex_lock(LOCK_POOL);
if (type == LOCAL) {
SPINE_LOG_DEBUG(("DEBUG: Traversing Local Connection Pool for free connection."));
for (id = 0; id < set.threads; id++) {
SPINE_LOG_DEBUG(("DEBUG: Checking Local Pool ID %i.", id));
if (db_pool_local[id].free == TRUE) {
SPINE_LOG_DEBUG(("DEBUG: Allocating Local Pool ID %i.", id));
db_pool_local[id].free = FALSE;
thread_mutex_unlock(LOCK_POOL);
return &db_pool_local[id];
}
}
} else {
SPINE_LOG_DEBUG(("DEBUG: Traversing Remote Connection Pool for free connection."));
for (id = 0; id < set.threads; id++) {
SPINE_LOG_DEBUG(("DEBUG: Checking Remote Pool ID %i.", id));
if (db_pool_remote[id].free == TRUE) {
SPINE_LOG_DEBUG(("DEBUG: Allocating Remote Pool ID %i.", id));
db_pool_remote[id].free = FALSE;
thread_mutex_unlock(LOCK_POOL);
return &db_pool_remote[id];
}
}
}
SPINE_LOG(("FATAL: Connection Pool Fatal Error."));
thread_mutex_unlock(LOCK_POOL);
return NULL;
}
/*! \fn voi db_release_connection(int id)
* \brief marks a database connection as free
* \param id the connection id
*
*/
void db_release_connection(int type, int id) {
thread_mutex_lock(LOCK_POOL);
if (type == LOCAL) {
SPINE_LOG_DEBUG(("DEBUG: Freeing Local Pool ID %i", id));
db_pool_local[id].free = TRUE;
} else {
SPINE_LOG_DEBUG(("DEBUG: Freeing Remote Pool ID %i", id));
db_pool_remote[id].free = TRUE;
}
thread_mutex_unlock(LOCK_POOL);
}
/*! \fn int append_hostrange(char *obuf, const char *colname, const config_t *set)
* \brief appends a host range to a sql select statement
* \param obuf the sql select statment to have the host range appended
* \param colname the sql column name that will have the host range checked
* \param set global runtime settings
*
* Several places in the code need to limit the range of hosts to
* those with a certain ID range, but only if those range values
* are actually nonzero.
*
* This appends the SQL clause if necessary, returning the # of
* characters added to the buffer. Else return 0.
*
* \return the number of characters added to the end of the character buffer
*
*/
int append_hostrange(char *obuf, const char *colname) {
if (HOSTID_DEFINED(set.start_host_id) && HOSTID_DEFINED(set.end_host_id)) {
return sprintf(obuf, " AND %s BETWEEN %d AND %d",
colname,
set.start_host_id,
set.end_host_id);
} else {
return 0;
}
}
/*! \fn void db_escape(MYSQL *mysql, char *output, int max_size, const char *input)
* \brief Escapse a text string to make it safe for mysql insert/updates
* \param mysql the connection object
* \param output a pointer to the output string
* \param a pointer to the input string
*
* A simple implementation of the mysql_real_escape_string that one
* day should be portable.
*
* \return void
*
*/
void db_escape(MYSQL *mysql, char *output, int max_size, const char *input) {
if (input == NULL) return;
char input_trimmed[DBL_BUFSIZE];
int max_escaped_input_size = (strlen(input) * 2) + 1;
if (max_escaped_input_size > max_size) {
snprintf(input_trimmed, (max_size / 2) - 1, "%s", input);
} else {
snprintf(input_trimmed, max_size, "%s", input);
}
mysql_real_escape_string(mysql, output, input_trimmed, strlen(input_trimmed));
}
void db_free_result(MYSQL_RES *result) {
mysql_free_result(result);
}
int db_column_exists(MYSQL *mysql, int type, const char *table, const char *column) {
char query_frag[BUFSIZE];
MYSQL_RES *result;
int exists;
/* save a fragment just in case */
memset(query_frag, 0, BUFSIZE);
snprintf(query_frag, BUFSIZE, "SHOW COLUMNS FROM `%s` LIKE '%s'", table, column);
/* show the sql query */
SPINE_LOG_DEVDBG(("DEVDBG: db_column_exists('%s','%s'): %s", table, column, query_frag));
result = db_query(mysql, LOCAL, query_frag);
if (mysql_num_rows(result)) {
exists = TRUE;
} else {
exists = FALSE;
}
db_free_result(result);
return exists;
}