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 #if defined(HAVE_SYSINFO_UPTIME)
27 	struct sysinfo info;
28 
29 	if (0 == sysinfo(&info))
30 	{
31 		SET_UI64_RESULT(result, info.uptime);
32 		return SYSINFO_RET_OK;
33 	}
34 	else
35 	{
36 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
37 		return SYSINFO_RET_FAIL;
38 	}
39 #elif defined(HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME)
40 	struct timeval	uptime;
41 	int		mib[2], len, now;
42 
43 	mib[0] = CTL_KERN;
44 	mib[1] = KERN_BOOTTIME;
45 
46 	len = sizeof(uptime);
47 
48 	if (0 != sysctl(mib, 2, &uptime, (size_t *)&len, NULL, 0))
49 	{
50 		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)));
51 		return SYSINFO_RET_FAIL;
52 	}
53 
54 	now = time(NULL);
55 
56 	SET_UI64_RESULT(result, now-uptime.tv_sec);
57 
58 	return SYSINFO_RET_OK;
59 #else
60 	SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for uptime information."));
61 	return SYSINFO_RET_FAIL;
62 #endif
63 }
64