Commit 33630ee2678b8455810490918974908cf09d14e4

Authored by Imanol-Mikel Barba Sabariego
1 parent 50560f6d

Added freebsd version of memfree

check_memfree_freebsd/README.md 0 → 100644
  1 +```
  2 +check_memfree v1.0
  3 +
  4 +Check free memory space on local machine.
  5 +
  6 +Usage:
  7 +check_memfree [-hV] -w <percent_free>% -c <percent_free>%
  8 +check_memfree [-hV] -w <bytes_free> -c <bytes_free>
  9 +
  10 +Options:
  11 + -h
  12 + Print detailed help screen
  13 + -V
  14 + Print version information
  15 + -w INTEGER
  16 + Exit with WARNING status if less than INTEGER bytes of memory space are free
  17 + -w PERCENT%
  18 + Exit with WARNING status if less than PERCENT of memory space is free
  19 + -c INTEGER
  20 + Exit with CRITICAL status if less than INTEGER bytes of memory space are free
  21 + -c PERCENT%
  22 + Exit with CRITCAL status if less than PERCENT of memory space is free
  23 +```
... ...
check_memfree_freebsd/check_memfree 0 → 100755
No preview for this file type
check_memfree_freebsd/check_memfree.cpp 0 → 100644
  1 +#include "check_memfree.h"
  2 +
  3 +using namespace std;
  4 +
  5 +char *servicename = (char*)"MEMORY";
  6 +
  7 +int getNum(string cmd)
  8 +{
  9 + char buffer[128];
  10 + stringstream sstr;
  11 + FILE* pipe = popen(cmd.c_str(), "r");
  12 + while (!feof(pipe))
  13 + {
  14 + if (fgets(buffer, 128, pipe) != NULL)
  15 + {
  16 + sstr.str(sstr.str()+buffer);
  17 + }
  18 + }
  19 + pclose(pipe);
  20 + int num;
  21 + sstr >> num;
  22 + return num;
  23 +}
  24 +
  25 +void printVersion()
  26 +{
  27 + cout << "check_memfree v" << VERSION << endl << endl;
  28 +}
  29 +
  30 +void printHelp(bool longVersion)
  31 +{
  32 + if(longVersion)
  33 + {
  34 + printVersion();
  35 + cout << "Check free memory space on local machine." << endl << endl;
  36 + printHelp(false);
  37 + cout << "Options:" << endl;
  38 + cout << " -h" << endl;
  39 + cout << " Print detailed help screen" << endl;
  40 + cout << " -V" << endl;
  41 + cout << " Print version information" << endl;
  42 + cout << " -w INTEGER" << endl;
  43 + cout << " Exit with WARNING status if less than INTEGER bytes of memory space are free" << endl;
  44 + cout << " -w PERCENT%" << endl;
  45 + cout << " Exit with WARNING status if less than PERCENT of memory space is free" << endl;
  46 + cout << " -c INTEGER" << endl;
  47 + cout << " Exit with CRITICAL status if less than INTEGER bytes of memory space are free" << endl;
  48 + cout << " -c PERCENT%" << endl;
  49 + cout << " Exit with CRITCAL status if less than PERCENT of memory space is free" << endl << endl;
  50 + return;
  51 + }
  52 + cout << "Usage: " << endl << "check_memfree [-hV] -w <percent_free>% -c <percent_free>%" << endl << "check_memfree [-hV] -w <bytes_free> -c <bytes_free>" << endl << endl;
  53 +}
  54 +
  55 +int main(int argc, char **argv)
  56 +{
  57 + struct itimerval timer;
  58 + timer.it_value.tv_sec = 10;
  59 + timer.it_value.tv_usec = 0;
  60 + timer.it_interval.tv_sec = 0;
  61 + timer.it_interval.tv_usec = 0;
  62 + setitimer (ITIMER_VIRTUAL, &timer, 0);
  63 +
  64 + struct sigaction sa;
  65 + memset (&sa, 0, sizeof (sa));
  66 + sa.sa_handler = &timer_handler;
  67 + sigaction (SIGVTALRM, &sa, 0);
  68 +
  69 + char *warningFlag = NULL;
  70 + char *criticalFlag = NULL;
  71 + int c;
  72 +
  73 + while ((c = getopt (argc, argv, "w:c:Vh")) != -1)
  74 + {
  75 + switch(c)
  76 + {
  77 + case 'w':
  78 + warningFlag = optarg;
  79 + break;
  80 + case 'c':
  81 + criticalFlag = optarg;
  82 + break;
  83 + case 'V':
  84 + printVersion();
  85 + return 0;
  86 + case 'h':
  87 + printHelp(true);
  88 + return 0;
  89 + case '?':
  90 + printHelp(false);
  91 + return 3;
  92 + }
  93 + }
  94 +
  95 + if(warningFlag == NULL)
  96 + {
  97 + cout << "Warning limit (-w) wasn't set. Exiting." << endl;
  98 + return 3;
  99 + }
  100 + if(criticalFlag == NULL)
  101 + {
  102 + cout << "Critical limit (-w) wasn't set. Exiting." << endl;
  103 + return 3;
  104 + }
  105 +
  106 + double freeMem = 0;
  107 + double totalMem = 0;
  108 + double pageSize = 0;
  109 +
  110 + totalMem = getNum("sysctl hw.physmem | cut -d' ' -f 2") / 1024;
  111 + pageSize = getNum("sysctl hw.pagesize | cut -d' ' -f 2");
  112 +
  113 + freeMem += getNum("sysctl vm.stats.vm.v_inactive_count | cut -d' ' -f 2") * pageSize;
  114 + freeMem += getNum("sysctl vm.stats.vm.v_cache_count | cut -d' ' -f 2") * pageSize;
  115 + freeMem += getNum("sysctl vm.stats.vm.v_free_count | cut -d' ' -f 2") * pageSize;
  116 + freeMem /= 1024;
  117 +
  118 + int freeMemMB = freeMem / 1024;
  119 + int totalMemMB = totalMem / 1024;
  120 + int percFree = ((double)freeMemMB/(double)totalMemMB)*100;
  121 +
  122 + range warningLimit, criticalLimit;
  123 +
  124 + try
  125 + {
  126 + warningLimit = parseRange(warningFlag,totalMemMB);
  127 + criticalLimit = parseRange(criticalFlag,totalMemMB);
  128 + }
  129 + catch(rangeException e)
  130 + {
  131 + cout << e.what() << endl;
  132 + exit(3);
  133 + }
  134 +
  135 + int returnCode;
  136 +
  137 + cout << servicename;
  138 +
  139 + if((freeMemMB > criticalLimit.min) && (freeMemMB < criticalLimit.max))
  140 + {
  141 + cout << " CRITICAL";
  142 + returnCode = 2;
  143 + }
  144 + else if((freeMemMB > warningLimit.min) && (freeMemMB < warningLimit.max))
  145 + {
  146 + cout << " WARNING";
  147 + returnCode = 1;
  148 + }
  149 + else
  150 + {
  151 + cout << " OK";
  152 + returnCode = 0;
  153 + }
  154 + cout << " - " << percFree << "% free (" << freeMemMB << " MB out of " << totalMemMB << " MB) |memory=" << freeMemMB << "MB;" << warningLimit.max << ";" << criticalLimit.max << ";" << "0;" << totalMemMB << endl;
  155 + return returnCode;
  156 +}
