Code

0e4120dda41516db54238c55af7cddc49c74d7f2
[nagiosplug.git] / plugins / check_game.c
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:
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
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.
22
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.
27
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/>.
30
31 * $Id$
32 *
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)
71 {
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   if (process_arguments (argc, argv) == ERROR)
83     usage_va(_("Could not parse arguments"));
85   result = STATE_OK;
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);
91   if (port)
92     asprintf (&command_line, "%s:%-d", command_line, port);
94   if (verbose > 0)
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);
101   /* sanity check */
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
109    */
111   if (!strncmp (chld_out.line[0], "unknown option", 14)) {
112     printf (_("CRITICAL - Host type parameter incorrect!\n"));
113     result = STATE_CRITICAL;
114     return result;
115   }
117   p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER);
118   while (p != NULL) {
119     ret[i] = p;
120     p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER);
121     i++;
122     if (i >= QSTAT_MAX_RETURN_ARGS)
123       break;
124   }
126   if (strstr (ret[2], QSTAT_HOST_ERROR)) {
127     printf (_("CRITICAL - Host not found\n"));
128     result = STATE_CRITICAL;
129   }
130   else if (strstr (ret[2], QSTAT_HOST_DOWN)) {
131     printf (_("CRITICAL - Game server down or unavailable\n"));
132     result = STATE_CRITICAL;
133   }
134   else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) {
135     printf (_("CRITICAL - Game server timeout\n"));
136     result = STATE_CRITICAL;
137   }
138   else {
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]), "",
146                       FALSE, 0, FALSE, 0,
147                       TRUE, 0, TRUE, atol(ret[qstat_game_players_max])),
148             fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "",
149                       FALSE, 0, FALSE, 0,
150                       TRUE, 0, FALSE, 0));
151   }
153   return result;
157 int
158 process_arguments (int argc, char **argv)
160   int c;
162   int opt_index = 0;
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},
176     {0, 0, 0, 0}
177   };
179   if (argc < 2)
180     return ERROR;
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");
189   }
191   while (1) {
192     c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
194     if (c == -1 || c == EOF)
195       break;
197     switch (c) {
198     case 'h': /* help */
199       print_help ();
200       exit (STATE_OK);
201     case 'V': /* version */
202       print_revision (progname, revision);
203       exit (STATE_OK);
204     case 'v': /* version */
205       verbose = TRUE;
206       break;
207     case 't': /* timeout period */
208       timeout_interval = atoi (optarg);
209       break;
210     case 'H': /* hostname */
211       if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH)
212         die (STATE_UNKNOWN, _("Input buffer overflow\n"));
213       server_ip = optarg;
214       break;
215     case 'P': /* port */
216       port = atoi (optarg);
217       break;
218     case 'G': /* hostname */
219       if (strlen (optarg) >= MAX_INPUT_BUFFER)
220         die (STATE_UNKNOWN, _("Input buffer overflow\n"));
221       game_type = optarg;
222       break;
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)
226         return ERROR;
227       break;
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)
231         return ERROR;
232       break;
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)
236         return ERROR;
237       break;
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)
243         return ERROR;
244       break;
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)
248         return ERROR;
249       break;
250     default: /* args not parsable */
251       usage5();
252     }
253   }
255   c = optind;
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 ();
268 int
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)
281     qstat_map_field = 3;
283   if (qstat_ping_field < 0)
284     qstat_ping_field = 5;
286   return OK;
290 void
291 print_help (void)
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."));
300   printf ("\n\n");
302   print_usage ();
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 ("\n");
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."));
323   printf (_(UT_SUPPORT));
328 void
329 print_usage (void)
331   printf (_("Usage:"));
332   printf (" %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field] [-pf ping_field]\n", progname);
335 /******************************************************************************
336  *
337  * Test Cases:
338  *
339  * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
340  * 
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
343  *
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
347  *
348  ******************************************************************************/