check_game.c 10 KB

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