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