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 "common.h"
21 #include "zbxconf.h"
22 
23 #include "cfg.h"
24 #include "log.h"
25 #include "alias.h"
26 #include "sysinfo.h"
27 #ifdef _WINDOWS
28 #	include "perfstat.h"
29 #endif
30 #include "comms.h"
31 
32 /******************************************************************************
33  *                                                                            *
34  * Function: load_aliases                                                     *
35  *                                                                            *
36  * Purpose: load aliases from configuration                                   *
37  *                                                                            *
38  * Parameters: lines - aliase entries from configuration file                 *
39  *                                                                            *
40  * Comments: calls add_alias() for each entry                                 *
41  *                                                                            *
42  ******************************************************************************/
load_aliases(char ** lines)43 void	load_aliases(char **lines)
44 {
45 	char	**pline;
46 
47 	for (pline = lines; NULL != *pline; pline++)
48 	{
49 		char		*c;
50 		const char	*r = *pline;
51 
52 		if (SUCCEED != parse_key(&r) || ':' != *r)
53 		{
54 			zabbix_log(LOG_LEVEL_CRIT, "cannot add alias \"%s\": invalid character at position %d",
55 					*pline, (int)((r - *pline) + 1));
56 			exit(EXIT_FAILURE);
57 		}
58 
59 		c = (char *)r++;
60 
61 		if (SUCCEED != parse_key(&r) || '\0' != *r)
62 		{
63 			zabbix_log(LOG_LEVEL_CRIT, "cannot add alias \"%s\": invalid character at position %d",
64 					*pline, (int)((r - *pline) + 1));
65 			exit(EXIT_FAILURE);
66 		}
67 
68 		*c++ = '\0';
69 
70 		add_alias(*pline, c);
71 
72 		*--c = ':';
73 	}
74 }
75 
76 /******************************************************************************
77  *                                                                            *
78  * Function: load_user_parameters                                             *
79  *                                                                            *
80  * Purpose: load user parameters from configuration                           *
81  *                                                                            *
82  * Parameters: lines - user parameter entries from configuration file         *
83  *                                                                            *
84  * Author: Vladimir Levijev                                                   *
85  *                                                                            *
86  * Comments: calls add_user_parameter() for each entry                        *
87  *                                                                            *
88  ******************************************************************************/
load_user_parameters(char ** lines)89 void	load_user_parameters(char **lines)
90 {
91 	char	*p, **pline, error[MAX_STRING_LEN];
92 
93 	for (pline = lines; NULL != *pline; pline++)
94 	{
95 		if (NULL == (p = strchr(*pline, ',')))
96 		{
97 			zabbix_log(LOG_LEVEL_CRIT, "cannot add user parameter \"%s\": not comma-separated", *pline);
98 			exit(EXIT_FAILURE);
99 		}
100 		*p = '\0';
101 
102 		if (FAIL == add_user_parameter(*pline, p + 1, error, sizeof(error)))
103 		{
104 			*p = ',';
105 			zabbix_log(LOG_LEVEL_CRIT, "cannot add user parameter \"%s\": %s", *pline, error);
106 			exit(EXIT_FAILURE);
107 		}
108 		*p = ',';
109 	}
110 }
111 
112 #ifdef _WINDOWS
113 /******************************************************************************
114  *                                                                            *
115  * Function: load_perf_counters                                               *
116  *                                                                            *
117  * Purpose: load performance counters from configuration                      *
118  *                                                                            *
119  * Parameters: def_lines - array of PerfCounter configuration entries         *
120  *             eng_lines - array of PerfCounterEn configuration entries       *
121  *                                                                            *
122  * Return value:                                                              *
123  *                                                                            *
124  * Author: Vladimir Levijev                                                   *
125  *                                                                            *
126  * Comments:                                                                  *
127  *                                                                            *
128  ******************************************************************************/
load_perf_counters(const char ** def_lines,const char ** eng_lines)129 void	load_perf_counters(const char **def_lines, const char **eng_lines)
130 {
131 	char		name[MAX_STRING_LEN], counterpath[PDH_MAX_COUNTER_PATH], interval[8];
132 	const char	**pline, **lines;
133 	char		*error = NULL;
134 	LPTSTR		wcounterPath;
135 	int		period;
136 
137 	for (lines = def_lines;; lines = eng_lines)
138 	{
139 		zbx_perf_counter_lang_t lang = (lines == def_lines) ? PERF_COUNTER_LANG_DEFAULT : PERF_COUNTER_LANG_EN;
140 
141 		for (pline = lines; NULL != *pline; pline++)
142 		{
143 			if (3 < num_param(*pline))
144 			{
145 				error = zbx_strdup(error, "Required parameter missing.");
146 				goto pc_fail;
147 			}
148 
149 			if (0 != get_param(*pline, 1, name, sizeof(name), NULL))
150 			{
151 				error = zbx_strdup(error, "Cannot parse key.");
152 				goto pc_fail;
153 			}
154 
155 			if (0 != get_param(*pline, 2, counterpath, sizeof(counterpath), NULL))
156 			{
157 				error = zbx_strdup(error, "Cannot parse counter path.");
158 				goto pc_fail;
159 			}
160 
161 			if (0 != get_param(*pline, 3, interval, sizeof(interval), NULL))
162 			{
163 				error = zbx_strdup(error, "Cannot parse interval.");
164 				goto pc_fail;
165 			}
166 
167 			wcounterPath = zbx_acp_to_unicode(counterpath);
168 			zbx_unicode_to_utf8_static(wcounterPath, counterpath, PDH_MAX_COUNTER_PATH);
169 			zbx_free(wcounterPath);
170 
171 			if (FAIL == check_counter_path(counterpath, lang == PERF_COUNTER_LANG_DEFAULT))
172 			{
173 				error = zbx_strdup(error, "Invalid counter path.");
174 				goto pc_fail;
175 			}
176 
177 			period = atoi(interval);
178 
179 			if (1 > period || MAX_COLLECTOR_PERIOD < period)
180 			{
181 				error = zbx_strdup(NULL, "Interval out of range.");
182 				goto pc_fail;
183 			}
184 
185 			if (NULL == add_perf_counter(name, counterpath, period, lang, &error))
186 			{
187 				if (NULL == error)
188 					error = zbx_strdup(error, "Failed to add new performance counter.");
189 				goto pc_fail;
190 			}
191 
192 			continue;
193 	pc_fail:
194 			zabbix_log(LOG_LEVEL_CRIT, "cannot add performance counter \"%s\": %s", *pline, error);
195 			zbx_free(error);
196 
197 			exit(EXIT_FAILURE);
198 		}
199 
200 		if (lines == eng_lines)
201 			break;
202 	}
203 }
204 #endif	/* _WINDOWS */
205 
206 #ifdef _AIX
tl_version(void)207 void	tl_version(void)
208 {
209 #ifdef _AIXVERSION_610
210 #	define ZBX_AIX_TL	"6100 and above"
211 #elif _AIXVERSION_530
212 #	ifdef HAVE_AIXOSLEVEL_530006
213 #		define ZBX_AIX_TL	"5300-06 and above"
214 #	else
215 #		define ZBX_AIX_TL	"5300-00,01,02,03,04,05"
216 #	endif
217 #elif _AIXVERSION_520
218 #	define ZBX_AIX_TL	"5200"
219 #elif _AIXVERSION_510
220 #	define ZBX_AIX_TL	"5100"
221 #endif
222 #ifdef ZBX_AIX_TL
223 	printf("Supported technology levels: %s\n", ZBX_AIX_TL);
224 #endif /* ZBX_AIX_TL */
225 #undef ZBX_AIX_TL
226 }
227 #endif /* _AIX */
228