Commit a0960c84cabc3f2b648ba113c029d792d8441088
1 parent
85d49520
Developed plugin for upnp
Showing
8 changed files
with
346 additions
and
2 deletions
CMakeLists.txt
... | ... | @@ -6,8 +6,8 @@ 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}) | |
9 | +set(SOURCE_FILES_UPNP check_upnp/check_upnp.cpp check_upnp/auxiliar.cpp check_upnp/udp.cpp) | |
10 | +add_executable(check_upnp ${SOURCE_FILES_UPNP}) | |
11 | 11 | |
12 | 12 | set(SOURCE_FILES_MEMFREE check_memfree/check_memfree.cpp check_memfree/range.cpp check_memfree/auxiliar.cpp) |
13 | 13 | add_executable(check_memfree ${SOURCE_FILES_MEMFREE}) | ... | ... |
check_upnp/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_upnp/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_upnp/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_upnp/check_upnp.cpp
0 → 100755
1 | +#include "check_upnp.h" | |
2 | + | |
3 | +using namespace std; | |
4 | + | |
5 | +char *servicename = (char*)"UPnP"; | |
6 | + | |
7 | +void printVersion() | |
8 | +{ | |
9 | + cout << "check_upnp 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_upnp(char *hostname, string *serverinfo) | |
34 | +{ | |
35 | + char buffer[MAX_UDP+1]; | |
36 | + struct sockaddr_in si; | |
37 | + struct hostent *host = NULL; | |
38 | + int s; | |
39 | + int timeout = 10; | |
40 | + struct hostent *he; | |
41 | + struct in_addr **addr_list; | |
42 | + | |
43 | + he = gethostbyname(hostname); | |
44 | + if(he == NULL) | |
45 | + { | |
46 | + *serverinfo = "Can't resolve " + string(hostname); | |
47 | + return 3; | |
48 | + } | |
49 | + addr_list = (struct in_addr **) he->h_addr_list; | |
50 | + char *targetIP = inet_ntoa(*addr_list[0]); | |
51 | + | |
52 | + setupSocket(1900,(char*)"239.255.255.250",host,timeout,&si,&s); | |
53 | + memset(buffer,0x00,MAX_UDP+1); | |
54 | + char *discover = (char*) "M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: urn:schemas-upnp-org:device:MediaServer:1\r\n\r\n"; | |
55 | + sendMsg(s,discover,strlen(discover),&si); | |
56 | + while(true) | |
57 | + { | |
58 | + recvMsg(s, buffer, MAX_UDP, &si); | |
59 | + char *sourceIP = inet_ntoa(si.sin_addr); | |
60 | + if(!strcmp(sourceIP,targetIP)) | |
61 | + { | |
62 | + stringstream ss(buffer); | |
63 | + string line; | |
64 | + while(getline(ss,line,'\n')) | |
65 | + { | |
66 | + if(line.length() > strlen("SERVER:")) | |
67 | + { | |
68 | + if (line.substr(0,strlen("SERVER:")) == "SERVER:") | |
69 | + { | |
70 | + *serverinfo = line.substr(0,line.size() - 1); | |
71 | + } | |
72 | + } | |
73 | + } | |
74 | + return 0; | |
75 | + } | |
76 | + | |
77 | + } | |
78 | +} | |
79 | + | |
80 | +int main(int argc, char **argv) | |
81 | +{ | |
82 | + struct itimerval timer; | |
83 | + timer.it_value.tv_sec = 10; | |
84 | + timer.it_value.tv_usec = 0; | |
85 | + timer.it_interval.tv_sec = 0; | |
86 | + timer.it_interval.tv_usec = 0; | |
87 | + setitimer (ITIMER_VIRTUAL, &timer, 0); | |
88 | + | |
89 | + struct sigaction sa; | |
90 | + memset (&sa, 0, sizeof (sa)); | |
91 | + sa.sa_handler = &timer_handler; | |
92 | + sigaction (SIGVTALRM, &sa, 0); | |
93 | + | |
94 | + uint16_t port = 27015; | |
95 | + char *hostname = NULL; | |
96 | + int c; | |
97 | + | |
98 | + while ((c = getopt (argc, argv, "H:p:Vh")) != -1) | |
99 | + { | |
100 | + switch(c) | |
101 | + { | |
102 | + case 'H': | |
103 | + hostname = optarg; | |
104 | + break; | |
105 | + case 'p': | |
106 | + port = (uint16_t) str2int(optarg); | |
107 | + break; | |
108 | + case 'V': | |
109 | + printVersion(); | |
110 | + return 0; | |
111 | + case 'h': | |
112 | + printHelp(true); | |
113 | + return 0; | |
114 | + case '?': | |
115 | + printHelp(false); | |
116 | + return 3; | |
117 | + } | |
118 | + } | |
119 | + | |
120 | + if(hostname == NULL) | |
121 | + { | |
122 | + cout << "No HOSTADDRESS specified. Exiting." << endl; | |
123 | + return 3; | |
124 | + } | |
125 | + | |
126 | + string serverinfo = ""; | |
127 | + int returnCode = check_upnp(hostname,&serverinfo); | |
128 | + | |
129 | + cout << servicename; | |
130 | + switch(returnCode) | |
131 | + { | |
132 | + case 0: | |
133 | + cout << " OK - " << serverinfo << endl ; | |
134 | + break; | |
135 | + | |
136 | + case 2: | |
137 | + cout << " CRITICAL - No response"; | |
138 | + break; | |
139 | + | |
140 | + case 3: | |
141 | + cout << " UNKNOWN - " << serverinfo << endl; | |
142 | + } | |
143 | + | |
144 | + return returnCode; | |
145 | +} | ... | ... |
check_upnp/check_upnp.h
0 → 100755
1 | +#ifndef CHECK_UPNP_H | |
2 | +#define CHECK_UPNP_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_upnp(char *hostname, string *serverinfo); | |
22 | +void printVersion(); | |
23 | +void printHelp(bool longVersion); | |
24 | + | |
25 | +#endif | ... | ... |
check_upnp/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_upnp/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 | +#define MAX_UDP 65507 | |
15 | + | |
16 | +using namespace std; | |
17 | + | |
18 | +void setupSocket(uint16_t port, char *hostname, struct hostent *host, int timeout, struct sockaddr_in *si, int *s); | |
19 | +int sendMsg(int s, char *msg, size_t msgLength, struct sockaddr_in *si); | |
20 | +int recvMsg(int s, char *msg, size_t msgLength, struct sockaddr_in *si); | |
21 | + | |
22 | +#endif | ... | ... |