check_game.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /******************************************************************************
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *
  17. * $Id$
  18. *****************************************************************************/
  19. const char *progname = "check_game";
  20. const char *revision = "$Revision$";
  21. const char *copyright = "2002-2005";
  22. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  23. #include "common.h"
  24. #include "utils.h"
  25. #include "runcmd.h"
  26. int process_arguments (int, char **);
  27. int validate_arguments (void);
  28. void print_help (void);
  29. void print_usage (void);
  30. #define QSTAT_DATA_DELIMITER ","
  31. #define QSTAT_HOST_ERROR "ERROR"
  32. #define QSTAT_HOST_DOWN "DOWN"
  33. #define QSTAT_HOST_TIMEOUT "TIMEOUT"
  34. #define QSTAT_MAX_RETURN_ARGS 12
  35. char *server_ip;
  36. char *game_type;
  37. int port = 0;
  38. int verbose;
  39. int qstat_game_players_max = -1;
  40. int qstat_game_players = -1;
  41. int qstat_game_field = -1;
  42. int qstat_map_field = -1;
  43. int qstat_ping_field = -1;
  44. int
  45. main (int argc, char **argv)
  46. {
  47. char *command_line;
  48. int result = STATE_UNKNOWN;
  49. char *p, *ret[QSTAT_MAX_RETURN_ARGS];
  50. size_t i = 0;
  51. output chld_out;
  52. setlocale (LC_ALL, "");
  53. bindtextdomain (PACKAGE, LOCALEDIR);
  54. textdomain (PACKAGE);
  55. if (process_arguments (argc, argv) == ERROR)
  56. usage_va(_("Could not parse arguments"));
  57. result = STATE_OK;
  58. /* create the command line to execute */
  59. asprintf (&command_line, "%s -raw %s -%s %s",
  60. PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip);
  61. if (port)
  62. asprintf (&command_line, "%s:%-d", command_line, port);
  63. if (verbose > 0)
  64. printf ("%s\n", command_line);
  65. /* run the command. historically, this plugin ignores output on stderr,
  66. * as well as return status of the qstat program */
  67. (void)np_runcmd(command_line, &chld_out, NULL, 0);
  68. /* sanity check */
  69. /* was thinking about running qstat without any options, capturing the
  70. -default line, parsing it & making an array of all know server types
  71. but thought this would be too much hassle considering this is a tool
  72. for intelligent sysadmins (ha). Could put a static array of known
  73. server types in a header file but then we'd be limiting ourselves
  74. In the end, I figured I'd simply let an error occur & then trap it
  75. */
  76. if (!strncmp (chld_out.line[0], "unknown option", 14)) {
  77. printf (_("CRITICAL - Host type parameter incorrect!\n"));
  78. result = STATE_CRITICAL;
  79. return result;
  80. }
  81. p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER);
  82. while (p != NULL) {
  83. ret[i] = p;
  84. p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER);
  85. i++;
  86. if (i >= QSTAT_MAX_RETURN_ARGS)
  87. break;
  88. }
  89. if (strstr (ret[2], QSTAT_HOST_ERROR)) {
  90. printf (_("CRITICAL - Host not found\n"));
  91. result = STATE_CRITICAL;
  92. }
  93. else if (strstr (ret[2], QSTAT_HOST_DOWN)) {
  94. printf (_("CRITICAL - Game server down or unavailable\n"));
  95. result = STATE_CRITICAL;
  96. }
  97. else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) {
  98. printf (_("CRITICAL - Game server timeout\n"));
  99. result = STATE_CRITICAL;
  100. }
  101. else {
  102. printf ("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n",
  103. ret[qstat_game_players],
  104. ret[qstat_game_players_max],
  105. ret[qstat_game_field],
  106. ret[qstat_map_field],
  107. ret[qstat_ping_field],
  108. perfdata ("players", atol(ret[qstat_game_players]), "",
  109. FALSE, 0, FALSE, 0,
  110. TRUE, 0, TRUE, atol(ret[qstat_game_players_max])),
  111. fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "",
  112. FALSE, 0, FALSE, 0,
  113. TRUE, 0, FALSE, 0));
  114. }
  115. return result;
  116. }
  117. int
  118. process_arguments (int argc, char **argv)
  119. {
  120. int c;
  121. int opt_index = 0;
  122. static struct option long_opts[] = {
  123. {"help", no_argument, 0, 'h'},
  124. {"version", no_argument, 0, 'V'},
  125. {"verbose", no_argument, 0, 'v'},
  126. {"timeout", required_argument, 0, 't'},
  127. {"hostname", required_argument, 0, 'H'},
  128. {"port", required_argument, 0, 'P'},
  129. {"game-type", required_argument, 0, 'G'},
  130. {"map-field", required_argument, 0, 'm'},
  131. {"ping-field", required_argument, 0, 'p'},
  132. {"game-field", required_argument, 0, 'g'},
  133. {"players-field", required_argument, 0, 129},
  134. {"max-players-field", required_argument, 0, 130},
  135. {0, 0, 0, 0}
  136. };
  137. if (argc < 2)
  138. return ERROR;
  139. for (c = 1; c < argc; c++) {
  140. if (strcmp ("-mf", argv[c]) == 0)
  141. strcpy (argv[c], "-m");
  142. else if (strcmp ("-pf", argv[c]) == 0)
  143. strcpy (argv[c], "-p");
  144. else if (strcmp ("-gf", argv[c]) == 0)
  145. strcpy (argv[c], "-g");
  146. }
  147. while (1) {
  148. c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
  149. if (c == -1 || c == EOF)
  150. break;
  151. switch (c) {
  152. case 'h': /* help */
  153. print_help ();
  154. exit (STATE_OK);
  155. case 'V': /* version */
  156. print_revision (progname, revision);
  157. exit (STATE_OK);
  158. case 'v': /* version */
  159. verbose = TRUE;
  160. break;
  161. case 't': /* timeout period */
  162. timeout_interval = atoi (optarg);
  163. break;
  164. case 'H': /* hostname */
  165. if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH)
  166. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  167. server_ip = optarg;
  168. break;
  169. case 'P': /* port */
  170. port = atoi (optarg);
  171. break;
  172. case 'G': /* hostname */
  173. if (strlen (optarg) >= MAX_INPUT_BUFFER)
  174. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  175. game_type = optarg;
  176. break;
  177. case 'p': /* index of ping field */
  178. qstat_ping_field = atoi (optarg);
  179. if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS)
  180. return ERROR;
  181. break;
  182. case 'm': /* index on map field */
  183. qstat_map_field = atoi (optarg);
  184. if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS)
  185. return ERROR;
  186. break;
  187. case 'g': /* index of game field */
  188. qstat_game_field = atoi (optarg);
  189. if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS)
  190. return ERROR;
  191. break;
  192. case 129: /* index of player count field */
  193. qstat_game_players = atoi (optarg);
  194. if (qstat_game_players_max == 0)
  195. qstat_game_players_max = qstat_game_players - 1;
  196. if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS)
  197. return ERROR;
  198. break;
  199. case 130: /* index of max players field */
  200. qstat_game_players_max = atoi (optarg);
  201. if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS)
  202. return ERROR;
  203. break;
  204. default: /* args not parsable */
  205. usage_va(_("Unknown argument - %s"), optarg);
  206. }
  207. }
  208. c = optind;
  209. /* first option is the game type */
  210. if (!game_type && c<argc)
  211. game_type = strdup (argv[c++]);
  212. /* Second option is the server name */
  213. if (!server_ip && c<argc)
  214. server_ip = strdup (argv[c++]);
  215. return validate_arguments ();
  216. }
  217. int
  218. validate_arguments (void)
  219. {
  220. if (qstat_game_players_max < 0)
  221. qstat_game_players_max = 4;
  222. if (qstat_game_players < 0)
  223. qstat_game_players = 5;
  224. if (qstat_game_field < 0)
  225. qstat_game_field = 2;
  226. if (qstat_map_field < 0)
  227. qstat_map_field = 3;
  228. if (qstat_ping_field < 0)
  229. qstat_ping_field = 5;
  230. return OK;
  231. }
  232. void
  233. print_help (void)
  234. {
  235. print_revision (progname, revision);
  236. printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
  237. printf (COPYRIGHT, copyright, email);
  238. printf (_("This plugin tests game server connections with the specified host."));
  239. printf ("\n\n");
  240. print_usage ();
  241. printf (_(UT_HELP_VRSN));
  242. printf (" %s\n", "-p");
  243. printf (" %s\n", _("Optional port of which to connect"));
  244. printf (" %s\n", "gf");
  245. printf (" %s\n", _("Field number in raw qstat output that contains game name"));
  246. printf (" %s\n", "-mf");
  247. printf (" %s\n", _("Field number in raw qstat output that contains map name"));
  248. printf (" %s\n", "-pf");
  249. printf (" %s\n", _("Field number in raw qstat output that contains ping time"));
  250. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  251. printf ("%s\n", _("Notes:"));
  252. printf ("%s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool ."));
  253. printf ("%s\n", _("If you don't have the package installed, you will need to download it from"));
  254. printf ("%s\n", _("http://www.activesw.com/people/steve/qstat.html before you can use this plugin."));
  255. printf (_(UT_SUPPORT));
  256. }
  257. void
  258. print_usage (void)
  259. {
  260. printf (_("Usage:"));
  261. printf (" %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field] [-pf ping_field]\n", progname);
  262. }
  263. /******************************************************************************
  264. *
  265. * Test Cases:
  266. *
  267. * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
  268. *
  269. * qstat -raw , -qs 67.20.190.61
  270. * ==> QS,67.20.190.61,Nightmare.fintek.ca,67.20.190.61:26000,3,e2m1,6,0,83,0
  271. *
  272. * qstat -qs 67.20.190.61
  273. * ==> ADDRESS PLAYERS MAP RESPONSE TIME NAME
  274. * ==> 67.20.190.61 0/ 6 e2m1 79 / 0 Nightmare.fintek.ca
  275. *
  276. ******************************************************************************/