Code

Removed old terminate function (Bug 1093491)
[nagiosplug.git] / plugins / check_game.c
index 811da2706e78572a636e6b71b262c451967b9bb5..05b363af509259c1a4ebc8556306fb90d99835e0 100644 (file)
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
+* $Id$
 *****************************************************************************/
 
 const char *progname = "check_game";
 const char *revision = "$Revision$";
-const char *copyright = "2002-2003";
+const char *copyright = "2002-2004";
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
 
 #include "common.h"
 #include "popen.h"
 #include "utils.h"
 
-void
-print_usage (void)
-{
-       printf (_("\
-Usage: %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field]\n\
-  [-pf ping_field]\n"), progname);
-       printf (_(UT_HLP_VRS), progname, progname);
-}
-
-void
-print_help (void)
-{
-       print_revision (progname, revision);
-
-       printf (_(COPYRIGHT), copyright, email);
-
-       printf (_("This plugin tests %s connections with the specified host."), progname);
-
-       print_usage ();
-
-       printf (_(UT_HELP_VRSN));
-
-       printf (_("\
-<game>        = Game type that is recognised by qstat (without the leading dash)\n\
-<ip_address>  = The IP address of the device you wish to query\n\
- [port]        = Optional port of which to connect\n\
- [game_field]  = Field number in raw qstat output that contains game name\n\
- [map_field]   = Field number in raw qstat output that contains map name\n\
- [ping_field]  = Field number in raw qstat output that contains ping time\n"),
-               DEFAULT_SOCKET_TIMEOUT);
-
-       printf (_("\n\
-Notes:\n\
-- This plugin uses the 'qstat' command, the popular game server status query tool .\n\
-  If you don't have the package installed, you will need to download it from\n\
-  http://www.activesw.com/people/steve/qstat.html before you can use this plugin.\n"));
-
-       printf (_(UT_SUPPORT));
-}
-\f
 int process_arguments (int, char **);
 int validate_arguments (void);
+void print_help (void);
+void print_usage (void);
 
 #define QSTAT_DATA_DELIMITER   ","
 
@@ -81,34 +44,29 @@ int port = 0;
 
 int verbose;
 
-int qstat_game_players_max = 4;
-int qstat_game_players = 5;
-int qstat_game_field = 2;
-int qstat_map_field = 3;
-int qstat_ping_field = 5;
+int qstat_game_players_max = -1;
+int qstat_game_players = -1;
+int qstat_game_field = -1;
+int qstat_map_field = -1;
+int qstat_ping_field = -1;
 
 
 int
 main (int argc, char **argv)
 {
        char *command_line;
-       int result;
+       int result = STATE_UNKNOWN;
        FILE *fp;
        char input_buffer[MAX_INPUT_BUFFER];
        char *p, *ret[QSTAT_MAX_RETURN_ARGS];
        int i;
 
-       result = process_arguments (argc, argv);
-
-       if (result != OK) {
-               printf (_("Incorrect arguments supplied\n"));
-               printf ("\n");
-               print_revision (progname, revision);
-               printf (_("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n"));
-               printf (_("License: GPL\n"));
-               printf ("\n");
-               return STATE_UNKNOWN;
-       }
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+       
+       if (process_arguments (argc, argv) == ERROR)
+               usage4 (_("Could not parse arguments"));
 
        result = STATE_OK;
 
@@ -125,7 +83,7 @@ main (int argc, char **argv)
        /* run the command */
        fp = spopen (command_line);
        if (fp == NULL) {
-               printf (_("Error - Could not open pipe: %s\n"), command_line);
+               printf (_("Could not open pipe: %s\n"), command_line);
                return STATE_UNKNOWN;
        }
 
@@ -145,14 +103,14 @@ main (int argc, char **argv)
         */
 
        if (!strncmp (input_buffer, "unknown option", 14)) {
-               printf (_("ERROR: Host type parameter incorrect!\n"));
+               printf (_("CRITICAL - Host type parameter incorrect!\n"));
                result = STATE_CRITICAL;
                return result;
        }
 
        /* initialize the returned data buffer */
        for (i = 0; i < QSTAT_MAX_RETURN_ARGS; i++)
-               ret[i] = "";
+               ret[i] = strdup("");
 
        i = 0;
        p = (char *) strtok (input_buffer, QSTAT_DATA_DELIMITER);
@@ -165,24 +123,30 @@ main (int argc, char **argv)
        }
 
        if (strstr (ret[2], QSTAT_HOST_ERROR)) {
-               printf ("ERROR: Host not found\n");
+               printf (_("CRITICAL - Host not found\n"));
                result = STATE_CRITICAL;
        }
        else if (strstr (ret[2], QSTAT_HOST_DOWN)) {
-               printf ("ERROR: Game server down or unavailable\n");
+               printf (_("CRITICAL - Game server down or unavailable\n"));
                result = STATE_CRITICAL;
        }
        else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) {
-               printf ("ERROR: Game server timeout\n");
+               printf (_("CRITICAL - Game server timeout\n"));
                result = STATE_CRITICAL;
        }
        else {
-               printf ("OK: %s/%s %s (%s), Ping: %s ms\n", 
+               printf ("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", 
                        ret[qstat_game_players],
                        ret[qstat_game_players_max],
                        ret[qstat_game_field], 
                        ret[qstat_map_field],
-                       ret[qstat_ping_field]);
+                       ret[qstat_ping_field],
+                                               perfdata ("players", atol(ret[qstat_game_players]), "",
+                                 FALSE, 0, FALSE, 0,
+                                 TRUE, 0, TRUE, atol(ret[qstat_game_players_max])),
+                                               fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "",
+                                 FALSE, 0, FALSE, 0,
+                                 TRUE, 0, FALSE, 0));
        }
 
        /* close the pipe */