... ...
check_memfree_freebsd/check_memfree.h 0 → 100644
  1 +#ifndef CHECK_MEMFREE_H
  2 +#define CHECK_MEMFREE_H
  3 +
  4 +#include <iostream>
  5 +#include <sstream>
  6 +#include <fstream>
  7 +#include <limits>
  8 +
  9 +#include <ctype.h>
  10 +#include <cstdlib>
  11 +#include <unistd.h>
  12 +#include <cstring>
  13 +#include <cstdio>
  14 +
  15 +#include <sys/time.h>
  16 +
  17 +#include "range.h"
  18 +#include "aux.h"
  19 +
  20 +#define VERSION "1.0"
  21 +
  22 +int getNum(string cmd);
  23 +void printVersion();
  24 +void printHelp(bool longVersion);
  25 +
  26 +#endif
... ...
check_memfree_freebsd/range.cpp 0 → 100644
  1 +#include "range.h"
  2 +
  3 +range parseRange(char* input, int total)
  4 +{
  5 + bool includeRange = false;
  6 + range r;
  7 + if(input[0] == '~')
  8 + {
  9 + if(strlen(input) > 2)
  10 + {
  11 + r.min = 0;
  12 + if(input[1] == ':')
  13 + {
  14 + if(input[strlen(input)-1] == '%')
  15 + {
  16 + input[strlen(input)-1] = '\0';
  17 + r.max = total*((double)str2int(string(input+2))/100);
  18 + }
  19 + r.max = str2int(string(input+2));
  20 + }
  21 + return r;
  22 + }
  23 + throw rangeException("Wrong range format or maximum and minimum are unsatisfiable");
  24 + }
  25 + else if(input[0] == '@')
  26 + {
  27 + includeRange = true;
  28 + input++;
  29 + }
  30 + string str = string(input);
  31 + size_t pos = str.find_first_of(":");
  32 + if(pos != string::npos)
  33 + {
  34 + if(str.find_first_of(":",pos+1) != string::npos)
  35 + {
  36 + throw rangeException("Wrong range format or maximum and minimum are unsatisfiable");
  37 + }
  38 + if(str[pos-1] == '%')
  39 + {
  40 + r.min = total*((double)str2int(str.substr(0,pos-1))/100);
  41 + }
  42 + else
  43 + {
  44 + r.min = str2int(str.substr(0,pos));
  45 + }
  46 + }
  47 + else
  48 + {
  49 + r.min = 0;
  50 + if(str[str.length()-1] == '%')
  51 + {
  52 + r.max = total*((double)str2int(str.substr(0,str.length()-1))/100);
  53 + }
  54 + else
  55 + {
  56 + r.max = str2int(str);
  57 + }
  58 + }
  59 + if(includeRange)
  60 + {
  61 + r.min--;
  62 + r.max++;
  63 + }
  64 + if(r.min > r.max)
  65 + {
  66 + throw rangeException("Wrong range format or maximum and minimum are unsatisfiable");
  67 + }
  68 + return r;
  69 +}
... ...
check_memfree_freebsd/range.h 0 → 100644
  1 +#ifndef RANGE_H
  2 +#define RANGE_H
  3 +
  4 +#include <exception>
  5 +
  6 +#include <string.h>
  7 +
  8 +#include "aux.h"
  9 +
  10 +struct range
  11 +{
  12 + int min;
  13 + int max;
  14 +};
  15 +
  16 +void wrongRange();
  17 +range parseRange(char* input, int total);
  18 +
  19 +class rangeException : public exception
  20 +{
  21 + public:
  22 + rangeException(std::string ss) : s(ss) {}
  23 + ~rangeException() throw () {}
  24 + const char* what() const throw() { return s.c_str(); }
  25 + private:
  26 + string s;
  27 +};
  28 +
  29 +#endif
  30 +
  31 +
... ...