Commit be710417c186e60a42989ffe1147ace4638021b0

Authored by Imanol-Mikel Barba Sabariego
1 parent 94ddb039

Added check_openvpn

CMakeLists.txt
... ... @@ -21,3 +21,6 @@ add_executable(check_smart ${SOURCE_FILES_SMART})
21 21 set(SOURCE_FILES_TFTP check_tftp/check_tftp.cpp check_tftp/udp.cpp check_tftp/auxiliar.cpp)
22 22 add_executable(check_tftp ${SOURCE_FILES_TFTP})
23 23  
  24 +set(SOURCE_FILES_OPENVPN check_openvpn/check_openvpn.cpp check_openvpn/tcp.cpp check_openvpn/auxiliar.cpp)
  25 +add_executable(check_openvpn ${SOURCE_FILES_OPENVPN})
  26 +
... ...
check_openvpn/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_openvpn/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 +void split(const string &s, const char* delim, vector<string> & v)
  38 +{
  39 + char *dup = strdup(s.c_str());
  40 + char *token = strtok(dup, delim);
  41 + while(token != NULL)
  42 + {
  43 + v.push_back(string(token));
  44 + token = strtok(NULL, delim);
  45 + }
  46 + free(dup);
  47 +}
  48 +
  49 +int exec(string cmd, string *output)
  50 +{
  51 + *output = "";
  52 + FILE* pipe = popen(cmd.c_str(), "r");
  53 + if (!pipe)
  54 + {
  55 + cout << "Error opening child process" << endl;
  56 + exit(3);
  57 + }
  58 + char buffer[128];
  59 + while(!feof(pipe))
  60 + {
  61 + if(fgets(buffer, 128, pipe) != NULL)
  62 + {
  63 + *output += buffer;
  64 + }
  65 + }
  66 + return pclose(pipe)/256;
  67 +}
... ...
check_openvpn/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 +#include <vector>
  12 +
  13 +#include <string.h>
  14 +#include <stdlib.h>
  15 +#include <stdio.h>
  16 +#include <signal.h>
  17 +
  18 +using namespace std;
  19 +
  20 +extern char *servicename;
  21 +
  22 +int str2int(string str);
  23 +string int2str(int x);
  24 +int exec(string cmd, string *output);
  25 +void timer_handler (int signum);
  26 +void split(const string &s, const char* delim, vector<string> & v);
  27 +
  28 +class integerConversionException : public exception
  29 +{
  30 +private:
  31 + string s;
  32 +public:
  33 + integerConversionException(std::string ss) : s(ss) {}
  34 + ~integerConversionException() throw () {}
  35 + const char* what() const throw() { return s.c_str(); }
  36 +};
  37 +
  38 +#endif //NAGIOS_PLUGINS_AUXILIAR_H
