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(const DC_ITEM * item,AGENT_RESULT * result)42 int	get_value_db(const DC_ITEM *item, AGENT_RESULT *result)
43 {
44 	AGENT_REQUEST		request;
45 	const char		*dsn, *connection = NULL;
46 	zbx_odbc_data_source_t	*data_source;
47 	zbx_odbc_query_result_t	*query_result;
48 	char			*error = NULL;
49 	int			(*query_result_to_text)(zbx_odbc_query_result_t *query_result, char **text, char **error),
50 				ret = NOTSUPPORTED;
51 
52 	zabbix_log(LOG_LEVEL_DEBUG, "In %s() key_orig:'%s' query:'%s'", __func__, item->key_orig, item->params);
53 
54 	init_request(&request);
55 
56 	if (SUCCEED != parse_item_key(item->key, &request))
57 	{
58 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid item key format."));
59 		goto out;
60 	}
61 
62 	if (0 == strcmp(request.key, "db.odbc.select"))
63 	{
64 		query_result_to_text = zbx_odbc_query_result_to_string;
65 	}
66 	else if (0 == strcmp(request.key, "db.odbc.discovery"))
67 	{
68 		query_result_to_text = zbx_odbc_query_result_to_lld_json;
69 	}
70 	else if (0 == strcmp(request.key, "db.odbc.get"))
71 	{
72 		query_result_to_text = zbx_odbc_query_result_to_json;
73 	}
74 	else
75 	{
76 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Unsupported item key for this item type."));
77 		goto out;
78 	}
79 
80 	if (2 > request.nparam || 3 < request.nparam)
81 	{
82 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
83 		goto out;
84 	}
85 
86 	/* request.params[0] is ignored and is only needed to distinguish queries of same DSN */
87 
88 	dsn = request.params[1];
89 
90 	if (2 < request.nparam)
91 		connection = request.params[2];
92 
93 	if ((NULL == dsn || '\0' == *dsn) && (NULL == connection || '\0' == *connection))
94 	{
95 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid database connection settings."));
96 		goto out;
97 	}
98 
99 	if (NULL != (data_source = zbx_odbc_connect(dsn, connection, item->username, item->password, CONFIG_TIMEOUT,
100 			&error)))
101 	{
102 		if (NULL != (query_result = zbx_odbc_select(data_source, item->params, &error)))
103 		{
104 			char	*text = NULL;
105 
106 			if (SUCCEED == query_result_to_text(query_result, &text, &error))
107 			{
108 				SET_TEXT_RESULT(result, text);
109 				ret = SUCCEED;
110 			}
111 
112 			zbx_odbc_query_result_free(query_result);
113 		}
114 
115 		zbx_odbc_data_source_free(data_source);
116 	}
117 
118 	if (SUCCEED != ret)
119 		SET_MSG_RESULT(result, error);
120 out:
121 	free_request(&request);
122 
123 	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(ret));
124 
125 	return ret;
126 }
127 
128 #endif	/* HAVE_UNIXODBC */
129