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