1 From af01dd6fa3eb458e2fbb272703b0cae37ea54a9b Mon Sep 17 00:00:00 2001
2 From: Marcin Jurkowski <marcin1j@gmail.com>
3 Date: Tue, 11 Jul 2017 15:00:25 +0200
4 Subject: [PATCH] uptime plugin: don't cache boot time and simplify Linux code
6 Caching boottime on startup yields incorrect uptime values if system
7 date changes after the daemon is started.
8 This is almost certain to happen on embedded systems without RTC, where
9 clock is set from NTP server at some point after boot process.
11 On Linux, we can retrieve uptime directly by either reading /proc/uptime
12 (it's sufficient to read a few bytes) or calling sysinfo() function.
13 Use the latter since it's the most efficient way in speed, memory
14 requirements and code simplicity terms.
16 src/uptime.c | 71 ++++++++++++++++--------------------------------------------
17 1 file changed, 19 insertions(+), 52 deletions(-)
25 -#define STAT_FILE "/proc/stat"
26 -/* Using /proc filesystem to retrieve the boot time, Linux only. */
27 +#include <sys/sysinfo.h>
28 /* #endif KERNEL_LINUX */
35 -/* boottime always used, no OS distinction */
36 -static time_t boottime;
39 extern kstat_ctl_t *kc;
40 @@ -72,8 +69,6 @@ static void uptime_submit(gauge_t value)
41 plugin_dispatch_values(&vl);
44 -static int uptime_init(void) /* {{{ */
47 * On most unix systems the uptime is calculated by looking at the boot
48 * time (stored in unix time, since epoch) and the current one. We are
49 @@ -84,48 +79,21 @@ static int uptime_init(void) /* {{{ */
50 * the boot time, the plugin is unregistered and there is no chance to
51 * try again later. Nevertheless, this is very unlikely to happen.
54 +static time_t uptime_get_sys(void) { /* {{{ */
57 - unsigned long starttime;
64 - fh = fopen(STAT_FILE, "r");
65 + struct sysinfo info;
69 + status = sysinfo(&info);
72 - ERROR("uptime plugin: Cannot open " STAT_FILE ": %s",
73 + ERROR("uptime plugin: Error calling sysinfo: %s",
74 sstrerror(errno, errbuf, sizeof(errbuf)));
78 - while (fgets(buffer, 1024, fh) != NULL) {
79 - /* look for the btime string and read the value */
80 - ret = sscanf(buffer, "btime %lu", &starttime);
81 - /* avoid further loops if btime has been found and read
82 - * correctly (hopefully) */
89 - /* loop done, check if no value has been found/read */
91 - ERROR("uptime plugin: No value read from " STAT_FILE "");
95 - boottime = (time_t)starttime;
97 - if (boottime == 0) {
98 - ERROR("uptime plugin: btime read from " STAT_FILE ", "
99 - "but `boottime' is zero!");
102 + result = (time_t)info.uptime;
103 /* #endif KERNEL_LINUX */
106 @@ -159,13 +127,13 @@ static int uptime_init(void) /* {{{ */
110 - boottime = (time_t)knp->value.ui32;
112 - if (boottime == 0) {
113 + if (knp->value.ui32 == 0) {
114 ERROR("uptime plugin: kstat_data_lookup returned success, "
115 "but `boottime' is zero!");
119 + result = time(NULL) - (time_t)knp->value.ui32;
120 /* #endif HAVE_LIBKSTAT */
122 #elif HAVE_SYS_SYSCTL_H
123 @@ -186,13 +154,13 @@ static int uptime_init(void) /* {{{ */
127 - boottime = boottv.tv_sec;
129 - if (boottime == 0) {
130 + if (boottv.tv_sec == 0) {
131 ERROR("uptime plugin: sysctl(3) returned success, "
132 "but `boottime' is zero!");
136 + result = time(NULL) - boottv.tv_sec;
137 /* #endif HAVE_SYS_SYSCTL_H */
140 @@ -212,18 +180,18 @@ static int uptime_init(void) /* {{{ */
144 - boottime = time(NULL) - cputotal.lbolt / hertz;
145 + result = cputotal.lbolt / hertz;
146 #endif /* HAVE_PERFSTAT */
149 -} /* }}} int uptime_init */
151 +} /* }}} int uptime_get_sys */
153 static int uptime_read(void) {
157 /* calculate the amount of time elapsed since boot, AKA uptime */
158 - elapsed = time(NULL) - boottime;
159 + elapsed = uptime_get_sys();
161 uptime = (gauge_t)elapsed;
163 @@ -233,6 +201,5 @@ static int uptime_read(void) {
166 void module_register(void) {
167 - plugin_register_init("uptime", uptime_init);
168 plugin_register_read("uptime", uptime_read);
169 } /* void module_register */