check_game.c 9.8 KB

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