1 /*****************************************************************************
3 * Nagios check_game plugin
6 * Copyright (c) 2002-2007 Nagios 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-2007";
34 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
40 int process_arguments (int, char **);
41 int validate_arguments (void);
42 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
58 int qstat_game_players_max
= -1;
59 int qstat_game_players
= -1;
60 int qstat_game_field
= -1;
61 int qstat_map_field
= -1;
62 int qstat_ping_field
= -1;
66 main (int argc
, char **argv
)
69 int result
= STATE_UNKNOWN
;
70 char *p
, *ret
[QSTAT_MAX_RETURN_ARGS
];
74 setlocale (LC_ALL
, "");
75 bindtextdomain (PACKAGE
, LOCALEDIR
);
78 /* Parse extra opts if any */
79 argv
=np_extra_opts (&argc
, argv
, progname
);
81 if (process_arguments (argc
, argv
) == ERROR
)
82 usage_va(_("Could not parse arguments"));
86 /* create the command line to execute */
87 asprintf (&command_line
, "%s -raw %s -%s %s",
88 PATH_TO_QSTAT
, QSTAT_DATA_DELIMITER
, game_type
, server_ip
);
91 asprintf (&command_line
, "%s:%-d", command_line
, port
);
94 printf ("%s\n", command_line
);
96 /* run the command. historically, this plugin ignores output on stderr,
97 * as well as return status of the qstat program */
98 (void)np_runcmd(command_line
, &chld_out
, NULL
, 0);
101 /* was thinking about running qstat without any options, capturing the
102 -default line, parsing it & making an array of all know server types
103 but thought this would be too much hassle considering this is a tool
104 for intelligent sysadmins (ha). Could put a static array of known
105 server types in a header file but then we'd be limiting ourselves
107 In the end, I figured I'd simply let an error occur & then trap it
110 if (!strncmp (chld_out
.line
[0], "unknown option", 14)) {
111 printf (_("CRITICAL - Host type parameter incorrect!\n"));
112 result
= STATE_CRITICAL
;
116 p
= (char *) strtok (chld_out
.line
[0], QSTAT_DATA_DELIMITER
);
119 p
= (char *) strtok (NULL
, QSTAT_DATA_DELIMITER
);
121 if (i
>= QSTAT_MAX_RETURN_ARGS
)
125 if (strstr (ret
[2], QSTAT_HOST_ERROR
)) {
126 printf (_("CRITICAL - Host not found\n"));
127 result
= STATE_CRITICAL
;
129 else if (strstr (ret
[2], QSTAT_HOST_DOWN
)) {
130 printf (_("CRITICAL - Game server down or unavailable\n"));
131 result
= STATE_CRITICAL
;
133 else if (strstr (ret
[2], QSTAT_HOST_TIMEOUT
)) {
134 printf (_("CRITICAL - Game server timeout\n"));
135 result
= STATE_CRITICAL
;
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
]), "",
146 TRUE
, 0, TRUE
, atol(ret
[qstat_game_players_max
])),
147 fperfdata ("ping", strtod(ret
[qstat_ping_field
], NULL
), "",
157 process_arguments (int argc
, char **argv
)
162 static struct option long_opts
[] = {
163 {"help", no_argument
, 0, 'h'},
164 {"version", no_argument
, 0, 'V'},
165 {"verbose", no_argument
, 0, 'v'},
166 {"timeout", required_argument
, 0, 't'},
167 {"hostname", required_argument
, 0, 'H'},
168 {"port", required_argument
, 0, 'P'},
169 {"game-type", required_argument
, 0, 'G'},
170 {"map-field", required_argument
, 0, 'm'},
171 {"ping-field", required_argument
, 0, 'p'},
172 {"game-field", required_argument
, 0, 'g'},
173 {"players-field", required_argument
, 0, 129},
174 {"max-players-field", required_argument
, 0, 130},
181 for (c
= 1; c
< argc
; c
++) {
182 if (strcmp ("-mf", argv
[c
]) == 0)
183 strcpy (argv
[c
], "-m");
184 else if (strcmp ("-pf", argv
[c
]) == 0)
185 strcpy (argv
[c
], "-p");
186 else if (strcmp ("-gf", argv
[c
]) == 0)
187 strcpy (argv
[c
], "-g");
191 c
= getopt_long (argc
, argv
, "hVvt:H:P:G:g:p:m:", long_opts
, &opt_index
);
193 if (c
== -1 || c
== EOF
)
200 case 'V': /* version */
201 print_revision (progname
, NP_VERSION
);
203 case 'v': /* version */
206 case 't': /* timeout period */
207 timeout_interval
= atoi (optarg
);
209 case 'H': /* hostname */
210 if (strlen (optarg
) >= MAX_HOST_ADDRESS_LENGTH
)
211 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
215 port
= atoi (optarg
);
217 case 'G': /* hostname */
218 if (strlen (optarg
) >= MAX_INPUT_BUFFER
)
219 die (STATE_UNKNOWN
, _("Input buffer overflow\n"));
222 case 'p': /* index of ping field */
223 qstat_ping_field
= atoi (optarg
);
224 if (qstat_ping_field
< 0 || qstat_ping_field
> QSTAT_MAX_RETURN_ARGS
)
227 case 'm': /* index on map field */
228 qstat_map_field
= atoi (optarg
);
229 if (qstat_map_field
< 0 || qstat_map_field
> QSTAT_MAX_RETURN_ARGS
)
232 case 'g': /* index of game field */
233 qstat_game_field
= atoi (optarg
);
234 if (qstat_game_field
< 0 || qstat_game_field
> QSTAT_MAX_RETURN_ARGS
)
237 case 129: /* index of player count field */
238 qstat_game_players
= atoi (optarg
);
239 if (qstat_game_players_max
== 0)
240 qstat_game_players_max
= qstat_game_players
- 1;
241 if (qstat_game_players
< 0 || qstat_game_players
> QSTAT_MAX_RETURN_ARGS
)
244 case 130: /* index of max players field */
245 qstat_game_players_max
= atoi (optarg
);
246 if (qstat_game_players_max
< 0 || qstat_game_players_max
> QSTAT_MAX_RETURN_ARGS
)
249 default: /* args not parsable */
255 /* first option is the game type */
256 if (!game_type
&& c
<argc
)
257 game_type
= strdup (argv
[c
++]);
259 /* Second option is the server name */
260 if (!server_ip
&& c
<argc
)
261 server_ip
= strdup (argv
[c
++]);
263 return validate_arguments ();
268 validate_arguments (void)
270 if (qstat_game_players_max
< 0)
271 qstat_game_players_max
= 4;
273 if (qstat_game_players
< 0)
274 qstat_game_players
= 5;
276 if (qstat_game_field
< 0)
277 qstat_game_field
= 2;
279 if (qstat_map_field
< 0)
282 if (qstat_ping_field
< 0)
283 qstat_ping_field
= 5;
292 print_revision (progname
, NP_VERSION
);
294 printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
295 printf (COPYRIGHT
, copyright
, email
);
297 printf (_("This plugin tests game server connections with the specified host."));
303 printf (UT_HELP_VRSN
);
304 printf (UT_EXTRA_OPTS
);
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
);
318 printf ("%s\n", _("Notes:"));
319 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"));
321 printf (" %s\n", _("http://www.activesw.com/people/steve/qstat.html before you can use this plugin."));
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
);
335 /******************************************************************************
339 * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
341 * qstat -raw , -qs 67.20.190.61
342 * ==> QS,67.20.190.61,Nightmare.fintek.ca,67.20.190.61:26000,3,e2m1,6,0,83,0
344 * qstat -qs 67.20.190.61
345 * ==> ADDRESS PLAYERS MAP RESPONSE TIME NAME
346 * ==> 67.20.190.61 0/ 6 e2m1 79 / 0 Nightmare.fintek.ca
348 ******************************************************************************/