Commit f2154f92e48e6577dcd04409a9062b41d353ea82

Authored by Imanol-Mikel Barba Sabariego
1 parent 5f2914bf

Cleaned code for new plugin

CMakeLists.txt
... ... @@ -3,10 +3,13 @@ project(nagios_plugins)
3 3  
4 4 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -U__STRICT_ANSI__")
5 5  
  6 +set(SOURCE_FILES_CSGO check_csgo/check_csgo.cpp check_memfree/auxiliar.cpp)
  7 +add_executable(check_csgo ${SOURCE_FILES_CSGO})
  8 +
6 9 set(SOURCE_FILES_MEMFREE check_memfree/check_memfree.cpp check_memfree/range.cpp check_memfree/auxiliar.cpp)
7 10 add_executable(check_memfree ${SOURCE_FILES_MEMFREE})
8 11  
9   -set(SOURCE_FILES_MEMFREE_FREEBSD check_memfree_freebsd/check_memfree.cpp check_memfree_freebsd/range.cpp check_memfree_freebsd/auxiliar.cpp)
  12 +set(SOURCE_FILES_MEMFREE_FREEBSD check_memfree_freebsd/check_memfree.cpp check_memfree_freebsd/range.cpp check_memfree_freebsd/auxiliar.cpp)
10 13 add_executable(check_memfree_freebsd ${SOURCE_FILES_MEMFREE_FREEBSD})
11 14  
12 15 set(SOURCE_FILES_SMART check_smart/check_smart.cpp check_smart/auxiliar.cpp)
... ...
check_csgo/README.md 0 โ†’ 100755
  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_csgo/auxiliar.cpp 0 โ†’ 100755
  1 +//
  2 +// Created by Imanol on 28-may-16.
  3 +//
  4 +
  5 +#include "auxiliar.h"
  6 +
  7 +void timer_handler (int signum)
  8 +{
  9 + if(signum == SIGVTALRM)
  10 + {
  11 + cout << servicename << " CRITICAL - timeout occurred" << endl;
  12 + exit(2);
  13 + }
  14 +}
  15 +
  16 +int str2int(string str)
  17 +{
  18 + int num;
  19 + stringstream sstream;
  20 + sstream << str;
  21 + if(!(sstream >> num))
  22 + {
  23 + throw integerConversionException("Integer conversion error");
  24 + }
  25 + return num;
  26 +}
  27 +
  28 +string int2str(int x)
  29 +{
  30 + string str;
  31 + stringstream sstream;
  32 + sstream << x;
  33 + sstream >> str;
  34 + return str;
  35 +}
  36 +
  37 +int exec(string cmd, string *output)
  38 +{
  39 + *output = "";
  40 + FILE* pipe = popen(cmd.c_str(), "r");
  41 + if (!pipe)
  42 + {
  43 + cout << "Error opening child process" << endl;
  44 + exit(3);
  45 + }
  46 + char buffer[128];
  47 + while(!feof(pipe))
  48 + {
  49 + if(fgets(buffer, 128, pipe) != NULL)
  50 + {
  51 + *output += buffer;
  52 + }
  53 + }
  54 + return pclose(pipe)/256;
  55 +}
0 56 \ No newline at end of file
... ...
check_csgo/auxiliar.h 0 โ†’ 100755
  1 +//
  2 +// Created by Imanol on 28-may-16.
  3 +//
  4 +
  5 +#ifndef NAGIOS_PLUGINS_AUXILIAR_H
  6 +#define NAGIOS_PLUGINS_AUXILIAR_H
  7 +
  8 +#include <sstream>
  9 +#include <iostream>
  10 +#include <exception>
  11 +
  12 +#include <stdlib.h>
  13 +#include <stdio.h>
  14 +#include <signal.h>
  15 +
  16 +using namespace std;
  17 +
  18 +extern char *servicename;
  19 +
  20 +int str2int(string str);
  21 +string int2str(int x);
  22 +int exec(string cmd, string *output);
  23 +void timer_handler (int signum);
  24 +
  25 +class integerConversionException : public exception
  26 +{
  27 +private:
  28 + string s;
  29 +public:
  30 + integerConversionException(std::string ss) : s(ss) {}
  31 + ~integerConversionException() throw () {}
  32 + const char* what() const throw() { return s.c_str(); }
  33 +};
  34 +
  35 +#endif //NAGIOS_PLUGINS_AUXILIAR_H
