File: sql.c

package info (click to toggle)
cacti-cactid 0.8.6d-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 668 kB
  • ctags: 282
  • sloc: sh: 3,186; ansic: 2,350; makefile: 69
file content (100 lines) | stat: -rw-r--r-- 3,446 bytes parent folder | download
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
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2004 Ian Berry                                            |
 |                                                                         |
 | 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 2          |
 | 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.                            |
 +-------------------------------------------------------------------------+
 | cactid: a backend data gatherer for cacti                               |
 +-------------------------------------------------------------------------+
 | This poller would not have been possible without:                       |
 |   - 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) |
 +-------------------------------------------------------------------------+
 | - raXnet - http://www.raxnet.net/                                       |
 +-------------------------------------------------------------------------+
*/

#include "common.h"
#include "cactid.h"
#include "locks.h"
#include "util.h"
#include "sql.h"

int db_insert(MYSQL *mysql, char *query) {
	char logmessage[LOGSIZE];

	if (set.verbose == POLLER_VERBOSITY_DEBUG) {
		snprintf(logmessage, LOGSIZE, "DEBUG: SQLCMD: %s\n", query);
		cacti_log(logmessage);
	}

	thread_mutex_lock(LOCK_MYSQL);
	if (mysql_query(mysql, query)) {
		snprintf(logmessage, LOGSIZE, "ERROR: Problem with MySQL: %s\n", mysql_error(mysql));
		cacti_log(logmessage);
  		thread_mutex_unlock(LOCK_MYSQL);
		return (FALSE);
	}else{
		thread_mutex_unlock(LOCK_MYSQL);
		return (TRUE);
	}
}

MYSQL_RES *db_query(MYSQL *mysql, char *query) {
	MYSQL_RES *mysql_res;

	thread_mutex_lock(LOCK_MYSQL);
 	mysql_query(mysql, query);
	mysql_res = mysql_store_result(mysql);
	thread_mutex_unlock(LOCK_MYSQL);

	return mysql_res;
}

int db_connect(char *database, MYSQL *mysql) {
	char logmessage[LOGSIZE];
	int retries;

	if (set.verbose == POLLER_VERBOSITY_DEBUG) {
		snprintf(logmessage, LOGSIZE, "MYSQL: Connecting to MySQL database '%s' on '%s'...\n", database, set.dbhost);
		cacti_log(logmessage);
	}

	retries = 1;
	mysql_init(mysql);
	mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, "5");
		
	while (1) {
		thread_mutex_lock(LOCK_MYSQL);

		if (!mysql_real_connect(mysql, set.dbhost, set.dbuser, set.dbpass, database, set.dbport, NULL, 0)) {
			if (retries > 10) {
				snprintf(logmessage, LOGSIZE, "MYSQL: Connection failed after 10 attempts : %s\n", mysql_error(mysql));
				cacti_log(logmessage);
				thread_mutex_unlock(LOCK_MYSQL);
				exit(-1);
			} else {
				retries++;
				usleep(100000);
			}
		}else {
			thread_mutex_unlock(LOCK_MYSQL);
			return (0);
		}
	}
}

void db_disconnect(MYSQL *mysql) {
	mysql_close(mysql);
}