summaryrefslogtreecommitdiffstats
path: root/plugins/check_game.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2024-10-31 13:57:43 (GMT)
committerGitHub <noreply@github.com>2024-10-31 13:57:43 (GMT)
commitb1d260a821b7d4916d6bf1a026fbc9b4f2b268ae (patch)
tree84a3fc6b26b181a1ed10d7ca55c32ac9170efbd4 /plugins/check_game.c
parentb4c5956591e9741ce9b190210e7b10940a6adbdd (diff)
parent8955f56de355403941bc8f02a4edb2bc72d1397a (diff)
downloadmonitoring-plugins-b1d260a821b7d4916d6bf1a026fbc9b4f2b268ae.tar.gz
Merge pull request #2035 from RincewindsHat/cleanup/rest-of-plugins
Cleanup for some more plugins
Diffstat (limited to 'plugins/check_game.c')
-rw-r--r--plugins/check_game.c567
1 files changed, 270 insertions, 297 deletions
diff --git a/plugins/check_game.c b/plugins/check_game.c
index ca12697..619277e 100644
--- a/plugins/check_game.c
+++ b/plugins/check_game.c
@@ -1,335 +1,308 @@
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 39
40int process_arguments (int, char **); 40static int process_arguments(int /*argc*/, char ** /*argv*/);
41int validate_arguments (void); 41static int validate_arguments(void);
42void print_help (void); 42static void print_help(void);
43void print_usage (void); 43void print_usage(void);
44 44
45#define QSTAT_DATA_DELIMITER "," 45#define QSTAT_DATA_DELIMITER ","
46 46
47#define QSTAT_HOST_ERROR "ERROR" 47#define QSTAT_HOST_ERROR "ERROR"
48#define QSTAT_HOST_DOWN "DOWN" 48#define QSTAT_HOST_DOWN "DOWN"
49#define QSTAT_HOST_TIMEOUT "TIMEOUT" 49#define QSTAT_HOST_TIMEOUT "TIMEOUT"
50#define QSTAT_MAX_RETURN_ARGS 12 50#define QSTAT_MAX_RETURN_ARGS 12
51 51
52char *server_ip; 52static char *server_ip;
53char *game_type; 53static char *game_type;
54int port = 0; 54static int port = 0;
55 55
56bool verbose = false; 56static bool verbose = false;
57 57
58int qstat_game_players_max = -1; 58static int qstat_game_players_max = -1;
59int qstat_game_players = -1; 59static int qstat_game_players = -1;
60int qstat_game_field = -1; 60static int qstat_game_field = -1;
61int qstat_map_field = -1; 61static int qstat_map_field = -1;
62int qstat_ping_field = -1; 62static int qstat_ping_field = -1;
63 63
64 64int main(int argc, char **argv) {
65int 65 char *command_line;
66main (int argc, char **argv) 66 int result = STATE_UNKNOWN;
67{ 67 char *p;
68 char *command_line; 68 char *ret[QSTAT_MAX_RETURN_ARGS];
69 int result = STATE_UNKNOWN; 69 size_t i = 0;
70 char *p, *ret[QSTAT_MAX_RETURN_ARGS]; 70 output chld_out;
71 size_t i = 0; 71
72 output chld_out; 72 setlocale(LC_ALL, "");
73 73 bindtextdomain(PACKAGE, LOCALEDIR);
74 setlocale (LC_ALL, ""); 74 textdomain(PACKAGE);
75 bindtextdomain (PACKAGE, LOCALEDIR); 75
76 textdomain (PACKAGE); 76 /* Parse extra opts if any */
77 77 argv = np_extra_opts(&argc, argv, progname);
78 /* Parse extra opts if any */ 78
79 argv=np_extra_opts (&argc, argv, progname); 79 if (process_arguments(argc, argv) == ERROR)
80 80 usage_va(_("Could not parse arguments"));
81 if (process_arguments (argc, argv) == ERROR) 81
82 usage_va(_("Could not parse arguments")); 82 result = STATE_OK;
83 83
84 result = STATE_OK; 84 /* create the command line to execute */
85 85 xasprintf(&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip);
86 /* create the command line to execute */ 86
87 xasprintf (&command_line, "%s -raw %s -%s %s", 87 if (port)
88 PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip); 88 xasprintf(&command_line, "%s:%-d", command_line, port);
89 89
90 if (port) 90 if (verbose)
91 xasprintf (&command_line, "%s:%-d", command_line, port); 91 printf("%s\n", command_line);
92 92
93 if (verbose) 93 /* run the command. historically, this plugin ignores output on stderr,
94 printf ("%s\n", command_line); 94 * as well as return status of the qstat program */
95 95 (void)np_runcmd(command_line, &chld_out, NULL, 0);
96 /* run the command. historically, this plugin ignores output on stderr, 96
97 * as well as return status of the qstat program */ 97 /* sanity check */
98 (void)np_runcmd(command_line, &chld_out, NULL, 0); 98 /* was thinking about running qstat without any options, capturing the
99 99 -default line, parsing it & making an array of all know server types
100 /* sanity check */ 100 but thought this would be too much hassle considering this is a tool
101 /* was thinking about running qstat without any options, capturing the 101 for intelligent sysadmins (ha). Could put a static array of known
102 -default line, parsing it & making an array of all know server types 102 server types in a header file but then we'd be limiting ourselves
103 but thought this would be too much hassle considering this is a tool 103
104 for intelligent sysadmins (ha). Could put a static array of known 104 In the end, I figured I'd simply let an error occur & then trap it
105 server types in a header file but then we'd be limiting ourselves 105 */
106 106
107 In the end, I figured I'd simply let an error occur & then trap it 107 if (!strncmp(chld_out.line[0], "unknown option", 14)) {
108 */ 108 printf(_("CRITICAL - Host type parameter incorrect!\n"));
109 109 result = STATE_CRITICAL;
110 if (!strncmp (chld_out.line[0], "unknown option", 14)) { 110 return result;
111 printf (_("CRITICAL - Host type parameter incorrect!\n")); 111 }
112 result = STATE_CRITICAL; 112
113 return result; 113 p = (char *)strtok(chld_out.line[0], QSTAT_DATA_DELIMITER);
114 } 114 while (p != NULL) {
115 115 ret[i] = p;
116 p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER); 116 p = (char *)strtok(NULL, QSTAT_DATA_DELIMITER);
117 while (p != NULL) { 117 i++;
118 ret[i] = p; 118 if (i >= QSTAT_MAX_RETURN_ARGS)
119 p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER); 119 break;
120 i++; 120 }
121 if (i >= QSTAT_MAX_RETURN_ARGS) 121
122 break; 122 if (strstr(ret[2], QSTAT_HOST_ERROR)) {
123 } 123 printf(_("CRITICAL - Host not found\n"));
124 124 result = STATE_CRITICAL;
125 if (strstr (ret[2], QSTAT_HOST_ERROR)) { 125 } else if (strstr(ret[2], QSTAT_HOST_DOWN)) {
126 printf (_("CRITICAL - Host not found\n")); 126 printf(_("CRITICAL - Game server down or unavailable\n"));
127 result = STATE_CRITICAL; 127 result = STATE_CRITICAL;
128 } 128 } else if (strstr(ret[2], QSTAT_HOST_TIMEOUT)) {
129 else if (strstr (ret[2], QSTAT_HOST_DOWN)) { 129 printf(_("CRITICAL - Game server timeout\n"));
130 printf (_("CRITICAL - Game server down or unavailable\n")); 130 result = STATE_CRITICAL;
131 result = STATE_CRITICAL; 131 } else {
132 } 132 printf("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[qstat_game_players], ret[qstat_game_players_max], ret[qstat_game_field],
133 else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) { 133 ret[qstat_map_field], ret[qstat_ping_field],
134 printf (_("CRITICAL - Game server timeout\n")); 134 perfdata("players", atol(ret[qstat_game_players]), "", false, 0, false, 0, true, 0, true, atol(ret[qstat_game_players_max])),
135 result = STATE_CRITICAL; 135 fperfdata("ping", strtod(ret[qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0));
136 } 136 }
137 else { 137
138 printf ("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", 138 return result;
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} 139}
154 140
155 141int process_arguments(int argc, char **argv) {
156int 142 int c;
157process_arguments (int argc, char **argv) 143
158{ 144 int opt_index = 0;
159 int c; 145 static struct option long_opts[] = {{"help", no_argument, 0, 'h'},
160 146 {"version", no_argument, 0, 'V'},
161 int opt_index = 0; 147 {"verbose", no_argument, 0, 'v'},
162 static struct option long_opts[] = { 148 {"timeout", required_argument, 0, 't'},
163 {"help", no_argument, 0, 'h'}, 149 {"hostname", required_argument, 0, 'H'},
164 {"version", no_argument, 0, 'V'}, 150 {"port", required_argument, 0, 'P'},
165 {"verbose", no_argument, 0, 'v'}, 151 {"game-type", required_argument, 0, 'G'},
166 {"timeout", required_argument, 0, 't'}, 152 {"map-field", required_argument, 0, 'm'},
167 {"hostname", required_argument, 0, 'H'}, 153 {"ping-field", required_argument, 0, 'p'},
168 {"port", required_argument, 0, 'P'}, 154 {"game-field", required_argument, 0, 'g'},
169 {"game-type", required_argument, 0, 'G'}, 155 {"players-field", required_argument, 0, 129},
170 {"map-field", required_argument, 0, 'm'}, 156 {"max-players-field", required_argument, 0, 130},
171 {"ping-field", required_argument, 0, 'p'}, 157 {0, 0, 0, 0}};
172 {"game-field", required_argument, 0, 'g'}, 158
173 {"players-field", required_argument, 0, 129}, 159 if (argc < 2)
174 {"max-players-field", required_argument, 0, 130}, 160 return ERROR;
175 {0, 0, 0, 0} 161
176 }; 162 for (c = 1; c < argc; c++) {
177 163 if (strcmp("-mf", argv[c]) == 0)
178 if (argc < 2) 164 strcpy(argv[c], "-m");
179 return ERROR; 165 else if (strcmp("-pf", argv[c]) == 0)
180 166 strcpy(argv[c], "-p");
181 for (c = 1; c < argc; c++) { 167 else if (strcmp("-gf", argv[c]) == 0)
182 if (strcmp ("-mf", argv[c]) == 0) 168 strcpy(argv[c], "-g");
183 strcpy (argv[c], "-m"); 169 }
184 else if (strcmp ("-pf", argv[c]) == 0) 170
185 strcpy (argv[c], "-p"); 171 while (1) {
186 else if (strcmp ("-gf", argv[c]) == 0) 172 c = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
187 strcpy (argv[c], "-g"); 173
188 } 174 if (c == -1 || c == EOF)
189 175 break;
190 while (1) { 176
191 c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); 177 switch (c) {
192 178 case 'h': /* help */
193 if (c == -1 || c == EOF) 179 print_help();
194 break; 180 exit(STATE_UNKNOWN);
195 181 case 'V': /* version */
196 switch (c) { 182 print_revision(progname, NP_VERSION);
197 case 'h': /* help */ 183 exit(STATE_UNKNOWN);
198 print_help (); 184 case 'v': /* version */
199 exit (STATE_UNKNOWN); 185 verbose = true;
200 case 'V': /* version */ 186 break;
201 print_revision (progname, NP_VERSION); 187 case 't': /* timeout period */
202 exit (STATE_UNKNOWN); 188 timeout_interval = atoi(optarg);
203 case 'v': /* version */ 189 break;
204 verbose = true; 190 case 'H': /* hostname */
205 break; 191 if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH)
206 case 't': /* timeout period */ 192 die(STATE_UNKNOWN, _("Input buffer overflow\n"));
207 timeout_interval = atoi (optarg); 193 server_ip = optarg;
208 break; 194 break;
209 case 'H': /* hostname */ 195 case 'P': /* port */
210 if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH) 196 port = atoi(optarg);
211 die (STATE_UNKNOWN, _("Input buffer overflow\n")); 197 break;
212 server_ip = optarg; 198 case 'G': /* hostname */
213 break; 199 if (strlen(optarg) >= MAX_INPUT_BUFFER)
214 case 'P': /* port */ 200 die(STATE_UNKNOWN, _("Input buffer overflow\n"));
215 port = atoi (optarg); 201 game_type = optarg;
216 break; 202 break;
217 case 'G': /* hostname */ 203 case 'p': /* index of ping field */
218 if (strlen (optarg) >= MAX_INPUT_BUFFER) 204 qstat_ping_field = atoi(optarg);
219 die (STATE_UNKNOWN, _("Input buffer overflow\n")); 205 if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS)
220 game_type = optarg; 206 return ERROR;
221 break; 207 break;
222 case 'p': /* index of ping field */ 208 case 'm': /* index on map field */
223 qstat_ping_field = atoi (optarg); 209 qstat_map_field = atoi(optarg);
224 if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS) 210 if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS)
225 return ERROR; 211 return ERROR;
226 break; 212 break;
227 case 'm': /* index on map field */ 213 case 'g': /* index of game field */
228 qstat_map_field = atoi (optarg); 214 qstat_game_field = atoi(optarg);
229 if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS) 215 if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS)
230 return ERROR; 216 return ERROR;
231 break; 217 break;
232 case 'g': /* index of game field */ 218 case 129: /* index of player count field */
233 qstat_game_field = atoi (optarg); 219 qstat_game_players = atoi(optarg);
234 if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS) 220 if (qstat_game_players_max == 0)
235 return ERROR; 221 qstat_game_players_max = qstat_game_players - 1;
236 break; 222 if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS)
237 case 129: /* index of player count field */ 223 return ERROR;
238 qstat_game_players = atoi (optarg); 224 break;
239 if (qstat_game_players_max == 0) 225 case 130: /* index of max players field */
240 qstat_game_players_max = qstat_game_players - 1; 226 qstat_game_players_max = atoi(optarg);
241 if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS) 227 if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS)
242 return ERROR; 228 return ERROR;
243 break; 229 break;
244 case 130: /* index of max players field */ 230 default: /* args not parsable */
245 qstat_game_players_max = atoi (optarg); 231 usage5();
246 if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) 232 }
247 return ERROR; 233 }
248 break; 234
249 default: /* args not parsable */ 235 c = optind;
250 usage5(); 236 /* first option is the game type */
251 } 237 if (!game_type && c < argc)
252 } 238 game_type = strdup(argv[c++]);
253 239
254 c = optind; 240 /* Second option is the server name */
255 /* first option is the game type */ 241 if (!server_ip && c < argc)
256 if (!game_type && c<argc) 242 server_ip = strdup(argv[c++]);
257 game_type = strdup (argv[c++]); 243
258 244 return validate_arguments();
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} 245}
265 246
247int validate_arguments(void) {
248 if (qstat_game_players_max < 0)
249 qstat_game_players_max = 4;
266 250
267int 251 if (qstat_game_players < 0)
268validate_arguments (void) 252 qstat_game_players = 5;
269{
270 if (qstat_game_players_max < 0)
271 qstat_game_players_max = 4;
272 253
273 if (qstat_game_players < 0) 254 if (qstat_game_field < 0)
274 qstat_game_players = 5; 255 qstat_game_field = 2;
275 256
276 if (qstat_game_field < 0) 257 if (qstat_map_field < 0)
277 qstat_game_field = 2; 258 qstat_map_field = 3;
278 259
279 if (qstat_map_field < 0) 260 if (qstat_ping_field < 0)
280 qstat_map_field = 3; 261 qstat_ping_field = 5;
281 262
282 if (qstat_ping_field < 0) 263 return OK;
283 qstat_ping_field = 5;
284
285 return OK;
286} 264}
287 265
266void print_help(void) {
267 print_revision(progname, NP_VERSION);
288 268
289void 269 printf("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
290print_help (void) 270 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 271
297 printf (_("This plugin tests game server connections with the specified host.")); 272 printf(_("This plugin tests game server connections with the specified host."));
298 273
299 printf ("\n\n"); 274 printf("\n\n");
300 275
301 print_usage (); 276 print_usage();
302 277
303 printf (UT_HELP_VRSN); 278 printf(UT_HELP_VRSN);
304 printf (UT_EXTRA_OPTS); 279 printf(UT_EXTRA_OPTS);
305 280
306 printf (" %s\n", "-p"); 281 printf(" %s\n", "-p");
307 printf (" %s\n", _("Optional port of which to connect")); 282 printf(" %s\n", _("Optional port of which to connect"));
308 printf (" %s\n", "gf"); 283 printf(" %s\n", "gf");
309 printf (" %s\n", _("Field number in raw qstat output that contains game name")); 284 printf(" %s\n", _("Field number in raw qstat output that contains game name"));
310 printf (" %s\n", "-mf"); 285 printf(" %s\n", "-mf");
311 printf (" %s\n", _("Field number in raw qstat output that contains map name")); 286 printf(" %s\n", _("Field number in raw qstat output that contains map name"));
312 printf (" %s\n", "-pf"); 287 printf(" %s\n", "-pf");
313 printf (" %s\n", _("Field number in raw qstat output that contains ping time")); 288 printf(" %s\n", _("Field number in raw qstat output that contains ping time"));
314 289
315 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 290 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
316 291
317 printf ("\n"); 292 printf("\n");
318 printf ("%s\n", _("Notes:")); 293 printf("%s\n", _("Notes:"));
319 printf (" %s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool.")); 294 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")); 295 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.")); 296 printf(" %s\n", _("https://github.com/multiplay/qstat before you can use this plugin."));
322 297
323 printf (UT_SUPPORT); 298 printf(UT_SUPPORT);
324} 299}
325 300
326 301void print_usage(void) {
327 302 printf("%s\n", _("Usage:"));
328void 303 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) 304 "<ip_address>\n",
330{ 305 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} 306}
334 307
335/****************************************************************************** 308/******************************************************************************