... ...
check_csgo/check_csgo.cpp 0 โ†’ 100755
  1 +#include "check_csgo.h"
  2 +
  3 +using namespace std;
  4 +
  5 +char *servicename = (char*)"CS:GO";
  6 +
  7 +void printVersion()
  8 +{
  9 + cout << "check_csgo v" << VERSION << endl << endl;
  10 +}
  11 +
  12 +void printHelp(bool longVersion)
  13 +{
  14 + if(longVersion)
  15 + {
  16 + printVersion();
  17 + cout << "Check Source DS instance." << endl << endl;
  18 + printHelp(false);
  19 + cout << "Options:" << endl;
  20 + cout << " -h" << endl;
  21 + cout << " Print detailed help screen" << endl;
  22 + cout << " -V" << endl;
  23 + cout << " Print version information" << endl;
  24 + cout << " -H HOSTADDRESS" << endl;
  25 + cout << " Host where the Source DS is running" << endl;
  26 + cout << " -p" << endl;
  27 + cout << " Port where the Source DS is listening. Default is 27015." << endl << endl;
  28 + return;
  29 + }
  30 + cout << "Usage: " << endl << "check_csgo [-hV] -H HOSTADDRESS [-p PORT]" << endl << endl;
  31 +}
  32 +
  33 +int check_csgo(char *hostname, uint16_t port, SERVERINFO *server_info)
  34 +{
  35 + return 0;
  36 +}
  37 +
  38 +int main(int argc, char **argv)
  39 +{
  40 + struct itimerval timer;
  41 + timer.it_value.tv_sec = 10;
  42 + timer.it_value.tv_usec = 0;
  43 + timer.it_interval.tv_sec = 0;
  44 + timer.it_interval.tv_usec = 0;
  45 + setitimer (ITIMER_VIRTUAL, &timer, 0);
  46 +
  47 + struct sigaction sa;
  48 + memset (&sa, 0, sizeof (sa));
  49 + sa.sa_handler = &timer_handler;
  50 + sigaction (SIGVTALRM, &sa, 0);
  51 +
  52 + uint16_t port = 27015;
  53 + char *hostname = NULL;
  54 + int c;
  55 +
  56 + while ((c = getopt (argc, argv, "H:p:Vh")) != -1)
  57 + {
  58 + switch(c)
  59 + {
  60 + case 'H':
  61 + hostname = optarg;
  62 + break;
  63 + case 'p':
  64 + port = str2int(optarg);
  65 + break;
  66 + case 'V':
  67 + printVersion();
  68 + return 0;
  69 + case 'h':
  70 + printHelp(true);
  71 + return 0;
  72 + case '?':
  73 + printHelp(false);
  74 + return 3;
  75 + }
  76 + }
  77 +
  78 + if(hostname == NULL)
  79 + {
  80 + cout << "No HOSTADDRESS specified. Exiting." << endl;
  81 + return 3;
  82 + }
  83 +
  84 + SERVERINFO server_info;
  85 + int returnCode = check_csgo(hostname,port,&server_info);
  86 +
  87 + cout << servicename;
  88 + switch(returnCode)
  89 + {
  90 + case 0:
  91 + cout << " OK";
  92 + break;
  93 +
  94 + case 1:
  95 + cout << " WARNING";
  96 + break;
  97 +
  98 + case 2:
  99 + cout << " CRITICAL";
  100 + break;
  101 + }
  102 +
  103 + cout << " - " << server_info.name << " " << server_info.ip << " " << server_info.name << " " << server_info.map << " " << server_info.players << " " << server_info.latency << endl;
  104 + return returnCode;
  105 +}
... ...
check_csgo/check_csgo.h 0 โ†’ 100755
  1 +#ifndef CHECK_CSGO_H
  2 +#define CHECK_CSGO_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 +
  14 +#include <sys/time.h>
  15 +
  16 +#include "auxiliar.h"
  17 +
  18 +#define VERSION "1.0"
  19 +
  20 +
  21 +struct serverinfo_struct {
  22 + char *name;
  23 + char *ip;
  24 + char *game;
  25 + char *map;
  26 + unsigned int players;
  27 + unsigned int latency;
  28 +};
  29 +
  30 +typedef struct serverinfo_struct SERVERINFO;
  31 +
  32 +int check_csgo(char *hostname, uint16_t port, SERVERINFO *server_info);
  33 +void printVersion();
  34 +void printHelp(bool longVersion);
  35 +
  36 +#endif
... ...