check_game.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #define QSTAT_DATA_DELIMITER ","
  71. #define QSTAT_HOST_ERROR "ERROR"
  72. #define QSTAT_HOST_DOWN "DOWN"
  73. #define QSTAT_HOST_TIMEOUT "TIMEOUT"
  74. #define QSTAT_MAX_RETURN_ARGS 12
  75. char server_ip[MAX_HOST_ADDRESS_LENGTH];
  76. char game_type[MAX_INPUT_BUFFER];
  77. char port[MAX_INPUT_BUFFER];
  78. int qstat_game_players_max = 4;
  79. int qstat_game_players = 5;
  80. int qstat_game_field = 2;
  81. int qstat_map_field = 3;
  82. int qstat_ping_field = 5;
  83. int
  84. main (int argc, char **argv)
  85. {
  86. char command_line[MAX_INPUT_BUFFER];
  87. int result;
  88. FILE *fp;
  89. char input_buffer[MAX_INPUT_BUFFER];
  90. char response[MAX_INPUT_BUFFER];
  91. char *temp_ptr;
  92. int found;
  93. char *p, *ret[QSTAT_MAX_RETURN_ARGS];
  94. int i;
  95. result = process_arguments (argc, argv);
  96. if (result != OK) {
  97. printf ("Incorrect arguments supplied\n");
  98. printf ("\n");
  99. print_revision (argv[0], "$Revision$");
  100. printf ("Copyright (c) 1999 Ian Cass, Knowledge Matters Limited\n");
  101. printf ("License: GPL\n");
  102. printf ("\n");
  103. printf
  104. ("Usage: %s <game> <ip_address> [-p port] [-gf game_field] [-mf map_field] [-pf ping_field]\n",
  105. argv[0]);
  106. printf ("\n");
  107. printf ("Options:\n");
  108. printf
  109. (" <game> = Game type that is recognised by qstat (without the leading dash)\n");
  110. printf
  111. (" <ip_address> = The IP address of the device you wish to query\n");
  112. printf (" [port] = Optional port of which to connect\n");
  113. printf
  114. (" [game_field] = Field number in raw qstat output that contains game name\n");
  115. printf
  116. (" [map_field] = Field number in raw qstat output that contains map name\n");
  117. printf
  118. (" [ping_field] = Field number in raw qstat output that contains ping time\n");
  119. printf ("\n");
  120. printf ("Notes:\n");
  121. printf
  122. ("- This plugin uses the 'qstat' command, the popular game server status query tool .\n");
  123. printf
  124. (" If you don't have the package installed, you will need to download it from\n");
  125. printf
  126. (" http://www.activesw.com/people/steve/qstat.html before you can use this plugin.\n");
  127. printf ("\n");
  128. return STATE_UNKNOWN;
  129. }
  130. result = STATE_OK;
  131. /* create the command line to execute */
  132. snprintf (command_line, sizeof (command_line) - 1, "%s -raw %s -%s %s%s",
  133. PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip, port);
  134. command_line[sizeof (command_line) - 1] = 0;
  135. /* run the command */
  136. fp = popen (command_line, "r");
  137. if (fp == NULL) {
  138. printf ("Error - Could not open pipe: %s\n", command_line);
  139. return STATE_UNKNOWN;
  140. }
  141. found = 0;
  142. fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp); /* Only interested in the first line */
  143. /* strip the newline character from the end of the input */
  144. input_buffer[strlen (input_buffer) - 1] = 0;
  145. /* sanity check */
  146. /* was thinking about running qstat without any options, capturing the
  147. -default line, parsing it & making an array of all know server types
  148. but thought this would be too much hassle considering this is a tool
  149. for intelligent sysadmins (ha). Could put a static array of known
  150. server types in a header file but then we'd be limiting ourselves
  151. In the end, I figured I'd simply let an error occur & then trap it
  152. */
  153. if (!strncmp (input_buffer, "unknown option", 14)) {
  154. printf ("ERROR: Host type parameter incorrect!\n");
  155. result = STATE_CRITICAL;
  156. return result;
  157. }
  158. /* initialize the returned data buffer */
  159. for (i = 0; i < QSTAT_MAX_RETURN_ARGS; i++)
  160. ret[i] = "";
  161. i = 0;
  162. p = (char *) strtok (input_buffer, QSTAT_DATA_DELIMITER);
  163. while (p != NULL) {
  164. ret[i] = p;
  165. p = (char *) strtok (NULL, QSTAT_DATA_DELIMITER);
  166. i++;
  167. if (i >= QSTAT_MAX_RETURN_ARGS)
  168. break;
  169. }
  170. if (strstr (ret[2], QSTAT_HOST_ERROR)) {
  171. printf ("ERROR: Host not found\n");
  172. result = STATE_CRITICAL;
  173. }
  174. else if (strstr (ret[2], QSTAT_HOST_DOWN)) {
  175. printf ("ERROR: Game server down or unavailable\n");
  176. result = STATE_CRITICAL;
  177. }
  178. else if (strstr (ret[2], QSTAT_HOST_TIMEOUT)) {
  179. printf ("ERROR: Game server timeout\n");
  180. result = STATE_CRITICAL;
  181. }
  182. else {
  183. printf ("OK: %s/%s %s (%s), Ping: %s ms\n",
  184. ret[qstat_game_players_max],
  185. ret[qstat_game_players],
  186. ret[qstat_game_field],
  187. ret[qstat_map_field],
  188. ret[qstat_ping_field]);
  189. }
  190. /* close the pipe */
  191. pclose (fp);
  192. return result;
  193. }
  194. int
  195. process_arguments (int argc, char **argv)
  196. {
  197. int x;
  198. /* not enough options were supplied */
  199. if (argc < 3)
  200. return ERROR;
  201. /* first option is always the game type */
  202. strncpy (game_type, argv[1], sizeof (game_type) - 1);
  203. game_type[sizeof (game_type) - 1] = 0;
  204. /* Second option is always the server name */
  205. strncpy (server_ip, argv[2], sizeof (server_ip) - 1);
  206. server_ip[sizeof (server_ip) - 1] = 0;
  207. /* process all remaining arguments */
  208. for (x = 4; x <= argc; x++) {
  209. /* we got the port number to connect to */
  210. if (!strcmp (argv[x - 1], "-p")) {
  211. if (x < argc) {
  212. snprintf (port, sizeof (port) - 2, ":%s", argv[x]);
  213. port[sizeof (port) - 1] = 0;
  214. x++;
  215. }
  216. else
  217. return ERROR;
  218. }
  219. /* we got the game field */
  220. else if (!strcmp (argv[x - 1], "-gf")) {
  221. if (x < argc) {
  222. qstat_game_field = atoi (argv[x]);
  223. if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS)
  224. return ERROR;
  225. x++;
  226. }
  227. else
  228. return ERROR;
  229. }
  230. /* we got the map field */
  231. else if (!strcmp (argv[x - 1], "-mf")) {
  232. if (x < argc) {
  233. qstat_map_field = atoi (argv[x]);
  234. if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS)
  235. return ERROR;
  236. x++;
  237. }
  238. else
  239. return ERROR;
  240. }
  241. /* we got the ping field */
  242. else if (!strcmp (argv[x - 1], "-pf")) {
  243. if (x < argc) {
  244. qstat_ping_field = atoi (argv[x]);
  245. if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS)
  246. return ERROR;
  247. x++;
  248. }
  249. else
  250. return ERROR;
  251. }
  252. /* else we got something else... */
  253. else
  254. return ERROR;
  255. }
  256. return OK;
  257. }