Not picking up solaris systems correctly for check_dhcp. Only
[monitoring-plugins.git] / plugins / check_game.c
blob4bc57c8f3b635b520132f7b90629c9f168568a5d
1 /*****************************************************************************
2 *
3 * Nagios check_game plugin
4 *
5 * License: GPL
6 * Copyright (c) 2002-2007 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
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/>.
31 * $Id$
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";
40 #include "common.h"
41 #include "utils.h"
42 #include "runcmd.h"
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
56 char *server_ip;
57 char *game_type;
58 int port = 0;
60 int verbose;
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;
69 int
70 main (int argc, char **argv)
72 char *command_line;
73 int result = STATE_UNKNOWN;
74 char *p, *ret[QSTAT_MAX_RETURN_ARGS];
75 size_t i = 0;
76 output chld_out;
78 setlocale (LC_ALL, "");
79 bindtextdomain (PACKAGE, LOCALEDIR);
80 textdomain (PACKAGE);
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"));
88 result = STATE_OK;
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);
94 if (port)
95 asprintf (&command_line, "%s:%-d", command_line, port);
97 if (verbose > 0)
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);
104 /* sanity check */
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;
117 return result;
120 p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER);
121 while (p != NULL) {
122 ret[i] = p;
123 p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER);
124 i++;
125 if (i >= QSTAT_MAX_RETURN_ARGS)
126 break;
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;
141 else {
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]), "",
149 FALSE, 0, FALSE, 0,
150 TRUE, 0, TRUE, atol(ret[qstat_game_players_max])),
151 fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "",
152 FALSE, 0, FALSE, 0,
153 TRUE, 0, FALSE, 0));
156 return result;
161 process_arguments (int argc, char **argv)
163 int c;
165 int opt_index = 0;
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},
179 {0, 0, 0, 0}
182 if (argc < 2)
183 return ERROR;
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");
194 while (1) {
195 c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
197 if (c == -1 || c == EOF)
198 break;
200 switch (c) {
201 case 'h': /* help */
202 print_help ();
203 exit (STATE_OK);
204 case 'V': /* version */
205 print_revision (progname, revision);
206 exit (STATE_OK);
207 case 'v': /* version */
208 verbose = TRUE;
209 break;
210 case 't': /* timeout period */
211 timeout_interval = atoi (optarg);
212 break;
213 case 'H': /* hostname */
214 if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH)
215 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
216 server_ip = optarg;
217 break;
218 case 'P': /* port */
219 port = atoi (optarg);
220 break;
221 case 'G': /* hostname */
222 if (strlen (optarg) >= MAX_INPUT_BUFFER)
223 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
224 game_type = optarg;
225 break;
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)
229 return ERROR;
230 break;
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)
234 return ERROR;
235 break;
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)
239 return ERROR;
240 break;
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)
246 return ERROR;
247 break;
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)
251 return ERROR;
252 break;
253 default: /* args not parsable */
254 usage5();
258 c = optind;
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)
284 qstat_map_field = 3;
286 if (qstat_ping_field < 0)
287 qstat_ping_field = 5;
289 return OK;
293 void
294 print_help (void)
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."));
303 printf ("\n\n");
305 print_usage ();
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);
321 printf ("\n");
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."));
326 #ifdef NP_EXTRA_OPTS
327 printf ("\n");
328 printf (_(UT_EXTRA_OPTS_NOTES));
329 #endif
331 printf (_(UT_SUPPORT));
336 void
337 print_usage (void)
339 printf (_("Usage:"));
340 printf (" %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field] [-pf ping_field]\n", progname);
343 /******************************************************************************
345 * Test Cases:
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 ******************************************************************************/