From: Florian Forster Date: Thu, 19 Mar 2009 13:21:16 +0000 (+0100) Subject: uptime plugin: Shuffle some preprocessor stuff around. X-Git-Tag: collectd-4.7.0~103 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=ec7ae67adc5ef53b67f2b0f91f12f4bc58a799b6;p=collectd.git uptime plugin: Shuffle some preprocessor stuff around. Fix some comments and possibly a Solaris bug, too. --- diff --git a/src/uptime.c b/src/uptime.c index f98be065..4edfa840 100644 --- a/src/uptime.c +++ b/src/uptime.c @@ -25,50 +25,43 @@ #if KERNEL_LINUX # define UPTIME_FILE "/proc/uptime" -/* - No need for includes, using /proc filesystem, Linux only. -*/ +/* No need for includes, using /proc filesystem, Linux only. */ /* #endif KERNEL_LINUX */ #elif HAVE_LIBKSTAT -/* something to include? maybe ? */ -/* - Using kstats chain to retrieve the boot time, this applies to: - - Solaris / OpenSolaris -*/ +/* Using kstats chain to retrieve the boot time, this applies to: + * - Solaris / OpenSolaris + */ /* #endif HAVE_LIBKSTAT */ #elif HAVE_SYS_SYSCTL_H # include -/* - Using sysctl interface to retrieve the boot time, this applies to: - - *BSD - - Darwin / OS X -*/ +/* Using sysctl interface to retrieve the boot time, this applies to: + * - *BSD + * - Darwin / OS X + */ /* #endif HAVE_SYS_SYSCTL_H */ #else # error "No applicable input method." #endif - -#if !KERNEL_LINUX -#define INIT_FAILED "uptime plugin: unable to calculate uptime, the plugin will be disabled." -#endif - - +/* + * Global variables + */ #if KERNEL_LINUX -/* global variables not needed*/ +/* global variables not needed */ +/* #endif KERNEL_LINUX */ -#elif HAVE_SYS_SYSCTL_H || HAVE_LIBKSTAT +#elif HAVE_LIBKSTAT static time_t boottime; -# if HAVE_LIBKSTAT extern kstat_ctl_t *kc; -# endif +/* #endif HAVE_LIBKSTAT */ +#elif HAVE_SYS_SYSCTL_H +static time_t boottime; #endif - static void uptime_submit (gauge_t uptime) { value_t values[1]; @@ -78,7 +71,6 @@ static void uptime_submit (gauge_t uptime) vl.values = values; vl.values_len = 1; - vl.time = time (NULL); sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "uptime", sizeof (vl.plugin)); @@ -87,7 +79,7 @@ static void uptime_submit (gauge_t uptime) plugin_dispatch_values (&vl); } -#if !KERNEL_LINUX +#if !defined(KERNEL_LINUX) || !KERNEL_LINUX static int uptime_init (void) { /* NOTE @@ -101,11 +93,9 @@ static int uptime_init (void) 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 HAVE_LIBKSTAT - kstat_t *ksp; kstat_named_t *knp; @@ -113,70 +103,77 @@ static int uptime_init (void) knp = NULL; /* kstats chain already opened by update_kstat (using *kc), let's verify everything went fine. */ - if ( kc == NULL ) + if (kc == NULL) { - ERROR ("uptime plugin: unable to open kstat control structure"); - ERROR (INIT_FAILED); + ERROR ("uptime plugin: kstat chain control structure not available."); return (-1); } - if (( ksp = kstat_lookup (kc, "unix", 0, "system_misc")) == NULL ) + ksp = kstat_lookup (kc, "unix", 0, "system_misc"); + if (ksp == NULL) { - ERROR ("uptime plugin: cannot find %s kstat", "unix:0:system_misc"); - ERROR (INIT_FAILED); + ERROR ("uptime plugin: Cannot find unix:0:system_misc kstat."); return (-1); } - if (( kstat_read (kc, ksp, NULL) < 0 ) || - (( knp = (kstat_named_t *) kstat_data_lookup (ksp, "boot_time")) != NULL )) + if (kstat_read (kc, ksp, NULL) < 0) { - ERROR ("uptime plugin: kstat data reading failed"); - ERROR (INIT_FAILED); + ERROR ("uptime plugin: kstat_read failed."); return (-1); } - boottime = (time_t) knp->value.ui32; + knp = (kstat_named_t *) kstat_data_lookup (ksp, "boot_time"); + if (knp == NULL) + { + ERROR ("uptime plugin: kstat_data_lookup (boot_time) failed."); + return (-1); + } + boottime = (time_t) knp->value.ui32; /* #endif HAVE_LIBKSTAT */ # elif HAVE_SYS_SYSCTL_H - struct timeval boottv; size_t boottv_len; + int status; int mib[2]; mib[0] = CTL_KERN; mib[1] = KERN_BOOTTIME; + memset (&boottv, 0, sizeof (boottv)); boottv_len = sizeof (boottv); - if (( sysctl (mib, 2, &boottv, &boottv_len, NULL, 0) != 0 ) || boottv.tv_sec == 0 ) + status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &boottv, &boottv_len, + /* new_value = */ NULL, /* new_length = */ 0); + if (status != 0) { char errbuf[1024]; - ERROR ("uptime plugin: no value read from sysctl interface: %s", + ERROR ("uptime plugin: No value read from sysctl interface: %s", sstrerror (errno, errbuf, sizeof (errbuf))); - ERROR (INIT_FAILED); return (-1); } boottime = boottv.tv_sec; - -/* #endif HAVE_SYS_SYSCTL_H */ - -# endif + if (boottime == 0) + { + ERROR ("uptime plugin: sysctl(3) returned success, " + "but `boottime' is zero!"); + return (-1); + } +#endif /* HAVE_SYS_SYSCTL_H */ return (0); } -#endif +#endif /* !KERNEL_LINUX */ static int uptime_read (void) { gauge_t uptime; #if KERNEL_LINUX - FILE *fh; fh = fopen (UPTIME_FILE, "r"); @@ -184,34 +181,28 @@ static int uptime_read (void) if (fh == NULL) { char errbuf[1024]; - ERROR ("uptime plugin: cannot open %s: %s", UPTIME_FILE, + ERROR ("uptime plugin: Cannot open "UPTIME_FILE": %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } if ( fscanf (fh, "%lf", &uptime) < 1 ) { - WARNING ("no value read from %s", UPTIME_FILE); + WARNING ("uptime plugin: No value read from "UPTIME_FILE); fclose (fh); return (-1); } fclose (fh); - /* #endif KERNEL_LINUX */ - -#elif HAVE_SYS_SYSCTL_H || HAVE_LIBKSTAT - +#elif HAVE_LIBKSTAT || HAVE_SYS_SYSCTL_H time_t elapsed; elapsed = time (NULL) - boottime; uptime = (gauge_t) elapsed; - -/* #endif HAVE_SYS_SYSCTL_H */ - -#endif +#endif /* HAVE_LIBKSTAT || HAVE_SYS_SYSCTL_H */ uptime_submit (uptime); @@ -220,7 +211,7 @@ static int uptime_read (void) void module_register (void) { -#if !KERNEL_LINUX +#if !defined(KERNEL_LINUX) || !KERNEL_LINUX plugin_register_init ("uptime", uptime_init); #endif plugin_register_read ("uptime", uptime_read);