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 "sysinfo.h"
22 #include "log.h"
23 
SYSTEM_UPTIME(AGENT_REQUEST * request,AGENT_RESULT * result)24 int	SYSTEM_UPTIME(AGENT_REQUEST *request, AGENT_RESULT *result)
25 {
26 #ifdef HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME
27 	int		mib[2], now;
28 	size_t		len;
29 	struct timeval	uptime;
30 
31 	mib[0] = CTL_KERN;
32 	mib[1] = KERN_BOOTTIME;
33 
34 	len = sizeof(struct timeval);
35 
36 	if (0 != sysctl(mib, 2, &uptime, &len, NULL, 0))
37 	{
38 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
39 		return SYSINFO_RET_FAIL;
40 	}
41 
42 	now = time(NULL);
43 
44 	SET_UI64_RESULT(result, now - uptime.tv_sec);
45 
46 	return SYSINFO_RET_OK;
47 #else
48 	SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for uptime information."));
49 	return SYSINFO_RET_FAIL;
50 #endif
51 }
52