X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fuptime.c;h=f0e1a6f42df4c48106a128ab2227037aa56f7171;hb=604607d3463fbe13eac252308f8a2497fb190167;hp=d2ba9633db290aeb17ac0a064874b3c652742b3a;hpb=8f40e8dacad2bfcb9d659e12740aa3ebc15ada65;p=collectd.git diff --git a/src/uptime.c b/src/uptime.c index d2ba9633..f0e1a6f4 100644 --- a/src/uptime.c +++ b/src/uptime.c @@ -20,6 +20,7 @@ **/ #include "collectd.h" + #include "common.h" #include "plugin.h" @@ -37,6 +38,12 @@ /* Using sysctl interface to retrieve the boot time on *BSD / Darwin / OS X systems */ /* #endif HAVE_SYS_SYSCTL_H */ +#elif HAVE_PERFSTAT +# include +# include +/* Using perfstat_cpu_total to retrive the boot time in AIX */ +/* #endif HAVE_PERFSTAT */ + #else # error "No applicable input method." #endif @@ -51,17 +58,13 @@ static time_t boottime; extern kstat_ctl_t *kc; #endif /* #endif HAVE_LIBKSTAT */ -static void uptime_submit (gauge_t uptime) +static void uptime_submit (gauge_t value) { - value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - values[0].gauge = uptime; - - vl.values = values; + vl.values = &(value_t) { .gauge = value }; vl.values_len = 1; - sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "uptime", sizeof (vl.plugin)); sstrncpy (vl.type, "uptime", sizeof (vl.type)); @@ -173,17 +176,13 @@ static int uptime_init (void) /* {{{ */ /* #endif HAVE_LIBKSTAT */ # elif HAVE_SYS_SYSCTL_H - struct timeval boottv; + struct timeval boottv = { 0 }; size_t boottv_len; int status; - int mib[2]; - - mib[0] = CTL_KERN; - mib[1] = KERN_BOOTTIME; + int mib[] = { CTL_KERN, KERN_BOOTTIME }; boottv_len = sizeof (boottv); - memset (&boottv, 0, boottv_len); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &boottv, &boottv_len, /* new_value = */ NULL, /* new_length = */ 0); @@ -203,7 +202,29 @@ static int uptime_init (void) /* {{{ */ "but `boottime' is zero!"); return (-1); } -#endif /* HAVE_SYS_SYSCTL_H */ +/* #endif HAVE_SYS_SYSCTL_H */ + +#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 */ @@ -213,7 +234,7 @@ static int uptime_read (void) gauge_t uptime; time_t elapsed; - /* calculate the ammount of time elapsed since boot, AKA uptime */ + /* calculate the amount of time elapsed since boot, AKA uptime */ elapsed = time (NULL) - boottime; uptime = (gauge_t) elapsed;