check_game.c 9.9 KB

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