From 6aec896d16575505f32c67f62ad107054ed301ca Mon Sep 17 00:00:00 2001 From: Lorin Scraba Date: Tue, 19 Jan 2010 15:22:32 -0800 Subject: [PATCH] swap plugin - swap in/out does not work for kernels <2.6 It works on RedHat9 and 6 (don't ask). A more gracefull solution is having a configuration option for choosing /proc/ format I suppose... Hope it helps some sysadmins like me that are forced to maintained anqituities in production. Cheers From 1da38c6d515f7fa4599b7b08e1284fb2ac9a504a Mon Sep 17 00:00:00 2001 From: Lorin Scraba Date: Tue, 19 Jan 2010 15:16:29 -0800 Subject: [PATCH] swap plugin - swapin/out fix for older kernels - <2.6 Signed-off-by: Lorin Scraba Signed-off-by: Florian Forster --- src/swap.c | 64 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/src/swap.c b/src/swap.c index 7f41c9ea..63a5c89d 100644 --- a/src/swap.c +++ b/src/swap.c @@ -193,6 +193,8 @@ static int swap_read (void) char *fields[8]; int numfields; + unsigned int old_kernel=0; + derive_t swap_used = 0; derive_t swap_cached = 0; derive_t swap_free = 0; @@ -243,31 +245,49 @@ static int swap_read (void) if ((fh = fopen ("/proc/vmstat", "r")) == NULL) { - char errbuf[1024]; - WARNING ("swap: fopen: %s", - sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); - } - - while (fgets (buffer, 1024, fh) != NULL) - { - derive_t *val = NULL; - - if (strncasecmp (buffer, "pswpin", 6) == 0) - val = &swap_in; - else if (strncasecmp (buffer, "pswpout", 7) == 0) - val = &swap_out; + // /proc/vmstat does not exist in kernels <2.6 + if ((fh = fopen ("/proc/stat", "r")) == NULL ) + { + char errbuf[1024]; + WARNING ("swap: fopen: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } else - continue; - - numfields = strsplit (buffer, fields, 8); - - if (numfields < 2) - continue; - - *val = (derive_t) atoll (fields[1]); + old_kernel = 1; } + if ( old_kernel ) + while (fgets (buffer, 1024, fh) != NULL) + { + if (strncasecmp (buffer, "page",4) == 0) + { + numfields = strsplit(buffer,fields,3); + if ( numfields < 3 ) + continue; + swap_in = (derive_t) atoll(fields[1]); + swap_out = (derive_t) atoll(fields[2]); + } + } + else + while (fgets (buffer, 1024, fh) != NULL) + { + derive_t *val = NULL; + if (strncasecmp (buffer, "pswpin", 6) == 0) + val = &swap_in; + else if (strncasecmp (buffer, "pswpout", 7) == 0) + val = &swap_out; + else + continue; + + numfields = strsplit (buffer, fields, 8); + + if (numfields < 2) + continue; + + *val = (derive_t) atoll (fields[1]); + } + if (fclose (fh)) { char errbuf[1024]; -- 2.30.2