... ...
check_openvpn/check_openvpn.cpp 0 → 100755
  1 +#include "check_openvpn.h"
  2 +
  3 +using namespace std;
  4 +
  5 +char *servicename = (char*)"OpenVPN";
  6 +
  7 +void printVersion()
  8 +{
  9 + cout << "check_openvpn v" << VERSION << endl << endl;
  10 +}
  11 +
  12 +void printHelp(bool longVersion)
  13 +{
  14 + if(longVersion)
  15 + {
  16 + printVersion();
  17 + cout << "Check OpenVPN server" << 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 UPnP server is running" << endl;
  26 + cout << " -p PORT" << endl;
  27 + cout << " OpenVPN Administration port" << endl;
  28 + cout << " -P PASSWORD" << endl;
  29 + cout << " OpenVPN Administration password" << endl;
  30 + return;
  31 + }
  32 + cout << "Usage: " << endl << "check_openvpn [-hV] -H HOSTADDRESS -p PORT -P PASSWORD" << endl << endl;
  33 +}
  34 +
  35 +int check_openvpn(uint16_t port, char *hostname, char *password, string *serverinfo)
  36 +{
  37 + size_t numClients = 0;
  38 + string ipList = "";
  39 + char buffer[MAX_TCP+1];
  40 + int s;
  41 + int timeout = 10;
  42 + int bytes = 0;
  43 + string output = "";
  44 +
  45 + s = createSocket();
  46 + connect(s,port,hostname,timeout);
  47 + while(output != "ENTER PASSWORD:")
  48 + {
  49 + recvMsg(s,buffer,MAX_TCP);
  50 + output += buffer;
  51 + memset(buffer,0x00,MAX_TCP+1);
  52 + }
  53 + sendMsg(s,strcat(password,"\n"),strlen(password)+1);
  54 + output = "";
  55 + do
  56 + {
  57 + bytes = recvMsg(s,buffer,MAX_TCP);
  58 + output += buffer;
  59 + memset(buffer,0x00,MAX_TCP+1);
  60 + }while(bytes);
  61 + if(output == "ENTER PASSWORD:")
  62 + {
  63 + *serverinfo = "Incorrect password";
  64 + close(s);
  65 + return 3;
  66 + }
  67 + output = "";
  68 + sendMsg(s,(char*)"status 2\n",9);
  69 + do
  70 + {
  71 + bytes = recvMsg(s,buffer,MAX_TCP);
  72 + output += buffer;
  73 + memset(buffer,0x00,MAX_TCP+1);
  74 + }while(output.rfind("END") != output.size()-5);
  75 + vector<string> splitstr;
  76 + vector<string> fields;
  77 + split(output,"\n",splitstr);
  78 + for(vector<string>::iterator it = splitstr.begin() ; it != splitstr.end(); ++it)
  79 + {
  80 + if(it->substr(0,11) == "CLIENT_LIST")
  81 + {
  82 + split(*it,",",fields);
  83 + ipList += fields[3] + ",";
  84 + numClients++;
  85 + }
  86 + }
  87 + if(numClients)
  88 + {
  89 + ipList.pop_back();
  90 + }
  91 + *serverinfo = int2str(numClients) + " clients connected (" + ipList + ")" ;
  92 + close(s);
  93 + return 0;
  94 +}
  95 +
  96 +int main(int argc, char **argv)
  97 +{
  98 + struct itimerval timer;
  99 + timer.it_value.tv_sec = 10;
  100 + timer.it_value.tv_usec = 0;
  101 + timer.it_interval.tv_sec = 0;
  102 + timer.it_interval.tv_usec = 0;
  103 + setitimer (ITIMER_VIRTUAL, &timer, 0);
  104 +
  105 + struct sigaction sa;
  106 + memset (&sa, 0, sizeof (sa));
  107 + sa.sa_handler = &timer_handler;
  108 + sigaction (SIGVTALRM, &sa, 0);
  109 +
  110 + uint16_t port = 1195;
  111 + char *hostname = NULL;
  112 + char *password = NULL;
  113 + int c;
  114 +
  115 + while ((c = getopt (argc, argv, "H:p:P:Vh")) != -1)
  116 + {
  117 + switch(c)
  118 + {
  119 + case 'H':
  120 + hostname = optarg;
  121 + break;
  122 + case 'p':
  123 + port = (uint16_t) str2int(optarg);
  124 + break;
  125 + case 'P':
  126 + password = optarg;
  127 + break;
  128 + case 'V':
  129 + printVersion();
  130 + return 0;
  131 + case 'h':
  132 + printHelp(true);
  133 + return 0;
  134 + case '?':
  135 + printHelp(false);
  136 + return 3;
  137 + }
  138 + }
  139 +
  140 + if(hostname == NULL)
  141 + {
  142 + cout << "No HOSTADDRESS specified. Exiting." << endl;
  143 + return 3;
  144 + }
  145 +
  146 + if(password == NULL)
  147 + {
  148 + cout << "No PASSWORD specified. Exiting." << endl;
  149 + return 3;
  150 + }
  151 +
  152 + string serverinfo = "";
  153 + int returnCode = check_openvpn(port,hostname,password,&serverinfo);
  154 +
  155 + cout << servicename;
  156 + switch(returnCode)
  157 + {
  158 + case 0:
  159 + cout << " OK - " << serverinfo << endl ;
  160 + break;
  161 +
  162 + case 2:
  163 + cout << " CRITICAL - No response" << endl;
  164 + break;
  165 +
  166 + case 3:
  167 + cout << " UNKNOWN - " << serverinfo << endl;
  168 + }
  169 +
  170 + return returnCode;
  171 +}
