1 /******************************************************************************
3 * Nagios check_game plugin
6 * Copyright (c) 1999-2006 nagios-plugins 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
17 * License Information:
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 *****************************************************************************/
36 const char *progname
= "check_game";
37 const char *revision
= "$Revision$";
38 const char *copyright
= "2002-2006";
39 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
45 int process_arguments (int, char **);
46 int validate_arguments (void);
47 void print_help (void);
48 void print_usage (void);
50 #define QSTAT_DATA_DELIMITER ","
52 #define QSTAT_HOST_ERROR "ERROR"
53 #define QSTAT_HOST_DOWN "DOWN"
54 #define QSTAT_HOST_TIMEOUT "TIMEOUT"
55 #define QSTAT_MAX_RETURN_ARGS 12
63 int qstat_game_players_max
= -1;
64 int qstat_game_players
= -1;
65 int qstat_game_field
= -1;
66 int qstat_map_field
= -1;
67 int qstat_ping_field
= -1;
71 main (int argc
, char **argv
)
74 int result
= STATE_UNKNOWN
;
75 char *p
, *ret
[QSTAT_MAX_RETURN_ARGS
];
79 setlocale (LC_ALL
, "");
80 bindtextdomain (PACKAGE
, LOCALEDIR
);
83 if (process_arguments (argc
, argv
) == ERROR
)
84 usage_va(_("Could not parse arguments"));
88 /* create the command line to execute */
89 asprintf (&command_line
, "%s -raw %s -%s %s",
90 PATH_TO_QSTAT
, QSTAT_DATA_DELIMITER
, game_type
, server_ip
);
93 asprintf (&command_line
, "%s:%-d", command_line
, port
);
96 printf ("%s\n", command_line
);
98 /* run the command. historically, this plugin ignores output on stderr,
99 * as well as return status of the qstat program */
100 (void)np_runcmd(command_line
, &chld_out
, NULL
, 0);
103 /* was thinking about running qstat without any options, capturing the
104 -default line, parsing it & making an array of all know server types
105 but thought this would be too much hassle considering this is a tool
106 for intelligent sysadmins (ha). Could put a static array of known
107 server types in a header file but then we'd be limiting ourselves
109 In the end, I figured I'd simply let an error occur & then trap it
112 if (!strncmp (chld_out
.line
[0], "unknown option", 14)) {
113 printf (_("CRITICAL - Host type parameter incorrect!\n"));
114 result
= STATE_CRITICAL
;
118 p
= (char *) strtok (chld_out
.line
[0], QSTAT_DATA_DELIMITER
);
121 p
= (char *) strtok (NULL
, QSTAT_DATA_DELIMITER
);
123 if (i
>= QSTAT_MAX_RETURN_ARGS
)
127 if (strstr (ret
[2], QSTAT_HOST_ERROR
)) {
128 printf (_("CRITICAL - Host not found\n"));
129 result
= STATE_CRITICAL
;
131 else if (strstr (ret
[2], QSTAT_HOST_DOWN
)) {
132 printf (_("CRITICAL - Game server down or unavailable\n"));
133 result
= STATE_CRITICAL
;
135 else if (strstr (ret
[2], QSTAT_HOST_TIMEOUT
)) {
136 printf (_("CRITICAL - Game server timeout\n"));
137 result
= STATE_CRITICAL
;
140 printf ("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n",
141 ret
[qstat_game_players
],
142 ret
[qstat_game_players_max
],
143 ret
[qstat_game_field
],
144 ret
[qstat_map_field
],
145 ret
[qstat_ping_field
],
146 perfdata ("players", atol(ret
[qstat_game_players
]), "",
148 TRUE
, 0, TRUE
, atol(ret
[qstat_game_players_max
])),
149 fperfdata ("ping", strtod(ret
[qstat_ping_field
], NULL
), "",
159 process_arguments (int argc
, char **argv
)
164 static struct option long_opts
[] = {
165 {"help", no_argument
, 0, 'h'},
166 {"version", no_argument
, 0, 'V'},
167 {"verbose", no_argument
, 0, 'v'},
168 {"timeout", required_argument
, 0, 't'},
169 {"hostname", required_argument
, 0, 'H'},
170 {"port", required_argument
, 0, 'P'},
171 {"game-type", required_argument
, 0, 'G'},
172 {"map-field", required_argument
, 0, 'm'},
173 {"ping-field", required_argument
, 0, 'p'},
174 {"game-field", required_argument
, 0, 'g'},
175 {"players-field", required_argument
, 0, 129},
176 {"max-players-field", required_argument
, 0, 130},
183 for (c
= 1; c
< argc
; c
++) {
184 if (strcmp ("-mf", argv
[c
]) == 0)
185 strcpy (argv
[c
], "-m");
186 else if (strcmp ("-pf", argv
[c
]) == 0)
187 strcpy (argv
[c
], "-p");
188 else if (strcmp ("-gf", argv
[c
]) == 0)
189 strcpy (argv
[c
], "-g");
193 c
= getopt_long (argc
, argv
, "hVvt:H:P:G:g:p:m:", long_opts
, &opt_index
);
195 if (c
== -1 || c
== EOF
)
202 case 'V': /* version */
203 print_revision (progname
, revision
);
205 case 'v': /* version */
208 case 't': /* timeout period */
209 timeout_interval
= atoi (optarg
);
211 case 'H': /* hostname */
212 if (strlen (optarg
) >= MAX_HOST_ADDRESS_LENGTH
)
213 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
217 port
= atoi (optarg
);
219 case 'G': /* hostname */
220 if (strlen (optarg
) >= MAX_INPUT_BUFFER
)
221 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
224 case 'p': /* index of ping field */
225 qstat_ping_field
= atoi (optarg
);
226 if (qstat_ping_field
< 0 || qstat_ping_field
> QSTAT_MAX_RETURN_ARGS
)
229 case 'm': /* index on map field */
230 qstat_map_field
= atoi (optarg
);
231 if (qstat_map_field
< 0 || qstat_map_field
> QSTAT_MAX_RETURN_ARGS
)
234 case 'g': /* index of game field */
235 qstat_game_field
= atoi (optarg
);
236 if (qstat_game_field
< 0 || qstat_game_field
> QSTAT_MAX_RETURN_ARGS
)
239 case 129: /* index of player count field */
240 qstat_game_players
= atoi (optarg
);
241 if (qstat_game_players_max
== 0)
242 qstat_game_players_max
= qstat_game_players
- 1;
243 if (qstat_game_players
< 0 || qstat_game_players
> QSTAT_MAX_RETURN_ARGS
)
246 case 130: /* index of max players field */
247 qstat_game_players_max
= atoi (optarg
);
248 if (qstat_game_players_max
< 0 || qstat_game_players_max
> QSTAT_MAX_RETURN_ARGS
)
251 default: /* args not parsable */
257 /* first option is the game type */
258 if (!game_type
&& c
<argc
)
259 game_type
= strdup (argv
[c
++]);
261 /* Second option is the server name */
262 if (!server_ip
&& c
<argc
)
263 server_ip
= strdup (argv
[c
++]);
265 return validate_arguments ();
270 validate_arguments (void)
272 if (qstat_game_players_max
< 0)
273 qstat_game_players_max
= 4;
275 if (qstat_game_players
< 0)
276 qstat_game_players
= 5;
278 if (qstat_game_field
< 0)
279 qstat_game_field
= 2;
281 if (qstat_map_field
< 0)
284 if (qstat_ping_field
< 0)
285 qstat_ping_field
= 5;
294 print_revision (progname
, revision
);
296 printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
297 printf (COPYRIGHT
, copyright
, email
);
299 printf (_("This plugin tests game server connections with the specified host."));
305 printf (_(UT_HELP_VRSN
));
307 printf (" %s\n", "-p");
308 printf (" %s\n", _("Optional port of which to connect"));
309 printf (" %s\n", "gf");
310 printf (" %s\n", _("Field number in raw qstat output that contains game name"));
311 printf (" %s\n", "-mf");
312 printf (" %s\n", _("Field number in raw qstat output that contains map name"));
313 printf (" %s\n", "-pf");
314 printf (" %s\n", _("Field number in raw qstat output that contains ping time"));
316 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
318 printf ("%s\n", _("Notes:"));
320 printf ("%s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool ."));
322 printf ("%s\n", _("If you don't have the package installed, you will need to download it from"));
324 printf ("%s\n", _("http://www.activesw.com/people/steve/qstat.html before you can use this plugin."));
326 printf (_(UT_SUPPORT
));
334 printf (_("Usage:"));
335 printf (" %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field] [-pf ping_field]\n", progname
);
338 /******************************************************************************
342 * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
344 * qstat -raw , -qs 67.20.190.61
345 * ==> QS,67.20.190.61,Nightmare.fintek.ca,67.20.190.61:26000,3,e2m1,6,0,83,0
347 * qstat -qs 67.20.190.61
348 * ==> ADDRESS PLAYERS MAP RESPONSE TIME NAME
349 * ==> 67.20.190.61 0/ 6 e2m1 79 / 0 Nightmare.fintek.ca
351 ******************************************************************************/