From f2154f92e48e6577dcd04409a9062b41d353ea82 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sat, 28 May 2016 17:14:29 +0200 Subject: [PATCH] Cleaned code for new plugin --- CMakeLists.txt | 5 ++++- check_csgo/README.md | 23 +++++++++++++++++++++++ check_csgo/auxiliar.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ check_csgo/auxiliar.h | 35 +++++++++++++++++++++++++++++++++++ check_csgo/check_csgo.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ check_csgo/check_csgo.h | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 258 insertions(+), 1 deletion(-) create mode 100755 check_csgo/README.md create mode 100755 check_csgo/auxiliar.cpp create mode 100755 check_csgo/auxiliar.h create mode 100755 check_csgo/check_csgo.cpp create mode 100755 check_csgo/check_csgo.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 649357e..8cdf98b 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,10 +3,13 @@ project(nagios_plugins) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -U__STRICT_ANSI__") +set(SOURCE_FILES_CSGO check_csgo/check_csgo.cpp check_memfree/auxiliar.cpp) +add_executable(check_csgo ${SOURCE_FILES_CSGO}) + set(SOURCE_FILES_MEMFREE check_memfree/check_memfree.cpp check_memfree/range.cpp check_memfree/auxiliar.cpp) add_executable(check_memfree ${SOURCE_FILES_MEMFREE}) -set(SOURCE_FILES_MEMFREE_FREEBSD check_memfree_freebsd/check_memfree.cpp check_memfree_freebsd/range.cpp check_memfree_freebsd/auxiliar.cpp) +set(SOURCE_FILES_MEMFREE_FREEBSD check_memfree_freebsd/check_memfree.cpp check_memfree_freebsd/range.cpp check_memfree_freebsd/auxiliar.cpp) add_executable(check_memfree_freebsd ${SOURCE_FILES_MEMFREE_FREEBSD}) set(SOURCE_FILES_SMART check_smart/check_smart.cpp check_smart/auxiliar.cpp) diff --git a/check_csgo/README.md b/check_csgo/README.md new file mode 100755 index 0000000..281556b --- /dev/null +++ b/check_csgo/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_csgo/auxiliar.cpp b/check_csgo/auxiliar.cpp new file mode 100755 index 0000000..53aca90 --- /dev/null +++ b/check_csgo/auxiliar.cpp @@ -0,0 +1,55 @@ +// +// Created by Imanol on 28-may-16. +// + +#include "auxiliar.h" + +void timer_handler (int signum) +{ + if(signum == SIGVTALRM) + { + cout << servicename << " CRITICAL - timeout occurred" << endl; + exit(2); + } +} + +int str2int(string str) +{ + int num; + stringstream sstream; + sstream << str; + if(!(sstream >> num)) + { + throw integerConversionException("Integer conversion error"); + } + return num; +} + +string int2str(int x) +{ + string str; + stringstream sstream; + sstream << x; + sstream >> str; + return str; +} + +int exec(string cmd, string *output) +{ + *output = ""; + FILE* pipe = popen(cmd.c_str(), "r"); + if (!pipe) + { + cout << "Error opening child process" << endl; + exit(3); + } + char buffer[128]; + while(!feof(pipe)) + { + if(fgets(buffer, 128, pipe) != NULL) + { + *output += buffer; + } + } + return pclose(pipe)/256; +} \ No newline at end of file diff --git a/check_csgo/auxiliar.h b/check_csgo/auxiliar.h new file mode 100755 index 0000000..6cc6e6a --- /dev/null +++ b/check_csgo/auxiliar.h @@ -0,0 +1,35 @@ +// +// Created by Imanol on 28-may-16. +// + +#ifndef NAGIOS_PLUGINS_AUXILIAR_H +#define NAGIOS_PLUGINS_AUXILIAR_H + +#include +#include +#include + +#include +#include +#include + +using namespace std; + +extern char *servicename; + +int str2int(string str); +string int2str(int x); +int exec(string cmd, string *output); +void timer_handler (int signum); + +class integerConversionException : public exception +{ +private: + string s; +public: + integerConversionException(std::string ss) : s(ss) {} + ~integerConversionException() throw () {} + const char* what() const throw() { return s.c_str(); } +}; + +#endif //NAGIOS_PLUGINS_AUXILIAR_H diff --git a/check_csgo/check_csgo.cpp b/check_csgo/check_csgo.cpp new file mode 100755 index 0000000..3beb3c3 --- /dev/null +++ b/check_csgo/check_csgo.cpp @@ -0,0 +1,105 @@ +#include "check_csgo.h" + +using namespace std; + +char *servicename = (char*)"CS:GO"; + +void printVersion() +{ + cout << "check_csgo v" << VERSION << endl << endl; +} + +void printHelp(bool longVersion) +{ + if(longVersion) + { + printVersion(); + cout << "Check Source DS instance." << 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 << " -H HOSTADDRESS" << endl; + cout << " Host where the Source DS is running" << endl; + cout << " -p" << endl; + cout << " Port where the Source DS is listening. Default is 27015." << endl << endl; + return; + } + cout << "Usage: " << endl << "check_csgo [-hV] -H HOSTADDRESS [-p PORT]" << endl << endl; +} + +int check_csgo(char *hostname, uint16_t port, SERVERINFO *server_info) +{ + return 0; +} + +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); + + uint16_t port = 27015; + char *hostname = NULL; + int c; + + while ((c = getopt (argc, argv, "H:p:Vh")) != -1) + { + switch(c) + { + case 'H': + hostname = optarg; + break; + case 'p': + port = str2int(optarg); + break; + case 'V': + printVersion(); + return 0; + case 'h': + printHelp(true); + return 0; + case '?': + printHelp(false); + return 3; + } + } + + if(hostname == NULL) + { + cout << "No HOSTADDRESS specified. Exiting." << endl; + return 3; + } + + SERVERINFO server_info; + int returnCode = check_csgo(hostname,port,&server_info); + + cout << servicename; + switch(returnCode) + { + case 0: + cout << " OK"; + break; + + case 1: + cout << " WARNING"; + break; + + case 2: + cout << " CRITICAL"; + break; + } + + cout << " - " << server_info.name << " " << server_info.ip << " " << server_info.name << " " << server_info.map << " " << server_info.players << " " << server_info.latency << endl; + return returnCode; +} diff --git a/check_csgo/check_csgo.h b/check_csgo/check_csgo.h new file mode 100755 index 0000000..5e91a4c --- /dev/null +++ b/check_csgo/check_csgo.h @@ -0,0 +1,36 @@ +#ifndef CHECK_CSGO_H +#define CHECK_CSGO_H + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include "auxiliar.h" + +#define VERSION "1.0" + + +struct serverinfo_struct { + char *name; + char *ip; + char *game; + char *map; + unsigned int players; + unsigned int latency; +}; + +typedef struct serverinfo_struct SERVERINFO; + +int check_csgo(char *hostname, uint16_t port, SERVERINFO *server_info); +void printVersion(); +void printHelp(bool longVersion); + +#endif -- libgit2 0.22.2