... ...
check_openvpn/check_openvpn.h 0 → 100755
  1 +#ifndef CHECK_OPENVPN_H
  2 +#define CHECK_OPENVPN_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 <sys/time.h>
  14 +
  15 +#include "auxiliar.h"
  16 +#include "tcp.h"
  17 +
  18 +#define VERSION "1.0"
  19 +
  20 +int check_openvpn(char *hostname, string *serverinfo);
  21 +void printVersion();
  22 +void printHelp(bool longVersion);
  23 +
  24 +#endif
... ...
check_openvpn/tcp.cpp 0 → 100644
  1 +#include "tcp.h"
  2 +
  3 +int createSocket()
  4 +{
  5 + int sockfd;
  6 + if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  7 + {
  8 + cout << "Could not create socket" << endl;
  9 + exit(3);
  10 + }
  11 + return sockfd;
  12 +}
  13 +
  14 +void connect(int s, uint16_t port, char *hostname, int timeout)
  15 +{
  16 + struct in_addr *addr_ptr;
  17 + struct hostent *hostPtr;
  18 + string add;
  19 + hostPtr = gethostbyname(hostname);
  20 + if(hostPtr == NULL)
  21 + {
  22 + cout << "Could not resolve hostname" << endl;
  23 + exit(3);
  24 + }
  25 + addr_ptr = (struct in_addr *)*hostPtr->h_addr_list;
  26 + add = inet_ntoa(*addr_ptr);
  27 + if(add == "")
  28 + {
  29 + cout << "Invalid address" << endl;
  30 + exit(3);
  31 + }
  32 + struct sockaddr_in newSockAddr;
  33 + newSockAddr.sin_family = AF_INET;
  34 + newSockAddr.sin_port = htons(port);
  35 + newSockAddr.sin_addr.s_addr = inet_addr(add.c_str());
  36 + if(connect(s, (struct sockaddr *)&newSockAddr, sizeof(struct sockaddr)) != 0)
  37 + {
  38 + cout << "Could not connect to " << hostname << " on port " << port << endl;
  39 + exit(3);
  40 + }
  41 +
  42 + struct timeval tv;
  43 + tv.tv_sec = timeout;
  44 + tv.tv_usec = 0;
  45 + if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0)
  46 + {
  47 + cout << "Error setting socket timeout" << endl;
  48 + exit(3);
  49 + }
  50 + fcntl(s, F_SETFL, O_NONBLOCK);
  51 +}
  52 +
  53 +int sendMsg(int s, const char *msg, size_t msgLength)
  54 +{
  55 + int bytes, total = 0;
  56 + while(total != msgLength)
  57 + {
  58 + bytes = send(s,msg+total,msgLength-total,0);
  59 + if(bytes == -1)
  60 + {
  61 + cout << "TCP: Could not write to socket." << endl;
  62 + exit(3);
  63 + }
  64 + total += bytes;
  65 + }
  66 + return total;
  67 +}
  68 +
  69 +int recvMsg(int s, char *msg, size_t msgLength)
  70 +{
  71 + int bytes, total = 0;
  72 + while(total != msgLength)
  73 + {
  74 + bytes = recv(s, msg+total, msgLength-total,0);
  75 + if ( bytes <= 0 )
  76 + {
  77 + if(errno == EWOULDBLOCK || errno == EAGAIN)
  78 + {
  79 + return 0;
  80 + }
  81 + cout << "TCP: Could not read from socket." << endl;
  82 + exit(3);
  83 + }
  84 + total += bytes;
  85 + }
  86 + return total;
  87 +}
... ...
check_openvpn/tcp.h 0 → 100644
  1 +#ifndef TCP_H
  2 +#define TCP_H
  3 +
  4 +#define MAX_TCP 65535
  5 +
  6 +#include <iostream>
  7 +
  8 +#include <string.h>
  9 +#include <cstdlib>
  10 +
  11 +#include <errno.h>
  12 +#include <arpa/inet.h>
  13 +#include <sys/socket.h>
  14 +#include <netdb.h>
  15 +#include <fcntl.h>
  16 +using namespace std;
  17 +
  18 +int createSocket();
  19 +void connect(int s, uint16_t port, char *hostname, int timeout);
  20 +int sendMsg(int s, const char *msg, size_t msgLength);
  21 +int recvMsg(int s, char *msg, size_t msgLength);
  22 +
  23 +#endif
... ...