@@ -192,7 +156,6 @@ main (int argc, char **argv)
 }
 
 
-
 int
 process_arguments (int argc, char **argv)
 {
@@ -235,9 +198,7 @@ process_arguments (int argc, char **argv)
 
                switch (c) {
                case '?': /* args not parsable */
-                       printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
-                       print_usage ();
-                       exit (STATE_UNKNOWN);
+                       usage2 (_("Unknown argument"), optarg);
                case 'h': /* help */
                        print_help ();
                        exit (STATE_OK);
@@ -252,16 +213,16 @@ process_arguments (int argc, char **argv)
                        break;
                case 'H': /* hostname */
                        if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH)
-                               terminate (STATE_UNKNOWN, _("Input buffer overflow\n"));
-                       server_ip = strdup (optarg);
+                               die (STATE_UNKNOWN, _("Input buffer overflow\n"));
+                       server_ip = optarg;
                        break;
                case 'P': /* port */
                        port = atoi (optarg);
                        break;
                case 'G': /* hostname */
                        if (strlen (optarg) >= MAX_INPUT_BUFFER)
-                               terminate (STATE_UNKNOWN, _("Input buffer overflow\n"));
-                       game_type = strdup (optarg);
+                               die (STATE_UNKNOWN, _("Input buffer overflow\n"));
+                       game_type = optarg;
                        break;
                case 'p': /* index of ping field */
                        qstat_ping_field = atoi (optarg);
@@ -280,6 +241,8 @@ process_arguments (int argc, char **argv)
                        break;
                case 129: /* index of player count field */
                        qstat_game_players = atoi (optarg);
+                       if (qstat_game_players_max == 0)
+                               qstat_game_players_max = qstat_game_players - 1;
                        if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS)
                                return ERROR;
                        break;
@@ -303,8 +266,83 @@ process_arguments (int argc, char **argv)
        return validate_arguments ();
 }
 
+
 int
 validate_arguments (void)
 {
-               return OK;
+       if (qstat_game_players_max < 0)
+               qstat_game_players_max = 4;
+
+       if (qstat_game_players < 0)
+               qstat_game_players = 5;
+
+       if (qstat_game_field < 0)
+               qstat_game_field = 2;
+
+       if (qstat_map_field < 0)
+               qstat_map_field = 3;
+
+       if (qstat_ping_field < 0)
+               qstat_ping_field = 5;
+
+       return OK;
 }
+
+
+void
+print_help (void)
+{
+       print_revision (progname, revision);
+
+       printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
+       printf (COPYRIGHT, copyright, email);
+
+       printf (_("This plugin tests game server connections with the specified host."));
+
+       print_usage ();
+
+       printf (_(UT_HELP_VRSN));
+
+       printf (_("\
+<game>        = Game type that is recognised by qstat (without the leading dash)\n\
+<ip_address>  = The IP address of the device you wish to query\n\
+ [port]        = Optional port of which to connect\n\
+ [game_field]  = Field number in raw qstat output that contains game name\n\
+ [map_field]   = Field number in raw qstat output that contains map name\n\
+ [ping_field]  = Field number in raw qstat output that contains ping time\n"));
+
+       printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
+
+       printf (_("\n\
+Notes:\n\
+- This plugin uses the 'qstat' command, the popular game server status query tool .\n\
+  If you don't have the package installed, you will need to download it from\n\
+  http://www.activesw.com/people/steve/qstat.html before you can use this plugin.\n"));
+
+       printf (_(UT_SUPPORT));
+}
+
+
+
+void
+print_usage (void)
+{
+       printf ("\
+Usage: %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field]\n\
+                  [-pf ping_field]\n", progname);
+}
+
+/******************************************************************************
+ *
+ * Test Cases:
+ *
+ * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
+ * 
+ * qstat -raw , -qs 67.20.190.61
+ *  ==> QS,67.20.190.61,Nightmare.fintek.ca,67.20.190.61:26000,3,e2m1,6,0,83,0
+ *
+ * qstat -qs 67.20.190.61
+ *  ==> ADDRESS           PLAYERS      MAP   RESPONSE TIME    NAME
+ *  ==> 67.20.190.61            0/ 6     e2m1     79 / 0   Nightmare.fintek.ca
+ *
+ ******************************************************************************/