Code

Fix translations when extra-opts aren't enabled
[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 * Description:
9
10 * This file contains the check_game plugin
11
12 * This plugin tests game server connections with the specified host.
13 * using the qstat program
14
15
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.
20
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.
25
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/>.
28
29 *
30 *****************************************************************************/
32 const char *progname = "check_game";
33 const char *copyright = "2002-2007";
34 const char *email = "nagiosplug-devel@lists.sourceforge.net";
36 #include "common.h"
37 #include "utils.h"
38 #include "runcmd.h"
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
52 char *server_ip;
53 char *game_type;
54 int port = 0;
56 int verbose;
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;
65 int
66 main (int argc, char **argv)
67 {
68   char *command_line;
69   int result = STATE_UNKNOWN;
70   char *p, *ret[QSTAT_MAX_RETURN_ARGS];
71   size_t i = 0;
72   output chld_out;
74   setlocale (LC_ALL, "");
75   bindtextdomain (PACKAGE, LOCALEDIR);
76   textdomain (PACKAGE);
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"));
84   result = STATE_OK;
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);
90   if (port)
91     asprintf (&command_line, "%s:%-d", command_line, port);
93   if (verbose > 0)
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);
100   /* sanity check */
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
108    */
110   if (!strncmp (chld_out.line[0], "unknown option", 14)) {
111     printf (_("CRITICAL - Host type parameter incorrect!\n"));
112     result = STATE_CRITICAL;
113     return result;
114   }
116   p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER);
117   while (p != NULL) {
118     ret[i] = p;
119     p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER);
120     i++;
121     if (i >= QSTAT_MAX_RETURN_ARGS)
122       break;
123   }
125   if (strstr (ret[2], QSTAT_HOST_ERROR)) {
126     printf (_("CRITICAL - Host not found\n"));
127     result = STATE_CRITICAL;
128   }
129   else if (strstr (ret[2], QSTAT_HOST_DOWN)) {
130     printf (_("CRITICAL - Game server down or unavailable\n"));
131     result = STATE_CRITICAL;
132   }
133   else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) {
134     printf (_("CRITICAL - Game server timeout\n"));
135     result = STATE_CRITICAL;
136   }
137   else {
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]), "",
145                       FALSE, 0, FALSE, 0,
146                       TRUE, 0, TRUE, atol(ret[qstat_game_players_max])),
147             fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "",
148                       FALSE, 0, FALSE, 0,
149                       TRUE, 0, FALSE, 0));
150   }
152   return result;
156 int
157 process_arguments (int argc, char **argv)
159   int c;
161   int opt_index = 0;
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},
175     {0, 0, 0, 0}
176   };
178   if (argc < 2)
179     return ERROR;
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");
188   }
190   while (1) {
191     c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
193     if (c == -1 || c == EOF)
194       break;
196     switch (c) {
197     case 'h': /* help */
198       print_help ();
199       exit (STATE_OK);
200     case 'V': /* version */
201       print_revision (progname, NP_VERSION);
202       exit (STATE_OK);
203     case 'v': /* version */
204       verbose = TRUE;
205       break;
206     case 't': /* timeout period */
207       timeout_interval = atoi (optarg);
208       break;
209     case 'H': /* hostname */
210       if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH)
211         die (STATE_UNKNOWN, _("Input buffer overflow\n"));
212       server_ip = optarg;
213       break;
214     case 'P': /* port */
215       port = atoi (optarg);
216       break;
217     case 'G': /* hostname */
218       if (strlen (optarg) >= MAX_INPUT_BUFFER)
219         die (STATE_UNKNOWN, _("Input buffer overflow\n"));
220       game_type = optarg;
221       break;
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)
225         return ERROR;
226       break;
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)
230         return ERROR;
231       break;
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)
235         return ERROR;
236       break;
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)
242         return ERROR;
243       break;
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)
247         return ERROR;
248       break;
249     default: /* args not parsable */
250       usage5();
251     }
252   }
254   c = optind;
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 ();
267 int
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)
280     qstat_map_field = 3;
282   if (qstat_ping_field < 0)
283     qstat_ping_field = 5;
285   return OK;
289 void
290 print_help (void)
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."));
299   printf ("\n\n");
301   print_usage ();
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);
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."));
322 #ifdef NP_EXTRA_OPTS
323   printf ("\n");
324   printf (UT_EXTRA_OPTS_NOTES);
325 #endif
327   printf (UT_SUPPORT);
332 void
333 print_usage (void)
335   printf (_("Usage:"));
336   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);
339 /******************************************************************************
340  *
341  * Test Cases:
342  *
343  * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
344  *
345  * qstat -raw , -qs 67.20.190.61
346  *  ==> QS,67.20.190.61,Nightmare.fintek.ca,67.20.190.61:26000,3,e2m1,6,0,83,0
347  *
348  * qstat -qs 67.20.190.61
349  *  ==> ADDRESS           PLAYERS      MAP   RESPONSE TIME    NAME
350  *  ==> 67.20.190.61            0/ 6     e2m1     79 / 0   Nightmare.fintek.ca
351  *
352  ******************************************************************************/