From 33630ee2678b8455810490918974908cf09d14e4 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Fri, 27 May 2016 01:08:21 +0200 Subject: [PATCH] Added freebsd version of memfree --- check_memfree_freebsd/README.md | 23 +++++++++++++++++++++++ check_memfree_freebsd/check_memfree | Bin 0 -> 76499 bytes check_memfree_freebsd/check_memfree.cpp | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ check_memfree_freebsd/check_memfree.h | 26 ++++++++++++++++++++++++++ check_memfree_freebsd/range.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ check_memfree_freebsd/range.h | 31 +++++++++++++++++++++++++++++++ 6 files changed, 305 insertions(+), 0 deletions(-) create mode 100644 check_memfree_freebsd/README.md create mode 100755 check_memfree_freebsd/check_memfree create mode 100644 check_memfree_freebsd/check_memfree.cpp create mode 100644 check_memfree_freebsd/check_memfree.h create mode 100644 check_memfree_freebsd/range.cpp create mode 100644 check_memfree_freebsd/range.h diff --git a/check_memfree_freebsd/README.md b/check_memfree_freebsd/README.md new file mode 100644 index 0000000..281556b --- /dev/null +++ b/check_memfree_freebsd/README.md @@ -0,0 +1,23 @@ +``` +check_memfree v1.0 + +Check free memory space on local machine. + +Usage: +check_memfree [-hV] -w % -c % +check_memfree [-hV] -w -c + +Options: + -h + Print detailed help screen + -V + Print version information + -w INTEGER + Exit with WARNING status if less than INTEGER bytes of memory space are free + -w PERCENT% + Exit with WARNING status if less than PERCENT of memory space is free + -c INTEGER + Exit with CRITICAL status if less than INTEGER bytes of memory space are free + -c PERCENT% + Exit with CRITCAL status if less than PERCENT of memory space is free +``` diff --git a/check_memfree_freebsd/check_memfree b/check_memfree_freebsd/check_memfree new file mode 100755 index 0000000..dbee3a1 Binary files /dev/null and b/check_memfree_freebsd/check_memfree differ diff --git a/check_memfree_freebsd/check_memfree.cpp b/check_memfree_freebsd/check_memfree.cpp new file mode 100644 index 0000000..6dae27a --- /dev/null +++ b/check_memfree_freebsd/check_memfree.cpp @@ -0,0 +1,156 @@ +#include "check_memfree.h" + +using namespace std; + +char *servicename = (char*)"MEMORY"; + +int getNum(string cmd) +{ + char buffer[128]; + stringstream sstr; + FILE* pipe = popen(cmd.c_str(), "r"); + while (!feof(pipe)) + { + if (fgets(buffer, 128, pipe) != NULL) + { + sstr.str(sstr.str()+buffer); + } + } + pclose(pipe); + int num; + sstr >> num; + return num; +} + +void printVersion() +{ + cout << "check_memfree v" << VERSION << endl << endl; +} + +void printHelp(bool longVersion) +{ + if(longVersion) + { + printVersion(); + cout << "Check free memory space on local machine." << endl << endl; + printHelp(false); + cout << "Options:" << endl; + cout << " -h" << endl; + cout << " Print detailed help screen" << endl; + cout << " -V" << endl; + cout << " Print version information" << endl; + cout << " -w INTEGER" << endl; + cout << " Exit with WARNING status if less than INTEGER bytes of memory space are free" << endl; + cout << " -w PERCENT%" << endl; + cout << " Exit with WARNING status if less than PERCENT of memory space is free" << endl; + cout << " -c INTEGER" << endl; + cout << " Exit with CRITICAL status if less than INTEGER bytes of memory space are free" << endl; + cout << " -c PERCENT%" << endl; + cout << " Exit with CRITCAL status if less than PERCENT of memory space is free" << endl << endl; + return; + } + cout << "Usage: " << endl << "check_memfree [-hV] -w % -c %" << endl << "check_memfree [-hV] -w -c " << endl << endl; +} + +int main(int argc, char **argv) +{ + struct itimerval timer; + timer.it_value.tv_sec = 10; + timer.it_value.tv_usec = 0; + timer.it_interval.tv_sec = 0; + timer.it_interval.tv_usec = 0; + setitimer (ITIMER_VIRTUAL, &timer, 0); + + struct sigaction sa; + memset (&sa, 0, sizeof (sa)); + sa.sa_handler = &timer_handler; + sigaction (SIGVTALRM, &sa, 0); + + char *warningFlag = NULL; + char *criticalFlag = NULL; + int c; + + while ((c = getopt (argc, argv, "w:c:Vh")) != -1) + { + switch(c) + { + case 'w': + warningFlag = optarg; + break; + case 'c': + criticalFlag = optarg; + break; + case 'V': + printVersion(); + return 0; + case 'h': + printHelp(true); + return 0; + case '?': + printHelp(false); + return 3; + } + } + + if(warningFlag == NULL) + { + cout << "Warning limit (-w) wasn't set. Exiting." << endl; + return 3; + } + if(criticalFlag == NULL) + { + cout << "Critical limit (-w) wasn't set. Exiting." << endl; + return 3; + } + + double freeMem = 0; + double totalMem = 0; + double pageSize = 0; + + totalMem = getNum("sysctl hw.physmem | cut -d' ' -f 2") / 1024; + pageSize = getNum("sysctl hw.pagesize | cut -d' ' -f 2"); + + freeMem += getNum("sysctl vm.stats.vm.v_inactive_count | cut -d' ' -f 2") * pageSize; + freeMem += getNum("sysctl vm.stats.vm.v_cache_count | cut -d' ' -f 2") * pageSize; + freeMem += getNum("sysctl vm.stats.vm.v_free_count | cut -d' ' -f 2") * pageSize; + freeMem /= 1024; + + int freeMemMB = freeMem / 1024; + int totalMemMB = totalMem / 1024; + int percFree = ((double)freeMemMB/(double)totalMemMB)*100; + + range warningLimit, criticalLimit; + + try + { + warningLimit = parseRange(warningFlag,totalMemMB); + criticalLimit = parseRange(criticalFlag,totalMemMB); + } + catch(rangeException e) + { + cout << e.what() << endl; + exit(3); + } + + int returnCode; + + cout << servicename; + + if((freeMemMB > criticalLimit.min) && (freeMemMB < criticalLimit.max)) + { + cout << " CRITICAL"; + returnCode = 2; + } + else if((freeMemMB > warningLimit.min) && (freeMemMB < warningLimit.max)) + { + cout << " WARNING"; + returnCode = 1; + } + else + { + cout << " OK"; + returnCode = 0; + } + 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_freebsd/check_memfree.h b/check_memfree_freebsd/check_memfree.h new file mode 100644 index 0000000..bf18eef --- /dev/null +++ b/check_memfree_freebsd/check_memfree.h @@ -0,0 +1,26 @@ +#ifndef CHECK_MEMFREE_H +#define CHECK_MEMFREE_H + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include "range.h" +#include "aux.h" + +#define VERSION "1.0" + +int getNum(string cmd); +void printVersion(); +void printHelp(bool longVersion); + +#endif diff --git a/check_memfree_freebsd/range.cpp b/check_memfree_freebsd/range.cpp new file mode 100644 index 0000000..df9e36f --- /dev/null +++ b/check_memfree_freebsd/range.cpp @@ -0,0 +1,69 @@ +#include "range.h" + +range parseRange(char* input, int total) +{ + bool includeRange = false; + range r; + if(input[0] == '~') + { + if(strlen(input) > 2) + { + r.min = 0; + if(input[1] == ':') + { + if(input[strlen(input)-1] == '%') + { + input[strlen(input)-1] = '\0'; + r.max = total*((double)str2int(string(input+2))/100); + } + r.max = str2int(string(input+2)); + } + return r; + } + throw rangeException("Wrong range format or maximum and minimum are unsatisfiable"); + } + else if(input[0] == '@') + { + includeRange = true; + input++; + } + string str = string(input); + size_t pos = str.find_first_of(":"); + if(pos != string::npos) + { + if(str.find_first_of(":",pos+1) != string::npos) + { + throw rangeException("Wrong range format or maximum and minimum are unsatisfiable"); + } + if(str[pos-1] == '%') + { + r.min = total*((double)str2int(str.substr(0,pos-1))/100); + } + else + { + r.min = str2int(str.substr(0,pos)); + } + } + else + { + r.min = 0; + if(str[str.length()-1] == '%') + { + r.max = total*((double)str2int(str.substr(0,str.length()-1))/100); + } + else + { + r.max = str2int(str); + } + } + if(includeRange) + { + r.min--; + r.max++; + } + if(r.min > r.max) + { + throw rangeException("Wrong range format or maximum and minimum are unsatisfiable"); + } + return r; +} diff --git a/check_memfree_freebsd/range.h b/check_memfree_freebsd/range.h new file mode 100644 index 0000000..505a5fd --- /dev/null +++ b/check_memfree_freebsd/range.h @@ -0,0 +1,31 @@ +#ifndef RANGE_H +#define RANGE_H + +#include + +#include + +#include "aux.h" + +struct range +{ + int min; + int max; +}; + +void wrongRange(); +range parseRange(char* input, int total); + +class rangeException : public exception +{ + public: + rangeException(std::string ss) : s(ss) {} + ~rangeException() throw () {} + const char* what() const throw() { return s.c_str(); } + private: + string s; +}; + +#endif + + -- libgit2 0.22.2