diff options
Diffstat (limited to 'plugins/check_game.c')
| -rw-r--r-- | plugins/check_game.c | 273 |
1 files changed, 147 insertions, 126 deletions
diff --git a/plugins/check_game.c b/plugins/check_game.c index 619277e7..974a7253 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,32 @@ 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, | ||
| 81 | config.game_type, config.server_ip); | ||
| 86 | 82 | ||
| 87 | if (port) | 83 | if (config.port) { |
| 88 | xasprintf(&command_line, "%s:%-d", command_line, port); | 84 | xasprintf(&command_line, "%s:%-d", command_line, config.port); |
| 85 | } | ||
| 89 | 86 | ||
| 90 | if (verbose) | 87 | if (verbose) { |
| 91 | printf("%s\n", command_line); | 88 | printf("%s\n", command_line); |
| 89 | } | ||
| 92 | 90 | ||
| 93 | /* run the command. historically, this plugin ignores output on stderr, | 91 | /* run the command. historically, this plugin ignores output on stderr, |
| 94 | * as well as return status of the qstat program */ | 92 | * as well as return status of the qstat program */ |
| 93 | output chld_out = {}; | ||
| 95 | (void)np_runcmd(command_line, &chld_out, NULL, 0); | 94 | (void)np_runcmd(command_line, &chld_out, NULL, 0); |
| 96 | 95 | ||
| 97 | /* sanity check */ | 96 | /* sanity check */ |
| @@ -104,19 +103,22 @@ int main(int argc, char **argv) { | |||
| 104 | In the end, I figured I'd simply let an error occur & then trap it | 103 | In the end, I figured I'd simply let an error occur & then trap it |
| 105 | */ | 104 | */ |
| 106 | 105 | ||
| 107 | if (!strncmp(chld_out.line[0], "unknown option", 14)) { | 106 | if (!strncmp(chld_out.line[0], "unknown option", strlen("unknown option"))) { |
| 108 | printf(_("CRITICAL - Host type parameter incorrect!\n")); | 107 | printf(_("CRITICAL - Host type parameter incorrect!\n")); |
| 109 | result = STATE_CRITICAL; | 108 | result = STATE_CRITICAL; |
| 110 | return result; | 109 | exit(result); |
| 111 | } | 110 | } |
| 112 | 111 | ||
| 113 | p = (char *)strtok(chld_out.line[0], QSTAT_DATA_DELIMITER); | 112 | char *ret[QSTAT_MAX_RETURN_ARGS]; |
| 114 | while (p != NULL) { | 113 | size_t i = 0; |
| 115 | ret[i] = p; | 114 | char *sequence = strtok(chld_out.line[0], QSTAT_DATA_DELIMITER); |
| 116 | p = (char *)strtok(NULL, QSTAT_DATA_DELIMITER); | 115 | while (sequence != NULL) { |
| 116 | ret[i] = sequence; | ||
| 117 | sequence = strtok(NULL, QSTAT_DATA_DELIMITER); | ||
| 117 | i++; | 118 | i++; |
| 118 | if (i >= QSTAT_MAX_RETURN_ARGS) | 119 | if (i >= QSTAT_MAX_RETURN_ARGS) { |
| 119 | break; | 120 | break; |
| 121 | } | ||
| 120 | } | 122 | } |
| 121 | 123 | ||
| 122 | if (strstr(ret[2], QSTAT_HOST_ERROR)) { | 124 | if (strstr(ret[2], QSTAT_HOST_ERROR)) { |
| @@ -129,52 +131,66 @@ int main(int argc, char **argv) { | |||
| 129 | printf(_("CRITICAL - Game server timeout\n")); | 131 | printf(_("CRITICAL - Game server timeout\n")); |
| 130 | result = STATE_CRITICAL; | 132 | result = STATE_CRITICAL; |
| 131 | } else { | 133 | } 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], | 134 | printf("OK: %s/%s %s (%s), Ping: %s ms|%s %s\n", ret[config.qstat_game_players], |
| 133 | ret[qstat_map_field], ret[qstat_ping_field], | 135 | ret[config.qstat_game_players_max], ret[config.qstat_game_field], |
| 134 | perfdata("players", atol(ret[qstat_game_players]), "", false, 0, false, 0, true, 0, true, atol(ret[qstat_game_players_max])), | 136 | ret[config.qstat_map_field], ret[config.qstat_ping_field], |
| 135 | fperfdata("ping", strtod(ret[qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0)); | 137 | perfdata("players", atol(ret[config.qstat_game_players]), "", false, 0, false, 0, |
| 138 | true, 0, true, atol(ret[config.qstat_game_players_max])), | ||
| 139 | fperfdata("ping", strtod(ret[config.qstat_ping_field], NULL), "", false, 0, false, 0, | ||
| 140 | true, 0, false, 0)); | ||
| 136 | } | 141 | } |
| 137 | 142 | ||
| 138 | return result; | 143 | exit(result); |
| 139 | } | 144 | } |
| 140 | 145 | ||
| 141 | int process_arguments(int argc, char **argv) { | 146 | #define players_field_index 129 |
| 142 | int c; | 147 | #define max_players_field_index 130 |
| 148 | |||
| 149 | check_game_config_wrapper process_arguments(int argc, char **argv) { | ||
| 150 | static struct option long_opts[] = { | ||
| 151 | {"help", no_argument, 0, 'h'}, | ||
| 152 | {"version", no_argument, 0, 'V'}, | ||
| 153 | {"verbose", no_argument, 0, 'v'}, | ||
| 154 | {"timeout", required_argument, 0, 't'}, | ||
| 155 | {"hostname", required_argument, 0, 'H'}, | ||
| 156 | {"port", required_argument, 0, 'P'}, | ||
| 157 | {"game-type", required_argument, 0, 'G'}, | ||
| 158 | {"map-field", required_argument, 0, 'm'}, | ||
| 159 | {"ping-field", required_argument, 0, 'p'}, | ||
| 160 | {"game-field", required_argument, 0, 'g'}, | ||
| 161 | {"players-field", required_argument, 0, players_field_index}, | ||
| 162 | {"max-players-field", required_argument, 0, max_players_field_index}, | ||
| 163 | {0, 0, 0, 0}}; | ||
| 164 | |||
| 165 | check_game_config_wrapper result = { | ||
| 166 | .config = check_game_config_init(), | ||
| 167 | .errorcode = OK, | ||
| 168 | }; | ||
| 169 | |||
| 170 | if (argc < 2) { | ||
| 171 | result.errorcode = ERROR; | ||
| 172 | return result; | ||
| 173 | } | ||
| 143 | 174 | ||
| 144 | int opt_index = 0; | 175 | for (int option_counter = 1; option_counter < argc; option_counter++) { |
| 145 | static struct option long_opts[] = {{"help", no_argument, 0, 'h'}, | 176 | if (strcmp("-mf", argv[option_counter]) == 0) { |
| 146 | {"version", no_argument, 0, 'V'}, | 177 | strcpy(argv[option_counter], "-m"); |
| 147 | {"verbose", no_argument, 0, 'v'}, | 178 | } else if (strcmp("-pf", argv[option_counter]) == 0) { |
| 148 | {"timeout", required_argument, 0, 't'}, | 179 | strcpy(argv[option_counter], "-p"); |
| 149 | {"hostname", required_argument, 0, 'H'}, | 180 | } else if (strcmp("-gf", argv[option_counter]) == 0) { |
| 150 | {"port", required_argument, 0, 'P'}, | 181 | strcpy(argv[option_counter], "-g"); |
| 151 | {"game-type", required_argument, 0, 'G'}, | 182 | } |
| 152 | {"map-field", required_argument, 0, 'm'}, | ||
| 153 | {"ping-field", required_argument, 0, 'p'}, | ||
| 154 | {"game-field", required_argument, 0, 'g'}, | ||
| 155 | {"players-field", required_argument, 0, 129}, | ||
| 156 | {"max-players-field", required_argument, 0, 130}, | ||
| 157 | {0, 0, 0, 0}}; | ||
| 158 | |||
| 159 | if (argc < 2) | ||
| 160 | return ERROR; | ||
| 161 | |||
| 162 | for (c = 1; c < argc; c++) { | ||
| 163 | if (strcmp("-mf", argv[c]) == 0) | ||
| 164 | strcpy(argv[c], "-m"); | ||
| 165 | else if (strcmp("-pf", argv[c]) == 0) | ||
| 166 | strcpy(argv[c], "-p"); | ||
| 167 | else if (strcmp("-gf", argv[c]) == 0) | ||
| 168 | strcpy(argv[c], "-g"); | ||
| 169 | } | 183 | } |
| 170 | 184 | ||
| 171 | while (1) { | 185 | int opt_index = 0; |
| 172 | c = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); | 186 | while (true) { |
| 187 | int option_index = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); | ||
| 173 | 188 | ||
| 174 | if (c == -1 || c == EOF) | 189 | if (option_index == -1 || option_index == EOF) { |
| 175 | break; | 190 | break; |
| 191 | } | ||
| 176 | 192 | ||
| 177 | switch (c) { | 193 | switch (option_index) { |
| 178 | case 'h': /* help */ | 194 | case 'h': /* help */ |
| 179 | print_help(); | 195 | print_help(); |
| 180 | exit(STATE_UNKNOWN); | 196 | exit(STATE_UNKNOWN); |
| @@ -188,79 +204,80 @@ int process_arguments(int argc, char **argv) { | |||
| 188 | timeout_interval = atoi(optarg); | 204 | timeout_interval = atoi(optarg); |
| 189 | break; | 205 | break; |
| 190 | case 'H': /* hostname */ | 206 | case 'H': /* hostname */ |
| 191 | if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) | 207 | if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) { |
| 192 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 208 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
| 193 | server_ip = optarg; | 209 | } |
| 210 | result.config.server_ip = optarg; | ||
| 194 | break; | 211 | break; |
| 195 | case 'P': /* port */ | 212 | case 'P': /* port */ |
| 196 | port = atoi(optarg); | 213 | result.config.port = atoi(optarg); |
| 197 | break; | 214 | break; |
| 198 | case 'G': /* hostname */ | 215 | case 'G': /* hostname */ |
| 199 | if (strlen(optarg) >= MAX_INPUT_BUFFER) | 216 | if (strlen(optarg) >= MAX_INPUT_BUFFER) { |
| 200 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); | 217 | die(STATE_UNKNOWN, _("Input buffer overflow\n")); |
| 201 | game_type = optarg; | 218 | } |
| 219 | result.config.game_type = optarg; | ||
| 202 | break; | 220 | break; |
| 203 | case 'p': /* index of ping field */ | 221 | case 'p': /* index of ping field */ |
| 204 | qstat_ping_field = atoi(optarg); | 222 | result.config.qstat_ping_field = atoi(optarg); |
| 205 | if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS) | 223 | if (result.config.qstat_ping_field < 0 || |
| 206 | return ERROR; | 224 | result.config.qstat_ping_field > QSTAT_MAX_RETURN_ARGS) { |
| 225 | result.errorcode = ERROR; | ||
| 226 | return result; | ||
| 227 | } | ||
| 207 | break; | 228 | break; |
| 208 | case 'm': /* index on map field */ | 229 | case 'm': /* index on map field */ |
| 209 | qstat_map_field = atoi(optarg); | 230 | result.config.qstat_map_field = atoi(optarg); |
| 210 | if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS) | 231 | if (result.config.qstat_map_field < 0 || |
| 211 | return ERROR; | 232 | result.config.qstat_map_field > QSTAT_MAX_RETURN_ARGS) { |
| 233 | result.errorcode = ERROR; | ||
| 234 | return result; | ||
| 235 | } | ||
| 212 | break; | 236 | break; |
| 213 | case 'g': /* index of game field */ | 237 | case 'g': /* index of game field */ |
| 214 | qstat_game_field = atoi(optarg); | 238 | result.config.qstat_game_field = atoi(optarg); |
| 215 | if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS) | 239 | if (result.config.qstat_game_field < 0 || |
| 216 | return ERROR; | 240 | result.config.qstat_game_field > QSTAT_MAX_RETURN_ARGS) { |
| 241 | result.errorcode = ERROR; | ||
| 242 | return result; | ||
| 243 | } | ||
| 217 | break; | 244 | break; |
| 218 | case 129: /* index of player count field */ | 245 | case players_field_index: /* index of player count field */ |
| 219 | qstat_game_players = atoi(optarg); | 246 | result.config.qstat_game_players = atoi(optarg); |
| 220 | if (qstat_game_players_max == 0) | 247 | if (result.config.qstat_game_players_max == 0) { |
| 221 | qstat_game_players_max = qstat_game_players - 1; | 248 | 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) | 249 | } |
| 223 | return ERROR; | 250 | if (result.config.qstat_game_players < 0 || |
| 251 | result.config.qstat_game_players > QSTAT_MAX_RETURN_ARGS) { | ||
| 252 | result.errorcode = ERROR; | ||
| 253 | return result; | ||
| 254 | } | ||
| 224 | break; | 255 | break; |
| 225 | case 130: /* index of max players field */ | 256 | case max_players_field_index: /* index of max players field */ |
| 226 | qstat_game_players_max = atoi(optarg); | 257 | result.config.qstat_game_players_max = atoi(optarg); |
| 227 | if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) | 258 | if (result.config.qstat_game_players_max < 0 || |
| 228 | return ERROR; | 259 | result.config.qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) { |
| 260 | result.errorcode = ERROR; | ||
| 261 | return result; | ||
| 262 | } | ||
| 229 | break; | 263 | break; |
| 230 | default: /* args not parsable */ | 264 | default: /* args not parsable */ |
| 231 | usage5(); | 265 | usage5(); |
| 232 | } | 266 | } |
| 233 | } | 267 | } |
| 234 | 268 | ||
| 235 | c = optind; | 269 | int option_counter = optind; |
| 236 | /* first option is the game type */ | 270 | /* first option is the game type */ |
| 237 | if (!game_type && c < argc) | 271 | if (!result.config.game_type && option_counter < argc) { |
| 238 | game_type = strdup(argv[c++]); | 272 | result.config.game_type = strdup(argv[option_counter++]); |
| 273 | } | ||
| 239 | 274 | ||
| 240 | /* Second option is the server name */ | 275 | /* Second option is the server name */ |
| 241 | if (!server_ip && c < argc) | 276 | if (!result.config.server_ip && option_counter < argc) { |
| 242 | server_ip = strdup(argv[c++]); | 277 | result.config.server_ip = strdup(argv[option_counter++]); |
| 243 | 278 | } | |
| 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 | 279 | ||
| 263 | return OK; | 280 | return result; |
| 264 | } | 281 | } |
| 265 | 282 | ||
| 266 | void print_help(void) { | 283 | void print_help(void) { |
| @@ -277,22 +294,25 @@ void print_help(void) { | |||
| 277 | 294 | ||
| 278 | printf(UT_HELP_VRSN); | 295 | printf(UT_HELP_VRSN); |
| 279 | printf(UT_EXTRA_OPTS); | 296 | printf(UT_EXTRA_OPTS); |
| 280 | 297 | printf(" -H, --hostname=ADDRESS\n" | |
| 281 | printf(" %s\n", "-p"); | 298 | " Host name, IP Address, or unix socket (must be an absolute path)\n"); |
| 282 | printf(" %s\n", _("Optional port of which to connect")); | 299 | printf(" %s\n", "-P"); |
| 283 | printf(" %s\n", "gf"); | 300 | printf(" %s\n", _("Optional port to connect to")); |
| 301 | printf(" %s\n", "-g"); | ||
| 284 | printf(" %s\n", _("Field number in raw qstat output that contains game name")); | 302 | printf(" %s\n", _("Field number in raw qstat output that contains game name")); |
| 285 | printf(" %s\n", "-mf"); | 303 | printf(" %s\n", "-m"); |
| 286 | printf(" %s\n", _("Field number in raw qstat output that contains map name")); | 304 | printf(" %s\n", _("Field number in raw qstat output that contains map name")); |
| 287 | printf(" %s\n", "-pf"); | 305 | printf(" %s\n", "-p"); |
| 288 | printf(" %s\n", _("Field number in raw qstat output that contains ping time")); | 306 | printf(" %s\n", _("Field number in raw qstat output that contains ping time")); |
| 289 | 307 | ||
| 290 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 308 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 291 | 309 | ||
| 292 | printf("\n"); | 310 | printf("\n"); |
| 293 | printf("%s\n", _("Notes:")); | 311 | printf("%s\n", _("Notes:")); |
| 294 | printf(" %s\n", _("This plugin uses the 'qstat' command, the popular game server status query tool.")); | 312 | printf(" %s\n", |
| 295 | printf(" %s\n", _("If you don't have the package installed, you will need to download it from")); | 313 | _("This plugin uses the 'qstat' command, the popular game server status query tool.")); |
| 314 | printf(" %s\n", | ||
| 315 | _("If you don't have the package installed, you will need to download it from")); | ||
| 296 | printf(" %s\n", _("https://github.com/multiplay/qstat before you can use this plugin.")); | 316 | printf(" %s\n", _("https://github.com/multiplay/qstat before you can use this plugin.")); |
| 297 | 317 | ||
| 298 | printf(UT_SUPPORT); | 318 | printf(UT_SUPPORT); |
| @@ -300,7 +320,8 @@ void print_help(void) { | |||
| 300 | 320 | ||
| 301 | void print_usage(void) { | 321 | void print_usage(void) { |
| 302 | printf("%s\n", _("Usage:")); | 322 | printf("%s\n", _("Usage:")); |
| 303 | printf(" %s [-hvV] [-P port] [-t timeout] [-g game_field] [-m map_field] [-p ping_field] [-G game-time] [-H hostname] <game> " | 323 | printf(" %s [-hvV] [-P port] [-t timeout] [-g game_field] [-m map_field] [-p ping_field] [-G " |
| 324 | "game-time] [-H hostname] <game> " | ||
| 304 | "<ip_address>\n", | 325 | "<ip_address>\n", |
| 305 | progname); | 326 | progname); |
| 306 | } | 327 | } |
