X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fswap.c;h=46ba6652953726ce0b7d90aecea2345f681ec64b;hb=69395dbecda0e01df70b69a61f72d12a897037d9;hp=e467818db4ca01f65af8fd66eb0c7780d571fec3;hpb=fabc18dac7e676b4e6577bcb6880d501dccc5b31;p=collectd.git diff --git a/src/swap.c b/src/swap.c index e467818d..46ba6652 100644 --- a/src/swap.c +++ b/src/swap.c @@ -210,25 +210,18 @@ static int swap_read (void) return (-1); } - while (fgets (buffer, 1024, fh) != NULL) + while (fgets (buffer, sizeof (buffer), fh) != NULL) { - derive_t *val = NULL; - - if (strncasecmp (buffer, "SwapTotal:", 10) == 0) - val = &swap_total; - else if (strncasecmp (buffer, "SwapFree:", 9) == 0) - val = &swap_free; - else if (strncasecmp (buffer, "SwapCached:", 11) == 0) - val = &swap_cached; - else - continue; - - numfields = strsplit (buffer, fields, 8); - + numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields)); if (numfields < 2) continue; - *val = (derive_t) atoll (fields[1]) * 1024LL; + if (strcasecmp (fields[0], "SwapTotal:") == 0) + strtoderive (fields[1], &swap_total); + else if (strcasecmp (fields[0], "SwapFree:") == 0) + strtoderive (fields[1], &swap_free); + else if (strcasecmp (fields[0], "SwapCached:") == 0) + strtoderive (fields[1], &swap_cached); } if (fclose (fh)) @@ -257,7 +250,7 @@ static int swap_read (void) old_kernel = 1; } - while (fgets (buffer, 1024, fh) != NULL) + while (fgets (buffer, sizeof (buffer), fh) != NULL) { numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields)); @@ -266,7 +259,7 @@ static int swap_read (void) if (numfields != 2) continue; - if (strcasecmp ("pswpin", fields[0]) != 0) + if (strcasecmp ("pswpin", fields[0]) == 0) strtoderive (fields[1], &swap_in); else if (strcasecmp ("pswpout", fields[0]) == 0) strtoderive (fields[1], &swap_out); @@ -291,10 +284,10 @@ static int swap_read (void) sstrerror (errno, errbuf, sizeof (errbuf))); } - swap_submit ("used", swap_used, DS_TYPE_GAUGE); - swap_submit ("free", swap_free, DS_TYPE_GAUGE); - swap_submit ("cached", swap_cached, DS_TYPE_GAUGE); - swap_submit ("in", swap_in, DS_TYPE_DERIVE); + swap_submit ("used", 1024 * swap_used, DS_TYPE_GAUGE); + swap_submit ("free", 1024 * swap_free, DS_TYPE_GAUGE); + swap_submit ("cached", 1024 * swap_cached, DS_TYPE_GAUGE); + swap_submit ("in", swap_in, DS_TYPE_DERIVE); swap_submit ("out", swap_out, DS_TYPE_DERIVE); /* #endif KERNEL_LINUX */