summaryrefslogtreecommitdiffstats
path: root/plugins/check_game.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_game.c')
-rw-r--r--plugins/check_game.c592
1 files changed, 287 insertions, 305 deletions
diff --git a/plugins/check_game.c b/plugins/check_game.c
index ca126973..c0193b03 100644
--- a/plugins/check_game.c
+++ b/plugins/check_game.c
@@ -1,335 +1,317 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_game plugin 3 * Monitoring check_game plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2002-2007 Monitoring Plugins Development Team 6 * Copyright (c) 2002-2024 Monitoring Plugins Development Team
7* 7 *
8* Description: 8 * Description:
9* 9 *
10* This file contains the check_game plugin 10 * This file contains the check_game plugin
11* 11 *
12* This plugin tests game server connections with the specified host. 12 * This plugin tests game server connections with the specified host.
13* using the qstat program 13 * using the qstat program
14* 14 *
15* 15 *
16* This program is free software: you can redistribute it and/or modify 16 * This program is free software: you can redistribute it and/or modify
17* it under the terms of the GNU General Public License as published by 17 * it under the terms of the GNU General Public License as published by
18* the Free Software Foundation, either version 3 of the License, or 18 * the Free Software Foundation, either version 3 of the License, or
19* (at your option) any later version. 19 * (at your option) any later version.
20* 20 *
21* This program is distributed in the hope that it will be useful, 21 * This program is distributed in the hope that it will be useful,
22* but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24* GNU General Public License for more details. 24 * GNU General Public License for more details.
25* 25 *
26* You should have received a copy of the GNU General Public License 26 * You should have received a copy of the GNU General Public License
27* along with this program. If not, see <http://www.gnu.org/licenses/>. 27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28* 28 *
29* 29 *
30*****************************************************************************/ 30 *****************************************************************************/
31 31
32const char *progname = "check_game"; 32const char *progname = "check_game";
33const char *copyright = "2002-2007"; 33const char *copyright = "2002-2024";
34const char *email = "devel@monitoring-plugins.org"; 34const char *email = "devel@monitoring-plugins.org";
35 35
36#include "common.h" 36#include "common.h"
37#include "utils.h" 37#include "utils.h"
38#include "runcmd.h" 38#include "runcmd.h"
39#include "check_game.d/config.h"
40#include "../lib/monitoringplug.h"
39 41
40int process_arguments (int, char **); 42typedef struct {
41int validate_arguments (void); 43 int errorcode;
42void print_help (void); 44 check_game_config config;
43void print_usage (void); 45} check_game_config_wrapper;
44 46
45#define QSTAT_DATA_DELIMITER "," 47static check_game_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
48static void print_help(void);
49void print_usage(void);
46 50
47#define QSTAT_HOST_ERROR "ERROR" 51#define QSTAT_DATA_DELIMITER ","
48#define QSTAT_HOST_DOWN "DOWN"
49#define QSTAT_HOST_TIMEOUT "TIMEOUT"
50#define QSTAT_MAX_RETURN_ARGS 12
51
52char *server_ip;
53char *game_type;
54int port = 0;
55
56bool verbose = false;
57
58int qstat_game_players_max = -1;
59int qstat_game_players = -1;
60int qstat_game_field = -1;
61int qstat_map_field = -1;
62int qstat_ping_field = -1;
63
64
65int
66main (int argc, char **argv)
67{
68 char *command_line;
69 int result = STATE_UNKNOWN;
70 char *p, *ret[QSTAT_MAX_RETURN_ARGS];
71 size_t i = 0;
72 output chld_out;
73
74 setlocale (LC_ALL, "");
75 bindtextdomain (PACKAGE, LOCALEDIR);
76 textdomain (PACKAGE);
77
78 /* Parse extra opts if any */
79 argv=np_extra_opts (&argc, argv, progname);
80
81 if (process_arguments (argc, argv) == ERROR)
82 usage_va(_("Could not parse arguments"));
83
84 result = STATE_OK;
85
86 /* create the command line to execute */
87 xasprintf (&command_line, "%s -raw %s -%s %s",
88 PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip);
89
90 if (port)
91 xasprintf (&command_line, "%s:%-d", command_line, port);
92
93 if (verbose)
94 printf ("%s\n", command_line);
95
96 /* run the command. historically, this plugin ignores output on stderr,
97 * as well as return status of the qstat program */
98 (void)np_runcmd(command_line, &chld_out, NULL, 0);
99
100 /* sanity check */
101 /* was thinking about running qstat without any options, capturing the
102 -default line, parsing it & making an array of all know server types
103 but thought this would be too much hassle considering this is a tool
104 for intelligent sysadmins (ha). Could put a static array of known
105 server types in a header file but then we'd be limiting ourselves
106
107 In the end, I figured I'd simply let an error occur & then trap it
108 */
109
110 if (!strncmp (chld_out.line[0], "unknown option", 14)) {
111 printf (_("CRITICAL - Host type parameter incorrect!\n"));
112 result = STATE_CRITICAL;
113 return result;
114 }
115
116 p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER);
117 while (p != NULL) {
118 ret[i] = p;
119 p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER);
120 i++;
121 if (i >= QSTAT_MAX_RETURN_ARGS)
122 break;
123 }
124
125 if (strstr (ret[2], QSTAT_HOST_ERROR)) {
126 printf (_("CRITICAL - Host not found\n"));
127 result = STATE_CRITICAL;
128 }
129 else if (strstr (ret[2], QSTAT_HOST_DOWN)) {
130 printf (_("CRITICAL - Game server down or unavailable\n"));
131 result = STATE_CRITICAL;
132 }
133 else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) {
134 printf (_("CRITICAL - Game server timeout\n"));
135 result = STATE_CRITICAL;
136 }
137 else {
138 printf ("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n",
139 ret[qstat_game_players],
140 ret[qstat_game_players_max],
141 ret[qstat_game_field],
142 ret[qstat_map_field],
143 ret[qstat_ping_field],
144 perfdata ("players", atol(ret[qstat_game_players]), "",
145 false, 0, false, 0,
146 true, 0, true, atol(ret[qstat_game_players_max])),
147 fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "",
148 false, 0, false, 0,
149 true, 0, false, 0));
150 }
151
152 return result;
153}
154 52
53#define QSTAT_HOST_ERROR "ERROR"
54#define QSTAT_HOST_DOWN "DOWN"
55#define QSTAT_HOST_TIMEOUT "TIMEOUT"
56#define QSTAT_MAX_RETURN_ARGS 12
155 57
156int 58static bool verbose = false;
157process_arguments (int argc, char **argv) 59
158{ 60int main(int argc, char **argv) {
159 int c; 61 setlocale(LC_ALL, "");
160 62 bindtextdomain(PACKAGE, LOCALEDIR);
161 int opt_index = 0; 63 textdomain(PACKAGE);
162 static struct option long_opts[] = { 64
163 {"help", no_argument, 0, 'h'}, 65 /* Parse extra opts if any */
164 {"version", no_argument, 0, 'V'}, 66 argv = np_extra_opts(&argc, argv, progname);
165 {"verbose", no_argument, 0, 'v'}, 67
166 {"timeout", required_argument, 0, 't'}, 68 check_game_config_wrapper tmp = process_arguments(argc, argv);
167 {"hostname", required_argument, 0, 'H'}, 69
168 {"port", required_argument, 0, 'P'}, 70 if (tmp.errorcode == ERROR) {
169 {"game-type", required_argument, 0, 'G'}, 71 usage_va(_("Could not parse arguments"));
170 {"map-field", required_argument, 0, 'm'}, 72 }
171 {"ping-field", required_argument, 0, 'p'}, 73
172 {"game-field", required_argument, 0, 'g'}, 74 check_game_config config = tmp.config;
173 {"players-field", required_argument, 0, 129}, 75
174 {"max-players-field", required_argument, 0, 130}, 76 mp_state_enum result = STATE_OK;
175 {0, 0, 0, 0} 77
176 }; 78 /* create the command line to execute */
177 79 char *command_line = NULL;
178 if (argc < 2) 80 xasprintf(&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, config.game_type, config.server_ip);
179 return ERROR; 81
180 82 if (config.port) {
181 for (c = 1; c < argc; c++) { 83 xasprintf(&command_line, "%s:%-d", command_line, config.port);
182 if (strcmp ("-mf", argv[c]) == 0) 84 }
183 strcpy (argv[c], "-m"); 85
184 else if (strcmp ("-pf", argv[c]) == 0) 86 if (verbose) {
185 strcpy (argv[c], "-p"); 87 printf("%s\n", command_line);
186 else if (strcmp ("-gf", argv[c]) == 0) 88 }
187 strcpy (argv[c], "-g"); 89
188 } 90 /* run the command. historically, this plugin ignores output on stderr,
189 91 * as well as return status of the qstat program */
190 while (1) { 92 output chld_out = {};
191 c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); 93 (void)np_runcmd(command_line, &chld_out, NULL, 0);
192 94
193 if (c == -1 || c == EOF) 95 /* sanity check */
194 break; 96 /* was thinking about running qstat without any options, capturing the
195 97 -default line, parsing it & making an array of all know server types
196 switch (c) { 98 but thought this would be too much hassle considering this is a tool
197 case 'h': /* help */ 99 for intelligent sysadmins (ha). Could put a static array of known
198 print_help (); 100 server types in a header file but then we'd be limiting ourselves
199 exit (STATE_UNKNOWN); 101
200 case 'V': /* version */ 102 In the end, I figured I'd simply let an error occur & then trap it
201 print_revision (progname, NP_VERSION); 103 */
202 exit (STATE_UNKNOWN); 104
203 case 'v': /* version */ 105 if (!strncmp(chld_out.line[0], "unknown option", strlen("unknown option"))) {
204 verbose = true; 106 printf(_("CRITICAL - Host type parameter incorrect!\n"));
205 break; 107 result = STATE_CRITICAL;
206 case 't': /* timeout period */ 108 exit(result);
207 timeout_interval = atoi (optarg); 109 }
208 break; 110
209 case 'H': /* hostname */ 111 char *ret[QSTAT_MAX_RETURN_ARGS];
210 if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH) 112 size_t i = 0;
211 die (STATE_UNKNOWN, _("Input buffer overflow\n")); 113 char *sequence = strtok(chld_out.line[0], QSTAT_DATA_DELIMITER);
212 server_ip = optarg; 114 while (sequence != NULL) {
213 break; 115 ret[i] = sequence;
214 case 'P': /* port */ 116 sequence = strtok(NULL, QSTAT_DATA_DELIMITER);
215 port = atoi (optarg); 117 i++;
216 break; 118 if (i >= QSTAT_MAX_RETURN_ARGS) {
217 case 'G': /* hostname */ 119 break;
218 if (strlen (optarg) >= MAX_INPUT_BUFFER) 120 }
219 die (STATE_UNKNOWN, _("Input buffer overflow\n")); 121 }
220 game_type = optarg; 122
221 break; 123 if (strstr(ret[2], QSTAT_HOST_ERROR)) {
222 case 'p': /* index of ping field */ 124 printf(_("CRITICAL - Host not found\n"));
223 qstat_ping_field = atoi (optarg); 125 result = STATE_CRITICAL;
224 if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS) 126 } else if (strstr(ret[2], QSTAT_HOST_DOWN)) {
225 return ERROR; 127 printf(_("CRITICAL - Game server down or unavailable\n"));
226 break; 128 result = STATE_CRITICAL;
227 case 'm': /* index on map field */ 129 } else if (strstr(ret[2], QSTAT_HOST_TIMEOUT)) {
228 qstat_map_field = atoi (optarg); 130 printf(_("CRITICAL - Game server timeout\n"));
229 if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS) 131 result = STATE_CRITICAL;
230 return ERROR; 132 } else {
231 break; 133 printf("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[config.qstat_game_players], ret[config.qstat_game_players_max],
232 case 'g': /* index of game field */ 134 ret[config.qstat_game_field], ret[config.qstat_map_field], ret[config.qstat_ping_field],
233 qstat_game_field = atoi (optarg); 135 perfdata("players", atol(ret[config.qstat_game_players]), "", false, 0, false, 0, true, 0, true,
234 if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS) 136 atol(ret[config.qstat_game_players_max])),
235 return ERROR; 137 fperfdata("ping", strtod(ret[config.qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0));
236 break; 138 }
237 case 129: /* index of player count field */ 139
238 qstat_game_players = atoi (optarg); 140 exit(result);
239 if (qstat_game_players_max == 0)
240 qstat_game_players_max = qstat_game_players - 1;
241 if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS)
242 return ERROR;
243 break;
244 case 130: /* index of max players field */
245 qstat_game_players_max = atoi (optarg);
246 if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS)
247 return ERROR;
248 break;
249 default: /* args not parsable */
250 usage5();
251 }
252 }
253
254 c = optind;
255 /* first option is the game type */
256 if (!game_type && c<argc)
257 game_type = strdup (argv[c++]);
258
259 /* Second option is the server name */
260 if (!server_ip && c<argc)
261 server_ip = strdup (argv[c++]);
262
263 return validate_arguments ();
264} 141}
265 142
266 143#define players_field_index 129
267int 144#define max_players_field_index 130
268validate_arguments (void) 145
269{ 146check_game_config_wrapper process_arguments(int argc, char **argv) {
270 if (qstat_game_players_max < 0) 147 static struct option long_opts[] = {{"help", no_argument, 0, 'h'},
271 qstat_game_players_max = 4; 148 {"version", no_argument, 0, 'V'},
272 149 {"verbose", no_argument, 0, 'v'},
273 if (qstat_game_players < 0) 150 {"timeout", required_argument, 0, 't'},
274 qstat_game_players = 5; 151 {"hostname", required_argument, 0, 'H'},
275 152 {"port", required_argument, 0, 'P'},
276 if (qstat_game_field < 0) 153 {"game-type", required_argument, 0, 'G'},
277 qstat_game_field = 2; 154 {"map-field", required_argument, 0, 'm'},
278 155 {"ping-field", required_argument, 0, 'p'},
279 if (qstat_map_field < 0) 156 {"game-field", required_argument, 0, 'g'},
280 qstat_map_field = 3; 157 {"players-field", required_argument, 0, players_field_index},
281 158 {"max-players-field", required_argument, 0, max_players_field_index},
282 if (qstat_ping_field < 0) 159 {0, 0, 0, 0}};
283 qstat_ping_field = 5; 160
284 161 check_game_config_wrapper result = {
285 return OK; 162 .config = check_game_config_init(),
163 .errorcode = OK,
164 };
165
166 if (argc < 2) {
167 result.errorcode = ERROR;
168 return result;
169 }
170
171 for (int option_counter = 1; option_counter < argc; option_counter++) {
172 if (strcmp("-mf", argv[option_counter]) == 0) {
173 strcpy(argv[option_counter], "-m");
174 } else if (strcmp("-pf", argv[option_counter]) == 0) {
175 strcpy(argv[option_counter], "-p");
176 } else if (strcmp("-gf", argv[option_counter]) == 0) {
177 strcpy(argv[option_counter], "-g");
178 }
179 }
180
181 int opt_index = 0;
182 while (true) {
183 int option_index = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
184
185 if (option_index == -1 || option_index == EOF) {
186 break;
187 }
188
189 switch (option_index) {
190 case 'h': /* help */
191 print_help();
192 exit(STATE_UNKNOWN);
193 case 'V': /* version */
194 print_revision(progname, NP_VERSION);
195 exit(STATE_UNKNOWN);
196 case 'v': /* version */
197 verbose = true;
198 break;
199 case 't': /* timeout period */
200 timeout_interval = atoi(optarg);
201 break;
202 case 'H': /* hostname */
203 if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) {
204 die(STATE_UNKNOWN, _("Input buffer overflow\n"));
205 }
206 result.config.server_ip = optarg;
207 break;
208 case 'P': /* port */
209 result.config.port = atoi(optarg);
210 break;
211 case 'G': /* hostname */
212 if (strlen(optarg) >= MAX_INPUT_BUFFER) {
213 die(STATE_UNKNOWN, _("Input buffer overflow\n"));
214 }
215 result.config.game_type = optarg;
216 break;
217 case 'p': /* index of ping field */
218 result.config.qstat_ping_field = atoi(optarg);
219 if (result.config.qstat_ping_field < 0 || result.config.qstat_ping_field > QSTAT_MAX_RETURN_ARGS) {
220 result.errorcode = ERROR;
221 return result;
222 }
223 break;
224 case 'm': /* index on map field */
225 result.config.qstat_map_field = atoi(optarg);
226 if (result.config.qstat_map_field < 0 || result.config.qstat_map_field > QSTAT_MAX_RETURN_ARGS) {
227 result.errorcode = ERROR;
228 return result;
229 }
230 break;
231 case 'g': /* index of game field */
232 result.config.qstat_game_field = atoi(optarg);
233 if (result.config.qstat_game_field < 0 || result.config.qstat_game_field > QSTAT_MAX_RETURN_ARGS) {
234 result.errorcode = ERROR;
235 return result;
236 }
237 break;
238 case players_field_index: /* index of player count field */
239 result.config.qstat_game_players = atoi(optarg);
240 if (result.config.qstat_game_players_max == 0) {
241 result.config.qstat_game_players_max = result.config.qstat_game_players - 1;
242 }
243 if (result.config.qstat_game_players < 0 || result.config.qstat_game_players > QSTAT_MAX_RETURN_ARGS) {
244 result.errorcode = ERROR;
245 return result;
246 }
247 break;
248 case max_players_field_index: /* index of max players field */
249 result.config.qstat_game_players_max = atoi(optarg);
250 if (result.config.qstat_game_players_max < 0 || result.config.qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) {
251 result.errorcode = ERROR;
252 return result;
253 }
254 break;
255 default: /* args not parsable */
256 usage5();
257 }
258 }
259
260 int option_counter = optind;
261 /* first option is the game type */
262 if (!result.config.game_type && option_counter < argc) {
263 result.config.game_type = strdup(argv[option_counter++]);
264 }
265
266 /* Second option is the server name */
267 if (!result.config.server_ip && option_counter < argc) {
268 result.config.server_ip = strdup(argv[option_counter++]);
269 }
270
271 return result;
286} 272}
287 273
274void print_help(void) {
275 print_revision(progname, NP_VERSION);
288 276
289void 277 printf("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
290print_help (void) 278 printf(COPYRIGHT, copyright, email);
291{
292 print_revision (progname, NP_VERSION);
293
294 printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
295 printf (COPYRIGHT, copyright, email);
296 279
297 printf (_("This plugin tests game server connections with the specified host.")); 280 printf(_("This plugin tests game server connections with the specified host."));
298 281
299 printf ("\n\n"); 282 printf("\n\n");
300 283
301 print_usage (); 284 print_usage();
302 285
303 printf (UT_HELP_VRSN); 286 printf(UT_HELP_VRSN);
304 printf (UT_EXTRA_OPTS); 287 printf(UT_EXTRA_OPTS);
288 printf(" -H, --hostname=ADDRESS\n"
289 " Host name, IP Address, or unix socket (must be an absolute path)\n");
290 printf(" %s\n", "-P");
291 printf(" %s\n", _("Optional port to connect to"));
292 printf(" %s\n", "-g");
293 printf(" %s\n", _("Field number in raw qstat output that contains game name"));
294 printf(" %s\n", "-m");
295 printf(" %s\n", _("Field number in raw qstat output that contains map name"));
296 printf(" %s\n", "-p");
297 printf(" %s\n", _("Field number in raw qstat output that contains ping time"));
305 298
306 printf (" %s\n", "-p"); 299 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
307 printf (" %s\n", _("Optional port of which to connect"));
308 printf (" %s\n", "gf");
309 printf (" %s\n", _("Field number in raw qstat output that contains game name"));
310 printf (" %s\n", "-mf");
311 printf (" %s\n", _("Field number in raw qstat output that contains map name"));
312 printf (" %s\n", "-pf");
313 printf (" %s\n", _("Field number in raw qstat output that contains ping time"));
314 300
315 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 301 printf("\n");
302 printf("%s\n", _("Notes:"));
303 printf(" %s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool."));
304 printf(" %s\n", _("If you don't have the package installed, you will need to download it from"));
305 printf(" %s\n", _("https://github.com/multiplay/qstat before you can use this plugin."));
316 306
317 printf ("\n"); 307 printf(UT_SUPPORT);
318 printf ("%s\n", _("Notes:"));
319 printf (" %s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool."));
320 printf (" %s\n", _("If you don't have the package installed, you will need to download it from"));
321 printf (" %s\n", _("https://github.com/multiplay/qstat before you can use this plugin."));
322
323 printf (UT_SUPPORT);
324} 308}
325 309
326 310void print_usage(void) {
327 311 printf("%s\n", _("Usage:"));
328void 312 printf(" %s [-hvV] [-P port] [-t timeout] [-g game_field] [-m map_field] [-p ping_field] [-G game-time] [-H hostname] <game> "
329print_usage (void) 313 "<ip_address>\n",
330{ 314 progname);
331 printf ("%s\n", _("Usage:"));
332 printf (" %s [-hvV] [-P port] [-t timeout] [-g game_field] [-m map_field] [-p ping_field] [-G game-time] [-H hostname] <game> <ip_address>\n", progname);
333} 315}
334 316
335/****************************************************************************** 317/******************************************************************************