1 /*****************************************************************************
3 * Monitoring check_game plugin
6 * Copyright (c) 2002-2024 Monitoring Plugins Development Team
10 * This file contains the check_game plugin
12 * This plugin tests game server connections with the specified host.
13 * using the qstat program
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
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
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/>.
30 *****************************************************************************/
32 const char *progname
= "check_game";
33 const char *copyright
= "2002-2024";
34 const char *email
= "devel@monitoring-plugins.org";
40 static int process_arguments(int /*argc*/, char ** /*argv*/);
41 static int validate_arguments(void);
42 static void print_help(void);
43 void print_usage(void);
45 #define QSTAT_DATA_DELIMITER ","
47 #define QSTAT_HOST_ERROR "ERROR"
48 #define QSTAT_HOST_DOWN "DOWN"
49 #define QSTAT_HOST_TIMEOUT "TIMEOUT"
50 #define QSTAT_MAX_RETURN_ARGS 12
52 static char *server_ip
;
53 static char *game_type
;
56 static bool verbose
= false;
58 static int qstat_game_players_max
= -1;
59 static int qstat_game_players
= -1;
60 static int qstat_game_field
= -1;
61 static int qstat_map_field
= -1;
62 static int qstat_ping_field
= -1;
64 int main(int argc
, char **argv
) {
66 int result
= STATE_UNKNOWN
;
68 char *ret
[QSTAT_MAX_RETURN_ARGS
];
72 setlocale(LC_ALL
, "");
73 bindtextdomain(PACKAGE
, LOCALEDIR
);
76 /* Parse extra opts if any */
77 argv
= np_extra_opts(&argc
, argv
, progname
);
79 if (process_arguments(argc
, argv
) == ERROR
)
80 usage_va(_("Could not parse arguments"));
84 /* create the command line to execute */
85 xasprintf(&command_line
, "%s -raw %s -%s %s", PATH_TO_QSTAT
, QSTAT_DATA_DELIMITER
, game_type
, server_ip
);
88 xasprintf(&command_line
, "%s:%-d", command_line
, port
);
91 printf("%s\n", command_line
);
93 /* run the command. historically, this plugin ignores output on stderr,
94 * as well as return status of the qstat program */
95 (void)np_runcmd(command_line
, &chld_out
, NULL
, 0);
98 /* was thinking about running qstat without any options, capturing the
99 -default line, parsing it & making an array of all know server types
100 but thought this would be too much hassle considering this is a tool
101 for intelligent sysadmins (ha). Could put a static array of known
102 server types in a header file but then we'd be limiting ourselves
104 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 printf(_("CRITICAL - Host type parameter incorrect!\n"));
109 result
= STATE_CRITICAL
;
113 p
= (char *)strtok(chld_out
.line
[0], QSTAT_DATA_DELIMITER
);
116 p
= (char *)strtok(NULL
, QSTAT_DATA_DELIMITER
);
118 if (i
>= QSTAT_MAX_RETURN_ARGS
)
122 if (strstr(ret
[2], QSTAT_HOST_ERROR
)) {
123 printf(_("CRITICAL - Host not found\n"));
124 result
= STATE_CRITICAL
;
125 } else if (strstr(ret
[2], QSTAT_HOST_DOWN
)) {
126 printf(_("CRITICAL - Game server down or unavailable\n"));
127 result
= STATE_CRITICAL
;
128 } else if (strstr(ret
[2], QSTAT_HOST_TIMEOUT
)) {
129 printf(_("CRITICAL - Game server timeout\n"));
130 result
= STATE_CRITICAL
;
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 ret
[qstat_map_field
], ret
[qstat_ping_field
],
134 perfdata("players", atol(ret
[qstat_game_players
]), "", false, 0, false, 0, true, 0, true, atol(ret
[qstat_game_players_max
])),
135 fperfdata("ping", strtod(ret
[qstat_ping_field
], NULL
), "", false, 0, false, 0, true, 0, false, 0));
141 int process_arguments(int argc
, char **argv
) {
145 static struct option long_opts
[] = {{"help", no_argument
, 0, 'h'},
146 {"version", no_argument
, 0, 'V'},
147 {"verbose", no_argument
, 0, 'v'},
148 {"timeout", required_argument
, 0, 't'},
149 {"hostname", required_argument
, 0, 'H'},
150 {"port", required_argument
, 0, 'P'},
151 {"game-type", required_argument
, 0, 'G'},
152 {"map-field", required_argument
, 0, 'm'},
153 {"ping-field", required_argument
, 0, 'p'},
154 {"game-field", required_argument
, 0, 'g'},
155 {"players-field", required_argument
, 0, 129},
156 {"max-players-field", required_argument
, 0, 130},
162 for (c
= 1; c
< argc
; c
++) {
163 if (strcmp("-mf", argv
[c
]) == 0)
164 strcpy(argv
[c
], "-m");
165 else if (strcmp("-pf", argv
[c
]) == 0)
166 strcpy(argv
[c
], "-p");
167 else if (strcmp("-gf", argv
[c
]) == 0)
168 strcpy(argv
[c
], "-g");
172 c
= getopt_long(argc
, argv
, "hVvt:H:P:G:g:p:m:", long_opts
, &opt_index
);
174 if (c
== -1 || c
== EOF
)
181 case 'V': /* version */
182 print_revision(progname
, NP_VERSION
);
184 case 'v': /* version */
187 case 't': /* timeout period */
188 timeout_interval
= atoi(optarg
);
190 case 'H': /* hostname */
191 if (strlen(optarg
) >= MAX_HOST_ADDRESS_LENGTH
)
192 die(STATE_UNKNOWN
, _("Input buffer overflow\n"));
198 case 'G': /* hostname */
199 if (strlen(optarg
) >= MAX_INPUT_BUFFER
)
200 die(STATE_UNKNOWN
, _("Input buffer overflow\n"));
203 case 'p': /* index of ping field */
204 qstat_ping_field
= atoi(optarg
);
205 if (qstat_ping_field
< 0 || qstat_ping_field
> QSTAT_MAX_RETURN_ARGS
)
208 case 'm': /* index on map field */
209 qstat_map_field
= atoi(optarg
);
210 if (qstat_map_field
< 0 || qstat_map_field
> QSTAT_MAX_RETURN_ARGS
)
213 case 'g': /* index of game field */
214 qstat_game_field
= atoi(optarg
);
215 if (qstat_game_field
< 0 || qstat_game_field
> QSTAT_MAX_RETURN_ARGS
)
218 case 129: /* index of player count field */
219 qstat_game_players
= atoi(optarg
);
220 if (qstat_game_players_max
== 0)
221 qstat_game_players_max
= qstat_game_players
- 1;
222 if (qstat_game_players
< 0 || qstat_game_players
> QSTAT_MAX_RETURN_ARGS
)
225 case 130: /* index of max players field */
226 qstat_game_players_max
= atoi(optarg
);
227 if (qstat_game_players_max
< 0 || qstat_game_players_max
> QSTAT_MAX_RETURN_ARGS
)
230 default: /* args not parsable */
236 /* first option is the game type */
237 if (!game_type
&& c
< argc
)
238 game_type
= strdup(argv
[c
++]);
240 /* Second option is the server name */
241 if (!server_ip
&& c
< argc
)
242 server_ip
= strdup(argv
[c
++]);
244 return validate_arguments();
247 int validate_arguments(void) {
248 if (qstat_game_players_max
< 0)
249 qstat_game_players_max
= 4;
251 if (qstat_game_players
< 0)
252 qstat_game_players
= 5;
254 if (qstat_game_field
< 0)
255 qstat_game_field
= 2;
257 if (qstat_map_field
< 0)
260 if (qstat_ping_field
< 0)
261 qstat_ping_field
= 5;
266 void print_help(void) {
267 print_revision(progname
, NP_VERSION
);
269 printf("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
270 printf(COPYRIGHT
, copyright
, email
);
272 printf(_("This plugin tests game server connections with the specified host."));
278 printf(UT_HELP_VRSN
);
279 printf(UT_EXTRA_OPTS
);
281 printf(" %s\n", "-p");
282 printf(" %s\n", _("Optional port of which to connect"));
283 printf(" %s\n", "gf");
284 printf(" %s\n", _("Field number in raw qstat output that contains game name"));
285 printf(" %s\n", "-mf");
286 printf(" %s\n", _("Field number in raw qstat output that contains map name"));
287 printf(" %s\n", "-pf");
288 printf(" %s\n", _("Field number in raw qstat output that contains ping time"));
290 printf(UT_CONN_TIMEOUT
, DEFAULT_SOCKET_TIMEOUT
);
293 printf("%s\n", _("Notes:"));
294 printf(" %s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool."));
295 printf(" %s\n", _("If you don't have the package installed, you will need to download it from"));
296 printf(" %s\n", _("https://github.com/multiplay/qstat before you can use this plugin."));
301 void print_usage(void) {
302 printf("%s\n", _("Usage:"));
303 printf(" %s [-hvV] [-P port] [-t timeout] [-g game_field] [-m map_field] [-p ping_field] [-G game-time] [-H hostname] <game> "
308 /******************************************************************************
312 * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
314 * qstat -raw , -qs 67.20.190.61
315 * ==> QS,67.20.190.61,Nightmare.fintek.ca,67.20.190.61:26000,3,e2m1,6,0,83,0
317 * qstat -qs 67.20.190.61
318 * ==> ADDRESS PLAYERS MAP RESPONSE TIME NAME
319 * ==> 67.20.190.61 0/ 6 e2m1 79 / 0 Nightmare.fintek.ca
321 ******************************************************************************/