From fa3a118de87d7600729dd07d2f24ca3138d9dd4d Mon Sep 17 00:00:00 2001 From: Hannu Nyman Date: Fri, 15 Sep 2017 19:10:37 +0300 Subject: [PATCH] collectd: uptime plugin: apply fix from upstream Backport from master the fix for uptime plugin. Adjust it for 5.5.3 Uptime plugin fails to adjust for system time changes after boot. As Openwrt/LEDE routers usually do not have a RTC, the system time gets adjusted with NTP possibly after collectd has already started. But collectd continues to use the initial time set by 'sysfixtime', which can lead to incorrect uptime calculations. Apply a proposed fix from upstream that uses /proc/uptime Signed-off-by: Hannu Nyman --- utils/collectd/Makefile | 2 +- .../patches/500-fix-uptime-reading.patch | 173 ++++++++++++++++++ 2 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 utils/collectd/patches/500-fix-uptime-reading.patch diff --git a/utils/collectd/Makefile b/utils/collectd/Makefile index 27491c6fe9..77ffd23d77 100644 --- a/utils/collectd/Makefile +++ b/utils/collectd/Makefile @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=collectd PKG_VERSION:=5.5.3 -PKG_RELEASE:=3 +PKG_RELEASE:=4 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.bz2 PKG_SOURCE_URL:=http://collectd.org/files/ diff --git a/utils/collectd/patches/500-fix-uptime-reading.patch b/utils/collectd/patches/500-fix-uptime-reading.patch new file mode 100644 index 0000000000..2e77c955dd --- /dev/null +++ b/utils/collectd/patches/500-fix-uptime-reading.patch @@ -0,0 +1,173 @@ +From af01dd6fa3eb458e2fbb272703b0cae37ea54a9b Mon Sep 17 00:00:00 2001 +From: Marcin Jurkowski +Date: Tue, 11 Jul 2017 15:00:25 +0200 +Subject: [PATCH] uptime plugin: don't cache boot time and simplify Linux code + +Caching boottime on startup yields incorrect uptime values if system +date changes after the daemon is started. +This is almost certain to happen on embedded systems without RTC, where +clock is set from NTP server at some point after boot process. + +On Linux, we can retrieve uptime directly by either reading /proc/uptime +(it's sufficient to read a few bytes) or calling sysinfo() function. +Use the latter since it's the most efficient way in speed, memory +requirements and code simplicity terms. + +--- a/src/uptime.c ++++ b/src/uptime.c +@@ -24,8 +24,7 @@ + #include "plugin.h" + + #if KERNEL_LINUX +-# define STAT_FILE "/proc/stat" +-/* Using /proc filesystem to retrieve the boot time, Linux only. */ ++#include + /* #endif KERNEL_LINUX */ + + #elif HAVE_LIBKSTAT +@@ -50,8 +49,6 @@ + /* + * Global variables + */ +-/* boottime always used, no OS distinction */ +-static time_t boottime; + + #if HAVE_LIBKSTAT + extern kstat_ctl_t *kc; +@@ -74,8 +71,6 @@ static void uptime_submit (gauge_t uptim + plugin_dispatch_values (&vl); + } + +-static int uptime_init (void) /* {{{ */ +-{ + /* + * On most unix systems the uptime is calculated by looking at the boot + * time (stored in unix time, since epoch) and the current one. We are +@@ -86,52 +81,21 @@ static int uptime_init (void) /* {{{ */ + * the boot time, the plugin is unregistered and there is no chance to + * try again later. Nevertheless, this is very unlikely to happen. + */ +- ++static time_t uptime_get_sys(void) { /* {{{ */ ++ time_t result; + #if KERNEL_LINUX +- unsigned long starttime; +- char buffer[1024]; +- int ret; +- FILE *fh; +- +- ret = 0; +- +- fh = fopen (STAT_FILE, "r"); ++ struct sysinfo info; ++ int status; + +- if (fh == NULL) +- { ++ status = sysinfo(&info); ++ if (status != 0) { + char errbuf[1024]; +- ERROR ("uptime plugin: Cannot open "STAT_FILE": %s", ++ ERROR ("uptime plugin: Error calling sysinfo: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } + +- while (fgets (buffer, 1024, fh) != NULL) +- { +- /* look for the btime string and read the value */ +- ret = sscanf (buffer, "btime %lu", &starttime); +- /* avoid further loops if btime has been found and read +- * correctly (hopefully) */ +- if (ret == 1) +- break; +- } +- +- fclose (fh); +- +- /* loop done, check if no value has been found/read */ +- if (ret != 1) +- { +- ERROR ("uptime plugin: No value read from "STAT_FILE""); +- return (-1); +- } +- +- boottime = (time_t) starttime; +- +- if (boottime == 0) +- { +- ERROR ("uptime plugin: btime read from "STAT_FILE", " +- "but `boottime' is zero!"); +- return (-1); +- } ++ result = (time_t)info.uptime; + /* #endif KERNEL_LINUX */ + + #elif HAVE_LIBKSTAT +@@ -168,14 +132,13 @@ static int uptime_init (void) /* {{{ */ + return (-1); + } + +- boottime = (time_t) knp->value.ui32; +- +- if (boottime == 0) +- { ++ if (knp->value.ui32 == 0) { + ERROR ("uptime plugin: kstat_data_lookup returned success, " + "but `boottime' is zero!"); + return (-1); + } ++ ++ result = time(NULL) - (time_t)knp->value.ui32; + /* #endif HAVE_LIBKSTAT */ + + # elif HAVE_SYS_SYSCTL_H +@@ -201,14 +164,13 @@ static int uptime_init (void) /* {{{ */ + return (-1); + } + +- boottime = boottv.tv_sec; +- +- if (boottime == 0) +- { ++ if (boottv.tv_sec == 0) { + ERROR ("uptime plugin: sysctl(3) returned success, " + "but `boottime' is zero!"); + return (-1); + } ++ ++ result = time(NULL) - boottv.tv_sec; + /* #endif HAVE_SYS_SYSCTL_H */ + + #elif HAVE_PERFSTAT +@@ -230,11 +192,11 @@ static int uptime_init (void) /* {{{ */ + if (hertz <= 0) + hertz = HZ; + +- boottime = time(NULL) - cputotal.lbolt / hertz; ++ result = cputotal.lbolt / hertz; + #endif /* HAVE_PERFSTAT */ + +- return (0); +-} /* }}} int uptime_init */ ++ return result; ++} /* }}} int uptime_get_sys */ + + static int uptime_read (void) + { +@@ -242,7 +204,7 @@ static int uptime_read (void) + time_t elapsed; + + /* calculate the amount of time elapsed since boot, AKA uptime */ +- elapsed = time (NULL) - boottime; ++ elapsed = uptime_get_sys(); + + uptime = (gauge_t) elapsed; + +@@ -253,6 +215,5 @@ static int uptime_read (void) + + void module_register (void) + { +- plugin_register_init ("uptime", uptime_init); + plugin_register_read ("uptime", uptime_read); + } /* void module_register */ -- 2.30.2