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 "system.h"
22 
23 #ifdef _WINDOWS
24 #	include "perfmon.h"
25 #	pragma comment(lib, "user32.lib")
26 #endif
27 
SYSTEM_LOCALTIME(AGENT_REQUEST * request,AGENT_RESULT * result)28 int	SYSTEM_LOCALTIME(AGENT_REQUEST *request, AGENT_RESULT *result)
29 {
30 	char		*type, buf[32];
31 	long		milliseconds;
32 	struct tm	tm;
33 	zbx_timezone_t	tz;
34 
35 	if (1 < request->nparam)
36 	{
37 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
38 		return SYSINFO_RET_FAIL;
39 	}
40 
41 	type = get_rparam(request, 0);
42 
43 	if (NULL == type || '\0' == *type || 0 == strcmp(type, "utc"))
44 	{
45 		SET_UI64_RESULT(result, time(NULL));
46 	}
47 	else if (0 == strcmp(type, "local"))
48 	{
49 		zbx_get_time(&tm, &milliseconds, &tz);
50 
51 		zbx_snprintf(buf, sizeof(buf), "%04d-%02d-%02d,%02d:%02d:%02d.%03ld,%1c%02d:%02d",
52 				1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
53 				tm.tm_hour, tm.tm_min, tm.tm_sec, milliseconds,
54 				tz.tz_sign, tz.tz_hour, tz.tz_min);
55 
56 		SET_STR_RESULT(result, strdup(buf));
57 	}
58 	else
59 	{
60 		SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter."));
61 		return SYSINFO_RET_FAIL;
62 	}
63 
64 	return SYSINFO_RET_OK;
65 }
66 
SYSTEM_USERS_NUM(AGENT_REQUEST * request,AGENT_RESULT * result)67 int	SYSTEM_USERS_NUM(AGENT_REQUEST *request, AGENT_RESULT *result)
68 {
69 #ifdef _WINDOWS
70 	char		counter_path[64];
71 	AGENT_REQUEST	request_tmp;
72 	int		ret;
73 
74 	ZBX_UNUSED(request);
75 
76 	zbx_snprintf(counter_path, sizeof(counter_path), "\\%u\\%u",
77 			(unsigned int)get_builtin_counter_index(PCI_TERMINAL_SERVICES),
78 			(unsigned int)get_builtin_counter_index(PCI_TOTAL_SESSIONS));
79 
80 	request_tmp.nparam = 1;
81 	request_tmp.params = zbx_malloc(NULL, request_tmp.nparam * sizeof(char *));
82 	request_tmp.params[0] = counter_path;
83 
84 	ret = PERF_COUNTER(&request_tmp, result);
85 
86 	zbx_free(request_tmp.params);
87 
88 	return ret;
89 #else
90 	ZBX_UNUSED(request);
91 
92 	return EXECUTE_INT("who | wc -l", result);
93 #endif
94 }
95