diff options
Diffstat (limited to 'plugins/check_game.c')
-rw-r--r-- | plugins/check_game.c | 229 |
1 files changed, 119 insertions, 110 deletions
diff --git a/plugins/check_game.c b/plugins/check_game.c index 619277e7..c0193b03 100644 --- a/plugins/check_game.c +++ b/plugins/check_game.c | |||
@@ -36,9 +36,15 @@ const char *email = "devel@monitoring-plugins.org"; | |||
36 | #include "common.h" | 36 | #include "common.h" |
37 | #include "utils.h" | 37 | #include "utils.h" |
38 | #include "runcmd.h" | 38 | #include "runcmd.h" |
39 | #include "check_game.d/config.h" | ||
40 | #include "../lib/monitoringplug.h" | ||
39 | 41 | ||
40 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 42 | typedef struct { |
41 | static int validate_arguments(void); | 43 | int errorcode; |
44 | check_game_config config; | ||
45 | } check_game_config_wrapper; | ||
46 | |||
47 | static check_game_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
42 | static void print_help(void); | 48 | static void print_help(void); |
43 | void print_usage(void); | 49 | void print_usage(void); |
44 | 50 | ||
@@ -49,26 +55,9 @@ void print_usage(void); | |||
49 | #define QSTAT_HOST_TIMEOUT "TIMEOUT" | 55 | #define QSTAT_HOST_TIMEOUT "TIMEOUT" |
50 | #define QSTAT_MAX_RETURN_ARGS 12 | 56 | #define QSTAT_MAX_RETURN_ARGS 12 |
51 | 57 | ||
52 | static char *server_ip; | ||
53 | static char *game_type; | ||
54 | static int port = 0; | ||
55 | |||
56 | static bool verbose = false; | 58 | static bool verbose = false; |
57 | 59 | ||
58 | static int qstat_game_players_max = -1; | ||
59 | static int qstat_game_players = -1; | ||
60 | static int qstat_game_field = -1; | ||
61 | static int qstat_map_field = -1; | ||
62 | static int qstat_ping_field = -1; | ||
63 | |||
64 | int main(int argc, char **argv) { | 60 | int main(int argc, char **argv) { |
65 | char *command_line; | ||
66 | int result = STATE_UNKNOWN; | ||
67 | char *p; | ||
68 | char *ret[QSTAT_MAX_RETURN_ARGS]; | ||
69 | size_t i = 0; | ||
70 | output chld_out; | ||
71 | |||
72 | setlocale(LC_ALL, ""); | 61 | setlocale(LC_ALL, ""); |
73 | bindtextdomain(PACKAGE, LOCALEDIR); | 62 | bindtextdomain(PACKAGE, LOCALEDIR); |
74 | textdomain(PACKAGE); | 63 | textdomain(PACKAGE); |
@@ -76,22 +65,31 @@ int main(int argc, char **argv) { | |||
76 | /* Parse extra opts if any */ | 65 | /* Parse extra opts if any */ |
77 | argv = np_extra_opts(&argc, argv, progname); | 66 | argv = np_extra_opts(&argc, argv, progname); |
78 | 67 | ||
79 | if (process_arguments(argc, argv) == ERROR) | 68 | check_game_config_wrapper tmp = process_arguments(argc, argv); |
69 | |||
70 | if (tmp.errorcode == ERROR) { | ||
80 | usage_va(_("Could not parse arguments")); | 71 | usage_va(_("Could not parse arguments")); |
72 | } | ||
73 | |||
74 | check_game_config config = tmp.config; | ||
81 | 75 | ||
82 | result = STATE_OK; | 76 | mp_state_enum result = STATE_OK; |
83 | 77 | ||
84 | /* create the command line to execute */ | 78 | /* create the command line to execute */ |
85 | xasprintf(&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip); | 79 | char *command_line = NULL; |
80 | xasprintf(&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, config.game_type, config.server_ip); | ||
86 | 81 | ||
87 | if (port) | 82 | if (config.port) { |
88 | xasprintf(&command_line, "%s:%-d", command_line, port); | 83 | xasprintf(&command_line, "%s:%-d", command_line, config.port); |
84 | } | ||
89 | 85 | ||
90 | if (verbose) | 86 | if (verbose) { |
91 | printf("%s\n", command_line); | 87 | printf("%s\n", command_line); |
88 | } | ||
92 | 89 | ||
93 | /* run the command. historically, this plugin ignores output on stderr, | 90 | /* run the command. historically, this plugin ignores output on stderr, |
94 | * as well as return status of the qstat program */ | 91 | * as well as return status of the qstat program */ |
92 | output chld_out = {}; | ||
95 | (void)np_runcmd(command_line, &chld_out, NULL, 0); | 93 | (void)np_runcmd(command_line, &chld_out, NULL, 0); |
96 | 94 | ||
97 | /* sanity check */ | 95 | /* sanity check */ |
@@ -104,19 +102,22 @@ int main(int argc, char **argv) { | |||
104 | In the end, I figured I'd simply let an error occur & then trap it | 102 | In the end, I figured I'd simply let an error occur & then trap it |
105 | */ | 103 | */ |
106 | 104 | ||
107 | if (!strncmp(chld_out.line[0], "unknown option", 14)) { | 105 | if (!strncmp(chld_out.line[0], "unknown option", strlen("unknown option"))) { |
108 | printf(_("CRITICAL - Host type parameter incorrect!\n")); | 106 | printf(_("CRITICAL - Host type parameter incorrect!\n")); |
109 | result = STATE_CRITICAL; | 107 | result = STATE_CRITICAL; |
110 | return result; | 108 | exit(result); |
111 | } | 109 | } |
112 | 110 | ||
113 | p = (char *)strtok(chld_out.line[0], QSTAT_DATA_DELIMITER); | 111 | char *ret[QSTAT_MAX_RETURN_ARGS]; |
114 | while (p != NULL) { | 112 | size_t i = 0; |
115 | ret[i] = p; | 113 | char *sequence = strtok(chld_out.line[0], QSTAT_DATA_DELIMITER); |
116 | p = (char *)strtok(NULL, QSTAT_DATA_DELIMITER); | 114 | while (sequence != NULL) { |
115 | ret[i] = sequence; | ||
116 | sequence = strtok(NULL, QSTAT_DATA_DELIMITER); | ||
117 | i++; | 117 | i++; |
118 | if (i >= QSTAT_MAX_RETURN_ARGS) | 118 | if (i >= QSTAT_MAX_RETURN_ARGS) { |
119 | break; | 119 | break; |
120 | } | ||
120 | } | 121 | } |
121 | 122 | ||
122 | if (strstr(ret[2], QSTAT_HOST_ERROR)) { | 123 | if (strstr(ret[2], QSTAT_HOST_ERROR)) { |
@@ -129,19 +130,20 @@ int main(int argc, char **argv) { | |||
129 | printf(_("CRITICAL - Game server timeout\n")); | 130 | printf(_("CRITICAL - Game server timeout\n")); |
130 | result = STATE_CRITICAL; | 131 | result = STATE_CRITICAL; |
131 | } else { | 132 | } else { |
132 | printf("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[qstat_game_players], ret[qstat_game_players_max], ret[qstat_game_field], | 133 | printf("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[config.qstat_game_players], ret[config.qstat_game_players_max], |
133 | ret[qstat_map_field], ret[qstat_ping_field], | 134 | ret[config.qstat_game_field], ret[config.qstat_map_field], ret[config.qstat_ping_field], |
134 | perfdata("players", atol(ret[qstat_game_players]), "", false, 0, false, 0, true, 0, true, atol(ret[qstat_game_players_max])), | 135 | perfdata("players", atol(ret[config.qstat_game_players]), "", false, 0, false, 0, true, 0, true, |
135 | fperfdata("ping", strtod(ret[qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0)); | 136 | atol(ret[config.qstat_game_players_max])), |
137 | fperfdata("ping", strtod(ret[config.qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0)); | ||
136 | } | 138 | } |
137 | 139 | ||
138 | return result; | 140 | exit(result); |
139 | } | 141 | } |
140 | 142 | ||
141 | int process_arguments(int argc, char **argv) { | 143 | #define players_field_index 129 |
142 | int c; | 144 | #define max_players_field_index 130 |
143 | 145 | ||
144 | int opt_index = 0; | 146 | check_game_config_wrapper process_arguments(int argc, char **argv) { |
145 | static struct option long_opts[] = {{"help", no_argument, 0, 'h'}, | 147 | static struct option long_opts[] = {{"help", no_argument, 0, 'h'}, |
146 | {"version", no_argument, 0, 'V'}, | 148 | {"version", no_argument, 0, 'V'}, |
147 | {"verbose", no_argument, 0, 'v'}, | 149 | {"verbose", no_argument, 0, 'v'}, |
@@ -152,29 +154,39 @@ int process_arguments(int argc, char **argv) { | |||
152 | {"map-field", required_argument, 0, 'm'}, | 154 | {"map-field", required_argument, 0, 'm'}, |
153 | {"ping-field", required_argument, 0, 'p'}, | 155 | {"ping-field", required_argument, 0, 'p'}, |
154 | {"game-field", required_argument, 0, 'g'}, | 156 | {"game-field", required_argument, 0, 'g'}, |
155 | {"players-field", required_argument, 0, 129}, | 157 | {"players-field", required_argument, 0, players_field_index}, |
156 | {"max-players-field", required_argument, 0, 130}, | 158 | {"max-players-field", required_argument, 0, max_players_field_index}, |
157 | {0, 0, 0, 0}}; | 159 | {0, 0, 0, 0}}; |
158 | 160 | ||
159 | if (argc < 2) | 161 | check_game_config_wrapper result = { |
160 | return ERROR; | 162 | .config = check_game_config_init(), |
163 | .errorcode = OK, | ||
164 | }; | ||
165 | |||
166 | if (argc < 2) { | ||
167 | result.errorcode = ERROR; | ||
168 | return result; | ||
169 | } | ||
161 | 170 | ||
162 | for (c = 1; c < argc; c++) { | 171 | for (int option_counter = 1; option_counter < argc; option_counter++) { |
163 | if (strcmp("-mf", argv[c]) == 0) | 172 | if (strcmp("-mf", argv[option_counter]) == 0) { |
164 | strcpy(argv[c], "-m"); | 173 | strcpy(argv[option_counter], "-m"); |
165 | else if (strcmp("-pf", argv[c]) == 0) | 174 | } else if (strcmp("-pf", argv[option_counter]) == 0) { |
166 | strcpy(argv[c], "-p"); | 175 | strcpy(argv[option_counter], "-p"); |
167 | else if (strcmp("-gf", argv[c]) == 0) | 176 | } else if (strcmp("-gf", argv[option_counter]) == 0) { |
168 | strcpy(argv[c], "-g"); | 177 | strcpy(argv[option_counter], "-g"); |
178 | } | ||
169 | } | 179 | } |
170 | 180 | ||
171 | while (1) { | 181 | int opt_index = 0; |
172 | c = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); | 182 | while (true) { |
183 | int option_index = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); | ||
173 | 184 | ||
174 | if (c == -1 || c == EOF) | 185 | if (option_index == -1 || option_index == EOF) { |
175 | break; | 186 | break; |
187 | } | ||
176 | 188 | ||
177 | switch (c) { | 189 | switch (option_index) { |
178 | case 'h': /* help */ | 190 | case 'h': /* help */ |
179 | print_help(); | 191 | print_help(); |
180 | exit(STATE_UNKNOWN); | 192 | exit(STATE_UNKNOWN); |
@@ -188,79 +200,75 @@ int process_arguments(int argc, char **argv) { | |||
188 | timeout_interval = atoi(optarg); | 200 | timeout_interval = atoi(optarg); |
189 | break; | 201 | break; |
190 | case 'H': /* hostname */ | 202 | case 'H': /* hostname */ |
191 | if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) | 203 | if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) { |
192 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 204 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
193 | server_ip = optarg; | 205 | } |
206 | result.config.server_ip = optarg; | ||
194 | break; | 207 | break; |
195 | case 'P': /* port */ | 208 | case 'P': /* port */ |
196 | port = atoi(optarg); | 209 | result.config.port = atoi(optarg); |
197 | break; | 210 | break; |
198 | case 'G': /* hostname */ | 211 | case 'G': /* hostname */ |
199 | if (strlen(optarg) >= MAX_INPUT_BUFFER) | 212 | if (strlen(optarg) >= MAX_INPUT_BUFFER) { |
200 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 213 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
201 | game_type = optarg; | 214 | } |
215 | result.config.game_type = optarg; | ||
202 | break; | 216 | break; |
203 | case 'p': /* index of ping field */ | 217 | case 'p': /* index of ping field */ |
204 | qstat_ping_field = atoi(optarg); | 218 | result.config.qstat_ping_field = atoi(optarg); |
205 | if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS) | 219 | if (result.config.qstat_ping_field < 0 || result.config.qstat_ping_field > QSTAT_MAX_RETURN_ARGS) { |
206 | return ERROR; | 220 | result.errorcode = ERROR; |
221 | return result; | ||
222 | } | ||
207 | break; | 223 | break; |
208 | case 'm': /* index on map field */ | 224 | case 'm': /* index on map field */ |
209 | qstat_map_field = atoi(optarg); | 225 | result.config.qstat_map_field = atoi(optarg); |
210 | if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS) | 226 | if (result.config.qstat_map_field < 0 || result.config.qstat_map_field > QSTAT_MAX_RETURN_ARGS) { |
211 | return ERROR; | 227 | result.errorcode = ERROR; |
228 | return result; | ||
229 | } | ||
212 | break; | 230 | break; |
213 | case 'g': /* index of game field */ | 231 | case 'g': /* index of game field */ |
214 | qstat_game_field = atoi(optarg); | 232 | result.config.qstat_game_field = atoi(optarg); |
215 | if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS) | 233 | if (result.config.qstat_game_field < 0 || result.config.qstat_game_field > QSTAT_MAX_RETURN_ARGS) { |
216 | return ERROR; | 234 | result.errorcode = ERROR; |
235 | return result; | ||
236 | } | ||
217 | break; | 237 | break; |
218 | case 129: /* index of player count field */ | 238 | case players_field_index: /* index of player count field */ |
219 | qstat_game_players = atoi(optarg); | 239 | result.config.qstat_game_players = atoi(optarg); |
220 | if (qstat_game_players_max == 0) | 240 | if (result.config.qstat_game_players_max == 0) { |
221 | qstat_game_players_max = qstat_game_players - 1; | 241 | result.config.qstat_game_players_max = result.config.qstat_game_players - 1; |
222 | if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS) | 242 | } |
223 | return ERROR; | 243 | if (result.config.qstat_game_players < 0 || result.config.qstat_game_players > QSTAT_MAX_RETURN_ARGS) { |
244 | result.errorcode = ERROR; | ||
245 | return result; | ||
246 | } | ||
224 | break; | 247 | break; |
225 | case 130: /* index of max players field */ | 248 | case max_players_field_index: /* index of max players field */ |
226 | qstat_game_players_max = atoi(optarg); | 249 | result.config.qstat_game_players_max = atoi(optarg); |
227 | if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) | 250 | if (result.config.qstat_game_players_max < 0 || result.config.qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) { |
228 | return ERROR; | 251 | result.errorcode = ERROR; |
252 | return result; | ||
253 | } | ||
229 | break; | 254 | break; |
230 | default: /* args not parsable */ | 255 | default: /* args not parsable */ |
231 | usage5(); | 256 | usage5(); |
232 | } | 257 | } |
233 | } | 258 | } |
234 | 259 | ||
235 | c = optind; | 260 | int option_counter = optind; |
236 | /* first option is the game type */ | 261 | /* first option is the game type */ |
237 | if (!game_type && c < argc) | 262 | if (!result.config.game_type && option_counter < argc) { |
238 | game_type = strdup(argv[c++]); | 263 | result.config.game_type = strdup(argv[option_counter++]); |
264 | } | ||
239 | 265 | ||
240 | /* Second option is the server name */ | 266 | /* Second option is the server name */ |
241 | if (!server_ip && c < argc) | 267 | if (!result.config.server_ip && option_counter < argc) { |
242 | server_ip = strdup(argv[c++]); | 268 | result.config.server_ip = strdup(argv[option_counter++]); |
243 | 269 | } | |
244 | return validate_arguments(); | ||
245 | } | ||
246 | |||
247 | int validate_arguments(void) { | ||
248 | if (qstat_game_players_max < 0) | ||
249 | qstat_game_players_max = 4; | ||
250 | |||
251 | if (qstat_game_players < 0) | ||
252 | qstat_game_players = 5; | ||
253 | |||
254 | if (qstat_game_field < 0) | ||
255 | qstat_game_field = 2; | ||
256 | |||
257 | if (qstat_map_field < 0) | ||
258 | qstat_map_field = 3; | ||
259 | |||
260 | if (qstat_ping_field < 0) | ||
261 | qstat_ping_field = 5; | ||
262 | 270 | ||
263 | return OK; | 271 | return result; |
264 | } | 272 | } |
265 | 273 | ||
266 | void print_help(void) { | 274 | void print_help(void) { |
@@ -277,14 +285,15 @@ void print_help(void) { | |||
277 | 285 | ||
278 | printf(UT_HELP_VRSN); | 286 | printf(UT_HELP_VRSN); |
279 | printf(UT_EXTRA_OPTS); | 287 | printf(UT_EXTRA_OPTS); |
280 | 288 | printf(" -H, --hostname=ADDRESS\n" | |
281 | printf(" %s\n", "-p"); | 289 | " Host name, IP Address, or unix socket (must be an absolute path)\n"); |
282 | printf(" %s\n", _("Optional port of which to connect")); | 290 | printf(" %s\n", "-P"); |
283 | printf(" %s\n", "gf"); | 291 | printf(" %s\n", _("Optional port to connect to")); |
292 | printf(" %s\n", "-g"); | ||
284 | printf(" %s\n", _("Field number in raw qstat output that contains game name")); | 293 | printf(" %s\n", _("Field number in raw qstat output that contains game name")); |
285 | printf(" %s\n", "-mf"); | 294 | printf(" %s\n", "-m"); |
286 | printf(" %s\n", _("Field number in raw qstat output that contains map name")); | 295 | printf(" %s\n", _("Field number in raw qstat output that contains map name")); |
287 | printf(" %s\n", "-pf"); | 296 | printf(" %s\n", "-p"); |
288 | printf(" %s\n", _("Field number in raw qstat output that contains ping time")); | 297 | printf(" %s\n", _("Field number in raw qstat output that contains ping time")); |
289 | 298 | ||
290 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 299 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |