X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fswap.c;h=b8b5f09830629e57dd34c80b739a38e0e7e6cee3;hb=d54dfab9b049de396e6a38d690d5f18917e093ea;hp=945c7580aef84dfdc944a0dd8f2e268773159d2a;hpb=838af4cdc6c8674ed3e14a95fea172118c707a85;p=collectd.git diff --git a/src/swap.c b/src/swap.c index 945c7580..b8b5f098 100644 --- a/src/swap.c +++ b/src/swap.c @@ -36,26 +36,13 @@ # include #endif -#if KERNEL_LINUX || HAVE_LIBKSTAT || defined(VM_SWAPUSAGE) || HAVE_LIBKVM || HAVE_LIBSTATGRAB -# define SWAP_HAVE_READ 1 -#else -# define SWAP_HAVE_READ 0 +#if HAVE_STATGRAB_H +# include #endif #undef MAX #define MAX(x,y) ((x) > (y) ? (x) : (y)) -static data_source_t data_source[1] = -{ - {"value", DS_TYPE_GAUGE, 0, 1099511627776.0} -}; - -static data_set_t data_set = -{ - "swap", 1, data_source -}; - -#if SWAP_HAVE_READ #if KERNEL_LINUX /* No global variables */ /* #endif KERNEL_LINUX */ @@ -69,13 +56,17 @@ static kstat_t *ksp; /* No global variables */ /* #endif defined(VM_SWAPUSAGE) */ -#elif HAVE_LIBKVM +#elif HAVE_LIBKVM_GETSWAPINFO static kvm_t *kvm_obj = NULL; int kvm_pagesize; -/* #endif HAVE_LIBKVM */ +/* #endif HAVE_LIBKVM_GETSWAPINFO */ #elif HAVE_LIBSTATGRAB /* No global variables */ +/* #endif HAVE_LIBSTATGRAB */ + +#else +# error "No applicable input method." #endif /* HAVE_LIBSTATGRAB */ static int swap_init (void) @@ -95,7 +86,7 @@ static int swap_init (void) /* No init stuff */ /* #endif defined(VM_SWAPUSAGE) */ -#elif HAVE_LIBKVM +#elif HAVE_LIBKVM_GETSWAPINFO if (kvm_obj != NULL) { kvm_close (kvm_obj); @@ -114,7 +105,7 @@ static int swap_init (void) ERROR ("swap plugin: kvm_open failed."); return (-1); } -/* #endif HAVE_LIBKVM */ +/* #endif HAVE_LIBKVM_GETSWAPINFO */ #elif HAVE_LIBSTATGRAB /* No init stuff */ @@ -132,12 +123,12 @@ static void swap_submit (const char *type_instance, double value) vl.values = values; vl.values_len = 1; - vl.time = time (NULL); - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "swap"); - strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "swap", sizeof (vl.plugin)); + sstrncpy (vl.type, "swap", sizeof (vl.type)); + sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); - plugin_dispatch_values ("swap", &vl); + plugin_dispatch_values (&vl); } /* void swap_submit */ static int swap_read (void) @@ -156,7 +147,9 @@ static int swap_read (void) if ((fh = fopen ("/proc/meminfo", "r")) == NULL) { - WARNING ("memory: fopen: %s", strerror (errno)); + char errbuf[1024]; + WARNING ("memory: fopen: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -182,7 +175,11 @@ static int swap_read (void) } if (fclose (fh)) - WARNING ("memory: fclose: %s", strerror (errno)); + { + char errbuf[1024]; + WARNING ("memory: fclose: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + } if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total)) return (-1); @@ -203,8 +200,9 @@ static int swap_read (void) if (swapctl (SC_AINFO, &ai) == -1) { + char errbuf[1024]; ERROR ("swap plugin: swapctl failed: %s", - strerror (errno)); + sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } @@ -238,7 +236,7 @@ static int swap_read (void) swap_submit ("used", swap_alloc); swap_submit ("free", swap_avail); - swap_submit ("reserved", swap_resv - swap_alloc); + swap_submit ("reserved", swap_resv); /* #endif HAVE_LIBKSTAT */ #elif defined(VM_SWAPUSAGE) @@ -261,7 +259,7 @@ static int swap_read (void) swap_submit ("free", sw_usage.xsu_avail); /* #endif VM_SWAPUSAGE */ -#elif HAVE_LIBKVM +#elif HAVE_LIBKVM_GETSWAPINFO struct kvm_swap data_s; int status; @@ -287,7 +285,7 @@ static int swap_read (void) swap_submit ("used", used); swap_submit ("free", free); -/* #endif HAVE_LIBKVM */ +/* #endif HAVE_LIBKVM_GETSWAPINFO */ #elif HAVE_LIBSTATGRAB sg_swap_stats *swap; @@ -303,14 +301,9 @@ static int swap_read (void) return (0); } /* int swap_read */ -#endif /* SWAP_HAVE_READ */ void module_register (void) { - plugin_register_data_set (&data_set); - -#if SWAP_HAVE_READ plugin_register_init ("swap", swap_init); plugin_register_read ("swap", swap_read); -#endif /* SWAP_HAVE_READ */ -} +} /* void module_register */