summaryrefslogtreecommitdiffstats
path: root/plugins/check_game.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-07 15:58:30 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-07 15:58:30 +0100
commit9f8a485744ba8de13c01cc48a4db0d8d73189c97 (patch)
tree3791d8ede7f1e3b8639138ce81b57acf6ca92a0e /plugins/check_game.c
parent065ed65a870bd973c751940ef8e6f47f62d88d26 (diff)
downloadmonitoring-plugins-9f8a485744ba8de13c01cc48a4db0d8d73189c97.tar.gz
Refactor check_game
Diffstat (limited to 'plugins/check_game.c')
-rw-r--r--plugins/check_game.c211
1 files changed, 109 insertions, 102 deletions
diff --git a/plugins/check_game.c b/plugins/check_game.c
index 619277e7..58014c3e 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
40static int process_arguments(int /*argc*/, char ** /*argv*/); 42typedef struct {
41static int validate_arguments(void); 43 int errorcode;
44 check_game_config config;
45} check_game_config_wrapper;
46
47static check_game_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
42static void print_help(void); 48static void print_help(void);
43void print_usage(void); 49void 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
52static char *server_ip;
53static char *game_type;
54static int port = 0;
55
56static bool verbose = false; 58static bool verbose = false;
57 59
58static int qstat_game_players_max = -1;
59static int qstat_game_players = -1;
60static int qstat_game_field = -1;
61static int qstat_map_field = -1;
62static int qstat_ping_field = -1;
63
64int main(int argc, char **argv) { 60int 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 }
81 73
82 result = STATE_OK; 74 check_game_config config = tmp.config;
75
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,18 +130,19 @@ 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], ret[config.qstat_game_field],
133 ret[qstat_map_field], ret[qstat_ping_field], 134 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, atol(ret[config.qstat_game_players_max])),
135 fperfdata("ping", strtod(ret[qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0)); 136 fperfdata("ping", strtod(ret[config.qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0));
136 } 137 }
137 138
138 return result; 139 exit(result);
139} 140}
140 141
141int process_arguments(int argc, char **argv) { 142#define players_field_index 129
142 int c; 143#define max_players_field_index 130
143 144
145check_game_config_wrapper process_arguments(int argc, char **argv) {
144 int opt_index = 0; 146 int opt_index = 0;
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'},
@@ -152,29 +154,38 @@ 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 };
161 165
162 for (c = 1; c < argc; c++) { 166 if (argc < 2) {
163 if (strcmp("-mf", argv[c]) == 0) 167 result.errorcode = ERROR;
164 strcpy(argv[c], "-m"); 168 return result;
165 else if (strcmp("-pf", argv[c]) == 0) 169 }
166 strcpy(argv[c], "-p"); 170
167 else if (strcmp("-gf", argv[c]) == 0) 171 for (int option_counter = 1; option_counter < argc; option_counter++) {
168 strcpy(argv[c], "-g"); 172 if (strcmp("-mf", argv[option_counter]) == 0) {
173 strcpy(argv[option_counter], "-m");
174 } else if (strcmp("-pf", argv[option_counter]) == 0) {
175 strcpy(argv[option_counter], "-p");
176 } else if (strcmp("-gf", argv[option_counter]) == 0) {
177 strcpy(argv[option_counter], "-g");
178 }
169 } 179 }
170 180
171 while (1) { 181 while (1) {
172 c = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); 182 int option_index = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
173 183
174 if (c == -1 || c == EOF) 184 if (option_index == -1 || option_index == EOF) {
175 break; 185 break;
186 }
176 187
177 switch (c) { 188 switch (option_index) {
178 case 'h': /* help */ 189 case 'h': /* help */
179 print_help(); 190 print_help();
180 exit(STATE_UNKNOWN); 191 exit(STATE_UNKNOWN);
@@ -188,79 +199,75 @@ int process_arguments(int argc, char **argv) {
188 timeout_interval = atoi(optarg); 199 timeout_interval = atoi(optarg);
189 break; 200 break;
190 case 'H': /* hostname */ 201 case 'H': /* hostname */
191 if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) 202 if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) {
192 die(STATE_UNKNOWN, _("Input buffer overflow\n")); 203 die(STATE_UNKNOWN, _("Input buffer overflow\n"));
193 server_ip = optarg; 204 }
205 result.config.server_ip = optarg;
194 break; 206 break;
195 case 'P': /* port */ 207 case 'P': /* port */
196 port = atoi(optarg); 208 result.config.port = atoi(optarg);
197 break; 209 break;
198 case 'G': /* hostname */ 210 case 'G': /* hostname */
199 if (strlen(optarg) >= MAX_INPUT_BUFFER) 211 if (strlen(optarg) >= MAX_INPUT_BUFFER) {
200 die(STATE_UNKNOWN, _("Input buffer overflow\n")); 212 die(STATE_UNKNOWN, _("Input buffer overflow\n"));
201 game_type = optarg; 213 }
214 result.config.game_type = optarg;
202 break; 215 break;
203 case 'p': /* index of ping field */ 216 case 'p': /* index of ping field */
204 qstat_ping_field = atoi(optarg); 217 result.config.qstat_ping_field = atoi(optarg);
205 if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS) 218 if (result.config.qstat_ping_field < 0 || result.config.qstat_ping_field > QSTAT_MAX_RETURN_ARGS) {
206 return ERROR; 219 result.errorcode = ERROR;
220 return result;
221 }
207 break; 222 break;
208 case 'm': /* index on map field */ 223 case 'm': /* index on map field */
209 qstat_map_field = atoi(optarg); 224 result.config.qstat_map_field = atoi(optarg);
210 if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS) 225 if (result.config.qstat_map_field < 0 || result.config.qstat_map_field > QSTAT_MAX_RETURN_ARGS) {
211 return ERROR; 226 result.errorcode = ERROR;
227 return result;
228 }
212 break; 229 break;
213 case 'g': /* index of game field */ 230 case 'g': /* index of game field */
214 qstat_game_field = atoi(optarg); 231 result.config.qstat_game_field = atoi(optarg);
215 if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS) 232 if (result.config.qstat_game_field < 0 || result.config.qstat_game_field > QSTAT_MAX_RETURN_ARGS) {
216 return ERROR; 233 result.errorcode = ERROR;
234 return result;
235 }
217 break; 236 break;
218 case 129: /* index of player count field */ 237 case players_field_index: /* index of player count field */
219 qstat_game_players = atoi(optarg); 238 result.config.qstat_game_players = atoi(optarg);
220 if (qstat_game_players_max == 0) 239 if (result.config.qstat_game_players_max == 0) {
221 qstat_game_players_max = qstat_game_players - 1; 240 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) 241 }
223 return ERROR; 242 if (result.config.qstat_game_players < 0 || result.config.qstat_game_players > QSTAT_MAX_RETURN_ARGS) {
243 result.errorcode = ERROR;
244 return result;
245 }
224 break; 246 break;
225 case 130: /* index of max players field */ 247 case max_players_field_index: /* index of max players field */
226 qstat_game_players_max = atoi(optarg); 248 result.config.qstat_game_players_max = atoi(optarg);
227 if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) 249 if (result.config.qstat_game_players_max < 0 || result.config.qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) {
228 return ERROR; 250 result.errorcode = ERROR;
251 return result;
252 }
229 break; 253 break;
230 default: /* args not parsable */ 254 default: /* args not parsable */
231 usage5(); 255 usage5();
232 } 256 }
233 } 257 }
234 258
235 c = optind; 259 int option_counter = optind;
236 /* first option is the game type */ 260 /* first option is the game type */
237 if (!game_type && c < argc) 261 if (!result.config.game_type && option_counter < argc) {
238 game_type = strdup(argv[c++]); 262 result.config.game_type = strdup(argv[option_counter++]);
263 }
239 264
240 /* Second option is the server name */ 265 /* Second option is the server name */
241 if (!server_ip && c < argc) 266 if (!result.config.server_ip && option_counter < argc) {
242 server_ip = strdup(argv[c++]); 267 result.config.server_ip = strdup(argv[option_counter++]);
243 268 }
244 return validate_arguments();
245}
246
247int 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 269
263 return OK; 270 return result;
264} 271}
265 272
266void print_help(void) { 273void print_help(void) {