check_game.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /******************************************************************************
  2. *
  3. * CHECK_GAME.C
  4. *
  5. * Program: GAME plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ian Cass (ian@knowledge.com)
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Mod History
  12. *
  13. * 25-8-99 Ethan Galstad <nagios@nagios.org>
  14. * Integrated with common plugin code, minor cleanup stuff
  15. *
  16. * 17-8-99 version 1.1b
  17. *
  18. * 17-8-99 make port a separate argument so we can use something like
  19. * check_game q2s!27910 with the probe set up as
  20. * check_game $ARG1$ $HOSTADDRESS$ $ARG2$
  21. *
  22. * 17-8-99 Put in sanity check for ppl who enter the wrong server type
  23. *
  24. * 17-8-99 Release version 1.0b
  25. *
  26. * Command line: CHECK_GAME <server type> <ip_address> [port]
  27. *
  28. * server type = a server type that qstat understands (type qstat & look at the -default line)
  29. * ip_address = either a dotted address or a FQD name
  30. * port = defaults game default port
  31. *
  32. *
  33. * Description:
  34. *
  35. * Needed to explore writing my own probes for nagios. It looked
  36. * pretty simple so I thought I'd write one for monitoring the status
  37. * of game servers. It uses qstat to do the actual monitoring and
  38. * analyses the result. Doing it this way means I can support all the
  39. * servers that qstat does and will do in the future.
  40. *
  41. *
  42. * Dependencies:
  43. *
  44. * This plugin uses the 'qstat' command If you don't
  45. * have the package installed you will need to download it from
  46. * http://www.activesw.com/people/steve/qstat.html or any popular files archive
  47. * before you can use this plugin.
  48. *
  49. * License Information:
  50. *
  51. * This program is free software; you can redistribute it and/or modify
  52. * it under the terms of the GNU General Public License as published by
  53. * the Free Software Foundation; either version 2 of the License, or
  54. * (at your option) any later version.
  55. *
  56. * This program is distributed in the hope that it will be useful,
  57. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  58. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  59. * GNU General Public License for more details.
  60. *
  61. * You should have received a copy of the GNU General Public License
  62. * along with this program; if not, write to the Free Software
  63. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  64. *
  65. *****************************************************************************/
  66. #include "config.h"
  67. #include "common.h"
  68. #include "utils.h"
  69. int process_arguments (int, char **);
  70. const char *progname = "check_game";
  71. #define QSTAT_DATA_DELIMITER ","
  72. #define QSTAT_HOST_ERROR "ERROR"
  73. #define QSTAT_HOST_DOWN "DOWN"
  74. #define QSTAT_HOST_TIMEOUT "TIMEOUT"
  75. #define QSTAT_MAX_RETURN_ARGS 12
  76. char server_ip[MAX_HOST_ADDRESS_LENGTH];
  77. char game_type[MAX_INPUT_BUFFER];
  78. char port[MAX_INPUT_BUFFER];
  79. int qstat_game_players_max = 4;
  80. int qstat_game_players = 5;
  81. int qstat_game_field = 2;
  82. int qstat_map_field = 3;
  83. int qstat_ping_field = 5;
  84. int
  85. main (int argc, char **argv)
  86. {
  87. char command_line[MAX_INPUT_BUFFER];
  88. int result;
  89. FILE *fp;
  90. char input_buffer[MAX_INPUT_BUFFER];
  91. char response[MAX_INPUT_BUFFER];
  92. char *temp_ptr;
  93. int found;
  94. char *p, *ret[QSTAT_MAX_RETURN_ARGS];
  95. int i;
  96. result = process_arguments (argc, argv);
  97. if (result != OK) {
  98. printf ("Incorrect arguments supplied\n");
  99. printf ("\n");
  100. print_revision (argv[0], "$Revision$");
  101. printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
  102. printf ("License: GPL\n");
  103. printf ("\n");
  104. printf
  105. ("Usage: %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field] [-pf ping_field]\n",
  106. argv[0]);
  107. printf ("\n");
  108. printf ("Options:\n");
  109. printf
  110. (" <game> = Game type that is recognised by qstat (without the leading dash)\n");
  111. printf
  112. (" <ip_address> = The IP address of the device you wish to query\n");
  113. printf (" [port] = Optional port of which to connect\n");
  114. printf
  115. (" [game_field] = Field number in raw qstat output that contains game name\n");
  116. printf
  117. (" [map_field] = Field number in raw qstat output that contains map name\n");
  118. printf
  119. (" [ping_field] = Field number in raw qstat output that contains ping time\n");
  120. printf ("\n");
  121. printf ("Notes:\n");
  122. printf
  123. ("- This plugin uses the 'qstat' command, the popular game server status query tool .\n");
  124. printf
  125. (" If you don't have the package installed, you will need to download it from\n");
  126. printf
  127. (" http://www.activesw.com/people/steve/qstat.html before you can use this plugin.\n");
  128. printf ("\n");
  129. return STATE_UNKNOWN;
  130. }
  131. result = STATE_OK;
  132. /* create the command line to execute */
  133. snprintf (command_line, sizeof (command_line) - 1, "%s -raw %s -%s %s%s",
  134. PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip, port);
  135. command_line[sizeof (command_line) - 1] = 0;
  136. /* run the command */
  137. fp = popen (command_line, "r");
  138. if (fp == NULL) {
  139. printf ("Error - Could not open pipe: %s\n", command_line);
  140. return STATE_UNKNOWN;
  141. }
  142. found = 0;
  143. fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp); /* Only interested in the first line */
  144. /* strip the newline character from the end of the input */
  145. input_buffer[strlen (input_buffer) - 1] = 0;
  146. /* sanity check */
  147. /* was thinking about running qstat without any options, capturing the
  148. -default line, parsing it & making an array of all know server types
  149. but thought this would be too much hassle considering this is a tool
  150. for intelligent sysadmins (ha). Could put a static array of known
  151. server types in a header file but then we'd be limiting ourselves
  152. In the end, I figured I'd simply let an error occur & then trap it
  153. */
  154. if (!strncmp (input_buffer, "unknown option", 14)) {
  155. printf ("ERROR: Host type parameter incorrect!\n");
  156. result = STATE_CRITICAL;
  157. return result;
  158. }
  159. /* initialize the returned data buffer */
  160. for (i = 0; i < QSTAT_MAX_RETURN_ARGS; i++)
  161. ret[i] = "";
  162. i = 0;
  163. p = (char *) strtok (input_buffer, QSTAT_DATA_DELIMITER);
  164. while (p != NULL) {
  165. ret[i] = p;
  166. p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER);
  167. i++;
  168. if (i >= QSTAT_MAX_RETURN_ARGS)
  169. break;
  170. }
  171. if (strstr (ret[2], QSTAT_HOST_ERROR)) {
  172. printf ("ERROR: Host not found\n");
  173. result = STATE_CRITICAL;
  174. }
  175. else if (strstr (ret[2], QSTAT_HOST_DOWN)) {
  176. printf ("ERROR: Game server down or unavailable\n");
  177. result = STATE_CRITICAL;
  178. }
  179. else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) {
  180. printf ("ERROR: Game server timeout\n");
  181. result = STATE_CRITICAL;
  182. }
  183. else {
  184. printf ("OK: %s/%s %s (%s), Ping: %s ms\n",
  185. ret[qstat_game_players_max],
  186. ret[qstat_game_players],
  187. ret[qstat_game_field],
  188. ret[qstat_map_field],
  189. ret[qstat_ping_field]);
  190. }
  191. /* close the pipe */
  192. pclose (fp);
  193. return result;
  194. }
  195. int
  196. process_arguments (int argc, char **argv)
  197. {
  198. int x;
  199. /* not enough options were supplied */
  200. if (argc < 3)
  201. return ERROR;
  202. /* first option is always the game type */
  203. strncpy (game_type, argv[1], sizeof (game_type) - 1);
  204. game_type[sizeof (game_type) - 1] = 0;
  205. /* Second option is always the server name */
  206. strncpy (server_ip, argv[2], sizeof (server_ip) - 1);
  207. server_ip[sizeof (server_ip) - 1] = 0;
  208. /* process all remaining arguments */
  209. for (x = 4; x <= argc; x++) {
  210. /* we got the port number to connect to */
  211. if (!strcmp (argv[x - 1], "-p")) {
  212. if (x < argc) {
  213. snprintf (port, sizeof (port) - 2, ":%s", argv[x]);
  214. port[sizeof (port) - 1] = 0;
  215. x++;
  216. }
  217. else
  218. return ERROR;
  219. }
  220. /* we got the game field */
  221. else if (!strcmp (argv[x - 1], "-gf")) {
  222. if (x < argc) {
  223. qstat_game_field = atoi (argv[x]);
  224. if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS)
  225. return ERROR;
  226. x++;
  227. }
  228. else
  229. return ERROR;
  230. }
  231. /* we got the map field */
  232. else if (!strcmp (argv[x - 1], "-mf")) {
  233. if (x < argc) {
  234. qstat_map_field = atoi (argv[x]);
  235. if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS)
  236. return ERROR;
  237. x++;
  238. }
  239. else
  240. return ERROR;
  241. }
  242. /* we got the ping field */
  243. else if (!strcmp (argv[x - 1], "-pf")) {
  244. if (x < argc) {
  245. qstat_ping_field = atoi (argv[x]);
  246. if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS)
  247. return ERROR;
  248. x++;
  249. }
  250. else
  251. return ERROR;
  252. }
  253. /* else we got something else... */
  254. else
  255. return ERROR;
  256. }
  257. return OK;
  258. }
  259. void print_usage (void)
  260. {
  261. return STATE_OK;
  262. }