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 if (process_arguments (argc
, argv
) == ERROR
)
83 usage_va(_("Could not parse arguments"));
87 /* create the command line to execute */
88 asprintf (&command_line
, "%s -raw %s -%s %s",
89 PATH_TO_QSTAT
, QSTAT_DATA_DELIMITER
, game_type
, server_ip
);
92 asprintf (&command_line
, "%s:%-d", command_line
, port
);
95 printf ("%s\n", command_line
);
97 /* run the command. historically, this plugin ignores output on stderr,
98 * as well as return status of the qstat program */
99 (void)np_runcmd(command_line
, &chld_out
, NULL
, 0);
102 /* was thinking about running qstat without any options, capturing the
103 -default line, parsing it & making an array of all know server types
104 but thought this would be too much hassle considering this is a tool
105 for intelligent sysadmins (ha). Could put a static array of known
106 server types in a header file but then we'd be limiting ourselves
108 In the end, I figured I'd simply let an error occur & then trap it
111 if (!strncmp (chld_out
.line
[0], "unknown option", 14)) {
112 printf (_("CRITICAL - Host type parameter incorrect!\n"));
113 result
= STATE_CRITICAL
;
117 p
= (char *) strtok (chld_out
.line
[0], QSTAT_DATA_DELIMITER
);
120 p
= (char *) strtok (NULL
, QSTAT_DATA_DELIMITER
);
122 if (i
>= QSTAT_MAX_RETURN_ARGS
)
126 if (strstr (ret
[2], QSTAT_HOST_ERROR
)) {
127 printf (_("CRITICAL - Host not found\n"));
128 result
= STATE_CRITICAL
;
130 else if (strstr (ret
[2], QSTAT_HOST_DOWN
)) {
131 printf (_("CRITICAL - Game server down or unavailable\n"));
132 result
= STATE_CRITICAL
;
134 else if (strstr (ret
[2], QSTAT_HOST_TIMEOUT
)) {
135 printf (_("CRITICAL - Game server timeout\n"));
136 result
= STATE_CRITICAL
;
139 printf ("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n",
140 ret
[qstat_game_players
],
141 ret
[qstat_game_players_max
],
142 ret
[qstat_game_field
],
143 ret
[qstat_map_field
],
144 ret
[qstat_ping_field
],
145 perfdata ("players", atol(ret
[qstat_game_players
]), "",
147 TRUE
, 0, TRUE
, atol(ret
[qstat_game_players_max
])),
148 fperfdata ("ping", strtod(ret
[qstat_ping_field
], NULL
), "",
158 process_arguments (int argc
, char **argv
)
163 static struct option long_opts
[] = {
164 {"help", no_argument
, 0, 'h'},
165 {"version", no_argument
, 0, 'V'},
166 {"verbose", no_argument
, 0, 'v'},
167 {"timeout", required_argument
, 0, 't'},
168 {"hostname", required_argument
, 0, 'H'},
169 {"port", required_argument
, 0, 'P'},
170 {"game-type", required_argument
, 0, 'G'},
171 {"map-field", required_argument
, 0, 'm'},
172 {"ping-field", required_argument
, 0, 'p'},
173 {"game-field", required_argument
, 0, 'g'},
174 {"players-field", required_argument
, 0, 129},
175 {"max-players-field", required_argument
, 0, 130},
182 for (c
= 1; c
< argc
; c
++) {
183 if (strcmp ("-mf", argv
[c
]) == 0)
184 strcpy (argv
[c
], "-m");
185 else if (strcmp ("-pf", argv
[c
]) == 0)
186 strcpy (argv
[c
], "-p");
187 else if (strcmp ("-gf", argv
[c
]) == 0)
188 strcpy (argv
[c
], "-g");
192 c
= getopt_long (argc
, argv
, "hVvt:H:P:G:g:p:m:", long_opts
, &opt_index
);
194 if (c
== -1 || c
== EOF
)
201 case 'V': /* version */
202 print_revision (progname
, revision
);
204 case 'v': /* version */
207 case 't': /* timeout period */
208 timeout_interval
= atoi (optarg
);
210 case 'H': /* hostname */
211 if (strlen (optarg
) >= MAX_HOST_ADDRESS_LENGTH
)
212 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
216 port
= atoi (optarg
);
218 case 'G': /* hostname */
219 if (strlen (optarg
) >= MAX_INPUT_BUFFER
)
220 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
223 case 'p': /* index of ping field */
224 qstat_ping_field
= atoi (optarg
);
225 if (qstat_ping_field
< 0 || qstat_ping_field
> QSTAT_MAX_RETURN_ARGS
)
228 case 'm': /* index on map field */
229 qstat_map_field
= atoi (optarg
);
230 if (qstat_map_field
< 0 || qstat_map_field
> QSTAT_MAX_RETURN_ARGS
)
233 case 'g': /* index of game field */
234 qstat_game_field
= atoi (optarg
);
235 if (qstat_game_field
< 0 || qstat_game_field
> QSTAT_MAX_RETURN_ARGS
)
238 case 129: /* index of player count field */
239 qstat_game_players
= atoi (optarg
);
240 if (qstat_game_players_max
== 0)
241 qstat_game_players_max
= qstat_game_players
- 1;
242 if (qstat_game_players
< 0 || qstat_game_players
> QSTAT_MAX_RETURN_ARGS
)
245 case 130: /* index of max players field */
246 qstat_game_players_max
= atoi (optarg
);
247 if (qstat_game_players_max
< 0 || qstat_game_players_max
> QSTAT_MAX_RETURN_ARGS
)
250 default: /* args not parsable */
256 /* first option is the game type */
257 if (!game_type
&& c
<argc
)
258 game_type
= strdup (argv
[c
++]);
260 /* Second option is the server name */
261 if (!server_ip
&& c
<argc
)
262 server_ip
= strdup (argv
[c
++]);
264 return validate_arguments ();
269 validate_arguments (void)
271 if (qstat_game_players_max
< 0)
272 qstat_game_players_max
= 4;
274 if (qstat_game_players
< 0)
275 qstat_game_players
= 5;
277 if (qstat_game_field
< 0)
278 qstat_game_field
= 2;
280 if (qstat_map_field
< 0)
283 if (qstat_ping_field
< 0)
284 qstat_ping_field
= 5;
293 print_revision (progname
, revision
);
295 printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
296 printf (COPYRIGHT
, copyright
, email
);
298 printf (_("This plugin tests game server connections with the specified host."));
304 printf (_(UT_HELP_VRSN
));
306 printf (" %s\n", "-p");
307 printf (" %s\n", _("Optional port of which to connect"));
308 printf (" %s\n", "gf");
309 printf (" %s\n", _("Field number in raw qstat output that contains game name"));
310 printf (" %s\n", "-mf");
311 printf (" %s\n", _("Field number in raw qstat output that contains map name"));
312 printf (" %s\n", "-pf");
313 printf (" %s\n", _("Field number in raw qstat output that contains ping time"));
315 printf (_(UT_TIMEOUT
), DEFAULT_SOCKET_TIMEOUT
);
317 printf ("%s\n", _("Notes:"));
319 printf ("%s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool ."));
321 printf ("%s\n", _("If you don't have the package installed, you will need to download it from"));
323 printf ("%s\n", _("http://www.activesw.com/people/steve/qstat.html before you can use this plugin."));
325 printf (_(UT_SUPPORT
));
333 printf (_("Usage:"));
334 printf (" %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field] [-pf ping_field]\n", progname
);
337 /******************************************************************************
341 * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
343 * qstat -raw , -qs 67.20.190.61
344 * ==> QS,67.20.190.61,Nightmare.fintek.ca,67.20.190.61:26000,3,e2m1,6,0,83,0
346 * qstat -qs 67.20.190.61
347 * ==> ADDRESS PLAYERS MAP RESPONSE TIME NAME
348 * ==> 67.20.190.61 0/ 6 e2m1 79 / 0 Nightmare.fintek.ca
350 ******************************************************************************/