Code

collectd-threshold(5): Correct the description of the "Interesting" config option.
[collectd.git] / src / uptime.c
index 9e0562de5a424d549e9585d83a61c16b538ea637..064c3ceed4752a359c7ec2e5b9d84afa27068543 100644 (file)
 /* Using sysctl interface to retrieve the boot time on *BSD / Darwin / OS X systems */
 /* #endif HAVE_SYS_SYSCTL_H */
 
+#elif HAVE_PERFSTAT
+# include <sys/protosw.h>
+# include <libperfstat.h>
+/* Using perfstat_cpu_total to retrive the boot time in AIX */
+/* #endif HAVE_PERFSTAT */
+
 #else
 # error "No applicable input method."
 #endif
 
-/* 
+/*
  * Global variables
  */
 /* boottime always used, no OS distinction */
@@ -51,7 +57,6 @@ static time_t boottime;
 extern kstat_ctl_t *kc;
 #endif /* #endif HAVE_LIBKSTAT */
 
-
 static void uptime_submit (gauge_t uptime)
 {
        value_t values[1];
@@ -69,19 +74,18 @@ static void uptime_submit (gauge_t uptime)
        plugin_dispatch_values (&vl);
 }
 
-static int uptime_init (void)
+static int uptime_init (void) /* {{{ */
 {
-/*     NOTE
-
-       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 going to
-       do the same, reading the boot time value while executing the uptime_init
-       function (there is no need to read, every time the plugin_read is called,
-       a value that won't change). However, since uptime_init is run only once,
-       if the function fails in retrieving the boot time, the plugin is
-       unregistered and there is no chance to try again later. Nevertheless,
-       this is very unlikely to happen.
- */
+       /*
+        * 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
+        * going to do the same, reading the boot time value while executing
+        * the uptime_init function (there is no need to read, every time the
+        * plugin_read is called, a value that won't change). However, since
+        * uptime_init is run only once, if the function fails in retrieving
+        * the boot time, the plugin is unregistered and there is no chance to
+        * try again later. Nevertheless, this is very unlikely to happen.
+        */
 
 #if KERNEL_LINUX
        unsigned long starttime;
@@ -104,16 +108,17 @@ static int uptime_init (void)
        while (fgets (buffer, 1024, fh) != NULL)
        {
                /* look for the btime string and read the value */
-               if (( ret = sscanf(buffer, "btime %lu", &starttime) ) == 1 )
-                       /* avoid further loops if btime has been found and read correctly (hopefully) */
+               ret = sscanf (buffer, "btime %lu", &starttime);
+               /* avoid further loops if btime has been found and read
+                * correctly (hopefully) */
+               if (ret == 1)
                        break;
-               /* else continue */
        }
 
        fclose (fh);
 
        /* loop done, check if no value has been found/read */
-       if ( ret != 1 )
+       if (ret != 1)
        {
                ERROR ("uptime plugin: No value read from "STAT_FILE"");
                return (-1);
@@ -204,12 +209,32 @@ static int uptime_init (void)
                                "but `boottime' is zero!");
                return (-1);
        }
-#endif /* HAVE_SYS_SYSCTL_H */
+/* #endif HAVE_SYS_SYSCTL_H */
 
-       return (0);
+#elif HAVE_PERFSTAT
+       int status;
+       perfstat_cpu_total_t cputotal;
+       int hertz;
 
-}
+       status = perfstat_cpu_total(NULL, &cputotal,
+               sizeof(perfstat_cpu_total_t), 1);
+       if (status < 0)
+       {
+               char errbuf[1024];
+               ERROR ("uptime plugin: perfstat_cpu_total: %s",
+                       sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
 
+       hertz = sysconf(_SC_CLK_TCK);
+       if (hertz <= 0)
+               hertz = HZ;
+
+       boottime = time(NULL) - cputotal.lbolt / hertz;
+#endif /* HAVE_PERFSTAT */
+
+       return (0);
+} /* }}} int uptime_init */
 
 static int uptime_read (void)
 {