From 94ddb039ec694926c2867099e8dc38efc8ae3f8a Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Mon, 13 Jun 2016 10:37:28 +0200 Subject: [PATCH] Fixed memfree linux --- check_memfree/check_memfree.cpp | 49 +++++++++++++++++++++++++++++++++++++------------ check_memfree/check_memfree.h | 9 ++++++++- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/check_memfree/check_memfree.cpp b/check_memfree/check_memfree.cpp index b505220..569970a 100755 --- a/check_memfree/check_memfree.cpp +++ b/check_memfree/check_memfree.cpp @@ -4,6 +4,36 @@ using namespace std; char *servicename = (char*)"MEMORY"; +void getMem(ifstream *file, MEMINFO *meminfo) +{ + meminfo->freeMem = 0; + meminfo->totalMem = 0; + string line, field, value; + getline(*file,line); + do + { + field = line.substr(0,line.find_first_of(':')); + value = line.substr(line.find_first_of(' '),line.find_last_of(' ')); + if(field == "MemTotal") + { + meminfo->totalMem = str2int(value); + } + else if(field == "MemFree") + { + meminfo->freeMem += str2int(value); + } + else if(field == "Buffers") + { + meminfo->freeMem += str2int(value); + } + else if(field == "Cached") + { + meminfo->freeMem += str2int(value); + } + getline(*file,line); + }while(!file->eof()); +} + int getNum(ifstream *file) { string numStr = ""; @@ -104,21 +134,15 @@ int main(int argc, char **argv) return 3; } - double freeMem = 0; - double totalMem = 0; - - ifstream meminfo(MEMINFO); + MEMINFO meminfo; + ifstream meminfo_file(MEMINFO_FILE); - totalMem = getNum(&meminfo); - freeMem += getNum(&meminfo); - getNum(&meminfo); - freeMem += getNum(&meminfo); - freeMem += getNum(&meminfo); + getMem(&meminfo_file,&meminfo); - meminfo.close(); + meminfo_file.close(); - int freeMemMB = freeMem / 1024; - int totalMemMB = totalMem / 1024; + int freeMemMB = meminfo.freeMem / 1024; + int totalMemMB = meminfo.totalMem / 1024; int percFree = ((double)freeMemMB/(double)totalMemMB)*100; range warningLimit, criticalLimit; @@ -156,3 +180,4 @@ int main(int argc, char **argv) cout << " - " << percFree << "% free (" << freeMemMB << " MB out of " << totalMemMB << " MB) |memory=" << freeMemMB << "MB;" << warningLimit.max << ";" << criticalLimit.max << ";" << "0;" << totalMemMB << endl; return returnCode; } + diff --git a/check_memfree/check_memfree.h b/check_memfree/check_memfree.h index 60091d6..e4c46e4 100755 --- a/check_memfree/check_memfree.h +++ b/check_memfree/check_memfree.h @@ -16,11 +16,18 @@ #include "range.h" #include "auxiliar.h" -#define MEMINFO "/proc/meminfo" +#define MEMINFO_FILE "/proc/meminfo" #define VERSION "1.0" +struct meminfo { + ssize_t totalMem; + ssize_t freeMem; +}; +typedef struct meminfo MEMINFO; + int getNum(ifstream *file); void printVersion(); void printHelp(bool longVersion); #endif + -- libgit2 0.22.2