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
|
/*
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** 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.
**
** 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., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
#include "common.h"
#ifdef HAVE_ODBC
# include "zbxodbc.h"
#endif /* HAVE_ODBC */
#include "checks_db.h"
#include "log.h"
#ifdef HAVE_ODBC
/******************************************************************************
* *
* Function: get_param_value *
* *
* Purpose: retrieve parameter value by name *
* *
* Parameters: params - list of params *
* param_name - name of requested parameter *
* *
* Return value: NULL - if parameter missedparam_name, *
* else return value in new allocated memory *
* *
* Author: Eugene Grigorjev *
* *
* Comments: this function allocate memory, required zbx_free for result!!! *
* one parameter format: param1_name=param1_value *
* parameters splited by '\n' *
* *
******************************************************************************/
static char* get_param_value(char* params, const char* param_name)
{
char
*p = NULL,
*l = NULL,
*n = NULL,
*r = NULL,
*buf = NULL;
for(p = params; p && *p; p++)
{
r = NULL;
/* trim left spaces */
for(; *p == ' '; p++);
/* find '=' symbol */
for(n = p; *n && *n != '\n'; n++)
{
if(*n == '=')
{
/* trim right spaces */
for(l = n - 1; *l == ' '; l--);
l++;
/* compare parameter name */
if(l - p != strlen(param_name)) break;
if(strncmp(p, param_name, l - p)) break;
r = n+1;
break;
}
}
/* find EOL */
for(p = n; *p && *p != '\n'; p++);
/* allocate result */
if(r)
{
/* trim right EOL symbols */
while(*p == '\r' || *p == '\n' || *p == '\0') p--;
p++;
/* allocate result */
buf = zbx_malloc(buf, p - r + 1);
memmove(buf, r, p - r);
buf[p - r] = '\0';
break;
}
}
if(buf == NULL)
{
/* allocate result */
buf = zbx_malloc(buf, 1);
*buf = '\0';
}
return buf;
}
#endif /* HAVE_ODBC */
/******************************************************************************
* *
* Function: get_value_db *
* *
* Purpose: retrieve data from database *
* *
* Parameters: item - item we are interested in *
* *
* Return value: SUCCEED - data succesfully retrieved and stored in result *
* NOTSUPPORTED - requested item is not supported *
* *
* Author: Eugene Grigorjev *
* *
* Comments: *
* *
******************************************************************************/
int get_value_db(DB_ITEM *item, AGENT_RESULT *result)
{
#ifdef HAVE_ODBC
ZBX_ODBC_DBH dbh;
ZBX_ODBC_ROW row;
char
*db_dsn = NULL,
*db_user = NULL,
*db_pass = NULL,
*db_sql = NULL;
#endif /* HAVE_ODBC */
int ret = NOTSUPPORTED;
init_result(result);
zabbix_log(LOG_LEVEL_DEBUG, "In database monitor: %s", item->key);
#ifdef HAVE_ODBC
#define DB_ODBC_SELECT_KEY "db.odbc.select["
if(strncmp(item->key, DB_ODBC_SELECT_KEY, strlen(DB_ODBC_SELECT_KEY)) == 0)
{
db_dsn = get_param_value(item->params,"DSN");
db_user = get_param_value(item->params,"user");
db_pass = get_param_value(item->params,"password");
db_sql = get_param_value(item->params,"sql");
if( SUCCEED == odbc_DBconnect(&dbh, db_dsn, db_user, db_pass) )
{
if( NULL != (row = odbc_DBfetch(odbc_DBselect(&dbh, db_sql))) )
{
if( SUCCEED == set_result_type(result, item->value_type, row[0]) )
{
zabbix_log(LOG_LEVEL_DEBUG, "Result accepted with type 0x%02X [%s]", item->value_type, row[0]);
ret = SUCCEED;
}
else
{
zabbix_log(LOG_LEVEL_DEBUG, "Cant determine result type [%s]", row[0]);
}
}
else
{
SET_MSG_RESULT(result, strdup(get_last_odbc_strerror()));
}
odbc_DBclose(&dbh);
}
else
{
SET_MSG_RESULT(result, strdup(get_last_odbc_strerror()));
}
zbx_free(db_dsn);
zbx_free(db_user);
zbx_free(db_pass);
zbx_free(db_sql);
}
#endif /* HAVE_ODBC */
/*
* TODO:
*
* db.*.select[]
* db.*.ping
* ...
*
*/
return ret;
}
|