From 065ed65a870bd973c751940ef8e6f47f62d88d26 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Mar 2025 15:57:46 +0100 Subject: Fix types in perfdata functions --- plugins/utils.c | 243 ++++++++++++++++++++++++++++++++++---------------------- plugins/utils.h | 141 ++++++++++++++++---------------- 2 files changed, 220 insertions(+), 164 deletions(-) diff --git a/plugins/utils.c b/plugins/utils.c index 09649429..34335c89 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -89,41 +89,46 @@ bool is_numeric(char *number) { char tmp[1]; float x; - if (!number) + if (!number) { return false; - else if (sscanf(number, "%f%c", &x, tmp) == 1) + } else if (sscanf(number, "%f%c", &x, tmp) == 1) { return true; - else + } else { return false; + } } bool is_positive(char *number) { - if (is_numeric(number) && atof(number) > 0.0) + if (is_numeric(number) && atof(number) > 0.0) { return true; - else + } else { return false; + } } bool is_negative(char *number) { - if (is_numeric(number) && atof(number) < 0.0) + if (is_numeric(number) && atof(number) < 0.0) { return true; - else + } else { return false; + } } bool is_nonnegative(char *number) { - if (is_numeric(number) && atof(number) >= 0.0) + if (is_numeric(number) && atof(number) >= 0.0) { return true; - else + } else { return false; + } } bool is_percentage(char *number) { int x; - if (is_numeric(number) && (x = atof(number)) >= 0 && x <= 100) + if (is_numeric(number) && (x = atof(number)) >= 0 && x <= 100) { return true; - else + } else { return false; + } } bool is_percentage_expression(const char str[]) { @@ -156,36 +161,41 @@ bool is_percentage_expression(const char str[]) { bool is_integer(char *number) { long int n; - if (!number || (strspn(number, "-0123456789 ") != strlen(number))) + if (!number || (strspn(number, "-0123456789 ") != strlen(number))) { return false; + } n = strtol(number, NULL, 10); - if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) + if (errno != ERANGE && n >= INT_MIN && n <= INT_MAX) { return true; - else + } else { return false; + } } bool is_intpos(char *number) { - if (is_integer(number) && atoi(number) > 0) + if (is_integer(number) && atoi(number) > 0) { return true; - else + } else { return false; + } } bool is_intneg(char *number) { - if (is_integer(number) && atoi(number) < 0) + if (is_integer(number) && atoi(number) < 0) { return true; - else + } else { return false; + } } bool is_intnonneg(char *number) { - if (is_integer(number) && atoi(number) >= 0) + if (is_integer(number) && atoi(number) >= 0) { return true; - else + } else { return false; + } } /* @@ -247,19 +257,21 @@ bool is_uint64(char *number, uint64_t *target) { bool is_intpercent(char *number) { int i; - if (is_integer(number) && (i = atoi(number)) >= 0 && i <= 100) + if (is_integer(number) && (i = atoi(number)) >= 0 && i <= 100) { return true; - else + } else { return false; + } } bool is_option(char *str) { - if (!str) + if (!str) { return false; - else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) + } else if (strspn(str, "-") == 1 || strspn(str, "-") == 2) { return true; - else + } else { return false; + } } #ifdef NEED_GETTIMEOFDAY @@ -288,10 +300,11 @@ void strip(char *buffer) { for (x = strlen(buffer); x >= 1; x--) { i = x - 1; - if (buffer[i] == ' ' || buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t') + if (buffer[i] == ' ' || buffer[i] == '\r' || buffer[i] == '\n' || buffer[i] == '\t') { buffer[i] = '\0'; - else + } else { break; + } } return; } @@ -309,8 +322,9 @@ void strip(char *buffer) { *****************************************************************************/ char *strscpy(char *dest, const char *src) { - if (src == NULL) + if (src == NULL) { return NULL; + } xasprintf(&dest, "%s", src); @@ -369,17 +383,21 @@ char *strscpy(char *dest, const char *src) { char *strnl(char *str) { size_t len; - if (str == NULL) + if (str == NULL) { return NULL; + } str = strpbrk(str, "\r\n"); - if (str == NULL) + if (str == NULL) { return NULL; + } len = strspn(str, "\r\n"); - if (str[len] == '\0') + if (str[len] == '\0') { return NULL; + } str += len; - if (strlen(str) == 0) + if (strlen(str) == 0) { return NULL; + } return str; } @@ -402,15 +420,18 @@ char *strnl(char *str) { char *strpcpy(char *dest, const char *src, const char *str) { size_t len; - if (src) + if (src) { len = strcspn(src, str); - else + } else { return NULL; + } - if (dest == NULL || strlen(dest) < len) + if (dest == NULL || strlen(dest) < len) { dest = realloc(dest, len + 1); - if (dest == NULL) + } + if (dest == NULL) { die(STATE_UNKNOWN, _("failed realloc in strpcpy\n")); + } strncpy(dest, src, len); dest[len] = '\0'; @@ -434,10 +455,11 @@ char *strpcpy(char *dest, const char *src, const char *str) { char *strpcat(char *dest, const char *src, const char *str) { size_t len, l2; - if (dest) + if (dest) { len = strlen(dest); - else + } else { len = 0; + } if (src) { l2 = strcspn(src, str); @@ -446,8 +468,9 @@ char *strpcat(char *dest, const char *src, const char *str) { } dest = realloc(dest, len + l2 + 1); - if (dest == NULL) + if (dest == NULL) { die(STATE_UNKNOWN, _("failed malloc in strscat\n")); + } strncpy(dest + len, src, l2); dest[len + l2] = '\0'; @@ -463,8 +486,9 @@ char *strpcat(char *dest, const char *src, const char *str) { int xvasprintf(char **strp, const char *fmt, va_list ap) { int result = vasprintf(strp, fmt, ap); - if (result == -1 || *strp == NULL) + if (result == -1 || *strp == NULL) { die(STATE_UNKNOWN, _("failed malloc in xvasprintf\n")); + } return result; } @@ -483,126 +507,145 @@ int xasprintf(char **strp, const char *fmt, ...) { * ******************************************************************************/ -char *perfdata(const char *label, long int val, const char *uom, int warnp, long int warn, int critp, long int crit, int minp, - long int minv, int maxp, long int maxv) { +char *perfdata(const char *label, long int val, const char *uom, bool warnp, long int warn, bool critp, long int crit, bool minp, + long int minv, bool maxp, long int maxv) { char *data = NULL; - if (strpbrk(label, "'= ")) + if (strpbrk(label, "'= ")) { xasprintf(&data, "'%s'=%ld%s;", label, val, uom); - else + } else { xasprintf(&data, "%s=%ld%s;", label, val, uom); + } - if (warnp) + if (warnp) { xasprintf(&data, "%s%ld;", data, warn); - else + } else { xasprintf(&data, "%s;", data); + } - if (critp) + if (critp) { xasprintf(&data, "%s%ld;", data, crit); - else + } else { xasprintf(&data, "%s;", data); + } - if (minp) + if (minp) { xasprintf(&data, "%s%ld;", data, minv); - else + } else { xasprintf(&data, "%s;", data); + } - if (maxp) + if (maxp) { xasprintf(&data, "%s%ld", data, maxv); + } return data; } -char *perfdata_uint64(const char *label, uint64_t val, const char *uom, int warnp, /* Warning present */ - uint64_t warn, int critp, /* Critical present */ - uint64_t crit, int minp, /* Minimum present */ - uint64_t minv, int maxp, /* Maximum present */ +char *perfdata_uint64(const char *label, uint64_t val, const char *uom, bool warnp, /* Warning present */ + uint64_t warn, bool critp, /* Critical present */ + uint64_t crit, bool minp, /* Minimum present */ + uint64_t minv, bool maxp, /* Maximum present */ uint64_t maxv) { char *data = NULL; - if (strpbrk(label, "'= ")) + if (strpbrk(label, "'= ")) { xasprintf(&data, "'%s'=%" PRIu64 "%s;", label, val, uom); - else + } else { xasprintf(&data, "%s=%" PRIu64 "%s;", label, val, uom); + } - if (warnp) + if (warnp) { xasprintf(&data, "%s%" PRIu64 ";", data, warn); - else + } else { xasprintf(&data, "%s;", data); + } - if (critp) + if (critp) { xasprintf(&data, "%s%" PRIu64 ";", data, crit); - else + } else { xasprintf(&data, "%s;", data); + } - if (minp) + if (minp) { xasprintf(&data, "%s%" PRIu64 ";", data, minv); - else + } else { xasprintf(&data, "%s;", data); + } - if (maxp) + if (maxp) { xasprintf(&data, "%s%" PRIu64, data, maxv); + } return data; } -char *perfdata_int64(const char *label, int64_t val, const char *uom, int warnp, /* Warning present */ - int64_t warn, int critp, /* Critical present */ - int64_t crit, int minp, /* Minimum present */ - int64_t minv, int maxp, /* Maximum present */ +char *perfdata_int64(const char *label, int64_t val, const char *uom, bool warnp, /* Warning present */ + int64_t warn, bool critp, /* Critical present */ + int64_t crit, bool minp, /* Minimum present */ + int64_t minv, bool maxp, /* Maximum present */ int64_t maxv) { char *data = NULL; - if (strpbrk(label, "'= ")) + if (strpbrk(label, "'= ")) { xasprintf(&data, "'%s'=%" PRId64 "%s;", label, val, uom); - else + } else { xasprintf(&data, "%s=%" PRId64 "%s;", label, val, uom); + } - if (warnp) + if (warnp) { xasprintf(&data, "%s%" PRId64 ";", data, warn); - else + } else { xasprintf(&data, "%s;", data); + } - if (critp) + if (critp) { xasprintf(&data, "%s%" PRId64 ";", data, crit); - else + } else { xasprintf(&data, "%s;", data); + } - if (minp) + if (minp) { xasprintf(&data, "%s%" PRId64 ";", data, minv); - else + } else { xasprintf(&data, "%s;", data); + } - if (maxp) + if (maxp) { xasprintf(&data, "%s%" PRId64, data, maxv); + } return data; } -char *fperfdata(const char *label, double val, const char *uom, int warnp, double warn, int critp, double crit, int minp, double minv, - int maxp, double maxv) { +char *fperfdata(const char *label, double val, const char *uom, bool warnp, double warn, bool critp, double crit, bool minp, double minv, + bool maxp, double maxv) { char *data = NULL; - if (strpbrk(label, "'= ")) + if (strpbrk(label, "'= ")) { xasprintf(&data, "'%s'=", label); - else + } else { xasprintf(&data, "%s=", label); + } xasprintf(&data, "%s%f", data, val); xasprintf(&data, "%s%s;", data, uom); - if (warnp) + if (warnp) { xasprintf(&data, "%s%f", data, warn); + } xasprintf(&data, "%s;", data); - if (critp) + if (critp) { xasprintf(&data, "%s%f", data, crit); + } xasprintf(&data, "%s;", data); - if (minp) + if (minp) { xasprintf(&data, "%s%f", data, minv); + } if (maxp) { xasprintf(&data, "%s;", data); @@ -612,28 +655,32 @@ char *fperfdata(const char *label, double val, const char *uom, int warnp, doubl return data; } -char *sperfdata(const char *label, double val, const char *uom, char *warn, char *crit, int minp, double minv, int maxp, double maxv) { +char *sperfdata(const char *label, double val, const char *uom, char *warn, char *crit, bool minp, double minv, bool maxp, double maxv) { char *data = NULL; - if (strpbrk(label, "'= ")) + if (strpbrk(label, "'= ")) { xasprintf(&data, "'%s'=", label); - else + } else { xasprintf(&data, "%s=", label); + } xasprintf(&data, "%s%f", data, val); xasprintf(&data, "%s%s;", data, uom); - if (warn != NULL) + if (warn != NULL) { xasprintf(&data, "%s%s", data, warn); + } xasprintf(&data, "%s;", data); - if (crit != NULL) + if (crit != NULL) { xasprintf(&data, "%s%s", data, crit); + } xasprintf(&data, "%s;", data); - if (minp) + if (minp) { xasprintf(&data, "%s%f", data, minv); + } if (maxp) { xasprintf(&data, "%s;", data); @@ -643,28 +690,32 @@ char *sperfdata(const char *label, double val, const char *uom, char *warn, char return data; } -char *sperfdata_int(const char *label, int val, const char *uom, char *warn, char *crit, int minp, int minv, int maxp, int maxv) { +char *sperfdata_int(const char *label, int val, const char *uom, char *warn, char *crit, bool minp, int minv, bool maxp, int maxv) { char *data = NULL; - if (strpbrk(label, "'= ")) + if (strpbrk(label, "'= ")) { xasprintf(&data, "'%s'=", label); - else + } else { xasprintf(&data, "%s=", label); + } xasprintf(&data, "%s%d", data, val); xasprintf(&data, "%s%s;", data, uom); - if (warn != NULL) + if (warn != NULL) { xasprintf(&data, "%s%s", data, warn); + } xasprintf(&data, "%s;", data); - if (crit != NULL) + if (crit != NULL) { xasprintf(&data, "%s%s", data, crit); + } xasprintf(&data, "%s;", data); - if (minp) + if (minp) { xasprintf(&data, "%s%d", data, minv); + } if (maxp) { xasprintf(&data, "%s;", data); diff --git a/plugins/utils.h b/plugins/utils.h index 029ae5a6..92a6c115 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -21,43 +21,43 @@ suite of plugins. */ #ifdef NP_EXTRA_OPTS /* Include extra-opts functions if compiled in */ -#include "extra_opts.h" +# include "extra_opts.h" #else /* else, fake np_extra_opts */ -#define np_extra_opts(acptr,av,pr) av +# define np_extra_opts(acptr, av, pr) av #endif /* Standardize version information, termination */ -void support (void); -void print_revision (const char *, const char *); +void support(void); +void print_revision(const char *, const char *); extern time_t start_time, end_time; /* Test input types */ -bool is_integer (char *); -bool is_intpos (char *); -bool is_intneg (char *); -bool is_intnonneg (char *); -bool is_intpercent (char *); +bool is_integer(char *); +bool is_intpos(char *); +bool is_intneg(char *); +bool is_intnonneg(char *); +bool is_intpercent(char *); bool is_uint64(char *number, uint64_t *target); bool is_int64(char *number, int64_t *target); -bool is_numeric (char *); -bool is_positive (char *); -bool is_negative (char *); -bool is_nonnegative (char *); -bool is_percentage (char *); -bool is_percentage_expression (const char[]); +bool is_numeric(char *); +bool is_positive(char *); +bool is_negative(char *); +bool is_nonnegative(char *); +bool is_percentage(char *); +bool is_percentage_expression(const char[]); -bool is_option (char *); +bool is_option(char *); /* Generalized timer that will do milliseconds if available */ #ifndef HAVE_STRUCT_TIMEVAL struct timeval { - long tv_sec; /* seconds */ - long tv_usec; /* microseconds */ + long tv_sec; /* seconds */ + long tv_usec; /* microseconds */ }; #endif @@ -65,137 +65,142 @@ struct timeval { int gettimeofday(struct timeval *, struct timezone *); #endif -double delta_time (struct timeval tv); -long deltime (struct timeval tv); +double delta_time(struct timeval tv); +long deltime(struct timeval tv); /* Handle strings safely */ -void strip (char *); -char *strscpy (char *, const char *); -char *strnl (char *); -char *strpcpy (char *, const char *, const char *); -char *strpcat (char *, const char *, const char *); -int xvasprintf (char **strp, const char *fmt, va_list ap); -int xasprintf (char **strp, const char *fmt, ...); +void strip(char *); +char *strscpy(char *, const char *); +char *strnl(char *); +char *strpcpy(char *, const char *, const char *); +char *strpcat(char *, const char *, const char *); +int xvasprintf(char **strp, const char *fmt, va_list ap); +int xasprintf(char **strp, const char *fmt, ...); -void usage (const char *) __attribute__((noreturn)); +void usage(const char *) __attribute__((noreturn)); void usage2(const char *, const char *) __attribute__((noreturn)); void usage3(const char *, int) __attribute__((noreturn)); void usage4(const char *) __attribute__((noreturn)); void usage5(void) __attribute__((noreturn)); void usage_va(const char *fmt, ...) __attribute__((noreturn)); -#define max(a,b) (((a)>(b))?(a):(b)) -#define min(a,b) (((a)<(b))?(a):(b)) +#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define min(a, b) (((a) < (b)) ? (a) : (b)) -char *perfdata (const char *, long int, const char *, int, long int, - int, long int, int, long int, int, long int); +char *perfdata(const char *, long int, const char *, bool, long int, bool, long int, bool, long int, bool, long int); -char *perfdata_uint64 (const char *, uint64_t , const char *, int, uint64_t, - int, uint64_t, int, uint64_t, int, uint64_t); +char *perfdata_uint64(const char *, uint64_t, const char *, bool, uint64_t, bool, uint64_t, bool, uint64_t, bool, uint64_t); -char *perfdata_int64 (const char *, int64_t, const char *, int, int64_t, - int, int64_t, int, int64_t, int, int64_t); +char *perfdata_int64(const char *, int64_t, const char *, bool, int64_t, bool, int64_t, bool, int64_t, bool, int64_t); -char *fperfdata (const char *, double, const char *, int, double, - int, double, int, double, int, double); +char *fperfdata(const char *, double, const char *, bool, double, bool, double, bool, double, bool, double); -char *sperfdata (const char *, double, const char *, char *, char *, - int, double, int, double); +char *sperfdata(const char *, double, const char *, char *, char *, bool, double, bool, double); -char *sperfdata_int (const char *, int, const char *, char *, char *, - int, int, int, int); +char *sperfdata_int(const char *, int, const char *, char *, char *, bool, int, bool, int); -/* The idea here is that, although not every plugin will use all of these, - most will or should. Therefore, for consistency, these very common +/* The idea here is that, although not every plugin will use all of these, + most will or should. Therefore, for consistency, these very common options should have only these meanings throughout the overall suite */ -#define STD_LONG_OPTS \ -{"version",no_argument,0,'V'},\ -{"verbose",no_argument,0,'v'},\ -{"help",no_argument,0,'h'},\ -{"timeout",required_argument,0,'t'},\ -{"critical",required_argument,0,'c'},\ -{"warning",required_argument,0,'w'},\ -{"hostname",required_argument,0,'H'} +#define STD_LONG_OPTS \ + {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, \ + {"timeout", required_argument, 0, 't'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, \ + {"hostname", required_argument, 0, 'H'} -#define COPYRIGHT "Copyright (c) %s Monitoring Plugins Development Team\n\ +#define COPYRIGHT \ + "Copyright (c) %s Monitoring Plugins Development Team\n\ \t<%s>\n\n" -#define UT_HLP_VRS _("\ +#define UT_HLP_VRS \ + _("\ %s (-h | --help) for detailed help\n\ %s (-V | --version) for version information\n") -#define UT_HELP_VRSN _("\ +#define UT_HELP_VRSN \ + _("\ \nOptions:\n\ -h, --help\n\ Print detailed help screen\n\ -V, --version\n\ Print version information\n") -#define UT_HOST_PORT _("\ +#define UT_HOST_PORT \ + _("\ -H, --hostname=ADDRESS\n\ Host name, IP Address, or unix socket (must be an absolute path)\n\ -%c, --port=INTEGER\n\ Port number (default: %s)\n") -#define UT_IPv46 _("\ +#define UT_IPv46 \ + _("\ -4, --use-ipv4\n\ Use IPv4 connection\n\ -6, --use-ipv6\n\ Use IPv6 connection\n") -#define UT_VERBOSE _("\ +#define UT_VERBOSE \ + _("\ -v, --verbose\n\ Show details for command-line debugging (output may be truncated by\n\ the monitoring system)\n") -#define UT_WARN_CRIT _("\ +#define UT_WARN_CRIT \ + _("\ -w, --warning=DOUBLE\n\ Response time to result in warning status (seconds)\n\ -c, --critical=DOUBLE\n\ Response time to result in critical status (seconds)\n") -#define UT_WARN_CRIT_RANGE _("\ +#define UT_WARN_CRIT_RANGE \ + _("\ -w, --warning=RANGE\n\ Warning range (format: start:end). Alert if outside this range\n\ -c, --critical=RANGE\n\ Critical range\n") -#define UT_CONN_TIMEOUT _("\ +#define UT_CONN_TIMEOUT \ + _("\ -t, --timeout=INTEGER\n\ Seconds before connection times out (default: %d)\n") -#define UT_PLUG_TIMEOUT _("\ +#define UT_PLUG_TIMEOUT \ + _("\ -t, --timeout=INTEGER\n\ Seconds before plugin times out (default: %d)\n") #ifdef NP_EXTRA_OPTS -#define UT_EXTRA_OPTS _("\ +# define UT_EXTRA_OPTS \ + _("\ --extra-opts=[section][@file]\n\ Read options from an ini file. See\n\ https://www.monitoring-plugins.org/doc/extra-opts.html\n\ for usage and examples.\n") #else -#define UT_EXTRA_OPTS " \b" +# define UT_EXTRA_OPTS " \b" #endif -#define UT_THRESHOLDS_NOTES _("\ +#define UT_THRESHOLDS_NOTES \ + _("\ See:\n\ https://www.monitoring-plugins.org/doc/guidelines.html#THRESHOLDFORMAT\n\ for THRESHOLD format and examples.\n") -#define UT_SUPPORT _("\n\ +#define UT_SUPPORT \ + _("\n\ Send email to help@monitoring-plugins.org if you have questions regarding\n\ use of this software. To submit patches or suggest improvements, send email\n\ to devel@monitoring-plugins.org\n\n") -#define UT_NOWARRANTY _("\n\ +#define UT_NOWARRANTY \ + _("\n\ The Monitoring Plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\n\ copies of the plugins under the terms of the GNU General Public License.\n\ For more information about these matters, see the file named COPYING.\n") -#define UT_OUTPUT_FORMAT _("\ +#define UT_OUTPUT_FORMAT \ + _("\ --output-format=OUTPUT_FORMAT\n\ Select output format. Valid values: \"multi-line\", \"mp-test-json\"\n") -- cgit v1.2.3-74-g34f1 From 9f8a485744ba8de13c01cc48a4db0d8d73189c97 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Mar 2025 15:58:30 +0100 Subject: Refactor check_game --- plugins/check_game.c | 211 ++++++++++++++++++++++-------------------- plugins/check_game.d/config.h | 30 ++++++ 2 files changed, 139 insertions(+), 102 deletions(-) create mode 100644 plugins/check_game.d/config.h 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"; #include "common.h" #include "utils.h" #include "runcmd.h" +#include "check_game.d/config.h" +#include "../lib/monitoringplug.h" -static int process_arguments(int /*argc*/, char ** /*argv*/); -static int validate_arguments(void); +typedef struct { + int errorcode; + check_game_config config; +} check_game_config_wrapper; + +static check_game_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); static void print_help(void); void print_usage(void); @@ -49,26 +55,9 @@ void print_usage(void); #define QSTAT_HOST_TIMEOUT "TIMEOUT" #define QSTAT_MAX_RETURN_ARGS 12 -static char *server_ip; -static char *game_type; -static int port = 0; - static bool verbose = false; -static int qstat_game_players_max = -1; -static int qstat_game_players = -1; -static int qstat_game_field = -1; -static int qstat_map_field = -1; -static int qstat_ping_field = -1; - int main(int argc, char **argv) { - char *command_line; - int result = STATE_UNKNOWN; - char *p; - char *ret[QSTAT_MAX_RETURN_ARGS]; - size_t i = 0; - output chld_out; - setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); @@ -76,22 +65,31 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) + check_game_config_wrapper tmp = process_arguments(argc, argv); + + if (tmp.errorcode == ERROR) { usage_va(_("Could not parse arguments")); + } - result = STATE_OK; + check_game_config config = tmp.config; + + mp_state_enum result = STATE_OK; /* create the command line to execute */ - xasprintf(&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, game_type, server_ip); + char *command_line = NULL; + xasprintf(&command_line, "%s -raw %s -%s %s", PATH_TO_QSTAT, QSTAT_DATA_DELIMITER, config.game_type, config.server_ip); - if (port) - xasprintf(&command_line, "%s:%-d", command_line, port); + if (config.port) { + xasprintf(&command_line, "%s:%-d", command_line, config.port); + } - if (verbose) + if (verbose) { printf("%s\n", command_line); + } /* run the command. historically, this plugin ignores output on stderr, * as well as return status of the qstat program */ + output chld_out = {}; (void)np_runcmd(command_line, &chld_out, NULL, 0); /* sanity check */ @@ -104,19 +102,22 @@ int main(int argc, char **argv) { In the end, I figured I'd simply let an error occur & then trap it */ - if (!strncmp(chld_out.line[0], "unknown option", 14)) { + if (!strncmp(chld_out.line[0], "unknown option", strlen("unknown option"))) { printf(_("CRITICAL - Host type parameter incorrect!\n")); result = STATE_CRITICAL; - return result; + exit(result); } - p = (char *)strtok(chld_out.line[0], QSTAT_DATA_DELIMITER); - while (p != NULL) { - ret[i] = p; - p = (char *)strtok(NULL, QSTAT_DATA_DELIMITER); + char *ret[QSTAT_MAX_RETURN_ARGS]; + size_t i = 0; + char *sequence = strtok(chld_out.line[0], QSTAT_DATA_DELIMITER); + while (sequence != NULL) { + ret[i] = sequence; + sequence = strtok(NULL, QSTAT_DATA_DELIMITER); i++; - if (i >= QSTAT_MAX_RETURN_ARGS) + if (i >= QSTAT_MAX_RETURN_ARGS) { break; + } } if (strstr(ret[2], QSTAT_HOST_ERROR)) { @@ -129,18 +130,19 @@ int main(int argc, char **argv) { printf(_("CRITICAL - Game server timeout\n")); result = STATE_CRITICAL; } else { - 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], - ret[qstat_map_field], ret[qstat_ping_field], - perfdata("players", atol(ret[qstat_game_players]), "", false, 0, false, 0, true, 0, true, atol(ret[qstat_game_players_max])), - fperfdata("ping", strtod(ret[qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0)); + 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], + ret[config.qstat_map_field], ret[config.qstat_ping_field], + perfdata("players", atol(ret[config.qstat_game_players]), "", false, 0, false, 0, true, 0, true, atol(ret[config.qstat_game_players_max])), + fperfdata("ping", strtod(ret[config.qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0)); } - return result; + exit(result); } -int process_arguments(int argc, char **argv) { - int c; +#define players_field_index 129 +#define max_players_field_index 130 +check_game_config_wrapper process_arguments(int argc, char **argv) { int opt_index = 0; static struct option long_opts[] = {{"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, @@ -152,29 +154,38 @@ int process_arguments(int argc, char **argv) { {"map-field", required_argument, 0, 'm'}, {"ping-field", required_argument, 0, 'p'}, {"game-field", required_argument, 0, 'g'}, - {"players-field", required_argument, 0, 129}, - {"max-players-field", required_argument, 0, 130}, + {"players-field", required_argument, 0, players_field_index}, + {"max-players-field", required_argument, 0, max_players_field_index}, {0, 0, 0, 0}}; - if (argc < 2) - return ERROR; + check_game_config_wrapper result = { + .config = check_game_config_init(), + .errorcode = OK, + }; - for (c = 1; c < argc; c++) { - if (strcmp("-mf", argv[c]) == 0) - strcpy(argv[c], "-m"); - else if (strcmp("-pf", argv[c]) == 0) - strcpy(argv[c], "-p"); - else if (strcmp("-gf", argv[c]) == 0) - strcpy(argv[c], "-g"); + if (argc < 2) { + result.errorcode = ERROR; + return result; + } + + for (int option_counter = 1; option_counter < argc; option_counter++) { + if (strcmp("-mf", argv[option_counter]) == 0) { + strcpy(argv[option_counter], "-m"); + } else if (strcmp("-pf", argv[option_counter]) == 0) { + strcpy(argv[option_counter], "-p"); + } else if (strcmp("-gf", argv[option_counter]) == 0) { + strcpy(argv[option_counter], "-g"); + } } while (1) { - c = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); + int option_index = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); - if (c == -1 || c == EOF) + if (option_index == -1 || option_index == EOF) { break; + } - switch (c) { + switch (option_index) { case 'h': /* help */ print_help(); exit(STATE_UNKNOWN); @@ -188,79 +199,75 @@ int process_arguments(int argc, char **argv) { timeout_interval = atoi(optarg); break; case 'H': /* hostname */ - if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) + if (strlen(optarg) >= MAX_HOST_ADDRESS_LENGTH) { die(STATE_UNKNOWN, _("Input buffer overflow\n")); - server_ip = optarg; + } + result.config.server_ip = optarg; break; case 'P': /* port */ - port = atoi(optarg); + result.config.port = atoi(optarg); break; case 'G': /* hostname */ - if (strlen(optarg) >= MAX_INPUT_BUFFER) + if (strlen(optarg) >= MAX_INPUT_BUFFER) { die(STATE_UNKNOWN, _("Input buffer overflow\n")); - game_type = optarg; + } + result.config.game_type = optarg; break; case 'p': /* index of ping field */ - qstat_ping_field = atoi(optarg); - if (qstat_ping_field < 0 || qstat_ping_field > QSTAT_MAX_RETURN_ARGS) - return ERROR; + result.config.qstat_ping_field = atoi(optarg); + if (result.config.qstat_ping_field < 0 || result.config.qstat_ping_field > QSTAT_MAX_RETURN_ARGS) { + result.errorcode = ERROR; + return result; + } break; case 'm': /* index on map field */ - qstat_map_field = atoi(optarg); - if (qstat_map_field < 0 || qstat_map_field > QSTAT_MAX_RETURN_ARGS) - return ERROR; + result.config.qstat_map_field = atoi(optarg); + if (result.config.qstat_map_field < 0 || result.config.qstat_map_field > QSTAT_MAX_RETURN_ARGS) { + result.errorcode = ERROR; + return result; + } break; case 'g': /* index of game field */ - qstat_game_field = atoi(optarg); - if (qstat_game_field < 0 || qstat_game_field > QSTAT_MAX_RETURN_ARGS) - return ERROR; + result.config.qstat_game_field = atoi(optarg); + if (result.config.qstat_game_field < 0 || result.config.qstat_game_field > QSTAT_MAX_RETURN_ARGS) { + result.errorcode = ERROR; + return result; + } break; - case 129: /* index of player count field */ - qstat_game_players = atoi(optarg); - if (qstat_game_players_max == 0) - qstat_game_players_max = qstat_game_players - 1; - if (qstat_game_players < 0 || qstat_game_players > QSTAT_MAX_RETURN_ARGS) - return ERROR; + case players_field_index: /* index of player count field */ + result.config.qstat_game_players = atoi(optarg); + if (result.config.qstat_game_players_max == 0) { + result.config.qstat_game_players_max = result.config.qstat_game_players - 1; + } + if (result.config.qstat_game_players < 0 || result.config.qstat_game_players > QSTAT_MAX_RETURN_ARGS) { + result.errorcode = ERROR; + return result; + } break; - case 130: /* index of max players field */ - qstat_game_players_max = atoi(optarg); - if (qstat_game_players_max < 0 || qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) - return ERROR; + case max_players_field_index: /* index of max players field */ + result.config.qstat_game_players_max = atoi(optarg); + if (result.config.qstat_game_players_max < 0 || result.config.qstat_game_players_max > QSTAT_MAX_RETURN_ARGS) { + result.errorcode = ERROR; + return result; + } break; default: /* args not parsable */ usage5(); } } - c = optind; + int option_counter = optind; /* first option is the game type */ - if (!game_type && c < argc) - game_type = strdup(argv[c++]); + if (!result.config.game_type && option_counter < argc) { + result.config.game_type = strdup(argv[option_counter++]); + } /* Second option is the server name */ - if (!server_ip && c < argc) - server_ip = strdup(argv[c++]); - - return validate_arguments(); -} - -int validate_arguments(void) { - if (qstat_game_players_max < 0) - qstat_game_players_max = 4; - - if (qstat_game_players < 0) - qstat_game_players = 5; - - if (qstat_game_field < 0) - qstat_game_field = 2; - - if (qstat_map_field < 0) - qstat_map_field = 3; - - if (qstat_ping_field < 0) - qstat_ping_field = 5; + if (!result.config.server_ip && option_counter < argc) { + result.config.server_ip = strdup(argv[option_counter++]); + } - return OK; + return result; } void print_help(void) { diff --git a/plugins/check_game.d/config.h b/plugins/check_game.d/config.h new file mode 100644 index 00000000..c95a1ced --- /dev/null +++ b/plugins/check_game.d/config.h @@ -0,0 +1,30 @@ +#pragma once +#include "../../config.h" +#include + +typedef struct { + char *server_ip; + char *game_type; + int port; + + int qstat_game_players_max; + int qstat_game_players; + int qstat_game_field; + int qstat_map_field; + int qstat_ping_field; +} check_game_config; + +check_game_config check_game_config_init() { + check_game_config tmp = { + .server_ip = NULL, + .game_type = NULL, + .port = 0, + + .qstat_game_players_max = 4, + .qstat_game_players = 5, + .qstat_map_field = 3, + .qstat_game_field = 2, + .qstat_ping_field = 5, + }; + return tmp; +} -- cgit v1.2.3-74-g34f1 From 4322bbf23b8f65cf070079aa024e04105e4ccf98 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Mar 2025 16:08:46 +0100 Subject: Pack new check_game files into tarball --- plugins/Makefile.am | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/Makefile.am b/plugins/Makefile.am index d43c1971..fb689f87 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -46,7 +46,11 @@ SUBDIRS = picohttpparser np_test_scripts = tests/test_check_swap.t -EXTRA_DIST = t tests $(np_test_scripts) check_swap.d +EXTRA_DIST = t \ + tests \ + $(np_test_scripts) \ + check_swap.d \ + check_game.d PLUGINHDRS = common.h -- cgit v1.2.3-74-g34f1 From 61d3dc00cbba6ab10cb8a181294c120741a34882 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Mar 2025 16:10:01 +0100 Subject: clang-format + add host option in usage --- plugins/check_game.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/plugins/check_game.c b/plugins/check_game.c index 58014c3e..d078e4b5 100644 --- a/plugins/check_game.c +++ b/plugins/check_game.c @@ -130,16 +130,17 @@ int main(int argc, char **argv) { printf(_("CRITICAL - Game server timeout\n")); result = STATE_CRITICAL; } else { - 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], - ret[config.qstat_map_field], ret[config.qstat_ping_field], - perfdata("players", atol(ret[config.qstat_game_players]), "", false, 0, false, 0, true, 0, true, atol(ret[config.qstat_game_players_max])), + 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], ret[config.qstat_map_field], ret[config.qstat_ping_field], + perfdata("players", atol(ret[config.qstat_game_players]), "", false, 0, false, 0, true, 0, true, + atol(ret[config.qstat_game_players_max])), fperfdata("ping", strtod(ret[config.qstat_ping_field], NULL), "", false, 0, false, 0, true, 0, false, 0)); } exit(result); } -#define players_field_index 129 +#define players_field_index 129 #define max_players_field_index 130 check_game_config_wrapper process_arguments(int argc, char **argv) { @@ -284,7 +285,8 @@ void print_help(void) { printf(UT_HELP_VRSN); printf(UT_EXTRA_OPTS); - + printf(" -H, --hostname=ADDRESS\n" + " Host name, IP Address, or unix socket (must be an absolute path)\n"); printf(" %s\n", "-p"); printf(" %s\n", _("Optional port of which to connect")); printf(" %s\n", "gf"); -- cgit v1.2.3-74-g34f1 From 6928d9e9ad0eb279ab513e95e802daa43d8409dc Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Mar 2025 19:23:46 +0100 Subject: More local variables, less implicit boolean --- plugins/check_game.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/check_game.c b/plugins/check_game.c index d078e4b5..fc1afb42 100644 --- a/plugins/check_game.c +++ b/plugins/check_game.c @@ -144,7 +144,6 @@ int main(int argc, char **argv) { #define max_players_field_index 130 check_game_config_wrapper process_arguments(int argc, char **argv) { - int opt_index = 0; static struct option long_opts[] = {{"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, @@ -179,7 +178,8 @@ check_game_config_wrapper process_arguments(int argc, char **argv) { } } - while (1) { + int opt_index = 0; + while (true) { int option_index = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index); if (option_index == -1 || option_index == EOF) { -- cgit v1.2.3-74-g34f1 From e3d2fccade397bbd6b318495192b4ca346becef0 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Mar 2025 19:25:14 +0100 Subject: check_game: Correct usage to only display the modern option variant --- plugins/check_game.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/check_game.c b/plugins/check_game.c index fc1afb42..c0193b03 100644 --- a/plugins/check_game.c +++ b/plugins/check_game.c @@ -287,13 +287,13 @@ void print_help(void) { printf(UT_EXTRA_OPTS); printf(" -H, --hostname=ADDRESS\n" " Host name, IP Address, or unix socket (must be an absolute path)\n"); - printf(" %s\n", "-p"); - printf(" %s\n", _("Optional port of which to connect")); - printf(" %s\n", "gf"); + printf(" %s\n", "-P"); + printf(" %s\n", _("Optional port to connect to")); + printf(" %s\n", "-g"); printf(" %s\n", _("Field number in raw qstat output that contains game name")); - printf(" %s\n", "-mf"); + printf(" %s\n", "-m"); printf(" %s\n", _("Field number in raw qstat output that contains map name")); - printf(" %s\n", "-pf"); + printf(" %s\n", "-p"); printf(" %s\n", _("Field number in raw qstat output that contains ping time")); printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); -- cgit v1.2.3-74-g34f1