Commit 0b5e6218228aa2ff76dd6facdeecd1e460a77010

Authored by Imanol-Mikel Barba Sabariego
1 parent 999ee4d0

Created plugin template for nfs4

CMakeLists.txt
... ... @@ -6,6 +6,9 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -U__STRICT_ANSI__")
6 6 set(SOURCE_FILES_CSGO check_csgo/check_csgo.cpp check_csgo/auxiliar.cpp check_csgo/udp.cpp)
7 7 add_executable(check_csgo ${SOURCE_FILES_CSGO})
8 8  
  9 +set(SOURCE_FILES_NFS4 check_nfs4/check_nfs4.cpp check_csgo/auxiliar.cpp check_csgo/udp.cpp)
  10 +add_executable(check_nfs4 ${SOURCE_FILES_NFS4})
  11 +
9 12 set(SOURCE_FILES_MEMFREE check_memfree/check_memfree.cpp check_memfree/range.cpp check_memfree/auxiliar.cpp)
10 13 add_executable(check_memfree ${SOURCE_FILES_MEMFREE})
11 14  
... ... @@ -16,4 +19,5 @@ set(SOURCE_FILES_SMART check_smart/check_smart.cpp check_smart/auxiliar.cpp)
16 19 add_executable(check_smart ${SOURCE_FILES_SMART})
17 20  
18 21 set(SOURCE_FILES_TFTP check_tftp/check_tftp.cpp check_tftp/udp.cpp check_tftp/auxiliar.cpp)
19   -add_executable(check_tftp ${SOURCE_FILES_TFTP})
20 22 \ No newline at end of file
  23 +add_executable(check_tftp ${SOURCE_FILES_TFTP})
  24 +
... ...
check_nfs4/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_nfs4/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_nfs4/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_nfs4/check_nfs4.cpp 0 → 100755
  1 +#include "check_nfs4.h"
  2 +
  3 +using namespace std;
  4 +
  5 +char *servicename = (char*)"NFSv4";
  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 CS:GO 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_nfs4(char *hostname, char *mountpoint, string *detail)
  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 + char *hostname = NULL;
  53 + char *mountpoint = NULL;
  54 + int c;
  55 +
  56 + while ((c = getopt (argc, argv, "H:m:Vh")) != -1)
  57 + {
  58 + switch(c)
  59 + {
  60 + case 'H':
  61 + hostname = optarg;
  62 + break;
  63 + case 'm':
  64 + mountpoint = 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 + if(mountpoint == NULL)
  84 + {
  85 + cout << "No mountpoint specified. Exiting." << endl;
  86 + return 3;
  87 + }
  88 +
  89 + string details = "";
  90 + int returnCode = check_nfs4(hostname,mountpoint,&details);
  91 +
  92 + cout << servicename;
  93 + switch(returnCode)
  94 + {
  95 + case 0:
  96 + cout << " OK";
  97 + cout << " - " << mountpoint << " " << details << endl;
  98 + break;
  99 +
  100 + case 2:
  101 + cout << " CRITICAL - " << details << endl;
  102 + break;
  103 + }
  104 +
  105 + return returnCode;
  106 +}
... ...
check_nfs4/check_nfs4.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 +#include "udp.h"
  18 +
  19 +#define VERSION "1.0"
  20 +
  21 +int check_nfs4(char *hostname, char *mountpoint, string *detail);
  22 +void printVersion();
  23 +void printHelp(bool longVersion);
  24 +
  25 +#endif
... ...
check_nfs4/udp.cpp 0 → 100644
  1 +#include "udp.h"
  2 +
  3 +void setupSocket(uint16_t port, char *hostname, struct hostent *host, int timeout, struct sockaddr_in *si, int *s)
  4 +{
  5 + if((*s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  6 + {
  7 + cout << "Couldn't create socket" << endl;
  8 + exit(3);
  9 + }
  10 + struct timeval tv;
  11 + tv.tv_sec = timeout;
  12 + tv.tv_usec = 0;
  13 + if(setsockopt(*s, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0)
  14 + {
  15 + cout << "Error setting socket timeout" << endl;
  16 + exit(3);
  17 + }
  18 + if(!(host = gethostbyname(hostname)))
  19 + {
  20 + cout << "Client could not get host address information" << endl;
  21 + exit(3);
  22 + }
  23 +
  24 + memset((char *) si, 0, sizeof(*si));
  25 + si->sin_family = AF_INET;
  26 + memcpy (&(si->sin_addr), host->h_addr, host->h_length);
  27 + si->sin_port = htons(port);
  28 +}
  29 +
  30 +int sendMsg(int s, char *msg, size_t msgLength, struct sockaddr_in *si)
  31 +{
  32 + return sendto(s, msg, msgLength, 0,(struct sockaddr*) si, sizeof(*si));
  33 +}
  34 +
  35 +int recvMsg(int s, char *msg, size_t msgLength, struct sockaddr_in *si)
  36 +{
  37 + size_t slen = sizeof(*si);
  38 + return recvfrom(s, msg, msgLength, 0, (struct sockaddr *) si, (socklen_t*)&slen);
  39 +}
... ...
check_nfs4/udp.h 0 → 100644
  1 +#ifndef UDP_H
  2 +#define UDP_H
  3 +
  4 +#include <iostream>
  5 +
  6 +#include <string.h>
  7 +#include <cstdlib>
  8 +
  9 +#include <arpa/inet.h>
  10 +#include <sys/socket.h>
  11 +#include <netdb.h>
  12 +#include <netinet/in.h>
  13 +
  14 +using namespace std;
  15 +
  16 +void setupSocket(uint16_t port, char *hostname, struct hostent *host, int timeout, struct sockaddr_in *si, int *s);
  17 +int sendMsg(int s, char *msg, size_t msgLength, struct sockaddr_in *si);
  18 +int recvMsg(int s, char *msg, size_t msgLength, struct sockaddr_in *si);
  19 +
  20 +#endif
... ...