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.c604
1 files changed, 299 insertions, 305 deletions
diff --git a/plugins/check_game.c b/plugins/check_game.c
index ca126973..974a7253 100644
--- a/plugins/check_game.c
+++ b/plugins/check_game.c
@@ -1,335 +1,329 @@
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,
179 return ERROR; 81 config.game_type, config.server_ip);
180 82
181 for (c = 1; c < argc; c++) { 83 if (config.port) {
182 if (strcmp ("-mf", argv[c]) == 0) 84 xasprintf(&command_line, "%s:%-d", command_line, config.port);
183 strcpy (argv[c], "-m"); 85 }
184 else if (strcmp ("-pf", argv[c]) == 0) 86
185 strcpy (argv[c], "-p"); 87 if (verbose) {
186 else if (strcmp ("-gf", argv[c]) == 0) 88 printf("%s\n", command_line);
187 strcpy (argv[c], "-g"); 89 }
188 } 90
189 91 /* run the command. historically, this plugin ignores output on stderr,
190 while (1) { 92 * as well as return status of the qstat program */
191 c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); 93 output chld_out = {};
192 94 (void)np_runcmd(command_line, &chld_out, NULL, 0);
193 if (c == -1 || c == EOF) 95
194 break; 96 /* sanity check */
195 97 /* was thinking about running qstat without any options, capturing the
196 switch (c) { 98 -default line, parsing it & making an array of all know server types
197 case 'h': /* help */ 99 but thought this would be too much hassle considering this is a tool
198 print_help (); 100 for intelligent sysadmins (ha). Could put a static array of known
199 exit (STATE_UNKNOWN); 101 server types in a header file but then we'd be limiting ourselves
200 case 'V': /* version */ 102
201 print_revision (progname, NP_VERSION); 103 In the end, I figured I'd simply let an error occur & then trap it
202 exit (STATE_UNKNOWN); 104 */
203 case 'v': /* version */ 105
204 verbose = true; 106 if (!strncmp(chld_out.line[0], "unknown option", strlen("unknown option"))) {
205 break; 107 printf(_("CRITICAL - Host type parameter incorrect!\n"));
206 case 't': /* timeout period */ 108 result = STATE_CRITICAL;
207 timeout_interval = atoi (optarg); 109 exit(result);
208 break; 110 }
209 case 'H': /* hostname */ 111
210 if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH) 112 char *ret[QSTAT_MAX_RETURN_ARGS];
211 die (STATE_UNKNOWN, _("Input buffer overflow\n")); 113 size_t i = 0;
212 server_ip = optarg; 114 char *sequence = strtok(chld_out.line[0], QSTAT_DATA_DELIMITER);
213 break; 115 while (sequence != NULL) {
214 case 'P': /* port */ 116 ret[i] = sequence;
215 port = atoi (optarg); 117 sequence = strtok(NULL, QSTAT_DATA_DELIMITER);
216 break; 118 i++;
217 case 'G': /* hostname */ 119 if (i >= QSTAT_MAX_RETURN_ARGS) {
218 if (strlen (optarg) >= MAX_INPUT_BUFFER) 120 break;
219 die (STATE_UNKNOWN, _("Input buffer overflow\n")); 121 }
220 game_type = optarg; 122 }
221 break; 123
222 case 'p': /* index of ping field */ 124 if (strstr(ret[2], QSTAT_HOST_ERROR)) {
223 qstat_ping_field = atoi (optarg); 125 printf(_("CRITICAL - Host not found\n"));
224 if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS) 126 result = STATE_CRITICAL;
225 return ERROR; 127 } else if (strstr(ret[2], QSTAT_HOST_DOWN)) {
226 break; 128 printf(_("CRITICAL - Game server down or unavailable\n"));
227 case 'm': /* index on map field */ 129 result = STATE_CRITICAL;
228 qstat_map_field = atoi (optarg); 130 } else if (strstr(ret[2], QSTAT_HOST_TIMEOUT)) {
229 if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS) 131 printf(_("CRITICAL - Game server timeout\n"));
230 return ERROR; 132 result = STATE_CRITICAL;
231 break; 133 } else {
232 case 'g': /* index of game field */ 134 printf("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[config.qstat_game_players],
233 qstat_game_field = atoi (optarg); 135 ret[config.qstat_game_players_max], ret[config.qstat_game_field],
234 if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS) 136 ret[config.qstat_map_field], ret[config.qstat_ping_field],
235 return ERROR; 137 perfdata("players", atol(ret[config.qstat_game_players]), "", false, 0, false, 0,
236 break; 138 true, 0, true, atol(ret[config.qstat_game_players_max])),
237 case 129: /* index of player count field */ 139 fperfdata("ping", strtod(ret[config.qstat_ping_field], NULL), "", false, 0, false, 0,
238 qstat_game_players = atoi (optarg); 140 true, 0, false, 0));
239 if (qstat_game_players_max == 0) 141 }
240 qstat_game_players_max = qstat_game_players - 1; 142
241 if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS) 143 exit(result);
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} 144}
265 145
266 146#define players_field_index 129
267int 147#define max_players_field_index 130
268validate_arguments (void) 148
269{ 149check_game_config_wrapper process_arguments(int argc, char **argv) {
270 if (qstat_game_players_max < 0) 150 static struct option long_opts[] = {
271 qstat_game_players_max = 4; 151 {"help", no_argument, 0, 'h'},
272 152 {"version", no_argument, 0, 'V'},
273 if (qstat_game_players < 0) 153 {"verbose", no_argument, 0, 'v'},
274 qstat_game_players = 5; 154 {"timeout", required_argument, 0, 't'},
275 155 {"hostname", required_argument, 0, 'H'},
276 if (qstat_game_field < 0) 156 {"port", required_argument, 0, 'P'},
277 qstat_game_field = 2; 157 {"game-type", required_argument, 0, 'G'},
278 158 {"map-field", required_argument, 0, 'm'},
279 if (qstat_map_field < 0) 159 {"ping-field", required_argument, 0, 'p'},
280 qstat_map_field = 3; 160 {"game-field", required_argument, 0, 'g'},
281 161 {"players-field", required_argument, 0, players_field_index},
282 if (qstat_ping_field < 0) 162 {"max-players-field", required_argument, 0, max_players_field_index},
283 qstat_ping_field = 5; 163 {0, 0, 0, 0}};
284 164
285 return OK; 165 check_game_config_wrapper result = {
166 .config = check_game_config_init(),
167 .errorcode = OK,
168 };
169
170 if (argc < 2) {
171 result.errorcode = ERROR;
172 return result;
173 }
174
175 for (int option_counter = 1; option_counter < argc; option_counter++) {
176 if (strcmp("-mf", argv[option_counter]) == 0) {
177 strcpy(argv[option_counter], "-m");
178 } else if (strcmp("-pf", argv[option_counter]) == 0) {
179 strcpy(argv[option_counter], "-p");
180 } else if (strcmp("-gf", argv[option_counter]) == 0) {
181 strcpy(argv[option_counter], "-g");
182 }
183 }
184
185 int opt_index = 0;
186 while (true) {
187 int option_index = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
188
189 if (option_index == -1 || option_index == EOF) {
190 break;
191 }
192
193 switch (option_index) {
194 case 'h': /* help */
195 print_help();
196 exit(STATE_UNKNOWN);
197 case 'V': /* version */
198 print_revision(progname, NP_VERSION);
199 exit(STATE_UNKNOWN);
200 case 'v': /* version */
201 verbose = true;
202 break;
203 case 't': /* timeout period */
204 timeout_interval = atoi(optarg);
205 break;
206 case 'H': /* hostname */
207 if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) {
208 die(STATE_UNKNOWN, _("Input buffer overflow\n"));
209 }
210 result.config.server_ip = optarg;
211 break;
212 case 'P': /* port */
213 result.config.port = atoi(optarg);
214 break;
215 case 'G': /* hostname */
216 if (strlen(optarg) >= MAX_INPUT_BUFFER) {
217 die(STATE_UNKNOWN, _("Input buffer overflow\n"));
218 }
219 result.config.game_type = optarg;
220 break;
221 case 'p': /* index of ping field */
222 result.config.qstat_ping_field = atoi(optarg);
223 if (result.config.qstat_ping_field < 0 ||
224 result.config.qstat_ping_field > QSTAT_MAX_RETURN_ARGS) {
225 result.errorcode = ERROR;
226 return result;
227 }
228 break;
229 case 'm': /* index on map field */
230 result.config.qstat_map_field = atoi(optarg);
231 if (result.config.qstat_map_field < 0 ||
232 result.config.qstat_map_field > QSTAT_MAX_RETURN_ARGS) {
233 result.errorcode = ERROR;
234 return result;
235 }
236 break;
237 case 'g': /* index of game field */
238 result.config.qstat_game_field = atoi(optarg);
239 if (result.config.qstat_game_field < 0 ||
240 result.config.qstat_game_field > QSTAT_MAX_RETURN_ARGS) {
241 result.errorcode = ERROR;
242 return result;
243 }
244 break;
245 case players_field_index: /* index of player count field */
246 result.config.qstat_game_players = atoi(optarg);
247 if (result.config.qstat_game_players_max == 0) {
248 result.config.qstat_game_players_max = result.config.qstat_game_players - 1;
249 }
250 if (result.config.qstat_game_players < 0 ||
251 result.config.qstat_game_players > QSTAT_MAX_RETURN_ARGS) {
252 result.errorcode = ERROR;
253 return result;
254 }
255 break;
256 case max_players_field_index: /* index of max players field */
257 result.config.qstat_game_players_max = atoi(optarg);
258 if (result.config.qstat_game_players_max < 0 ||
259 result.config.qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) {
260 result.errorcode = ERROR;
261 return result;
262 }
263 break;
264 default: /* args not parsable */
265 usage5();
266 }
267 }
268
269 int option_counter = optind;
270 /* first option is the game type */
271 if (!result.config.game_type && option_counter < argc) {
272 result.config.game_type = strdup(argv[option_counter++]);
273 }
274
275 /* Second option is the server name */
276 if (!result.config.server_ip && option_counter < argc) {
277 result.config.server_ip = strdup(argv[option_counter++]);
278 }
279
280 return result;
286} 281}
287 282
283void print_help(void) {
284 print_revision(progname, NP_VERSION);
288 285
289void 286 printf("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
290print_help (void) 287 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 288
297 printf (_("This plugin tests game server connections with the specified host.")); 289 printf(_("This plugin tests game server connections with the specified host."));
298 290
299 printf ("\n\n"); 291 printf("\n\n");
300 292
301 print_usage (); 293 print_usage();
302 294
303 printf (UT_HELP_VRSN); 295 printf(UT_HELP_VRSN);
304 printf (UT_EXTRA_OPTS); 296 printf(UT_EXTRA_OPTS);
297 printf(" -H, --hostname=ADDRESS\n"
298 " Host name, IP Address, or unix socket (must be an absolute path)\n");
299 printf(" %s\n", "-P");
300 printf(" %s\n", _("Optional port to connect to"));
301 printf(" %s\n", "-g");
302 printf(" %s\n", _("Field number in raw qstat output that contains game name"));
303 printf(" %s\n", "-m");
304 printf(" %s\n", _("Field number in raw qstat output that contains map name"));
305 printf(" %s\n", "-p");
306 printf(" %s\n", _("Field number in raw qstat output that contains ping time"));
305 307
306 printf (" %s\n", "-p"); 308 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 309
315 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 310 printf("\n");
311 printf("%s\n", _("Notes:"));
312 printf(" %s\n",
313 _("This plugin uses the 'qstat' command, the popular game server status query tool."));
314 printf(" %s\n",
315 _("If you don't have the package installed, you will need to download it from"));
316 printf(" %s\n", _("https://github.com/multiplay/qstat before you can use this plugin."));
316 317
317 printf ("\n"); 318 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} 319}
325 320
326 321void print_usage(void) {
327 322 printf("%s\n", _("Usage:"));
328void 323 printf(" %s [-hvV] [-P port] [-t timeout] [-g game_field] [-m map_field] [-p ping_field] [-G "
329print_usage (void) 324 "game-time] [-H hostname] <game> "
330{ 325 "<ip_address>\n",
331 printf ("%s\n", _("Usage:")); 326 progname);
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} 327}
334 328
335/****************************************************************************** 329/******************************************************************************