Commit 94ddb039ec694926c2867099e8dc38efc8ae3f8a

Authored by Imanol-Mikel Barba Sabariego
1 parent 0ff2d46d

Fixed memfree linux

check_memfree/check_memfree.cpp
... ... @@ -4,6 +4,36 @@ using namespace std;
4 4  
5 5 char *servicename = (char*)"MEMORY";
6 6  
  7 +void getMem(ifstream *file, MEMINFO *meminfo)
  8 +{
  9 + meminfo->freeMem = 0;
  10 + meminfo->totalMem = 0;
  11 + string line, field, value;
  12 + getline(*file,line);
  13 + do
  14 + {
  15 + field = line.substr(0,line.find_first_of(':'));
  16 + value = line.substr(line.find_first_of(' '),line.find_last_of(' '));
  17 + if(field == "MemTotal")
  18 + {
  19 + meminfo->totalMem = str2int(value);
  20 + }
  21 + else if(field == "MemFree")
  22 + {
  23 + meminfo->freeMem += str2int(value);
  24 + }
  25 + else if(field == "Buffers")
  26 + {
  27 + meminfo->freeMem += str2int(value);
  28 + }
  29 + else if(field == "Cached")
  30 + {
  31 + meminfo->freeMem += str2int(value);
  32 + }
  33 + getline(*file,line);
  34 + }while(!file->eof());
  35 +}
  36 +
7 37 int getNum(ifstream *file)
8 38 {
9 39 string numStr = "";
... ... @@ -104,21 +134,15 @@ int main(int argc, char **argv)
104 134 return 3;
105 135 }
106 136  
107   - double freeMem = 0;
108   - double totalMem = 0;
109   -
110   - ifstream meminfo(MEMINFO);
  137 + MEMINFO meminfo;
  138 + ifstream meminfo_file(MEMINFO_FILE);
111 139  
112   - totalMem = getNum(&meminfo);
113   - freeMem += getNum(&meminfo);
114   - getNum(&meminfo);
115   - freeMem += getNum(&meminfo);
116   - freeMem += getNum(&meminfo);
  140 + getMem(&meminfo_file,&meminfo);
117 141  
118   - meminfo.close();
  142 + meminfo_file.close();
119 143  
120   - int freeMemMB = freeMem / 1024;
121   - int totalMemMB = totalMem / 1024;
  144 + int freeMemMB = meminfo.freeMem / 1024;
  145 + int totalMemMB = meminfo.totalMem / 1024;
122 146 int percFree = ((double)freeMemMB/(double)totalMemMB)*100;
123 147  
124 148 range warningLimit, criticalLimit;
... ... @@ -156,3 +180,4 @@ int main(int argc, char **argv)
156 180 cout << " - " << percFree << "% free (" << freeMemMB << " MB out of " << totalMemMB << " MB) |memory=" << freeMemMB << "MB;" << warningLimit.max << ";" << criticalLimit.max << ";" << "0;" << totalMemMB << endl;
157 181 return returnCode;
158 182 }
  183 +
... ...
check_memfree/check_memfree.h
... ... @@ -16,11 +16,18 @@
16 16 #include "range.h"
17 17 #include "auxiliar.h"
18 18  
19   -#define MEMINFO "/proc/meminfo"
  19 +#define MEMINFO_FILE "/proc/meminfo"
20 20 #define VERSION "1.0"
21 21  
  22 +struct meminfo {
  23 + ssize_t totalMem;
  24 + ssize_t freeMem;
  25 +};
  26 +typedef struct meminfo MEMINFO;
  27 +
22 28 int getNum(ifstream *file);
23 29 void printVersion();
24 30 void printHelp(bool longVersion);
25 31  
26 32 #endif
  33 +
... ...