Back to index

nagios-plugins  1.4.16
check_game.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios check_game plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2002-2007 Nagios Plugins Development Team
00007 * 
00008 * Description:
00009 * 
00010 * This file contains the check_game plugin
00011 * 
00012 * This plugin tests game server connections with the specified host.
00013 * using the qstat program
00014 * 
00015 * 
00016 * This program is free software: you can redistribute it and/or modify
00017 * it under the terms of the GNU General Public License as published by
00018 * the Free Software Foundation, either version 3 of the License, or
00019 * (at your option) any later version.
00020 * 
00021 * This program is distributed in the hope that it will be useful,
00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 * GNU General Public License for more details.
00025 * 
00026 * You should have received a copy of the GNU General Public License
00027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00028 * 
00029 *
00030 *****************************************************************************/
00031 
00032 const char *progname = "check_game";
00033 const char *copyright = "2002-2007";
00034 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00035 
00036 #include "common.h"
00037 #include "utils.h"
00038 #include "runcmd.h"
00039 
00040 int process_arguments (int, char **);
00041 int validate_arguments (void);
00042 void print_help (void);
00043 void print_usage (void);
00044 
00045 #define QSTAT_DATA_DELIMITER  ","
00046 
00047 #define QSTAT_HOST_ERROR  "ERROR"
00048 #define QSTAT_HOST_DOWN   "DOWN"
00049 #define QSTAT_HOST_TIMEOUT  "TIMEOUT"
00050 #define QSTAT_MAX_RETURN_ARGS 12
00051 
00052 char *server_ip;
00053 char *game_type;
00054 int port = 0;
00055 
00056 int verbose;
00057 
00058 int qstat_game_players_max = -1;
00059 int qstat_game_players = -1;
00060 int qstat_game_field = -1;
00061 int qstat_map_field = -1;
00062 int qstat_ping_field = -1;
00063 
00064 
00065 int
00066 main (int argc, char **argv)
00067 {
00068   char *command_line;
00069   int result = STATE_UNKNOWN;
00070   char *p, *ret[QSTAT_MAX_RETURN_ARGS];
00071   size_t i = 0;
00072   output chld_out;
00073 
00074   setlocale (LC_ALL, "");
00075   bindtextdomain (PACKAGE, LOCALEDIR);
00076   textdomain (PACKAGE);
00077 
00078   /* Parse extra opts if any */
00079   argv=np_extra_opts (&argc, argv, progname);
00080 
00081   if (process_arguments (argc, argv) == ERROR)
00082     usage_va(_("Could not parse arguments"));
00083 
00084   result = STATE_OK;
00085 
00086   /* create the command line to execute */
00087   asprintf (&command_line, "%s -raw %s -%s %s",
00088             PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip);
00089 
00090   if (port)
00091     asprintf (&command_line, "%s:%-d", command_line, port);
00092 
00093   if (verbose > 0)
00094     printf ("%s\n", command_line);
00095 
00096   /* run the command. historically, this plugin ignores output on stderr,
00097    * as well as return status of the qstat program */
00098   (void)np_runcmd(command_line, &chld_out, NULL, 0);
00099 
00100   /* sanity check */
00101   /* was thinking about running qstat without any options, capturing the
00102      -default line, parsing it & making an array of all know server types
00103      but thought this would be too much hassle considering this is a tool
00104      for intelligent sysadmins (ha). Could put a static array of known
00105      server types in a header file but then we'd be limiting ourselves
00106 
00107      In the end, I figured I'd simply let an error occur & then trap it
00108    */
00109 
00110   if (!strncmp (chld_out.line[0], "unknown option", 14)) {
00111     printf (_("CRITICAL - Host type parameter incorrect!\n"));
00112     result = STATE_CRITICAL;
00113     return result;
00114   }
00115 
00116   p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER);
00117   while (p != NULL) {
00118     ret[i] = p;
00119     p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER);
00120     i++;
00121     if (i >= QSTAT_MAX_RETURN_ARGS)
00122       break;
00123   }
00124 
00125   if (strstr (ret[2], QSTAT_HOST_ERROR)) {
00126     printf (_("CRITICAL - Host not found\n"));
00127     result = STATE_CRITICAL;
00128   }
00129   else if (strstr (ret[2], QSTAT_HOST_DOWN)) {
00130     printf (_("CRITICAL - Game server down or unavailable\n"));
00131     result = STATE_CRITICAL;
00132   }
00133   else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) {
00134     printf (_("CRITICAL - Game server timeout\n"));
00135     result = STATE_CRITICAL;
00136   }
00137   else {
00138     printf ("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n",
00139             ret[qstat_game_players],
00140             ret[qstat_game_players_max],
00141             ret[qstat_game_field],
00142             ret[qstat_map_field],
00143             ret[qstat_ping_field],
00144             perfdata ("players", atol(ret[qstat_game_players]), "",
00145                       FALSE, 0, FALSE, 0,
00146                       TRUE, 0, TRUE, atol(ret[qstat_game_players_max])),
00147             fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "",
00148                       FALSE, 0, FALSE, 0,
00149                       TRUE, 0, FALSE, 0));
00150   }
00151 
00152   return result;
00153 }
00154 
00155 
00156 int
00157 process_arguments (int argc, char **argv)
00158 {
00159   int c;
00160 
00161   int opt_index = 0;
00162   static struct option long_opts[] = {
00163     {"help", no_argument, 0, 'h'},
00164     {"version", no_argument, 0, 'V'},
00165     {"verbose", no_argument, 0, 'v'},
00166     {"timeout", required_argument, 0, 't'},
00167     {"hostname", required_argument, 0, 'H'},
00168     {"port", required_argument, 0, 'P'},
00169     {"game-type", required_argument, 0, 'G'},
00170     {"map-field", required_argument, 0, 'm'},
00171     {"ping-field", required_argument, 0, 'p'},
00172     {"game-field", required_argument, 0, 'g'},
00173     {"players-field", required_argument, 0, 129},
00174     {"max-players-field", required_argument, 0, 130},
00175     {0, 0, 0, 0}
00176   };
00177 
00178   if (argc < 2)
00179     return ERROR;
00180 
00181   for (c = 1; c < argc; c++) {
00182     if (strcmp ("-mf", argv[c]) == 0)
00183       strcpy (argv[c], "-m");
00184     else if (strcmp ("-pf", argv[c]) == 0)
00185       strcpy (argv[c], "-p");
00186     else if (strcmp ("-gf", argv[c]) == 0)
00187       strcpy (argv[c], "-g");
00188   }
00189 
00190   while (1) {
00191     c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
00192 
00193     if (c == -1 || c == EOF)
00194       break;
00195 
00196     switch (c) {
00197     case 'h': /* help */
00198       print_help ();
00199       exit (STATE_OK);
00200     case 'V': /* version */
00201       print_revision (progname, NP_VERSION);
00202       exit (STATE_OK);
00203     case 'v': /* version */
00204       verbose = TRUE;
00205       break;
00206     case 't': /* timeout period */
00207       timeout_interval = atoi (optarg);
00208       break;
00209     case 'H': /* hostname */
00210       if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH)
00211         die (STATE_UNKNOWN, _("Input buffer overflow\n"));
00212       server_ip = optarg;
00213       break;
00214     case 'P': /* port */
00215       port = atoi (optarg);
00216       break;
00217     case 'G': /* hostname */
00218       if (strlen (optarg) >= MAX_INPUT_BUFFER)
00219         die (STATE_UNKNOWN, _("Input buffer overflow\n"));
00220       game_type = optarg;
00221       break;
00222     case 'p': /* index of ping field */
00223       qstat_ping_field = atoi (optarg);
00224       if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS)
00225         return ERROR;
00226       break;
00227     case 'm': /* index on map field */
00228       qstat_map_field = atoi (optarg);
00229       if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS)
00230         return ERROR;
00231       break;
00232     case 'g': /* index of game field */
00233       qstat_game_field = atoi (optarg);
00234       if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS)
00235         return ERROR;
00236       break;
00237     case 129: /* index of player count field */
00238       qstat_game_players = atoi (optarg);
00239       if (qstat_game_players_max == 0)
00240         qstat_game_players_max = qstat_game_players - 1;
00241       if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS)
00242         return ERROR;
00243       break;
00244     case 130: /* index of max players field */
00245       qstat_game_players_max = atoi (optarg);
00246       if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS)
00247         return ERROR;
00248       break;
00249     default: /* args not parsable */
00250       usage5();
00251     }
00252   }
00253 
00254   c = optind;
00255   /* first option is the game type */
00256   if (!game_type && c<argc)
00257     game_type = strdup (argv[c++]);
00258 
00259   /* Second option is the server name */
00260   if (!server_ip && c<argc)
00261     server_ip = strdup (argv[c++]);
00262 
00263   return validate_arguments ();
00264 }
00265 
00266 
00267 int
00268 validate_arguments (void)
00269 {
00270   if (qstat_game_players_max < 0)
00271     qstat_game_players_max = 4;
00272 
00273   if (qstat_game_players < 0)
00274     qstat_game_players = 5;
00275 
00276   if (qstat_game_field < 0)
00277     qstat_game_field = 2;
00278 
00279   if (qstat_map_field < 0)
00280     qstat_map_field = 3;
00281 
00282   if (qstat_ping_field < 0)
00283     qstat_ping_field = 5;
00284 
00285   return OK;
00286 }
00287 
00288 
00289 void
00290 print_help (void)
00291 {
00292   print_revision (progname, NP_VERSION);
00293 
00294   printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
00295   printf (COPYRIGHT, copyright, email);
00296 
00297   printf (_("This plugin tests game server connections with the specified host."));
00298 
00299   printf ("\n\n");
00300 
00301   print_usage ();
00302 
00303   printf (UT_HELP_VRSN);
00304   printf (UT_EXTRA_OPTS);
00305 
00306   printf (" %s\n", "-p");
00307   printf ("    %s\n", _("Optional port of which to connect"));
00308   printf (" %s\n", "gf");
00309   printf ("    %s\n", _("Field number in raw qstat output that contains game name"));
00310   printf (" %s\n", "-mf");
00311   printf ("    %s\n", _("Field number in raw qstat output that contains map name"));
00312   printf (" %s\n", "-pf");
00313   printf ("    %s\n", _("Field number in raw qstat output that contains ping time"));
00314 
00315   printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
00316 
00317   printf ("\n");
00318   printf ("%s\n", _("Notes:"));
00319   printf (" %s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool."));
00320   printf (" %s\n", _("If you don't have the package installed, you will need to download it from"));
00321   printf (" %s\n", _("http://www.activesw.com/people/steve/qstat.html before you can use this plugin."));
00322 
00323   printf (UT_SUPPORT);
00324 }
00325 
00326 
00327 
00328 void
00329 print_usage (void)
00330 {
00331   printf ("%s\n", _("Usage:"));
00332   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);
00333 }
00334 
00335 /******************************************************************************
00336  *
00337  * Test Cases:
00338  *
00339  * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
00340  *
00341  * qstat -raw , -qs 67.20.190.61
00342  *  ==> QS,67.20.190.61,Nightmare.fintek.ca,67.20.190.61:26000,3,e2m1,6,0,83,0
00343  *
00344  * qstat -qs 67.20.190.61
00345  *  ==> ADDRESS           PLAYERS      MAP   RESPONSE TIME    NAME
00346  *  ==> 67.20.190.61            0/ 6     e2m1     79 / 0   Nightmare.fintek.ca
00347  *
00348  ******************************************************************************/