check_game.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. /* Parse extra opts if any */
  71. argv=np_extra_opts (&argc, argv, progname);
  72. if (process_arguments (argc, argv) == ERROR)
  73. usage_va(_("Could not parse arguments"));
  74. result = STATE_OK;
  75. /* create the command line to execute */
  76. asprintf (&command_line, "%s -raw %s -%s %s",
  77. PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip);
  78. if (port)
  79. asprintf (&command_line, "%s:%-d", command_line, port);
  80. if (verbose > 0)
  81. printf ("%s\n", command_line);
  82. /* run the command. historically, this plugin ignores output on stderr,
  83. * as well as return status of the qstat program */
  84. (void)np_runcmd(command_line, &chld_out, NULL, 0);
  85. /* sanity check */
  86. /* was thinking about running qstat without any options, capturing the
  87. -default line, parsing it & making an array of all know server types
  88. but thought this would be too much hassle considering this is a tool
  89. for intelligent sysadmins (ha). Could put a static array of known
  90. server types in a header file but then we'd be limiting ourselves
  91. In the end, I figured I'd simply let an error occur & then trap it
  92. */
  93. if (!strncmp (chld_out.line[0], "unknown option", 14)) {
  94. printf (_("CRITICAL - Host type parameter incorrect!\n"));
  95. result = STATE_CRITICAL;
  96. return result;
  97. }
  98. p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER);
  99. while (p != NULL) {
  100. ret[i] = p;
  101. p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER);
  102. i++;
  103. if (i >= QSTAT_MAX_RETURN_ARGS)
  104. break;
  105. }
  106. if (strstr (ret[2], QSTAT_HOST_ERROR)) {
  107. printf (_("CRITICAL - Host not found\n"));
  108. result = STATE_CRITICAL;
  109. }
  110. else if (strstr (ret[2], QSTAT_HOST_DOWN)) {
  111. printf (_("CRITICAL - Game server down or unavailable\n"));
  112. result = STATE_CRITICAL;
  113. }
  114. else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) {
  115. printf (_("CRITICAL - Game server timeout\n"));
  116. result = STATE_CRITICAL;
  117. }
  118. else {
  119. printf ("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n",
  120. ret[qstat_game_players],
  121. ret[qstat_game_players_max],
  122. ret[qstat_game_field],
  123. ret[qstat_map_field],
  124. ret[qstat_ping_field],
  125. perfdata ("players", atol(ret[qstat_game_players]), "",
  126. FALSE, 0, FALSE, 0,
  127. TRUE, 0, TRUE, atol(ret[qstat_game_players_max])),
  128. fperfdata ("ping", strtod(ret[qstat_ping_field], NULL), "",
  129. FALSE, 0, FALSE, 0,
  130. TRUE, 0, FALSE, 0));
  131. }
  132. return result;
  133. }
  134. int
  135. process_arguments (int argc, char **argv)
  136. {
  137. int c;
  138. int opt_index = 0;
  139. static struct option long_opts[] = {
  140. {"help", no_argument, 0, 'h'},
  141. {"version", no_argument, 0, 'V'},
  142. {"verbose", no_argument, 0, 'v'},
  143. {"timeout", required_argument, 0, 't'},
  144. {"hostname", required_argument, 0, 'H'},
  145. {"port", required_argument, 0, 'P'},
  146. {"game-type", required_argument, 0, 'G'},
  147. {"map-field", required_argument, 0, 'm'},
  148. {"ping-field", required_argument, 0, 'p'},
  149. {"game-field", required_argument, 0, 'g'},
  150. {"players-field", required_argument, 0, 129},
  151. {"max-players-field", required_argument, 0, 130},
  152. {0, 0, 0, 0}
  153. };
  154. if (argc < 2)
  155. return ERROR;
  156. for (c = 1; c < argc; c++) {
  157. if (strcmp ("-mf", argv[c]) == 0)
  158. strcpy (argv[c], "-m");
  159. else if (strcmp ("-pf", argv[c]) == 0)
  160. strcpy (argv[c], "-p");
  161. else if (strcmp ("-gf", argv[c]) == 0)
  162. strcpy (argv[c], "-g");
  163. }
  164. while (1) {
  165. c = getopt_long (argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
  166. if (c == -1 || c == EOF)
  167. break;
  168. switch (c) {
  169. case 'h': /* help */
  170. print_help ();
  171. exit (STATE_OK);
  172. case 'V': /* version */
  173. print_revision (progname, revision);
  174. exit (STATE_OK);
  175. case 'v': /* version */
  176. verbose = TRUE;
  177. break;
  178. case 't': /* timeout period */
  179. timeout_interval = atoi (optarg);
  180. break;
  181. case 'H': /* hostname */
  182. if (strlen (optarg) >= MAX_HOST_ADDRESS_LENGTH)
  183. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  184. server_ip = optarg;
  185. break;
  186. case 'P': /* port */
  187. port = atoi (optarg);
  188. break;
  189. case 'G': /* hostname */
  190. if (strlen (optarg) >= MAX_INPUT_BUFFER)
  191. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  192. game_type = optarg;
  193. break;
  194. case 'p': /* index of ping field */
  195. qstat_ping_field = atoi (optarg);
  196. if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS)
  197. return ERROR;
  198. break;
  199. case 'm': /* index on map field */
  200. qstat_map_field = atoi (optarg);
  201. if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS)
  202. return ERROR;
  203. break;
  204. case 'g': /* index of game field */
  205. qstat_game_field = atoi (optarg);
  206. if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS)
  207. return ERROR;
  208. break;
  209. case 129: /* index of player count field */
  210. qstat_game_players = atoi (optarg);
  211. if (qstat_game_players_max == 0)
  212. qstat_game_players_max = qstat_game_players - 1;
  213. if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS)
  214. return ERROR;
  215. break;
  216. case 130: /* index of max players field */
  217. qstat_game_players_max = atoi (optarg);
  218. if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS)
  219. return ERROR;
  220. break;
  221. default: /* args not parsable */
  222. usage5();
  223. }
  224. }
  225. c = optind;
  226. /* first option is the game type */
  227. if (!game_type && c<argc)
  228. game_type = strdup (argv[c++]);
  229. /* Second option is the server name */
  230. if (!server_ip && c<argc)
  231. server_ip = strdup (argv[c++]);
  232. return validate_arguments ();
  233. }
  234. int
  235. validate_arguments (void)
  236. {
  237. if (qstat_game_players_max < 0)
  238. qstat_game_players_max = 4;
  239. if (qstat_game_players < 0)
  240. qstat_game_players = 5;
  241. if (qstat_game_field < 0)
  242. qstat_game_field = 2;
  243. if (qstat_map_field < 0)
  244. qstat_map_field = 3;
  245. if (qstat_ping_field < 0)
  246. qstat_ping_field = 5;
  247. return OK;
  248. }
  249. void
  250. print_help (void)
  251. {
  252. print_revision (progname, revision);
  253. printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
  254. printf (COPYRIGHT, copyright, email);
  255. printf (_("This plugin tests game server connections with the specified host."));
  256. printf ("\n\n");
  257. print_usage ();
  258. printf (_(UT_HELP_VRSN));
  259. printf (_(UT_EXTRA_OPTS));
  260. printf (" %s\n", "-p");
  261. printf (" %s\n", _("Optional port of which to connect"));
  262. printf (" %s\n", "gf");
  263. printf (" %s\n", _("Field number in raw qstat output that contains game name"));
  264. printf (" %s\n", "-mf");
  265. printf (" %s\n", _("Field number in raw qstat output that contains map name"));
  266. printf (" %s\n", "-pf");
  267. printf (" %s\n", _("Field number in raw qstat output that contains ping time"));
  268. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  269. printf ("\n");
  270. printf ("%s\n", _("Notes:"));
  271. printf (" %s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool."));
  272. printf (" %s\n", _("If you don't have the package installed, you will need to download it from"));
  273. printf (" %s\n", _("http://www.activesw.com/people/steve/qstat.html before you can use this plugin."));
  274. #ifdef NP_EXTRA_OPTS
  275. printf ("\n");
  276. printf (_(UT_EXTRA_OPTS_NOTES));
  277. #endif
  278. printf (_(UT_SUPPORT));
  279. }
  280. void
  281. print_usage (void)
  282. {
  283. printf (_("Usage:"));
  284. printf (" %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field] [-pf ping_field]\n", progname);
  285. }
  286. /******************************************************************************
  287. *
  288. * Test Cases:
  289. *
  290. * ./check_game --players 7 -p 8 --map 5 qs 67.20.190.61 26000
  291. *
  292. * qstat -raw , -qs 67.20.190.61
  293. * ==> QS,67.20.190.61,Nightmare.fintek.ca,67.20.190.61:26000,3,e2m1,6,0,83,0
  294. *
  295. * qstat -qs 67.20.190.61
  296. * ==> ADDRESS PLAYERS MAP RESPONSE TIME NAME
  297. * ==> 67.20.190.61 0/ 6 e2m1 79 / 0 Nightmare.fintek.ca
  298. *
  299. ******************************************************************************/