Code

Changed default test values for check_dns (using nagios.com)
[nagiosplug.git] / plugins / check_game.c
1 /******************************************************************************
2 *
3 * Nagios check_game plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_game plugin
13 *
14 * This plugin tests game server connections with the specified host.
15 * using the qstat program
16 *
17 * License Information:
18 *
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.
23 *
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.
28 *
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.
32 *
33 * $Id$
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";
41 #include "common.h"
42 #include "utils.h"
43 #include "runcmd.h"
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
57 char *server_ip;
58 char *game_type;
59 int port = 0;
61 int verbose;
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;
70 int
71 main (int argc, char **argv)
72 {
73   char *command_line;
74   int result = STATE_UNKNOWN;
75   char *p, *ret[QSTAT_MAX_RETURN_ARGS];
76   size_t i = 0;
77   output chld_out;
79   setlocale (LC_ALL, "");
80   bindtextdomain (PACKAGE, LOCALEDIR);
81   textdomain (PACKAGE);
82   
83   if (process_arguments (argc, argv) == ERROR)
84     usage_va(_("Could not parse arguments"));
86   result = STATE_OK;
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);
91   
92   if (port)
93     asprintf (&command_line, "%s:%-d", command_line, port);
95   if (verbose > 0)
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);
102   /* sanity check */
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
110    */
112   if (!strncmp (chld_out.line[0], "unknown option", 14)) {
113     printf (_("CRITICAL - Host type parameter incorrect!\n"));
114     result = STATE_CRITICAL;
115     return result;
116   }
118   p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER);
119   while (p != NULL) {
120     ret[i] = p;
121     p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER);
122     i++;
123     if (i >= QSTAT_MAX_RETURN_ARGS)
124       break;
125   }
127   if (strstr (ret[2], QSTAT_HOST_ERROR)) {
128     printf (_("CRITICAL - Host not found\n"));
129     result = STATE_CRITICAL;
130   }
131   else if (strstr (ret[2], QSTAT_HOST_DOWN)) {
132     printf (_("CRITICAL - Game server down or unavailable\n"));
133     result = STATE_CRITICAL;
134   }
135   else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) {
136     printf (_("CRITICAL - Game server timeout\n"));
137     result = STATE_CRITICAL;
138   }
139   else {
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]), "",
147                       FALSE, 0, FALSE, 0,
148                       TRUE, 0, TRUE, atol(ret[qstat_game_players_max])),
149             fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "",
150                       FALSE, 0, FALSE, 0,
151                       TRUE, 0, FALSE, 0));
152   }
154   return result;
158 int
159 process_arguments (int argc, char **argv)
161   int c;
163   int opt_index = 0;
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},
177     {0, 0, 0, 0}
178   };
180   if (argc < 2)
181     return ERROR;
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");
190   }
192   while (1) {
193     c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
195     if (c == -1 || c == EOF)
196       break;
198     switch (c) {
199     case 'h': /* help */
200       print_help ();
201       exit (STATE_OK);
202     case 'V': /* version */
203       print_revision (progname, revision);
204       exit (STATE_OK);
205     case 'v': /* version */
206       verbose = TRUE;
207       break;
208     case 't': /* timeout period */
209       timeout_interval = atoi (optarg);
210       break;
211     case 'H': /* hostname */
212       if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH)
213         die (STATE_UNKNOWN, _("Input buffer overflow\n"));
214       server_ip = optarg;
215       break;
216     case 'P': /* port */
217       port = atoi (optarg);
218       break;
219     case 'G': /* hostname */
220       if (strlen (optarg) >= MAX_INPUT_BUFFER)
221         die (STATE_UNKNOWN, _("Input buffer overflow\n"));
222       game_type = optarg;
223       break;
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)
227         return ERROR;
228       break;
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)
232         return ERROR;
233       break;
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)
237         return ERROR;
238       break;
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)
244         return ERROR;
245       break;
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)
249         return ERROR;
250       break;
251     default: /* args not parsable */
252       usage5();
253     }
254   }
256   c = optind;
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 ();
269 int
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)
282     qstat_map_field = 3;
284   if (qstat_ping_field < 0)
285     qstat_ping_field = 5;
287   return OK;
291 void
292 print_help (void)
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."));
301   printf ("\n\n");
302  
303   print_usage ();
305   printf (_(UT_HELP_VRSN));
306   
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));
331 void
332 print_usage (void)
334   printf (_("Usage:"));
335   printf (" %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field] [-pf ping_field]\n", progname);
338 /******************************************************************************
339  *
340  * Test Cases:
341  *
342  * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
343  * 
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
346  *
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
350  *
351  ******************************************************************************/