check_game.c 9.9 KB

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