4
0

check_game.c 8.0 KB

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