1 /*****************************************************************************
3 * Nagios check_game plugin
6 * Copyright (c) 2002-2007 Nagios Plugins Development Team
8 * Last Modified: $Date$
12 * This file contains the check_game plugin
14 * This plugin tests game server connections with the specified host.
15 * using the qstat program
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 *****************************************************************************/
35 const char *progname
= "check_game";
36 const char *revision
= "$Revision$";
37 const char *copyright
= "2002-2007";
38 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
44 int process_arguments (int, char **);
45 int validate_arguments (void);
46 void print_help (void);
47 void print_usage (void);
49 #define QSTAT_DATA_DELIMITER ","
51 #define QSTAT_HOST_ERROR "ERROR"
52 #define QSTAT_HOST_DOWN "DOWN"
53 #define QSTAT_HOST_TIMEOUT "TIMEOUT"
54 #define QSTAT_MAX_RETURN_ARGS 12
62 int qstat_game_players_max
= -1;
63 int qstat_game_players
= -1;
64 int qstat_game_field
= -1;
65 int qstat_map_field
= -1;
66 int qstat_ping_field
= -1;
70 main (int argc
, char **argv
)
73 int result
= STATE_UNKNOWN
;
74 char *p
, *ret
[QSTAT_MAX_RETURN_ARGS
];
78 setlocale (LC_ALL
, "");
79 bindtextdomain (PACKAGE
, LOCALEDIR
);
82 /* Parse extra opts if any */
83 argv
=np_extra_opts (&argc
, argv
, progname
);
85 if (process_arguments (argc
, argv
) == ERROR
)
86 usage_va(_("Could not parse arguments"));
90 /* create the command line to execute */
91 asprintf (&command_line
, "%s -raw %s -%s %s",
92 PATH_TO_QSTAT
, QSTAT_DATA_DELIMITER
, game_type
, server_ip
);
95 asprintf (&command_line
, "%s:%-d", command_line
, port
);
98 printf ("%s\n", command_line
);
100 /* run the command. historically, this plugin ignores output on stderr,
101 * as well as return status of the qstat program */
102 (void)np_runcmd(command_line
, &chld_out
, NULL
, 0);
105 /* was thinking about running qstat without any options, capturing the
106 -default line, parsing it & making an array of all know server types
107 but thought this would be too much hassle considering this is a tool
108 for intelligent sysadmins (ha). Could put a static array of known
109 server types in a header file but then we'd be limiting ourselves
111 In the end, I figured I'd simply let an error occur & then trap it
114 if (!strncmp (chld_out
.line
[0], "unknown option", 14)) {
115 printf (_("CRITICAL - Host type parameter incorrect!\n"));
116 result
= STATE_CRITICAL
;
120 p
= (char *) strtok (chld_out
.line
[0], QSTAT_DATA_DELIMITER
);
123 p
= (char *) strtok (NULL
, QSTAT_DATA_DELIMITER
);
125 if (i
>= QSTAT_MAX_RETURN_ARGS
)
129 if (strstr (ret
[2], QSTAT_HOST_ERROR
)) {
130 printf (_("CRITICAL - Host not found\n"));
131 result
= STATE_CRITICAL
;
133 else if (strstr (ret
[2], QSTAT_HOST_DOWN
)) {
134 printf (_("CRITICAL - Game server down or unavailable\n"));
135 result
= STATE_CRITICAL
;
137 else if (strstr (ret
[2], QSTAT_HOST_TIMEOUT
)) {
138 printf (_("CRITICAL - Game server timeout\n"));
139 result
= STATE_CRITICAL
;
142 printf ("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n",
143 ret
[qstat_game_players
],
144 ret
[qstat_game_players_max
],
145 ret
[qstat_game_field
],
146 ret
[qstat_map_field
],
147 ret
[qstat_ping_field
],
148 perfdata ("players", atol(ret
[qstat_game_players
]), "",
150 TRUE
, 0, TRUE
, atol(ret
[qstat_game_players_max
])),
151 fperfdata ("ping", strtod(ret
[qstat_ping_field
], NULL
), "",
161 process_arguments (int argc
, char **argv
)
166 static struct option long_opts
[] = {
167 {"help", no_argument
, 0, 'h'},
168 {"version", no_argument
, 0, 'V'},
169 {"verbose", no_argument
, 0, 'v'},
170 {"timeout", required_argument
, 0, 't'},
171 {"hostname", required_argument
, 0, 'H'},
172 {"port", required_argument
, 0, 'P'},
173 {"game-type", required_argument
, 0, 'G'},
174 {"map-field", required_argument
, 0, 'm'},
175 {"ping-field", required_argument
, 0, 'p'},
176 {"game-field", required_argument
, 0, 'g'},
177 {"players-field", required_argument
, 0, 129},
178 {"max-players-field", required_argument
, 0, 130},
185 for (c
= 1; c
< argc
; c
++) {
186 if (strcmp ("-mf", argv
[c
]) == 0)
187 strcpy (argv
[c
], "-m");
188 else if (strcmp ("-pf", argv
[c
]) == 0)
189 strcpy (argv
[c
], "-p");
190 else if (strcmp ("-gf", argv
[c
]) == 0)
191 strcpy (argv
[c
], "-g");
195 c
= getopt_long (argc
, argv
, "hVvt:H:P:G:g:p:m:", long_opts
, &opt_index
);
197 if (c
== -1 || c
== EOF
)
204 case 'V': /* version */
205 print_revision (progname
, revision
);
207 case 'v': /* version */
210 case 't': /* timeout period */
211 timeout_interval
= atoi (optarg
);
213 case 'H': /* hostname */
214 if (strlen (optarg
) >= MAX_HOST_ADDRESS_LENGTH
)
215 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
219 port
= atoi (optarg
);
221 case 'G': /* hostname */
222 if (strlen (optarg
) >= MAX_INPUT_BUFFER
)
223 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
226 case 'p': /* index of ping field */
227 qstat_ping_field
= atoi (optarg
);
228 if (qstat_ping_field
< 0 || qstat_ping_field
> QSTAT_MAX_RETURN_ARGS
)
231 case 'm': /* index on map field */
232 qstat_map_field
= atoi (optarg
);
233 if (qstat_map_field
< 0 || qstat_map_field
> QSTAT_MAX_RETURN_ARGS
)
236 case 'g': /* index of game field */
237 qstat_game_field
= atoi (optarg
);
238 if (qstat_game_field
< 0 || qstat_game_field
> QSTAT_MAX_RETURN_ARGS
)
241 case 129: /* index of player count field */
242 qstat_game_players
= atoi (optarg
);
243 if (qstat_game_players_max
== 0)
244 qstat_game_players_max
= qstat_game_players
- 1;
245 if (qstat_game_players
< 0 || qstat_game_players
> QSTAT_MAX_RETURN_ARGS
)
248 case 130: /* index of max players field */
249 qstat_game_players_max
= atoi (optarg
);
250 if (qstat_game_players_max
< 0 || qstat_game_players_max
> QSTAT_MAX_RETURN_ARGS
)
253 default: /* args not parsable */
259 /* first option is the game type */
260 if (!game_type
&& c
<argc
)
261 game_type
= strdup (argv
[c
++]);
263 /* Second option is the server name */
264 if (!server_ip
&& c
<argc
)
265 server_ip
= strdup (argv
[c
++]);
267 return validate_arguments ();
272 validate_arguments (void)
274 if (qstat_game_players_max
< 0)
275 qstat_game_players_max
= 4;
277 if (qstat_game_players
< 0)
278 qstat_game_players
= 5;
280 if (qstat_game_field
< 0)
281 qstat_game_field
= 2;
283 if (qstat_map_field
< 0)
286 if (qstat_ping_field
< 0)
287 qstat_ping_field
= 5;
296 print_revision (progname
, revision
);
298 printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
299 printf (COPYRIGHT
, copyright
, email
);
301 printf (_("This plugin tests game server connections with the specified host."));
307 printf (_(UT_HELP_VRSN
));
308 printf (_(UT_EXTRA_OPTS
));
310 printf (" %s\n", "-p");
311 printf (" %s\n", _("Optional port of which to connect"));
312 printf (" %s\n", "gf");
313 printf (" %s\n", _("Field number in raw qstat output that contains game name"));
314 printf (" %s\n", "-mf");
315 printf (" %s\n", _("Field number in raw qstat output that contains map name"));
316 printf (" %s\n", "-pf");
317 printf (" %s\n", _("Field number in raw qstat output that contains ping time"));
319 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
322 printf ("%s\n", _("Notes:"));
323 printf (" %s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool."));
324 printf (" %s\n", _("If you don't have the package installed, you will need to download it from"));
325 printf (" %s\n", _("http://www.activesw.com/people/steve/qstat.html before you can use this plugin."));
328 printf (_(UT_EXTRA_OPTS_NOTES
));
331 printf (_(UT_SUPPORT
));
339 printf (_("Usage:"));
340 printf (" %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field] [-pf ping_field]\n", progname
);
343 /******************************************************************************
347 * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
349 * qstat -raw , -qs 67.20.190.61
350 * ==> QS,67.20.190.61,Nightmare.fintek.ca,67.20.190.61:26000,3,e2m1,6,0,83,0
352 * qstat -qs 67.20.190.61
353 * ==> ADDRESS PLAYERS MAP RESPONSE TIME NAME
354 * ==> 67.20.190.61 0/ 6 e2m1 79 / 0 Nightmare.fintek.ca
356 ******************************************************************************/