Commit 656921a2517be73d7cb8ad8797aaefc8e5769ec3
1 parent
f2154f92
Plugin functional
Showing
5 changed files
with
129 additions
and
16 deletions
CMakeLists.txt
@@ -3,7 +3,7 @@ project(nagios_plugins) | @@ -3,7 +3,7 @@ project(nagios_plugins) | ||
3 | 3 | ||
4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -U__STRICT_ANSI__") | 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) | 6 | +set(SOURCE_FILES_CSGO check_csgo/check_csgo.cpp check_csgo/auxiliar.cpp check_csgo/udp.cpp) |
7 | add_executable(check_csgo ${SOURCE_FILES_CSGO}) | 7 | add_executable(check_csgo ${SOURCE_FILES_CSGO}) |
8 | 8 | ||
9 | set(SOURCE_FILES_MEMFREE check_memfree/check_memfree.cpp check_memfree/range.cpp check_memfree/auxiliar.cpp) | 9 | set(SOURCE_FILES_MEMFREE check_memfree/check_memfree.cpp check_memfree/range.cpp check_memfree/auxiliar.cpp) |
check_csgo/check_csgo.cpp
@@ -14,7 +14,7 @@ void printHelp(bool longVersion) | @@ -14,7 +14,7 @@ void printHelp(bool longVersion) | ||
14 | if(longVersion) | 14 | if(longVersion) |
15 | { | 15 | { |
16 | printVersion(); | 16 | printVersion(); |
17 | - cout << "Check Source DS instance." << endl << endl; | 17 | + cout << "Check CS:GO DS instance." << endl << endl; |
18 | printHelp(false); | 18 | printHelp(false); |
19 | cout << "Options:" << endl; | 19 | cout << "Options:" << endl; |
20 | cout << " -h" << endl; | 20 | cout << " -h" << endl; |
@@ -30,8 +30,59 @@ void printHelp(bool longVersion) | @@ -30,8 +30,59 @@ void printHelp(bool longVersion) | ||
30 | cout << "Usage: " << endl << "check_csgo [-hV] -H HOSTADDRESS [-p PORT]" << endl << endl; | 30 | cout << "Usage: " << endl << "check_csgo [-hV] -H HOSTADDRESS [-p PORT]" << endl << endl; |
31 | } | 31 | } |
32 | 32 | ||
33 | +SOURCEDS_PACKET* getSourceDSResponse(int s, sockaddr_in *server) | ||
34 | +{ | ||
35 | + char *data = new char[DATAGRAM_LENGTH]; | ||
36 | + int dataLength = recvMsg(s, data, DATAGRAM_LENGTH, server); | ||
37 | + if(dataLength == -1) | ||
38 | + { | ||
39 | + return NULL; | ||
40 | + } | ||
41 | + char *header = (char*) "\xFF\xFF\xFF\xFF"; | ||
42 | + if(memcmp(data,header,HDR_SIZE)) | ||
43 | + { | ||
44 | + //Invalid response, or non-supported multi-packet response format | ||
45 | + return NULL; | ||
46 | + } | ||
47 | + SOURCEDS_PACKET *response = new SOURCEDS_PACKET; | ||
48 | + response->data = data+HDR_SIZE; | ||
49 | + response->length = dataLength; | ||
50 | + return response; | ||
51 | +} | ||
52 | + | ||
33 | int check_csgo(char *hostname, uint16_t port, SERVERINFO *server_info) | 53 | int check_csgo(char *hostname, uint16_t port, SERVERINFO *server_info) |
34 | { | 54 | { |
55 | + struct sockaddr_in si; | ||
56 | + struct hostent *host = NULL; | ||
57 | + int s; | ||
58 | + int timeout = 10; | ||
59 | + setupSocket(port,hostname,host,timeout,&si,&s); | ||
60 | + string discard; | ||
61 | + | ||
62 | + char *info_query = (char*) "\xFF\xFF\xFF\xFF\x54Source Engine Query\x00"; | ||
63 | + | ||
64 | + sendMsg(s,info_query, strlen(info_query)+1,&si); | ||
65 | + SOURCEDS_PACKET *response = getSourceDSResponse(s,&si); | ||
66 | + if(response == NULL) | ||
67 | + { | ||
68 | + return 2; | ||
69 | + } | ||
70 | + | ||
71 | + ssize_t offset = 2; | ||
72 | + server_info->name = string(response->data+offset,strlen(response->data+offset)); | ||
73 | + offset += server_info->name.size()+1; | ||
74 | + server_info->map = string(response->data+offset,strlen(response->data+offset)); | ||
75 | + offset += server_info->map.size()+1; | ||
76 | + discard = string(response->data+offset,strlen(response->data+offset)); | ||
77 | + offset += discard.size() + 1; | ||
78 | + server_info->game = string(response->data+offset,strlen(response->data+offset)); | ||
79 | + offset += server_info->game.size()+1; | ||
80 | + offset += 2; | ||
81 | + server_info->players = (uint8_t) *((response->data)+offset); | ||
82 | + | ||
83 | + delete[] (response->data-HDR_SIZE); | ||
84 | + delete response; | ||
85 | + | ||
35 | return 0; | 86 | return 0; |
36 | } | 87 | } |
37 | 88 | ||
@@ -61,7 +112,7 @@ int main(int argc, char **argv) | @@ -61,7 +112,7 @@ int main(int argc, char **argv) | ||
61 | hostname = optarg; | 112 | hostname = optarg; |
62 | break; | 113 | break; |
63 | case 'p': | 114 | case 'p': |
64 | - port = str2int(optarg); | 115 | + port = (uint16_t) str2int(optarg); |
65 | break; | 116 | break; |
66 | case 'V': | 117 | case 'V': |
67 | printVersion(); | 118 | printVersion(); |
@@ -89,17 +140,14 @@ int main(int argc, char **argv) | @@ -89,17 +140,14 @@ int main(int argc, char **argv) | ||
89 | { | 140 | { |
90 | case 0: | 141 | case 0: |
91 | cout << " OK"; | 142 | cout << " OK"; |
92 | - break; | ||
93 | - | ||
94 | - case 1: | ||
95 | - cout << " WARNING"; | 143 | + cout << " - " << hostname << " " << server_info.name << " " << server_info.game << " " << server_info.map << " " << server_info.players << endl; |
96 | break; | 144 | break; |
97 | 145 | ||
98 | case 2: | 146 | case 2: |
99 | - cout << " CRITICAL"; | 147 | + cout << " CRITICAL - No response"; |
100 | break; | 148 | break; |
101 | } | 149 | } |
102 | 150 | ||
103 | - cout << " - " << server_info.name << " " << server_info.ip << " " << server_info.name << " " << server_info.map << " " << server_info.players << " " << server_info.latency << endl; | 151 | + |
104 | return returnCode; | 152 | return returnCode; |
105 | } | 153 | } |
check_csgo/check_csgo.h
@@ -14,21 +14,27 @@ | @@ -14,21 +14,27 @@ | ||
14 | #include <sys/time.h> | 14 | #include <sys/time.h> |
15 | 15 | ||
16 | #include "auxiliar.h" | 16 | #include "auxiliar.h" |
17 | +#include "udp.h" | ||
17 | 18 | ||
18 | #define VERSION "1.0" | 19 | #define VERSION "1.0" |
20 | +#define DATAGRAM_LENGTH 1400 | ||
21 | +#define HDR_SIZE 4 | ||
19 | 22 | ||
23 | +struct sourcedspacket_struct { | ||
24 | + ssize_t length; | ||
25 | + char* data; | ||
26 | +}; | ||
27 | +typedef struct sourcedspacket_struct SOURCEDS_PACKET; | ||
20 | 28 | ||
21 | struct serverinfo_struct { | 29 | struct serverinfo_struct { |
22 | - char *name; | ||
23 | - char *ip; | ||
24 | - char *game; | ||
25 | - char *map; | ||
26 | - unsigned int players; | ||
27 | - unsigned int latency; | 30 | + string name; |
31 | + string game; | ||
32 | + string map; | ||
33 | + uint8_t players; | ||
28 | }; | 34 | }; |
29 | - | ||
30 | typedef struct serverinfo_struct SERVERINFO; | 35 | typedef struct serverinfo_struct SERVERINFO; |
31 | 36 | ||
37 | +SOURCEDS_PACKET* getSourceDSResponse(int s, sockaddr_in *server); | ||
32 | int check_csgo(char *hostname, uint16_t port, SERVERINFO *server_info); | 38 | int check_csgo(char *hostname, uint16_t port, SERVERINFO *server_info); |
33 | void printVersion(); | 39 | void printVersion(); |
34 | void printHelp(bool longVersion); | 40 | void printHelp(bool longVersion); |
check_csgo/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_csgo/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 |