You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.5 KiB

  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
  5. Caching boottime on startup yields incorrect uptime values if system
  6. date changes after the daemon is started.
  7. This is almost certain to happen on embedded systems without RTC, where
  8. clock is set from NTP server at some point after boot process.
  9. On Linux, we can retrieve uptime directly by either reading /proc/uptime
  10. (it's sufficient to read a few bytes) or calling sysinfo() function.
  11. Use the latter since it's the most efficient way in speed, memory
  12. requirements and code simplicity terms.
  13. ---
  14. src/uptime.c | 71 ++++++++++++++++--------------------------------------------
  15. 1 file changed, 19 insertions(+), 52 deletions(-)
  16. --- a/src/uptime.c
  17. +++ b/src/uptime.c
  18. @@ -25,8 +25,7 @@
  19. #include "plugin.h"
  20. #if KERNEL_LINUX
  21. -#define STAT_FILE "/proc/stat"
  22. -/* Using /proc filesystem to retrieve the boot time, Linux only. */
  23. +#include <sys/sysinfo.h>
  24. /* #endif KERNEL_LINUX */
  25. #elif HAVE_LIBKSTAT
  26. @@ -53,8 +52,6 @@
  27. /*
  28. * Global variables
  29. */
  30. -/* boottime always used, no OS distinction */
  31. -static time_t boottime;
  32. #if HAVE_LIBKSTAT
  33. extern kstat_ctl_t *kc;
  34. @@ -72,8 +69,6 @@ static void uptime_submit(gauge_t value)
  35. plugin_dispatch_values(&vl);
  36. }
  37. -static int uptime_init(void) /* {{{ */
  38. -{
  39. /*
  40. * On most unix systems the uptime is calculated by looking at the boot
  41. * time (stored in unix time, since epoch) and the current one. We are
  42. @@ -84,48 +79,21 @@ static int uptime_init(void) /* {{{ */
  43. * the boot time, the plugin is unregistered and there is no chance to
  44. * try again later. Nevertheless, this is very unlikely to happen.
  45. */
  46. -
  47. +static time_t uptime_get_sys(void) { /* {{{ */
  48. + time_t result;
  49. #if KERNEL_LINUX
  50. - unsigned long starttime;
  51. - char buffer[1024];
  52. - int ret;
  53. - FILE *fh;
  54. -
  55. - ret = 0;
  56. -
  57. - fh = fopen(STAT_FILE, "r");
  58. + struct sysinfo info;
  59. + int status;
  60. - if (fh == NULL) {
  61. + status = sysinfo(&info);
  62. + if (status != 0) {
  63. char errbuf[1024];
  64. - ERROR("uptime plugin: Cannot open " STAT_FILE ": %s",
  65. + ERROR("uptime plugin: Error calling sysinfo: %s",
  66. sstrerror(errno, errbuf, sizeof(errbuf)));
  67. return (-1);
  68. }
  69. - while (fgets(buffer, 1024, fh) != NULL) {
  70. - /* look for the btime string and read the value */
  71. - ret = sscanf(buffer, "btime %lu", &starttime);
  72. - /* avoid further loops if btime has been found and read
  73. - * correctly (hopefully) */
  74. - if (ret == 1)
  75. - break;
  76. - }
  77. -
  78. - fclose(fh);
  79. -
  80. - /* loop done, check if no value has been found/read */
  81. - if (ret != 1) {
  82. - ERROR("uptime plugin: No value read from " STAT_FILE "");
  83. - return (-1);
  84. - }
  85. -
  86. - boottime = (time_t)starttime;
  87. -
  88. - if (boottime == 0) {
  89. - ERROR("uptime plugin: btime read from " STAT_FILE ", "
  90. - "but `boottime' is zero!");
  91. - return (-1);
  92. - }
  93. + result = (time_t)info.uptime;
  94. /* #endif KERNEL_LINUX */
  95. #elif HAVE_LIBKSTAT
  96. @@ -159,13 +127,13 @@ static int uptime_init(void) /* {{{ */
  97. return (-1);
  98. }
  99. - boottime = (time_t)knp->value.ui32;
  100. -
  101. - if (boottime == 0) {
  102. + if (knp->value.ui32 == 0) {
  103. ERROR("uptime plugin: kstat_data_lookup returned success, "
  104. "but `boottime' is zero!");
  105. return (-1);
  106. }
  107. +
  108. + result = time(NULL) - (time_t)knp->value.ui32;
  109. /* #endif HAVE_LIBKSTAT */
  110. #elif HAVE_SYS_SYSCTL_H
  111. @@ -186,13 +154,13 @@ static int uptime_init(void) /* {{{ */
  112. return (-1);
  113. }
  114. - boottime = boottv.tv_sec;
  115. -
  116. - if (boottime == 0) {
  117. + if (boottv.tv_sec == 0) {
  118. ERROR("uptime plugin: sysctl(3) returned success, "
  119. "but `boottime' is zero!");
  120. return (-1);
  121. }
  122. +
  123. + result = time(NULL) - boottv.tv_sec;
  124. /* #endif HAVE_SYS_SYSCTL_H */
  125. #elif HAVE_PERFSTAT
  126. @@ -212,18 +180,18 @@ static int uptime_init(void) /* {{{ */
  127. if (hertz <= 0)
  128. hertz = HZ;
  129. - boottime = time(NULL) - cputotal.lbolt / hertz;
  130. + result = cputotal.lbolt / hertz;
  131. #endif /* HAVE_PERFSTAT */
  132. - return (0);
  133. -} /* }}} int uptime_init */
  134. + return result;
  135. +} /* }}} int uptime_get_sys */
  136. static int uptime_read(void) {
  137. gauge_t uptime;
  138. time_t elapsed;
  139. /* calculate the amount of time elapsed since boot, AKA uptime */
  140. - elapsed = time(NULL) - boottime;
  141. + elapsed = uptime_get_sys();
  142. uptime = (gauge_t)elapsed;
  143. @@ -233,6 +201,5 @@ static int uptime_read(void) {
  144. }
  145. void module_register(void) {
  146. - plugin_register_init("uptime", uptime_init);
  147. plugin_register_read("uptime", uptime_read);
  148. } /* void module_register */