1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **/
19
20 #include "checks_db.h"
21
22 #ifdef HAVE_UNIXODBC
23
24 #include "log.h"
25 #include "../odbc/odbc.h"
26
27 /******************************************************************************
28 * *
29 * Function: get_value_db *
30 * *
31 * Purpose: retrieve data from database *
32 * *
33 * Parameters: item - [IN] item we are interested in *
34 * result - [OUT] check result *
35 * *
36 * Return value: SUCCEED - data successfully retrieved and stored in result *
37 * NOTSUPPORTED - requested item is not supported *
38 * *
39 * Author: Eugene Grigorjev *
40 * *
41 ******************************************************************************/
get_value_db(DC_ITEM * item,AGENT_RESULT * result)42 int get_value_db(DC_ITEM *item, AGENT_RESULT *result)
43 {
44 const char *__function_name = "get_value_db";
45
46 AGENT_REQUEST request;
47 const char *dsn;
48 zbx_odbc_data_source_t *data_source;
49 zbx_odbc_query_result_t *query_result;
50 char *error = NULL;
51 int (*query_result_to_text)(zbx_odbc_query_result_t *query_result, char **text, char **error),
52 ret = NOTSUPPORTED;
53
54 zabbix_log(LOG_LEVEL_DEBUG, "In %s() key_orig:'%s' query:'%s'", __function_name, item->key_orig, item->params);
55
56 init_request(&request);
57
58 if (SUCCEED != parse_item_key(item->key, &request))
59 {
60 SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid item key format."));
61 goto out;
62 }
63
64 if (0 == strcmp(request.key, "db.odbc.select"))
65 {
66 query_result_to_text = zbx_odbc_query_result_to_string;
67 }
68 else if (0 == strcmp(request.key, "db.odbc.discovery"))
69 {
70 query_result_to_text = zbx_odbc_query_result_to_lld_json;
71 }
72 else
73 {
74 SET_MSG_RESULT(result, zbx_strdup(NULL, "Unsupported item key for this item type."));
75 goto out;
76 }
77
78 if (2 != request.nparam)
79 {
80 SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
81 goto out;
82 }
83
84 /* request.params[0] is ignored and is only needed to distinguish queries of same DSN */
85
86 dsn = request.params[1];
87
88 if (NULL == dsn || '\0' == *dsn)
89 {
90 SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
91 goto out;
92 }
93
94 if (NULL != (data_source = zbx_odbc_connect(dsn, item->username, item->password, CONFIG_TIMEOUT, &error)))
95 {
96 if (NULL != (query_result = zbx_odbc_select(data_source, item->params, &error)))
97 {
98 char *text = NULL;
99
100 if (SUCCEED == query_result_to_text(query_result, &text, &error))
101 {
102 SET_TEXT_RESULT(result, text);
103 ret = SUCCEED;
104 }
105
106 zbx_odbc_query_result_free(query_result);
107 }
108
109 zbx_odbc_data_source_free(data_source);
110 }
111
112 if (SUCCEED != ret)
113 SET_MSG_RESULT(result, error);
114 out:
115 free_request(&request);
116
117 zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
118
119 return ret;
120 }
121
122 #endif /* HAVE_UNIXODBC */
123