Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sensors.c « linux « zbxsysinfo « libs « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d37e6973c4b4226c2c36b696d45e337ccb8746f7 (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

#include "zbxsysinfo.h"
#include "../sysinfo.h"

#include "zbxregexp.h"

#ifdef KERNEL_2_4
#define DEVICE_DIR	"/proc/sys/dev/sensors"
#else
#define DEVICE_DIR	"/sys/class/hwmon"
static const char	*locations[] = {"", "/device", NULL};
#endif

static void	count_sensor(int do_task, const char *filename, double *aggr, int *cnt)
{
	FILE	*f;
	char	line[MAX_STRING_LEN];
	double	value;

	if (NULL == (f = fopen(filename, "r")))
		return;

	if (NULL == fgets(line, sizeof(line), f))
	{
		zbx_fclose(f);
		return;
	}

	zbx_fclose(f);

#ifdef KERNEL_2_4
	if (1 == sscanf(line, "%*f\t%*f\t%lf\n", &value))
	{
#else
	if (1 == sscanf(line, "%lf", &value))
	{
		if (NULL == strstr(filename, "fan"))
			value = value / 1000;
#endif
		(*cnt)++;

		switch (do_task)
		{
			case ZBX_DO_ONE:
				*aggr = value;
				break;
			case ZBX_DO_AVG:
				*aggr += value;
				break;
			case ZBX_DO_MAX:
				*aggr = (1 == *cnt ? value : MAX(*aggr, value));
				break;
			case ZBX_DO_MIN:
				*aggr = (1 == *cnt ? value : MIN(*aggr, value));
				break;
		}
	}
}

#ifndef KERNEL_2_4
/*********************************************************************************
 *                                                                               *
 * Purpose: locate and read the name attribute of a sensor from sysfs            *
 *                                                                               *
 * Parameters:  device        - [IN] the path to sensor data in sysfs            *
 *              attribute     - [OUT] the sensor name                            *
 *                                                                               *
 * Return value: Subfolder where the sensor name file was found or NULL          *
 *                                                                               *
 * Comments: attribute string must be freed by caller after it's been used.      *
 *                                                                               *
 *********************************************************************************/
static const char	*sysfs_read_attr(const char *device, char **attribute)
{
#define ATTR_MAX	128
	const char	**location;
	char		path[MAX_STRING_LEN], buf[ATTR_MAX], *p;
	FILE		*f;

	for (location = locations; NULL != *location; location++)
	{
		zbx_snprintf(path, MAX_STRING_LEN, "%s%s/name", device, *location);

		if (NULL != (f = fopen(path, "r")))
		{
			p = fgets(buf, ATTR_MAX, f);
			zbx_fclose(f);

			if (NULL == p)
				break;

			/* Last byte is a '\n'; chop that off */
			buf[strlen(buf) - 1] = '\0';

			if (NULL != attribute)
				*attribute = zbx_strdup(*attribute, buf);

			return *location;
		}
	}

	return NULL;
#undef ATTR_MAX
}

static int	get_device_info(const char *dev_path, const char *dev_name, char *device_info,
		const char **name_subfolder)
{
	int		ret = FAIL;
	unsigned int	addr;
	ssize_t		sub_len;
	char		*subsys, *prefix = NULL, linkpath[MAX_STRING_LEN], subsys_path[MAX_STRING_LEN];

	/* ignore any device without name attribute */
	if (NULL == (*name_subfolder = sysfs_read_attr(dev_path, &prefix)))
		goto out;

	if (NULL == dev_name)
	{
		/* Virtual device */
		/* Assuming that virtual devices are unique */
		zbx_snprintf(device_info, MAX_STRING_LEN, "%s-virtual-0", prefix);
		ret = SUCCEED;

		goto out;
	}

	/* Find bus type */
	zbx_snprintf(linkpath, MAX_STRING_LEN, "%s/device/subsystem", dev_path);

	sub_len = readlink(linkpath, subsys_path, MAX_STRING_LEN - 1);

	if (0 > sub_len && ENOENT == errno)
	{
		/* Fallback to "bus" link for kernels <= 2.6.17 */
		zbx_snprintf(linkpath, MAX_STRING_LEN, "%s/device/bus", dev_path);
		sub_len = readlink(linkpath, subsys_path, MAX_STRING_LEN - 1);
	}

	if (0 > sub_len)
	{
		/* Older kernels (<= 2.6.11) have neither the subsystem symlink nor the bus symlink */
		if (errno == ENOENT)
			subsys = NULL;
		else
			goto out;
	}
	else
	{
		subsys_path[sub_len] = '\0';
		subsys = strrchr(subsys_path, '/') + 1;
	}

	if ((NULL == subsys || 0 == strcmp(subsys, "i2c")))
	{
		short int	bus_i2c;

		if (2 != sscanf(dev_name, "%hd-%x", &bus_i2c, &addr))
			goto out;

		/* find out if legacy ISA or not */
		if (9191 == bus_i2c)
		{
			zbx_snprintf(device_info, MAX_STRING_LEN, "%s-isa-%04x", prefix, addr);
		}
		else
		{
			const char	*bus_subfolder;
			char		*bus_attr = NULL, bus_path[MAX_STRING_LEN];

			zbx_snprintf(bus_path, sizeof(bus_path), "/sys/class/i2c-adapter/i2c-%d", bus_i2c);
			bus_subfolder = sysfs_read_attr(bus_path, &bus_attr);

			if (NULL != bus_subfolder && '\0' != *bus_subfolder)
			{
				if (0 != strncmp(bus_attr, "ISA ", 4))
				{
					zbx_free(bus_attr);
					goto out;
				}

				zbx_snprintf(device_info, MAX_STRING_LEN, "%s-isa-%04x", prefix, addr);
			}
			else
				zbx_snprintf(device_info, MAX_STRING_LEN, "%s-i2c-%hd-%02x", prefix, bus_i2c, addr);

			zbx_free(bus_attr);
		}

		ret = SUCCEED;
	}
	else if (0 == strcmp(subsys, "spi"))
	{
		int		address;
		short int	bus_spi;

		/* SPI */
		if (2 != sscanf(dev_name, "spi%hd.%d", &bus_spi, &address))
			goto out;

		zbx_snprintf(device_info, MAX_STRING_LEN, "%s-spi-%hd-%x", prefix, bus_spi, (unsigned int)address);

		ret = SUCCEED;
	}
	else if (0 == strcmp(subsys, "pci"))
	{
		unsigned int	domain, bus, slot, fn;

		/* PCI */
		if (4 != sscanf(dev_name, "%x:%x:%x.%x", &domain, &bus, &slot, &fn))
			goto out;

		addr = (domain << 16) + (bus << 8) + (slot << 3) + fn;
		zbx_snprintf(device_info, MAX_STRING_LEN, "%s-pci-%04x", prefix, addr);

		ret = SUCCEED;
	}
	else if (0 == strcmp(subsys, "platform") || 0 == strcmp(subsys, "of_platform"))
	{
		int	address;

		/* must be new ISA (platform driver) */
		if (1 != sscanf(dev_name, "%*[a-z0-9_].%d", &address))
			address = 0;

		zbx_snprintf(device_info, MAX_STRING_LEN, "%s-isa-%04x", prefix, (unsigned int)address);

		ret = SUCCEED;
	}
	else if (0 == strcmp(subsys, "acpi"))
	{
		/* Assuming that acpi devices are unique */
		zbx_snprintf(device_info, MAX_STRING_LEN, "%s-acpi-0", prefix);

		ret = SUCCEED;
	}
	else if (0 == strcmp(subsys, "hid"))
	{
		unsigned int	bus, vendor, product;

		/* As of kernel 2.6.32, the hid device names do not look good */
		if (4 != sscanf(dev_name, "%x:%x:%x.%x", &bus, &vendor, &product, &addr))
			goto out;

		zbx_snprintf(device_info, MAX_STRING_LEN, "%s-hid-%hd-%x", prefix, (short int)bus, addr);

		ret = SUCCEED;
	}
out:
	zbx_free(prefix);

	return ret;
}
#endif

static void	get_device_sensors(int do_task, const char *device, const char *name, double *aggr, int *cnt)
{
	char	sensorname[MAX_STRING_LEN];

#ifdef KERNEL_2_4
	if (ZBX_DO_ONE == do_task)
	{
		zbx_snprintf(sensorname, sizeof(sensorname), "%s/%s/%s", DEVICE_DIR, device, name);
		count_sensor(do_task, sensorname, aggr, cnt);
	}
	else
	{
		DIR		*devicedir = NULL, *sensordir = NULL;
		struct dirent	*deviceent, *sensorent;
		char		devicename[MAX_STRING_LEN];

		if (NULL == (devicedir = opendir(DEVICE_DIR)))
			return;

		while (NULL != (deviceent = readdir(devicedir)))
		{
			if (0 == strcmp(deviceent->d_name, ".") || 0 == strcmp(deviceent->d_name, ".."))
				continue;

			if (NULL == zbx_regexp_match(deviceent->d_name, device, NULL))
				continue;

			zbx_snprintf(devicename, sizeof(devicename), "%s/%s", DEVICE_DIR, deviceent->d_name);

			if (NULL == (sensordir = opendir(devicename)))
				continue;

			while (NULL != (sensorent = readdir(sensordir)))
			{
				if (0 == strcmp(sensorent->d_name, ".") || 0 == strcmp(sensorent->d_name, ".."))
					continue;

				if (NULL == zbx_regexp_match(sensorent->d_name, name, NULL))
					continue;

				zbx_snprintf(sensorname, sizeof(sensorname), "%s/%s", devicename, sensorent->d_name);
				count_sensor(do_task, sensorname, aggr, cnt);
			}
			closedir(sensordir);
		}
		closedir(devicedir);
	}
#else
	DIR		*sensordir = NULL, *devicedir = NULL;
	struct dirent	*sensorent, *deviceent;
	char		hwmon_dir[MAX_STRING_LEN], devicepath[MAX_STRING_LEN], deviced[MAX_STRING_LEN],
			device_info[MAX_STRING_LEN], regex[MAX_STRING_LEN], *device_p;
	const char	*subfolder;
	int		err;

	zbx_snprintf(hwmon_dir, sizeof(hwmon_dir), "%s", DEVICE_DIR);

	if (NULL == (devicedir = opendir(hwmon_dir)))
		return;

	while (NULL != (deviceent = readdir(devicedir)))
	{
		ssize_t	dev_len;

		if (0 == strcmp(deviceent->d_name, ".") || 0 == strcmp(deviceent->d_name, ".."))
			continue;

		zbx_snprintf(devicepath, sizeof(devicepath), "%s/%s/device", DEVICE_DIR, deviceent->d_name);
		dev_len = readlink(devicepath, deviced, MAX_STRING_LEN - 1);
		zbx_snprintf(devicepath, sizeof(devicepath), "%s/%s", DEVICE_DIR, deviceent->d_name);

		if (0 > dev_len)
		{
			/* No device link? Treat device as virtual */
			err = get_device_info(devicepath, NULL, device_info, &subfolder);
		}
		else
		{
			deviced[dev_len] = '\0';
			device_p = strrchr(deviced, '/') + 1;

			if (0 == strcmp(device, device_p))
			{
				zbx_snprintf(device_info, sizeof(device_info), "%s", device);
				err = (NULL != (subfolder = sysfs_read_attr(devicepath, NULL)) ? SUCCEED : FAIL);
			}
			else
				err = get_device_info(devicepath, device_p, device_info, &subfolder);
		}

		if (SUCCEED == err && 0 == strcmp(device_info, device))
		{
			zbx_snprintf(devicepath, sizeof(devicepath), "%s/%s%s", DEVICE_DIR, deviceent->d_name,
					subfolder);

			if (ZBX_DO_ONE == do_task)
			{
				zbx_snprintf(sensorname, sizeof(sensorname), "%s/%s_input", devicepath, name);
				count_sensor(do_task, sensorname, aggr, cnt);
			}
			else
			{
				zbx_snprintf(regex, sizeof(regex), "%s[0-9]*_input", name);

				if (NULL == (sensordir = opendir(devicepath)))
					goto out;

				while (NULL != (sensorent = readdir(sensordir)))
				{
					if (0 == strcmp(sensorent->d_name, ".") ||
							0 == strcmp(sensorent->d_name, ".."))
						continue;

					if (NULL == zbx_regexp_match(sensorent->d_name, regex, NULL))
						continue;

					zbx_snprintf(sensorname, sizeof(sensorname), "%s/%s", devicepath,
							sensorent->d_name);
					count_sensor(do_task, sensorname, aggr, cnt);
				}
				closedir(sensordir);
			}
		}
	}
out:
	closedir(devicedir);
#endif
}

int	GET_SENSOR(AGENT_REQUEST *request, AGENT_RESULT *result)
{
	char	*device, *name, *function;
	int	do_task, cnt = 0;
	double	aggr = 0;

	if (3 < request->nparam)
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
		return SYSINFO_RET_FAIL;
	}

	device = get_rparam(request, 0);
	name = get_rparam(request, 1);
	function = get_rparam(request, 2);

	if (NULL == device || '\0' == *device)
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
		return SYSINFO_RET_FAIL;
	}

	if (NULL == name || '\0' == *name)
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter."));
		return SYSINFO_RET_FAIL;
	}

	if (NULL == function || '\0' == *function)
		do_task = ZBX_DO_ONE;
	else if (0 == strcmp(function, "avg"))
		do_task = ZBX_DO_AVG;
	else if (0 == strcmp(function, "max"))
		do_task = ZBX_DO_MAX;
	else if (0 == strcmp(function, "min"))
		do_task = ZBX_DO_MIN;
	else
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
		return SYSINFO_RET_FAIL;
	}

	if (ZBX_DO_ONE != do_task && 0 != isdigit(name[strlen(name) - 1]))
		do_task = ZBX_DO_ONE;

	if (ZBX_DO_ONE != do_task && 0 == isalpha(name[strlen(name) - 1]))
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Generic sensor name must be specified for selected mode."));
		return SYSINFO_RET_FAIL;
	}

	get_device_sensors(do_task, device, name, &aggr, &cnt);

	if (0 == cnt)
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain sensor information."));
		return SYSINFO_RET_FAIL;
	}

	if (ZBX_DO_AVG == do_task)
		SET_DBL_RESULT(result, aggr / cnt);
	else
		SET_DBL_RESULT(result, aggr);

	return SYSINFO_RET_OK;
}