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 +