Merge branch 'master' into refactor/check_smtp

This commit is contained in:
Lorenz Kästle 2025-03-10 09:58:27 +01:00
commit 3cadfa2dbe
5 changed files with 377 additions and 281 deletions

View file

@ -47,12 +47,13 @@ SUBDIRS = picohttpparser
np_test_scripts = tests/test_check_swap.t
EXTRA_DIST = t \
tests \
$(np_test_scripts) \
check_swap.d \
check_dbi.d \
check_ssh.d \
check_dns.d \
tests \
$(np_test_scripts) \
check_swap.d \
check_game.d \
check_dbi.d \
check_ssh.d \
check_dns.d \
check_smtp.d
PLUGINHDRS = common.h

View file

@ -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)
usage_va(_("Could not parse arguments"));
check_game_config_wrapper tmp = process_arguments(argc, argv);
result = STATE_OK;
if (tmp.errorcode == ERROR) {
usage_va(_("Could not parse arguments"));
}
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,19 +130,20 @@ 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
int opt_index = 0;
check_game_config_wrapper process_arguments(int argc, char **argv) {
static struct option long_opts[] = {{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"verbose", no_argument, 0, 'v'},
@ -152,29 +154,39 @@ 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;
}
while (1) {
c = getopt_long(argc, argv, "hVvt:H:P:G:g:p:m:", long_opts, &opt_index);
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");
}
}
if (c == -1 || c == EOF)
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) {
break;
}
switch (c) {
switch (option_index) {
case 'h': /* help */
print_help();
exit(STATE_UNKNOWN);
@ -188,79 +200,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++]);
if (!result.config.server_ip && option_counter < argc) {
result.config.server_ip = strdup(argv[option_counter++]);
}
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;
return OK;
return result;
}
void print_help(void) {
@ -277,14 +285,15 @@ void print_help(void) {
printf(UT_HELP_VRSN);
printf(UT_EXTRA_OPTS);
printf(" %s\n", "-p");
printf(" %s\n", _("Optional port of which to connect"));
printf(" %s\n", "gf");
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 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);

View file

@ -0,0 +1,30 @@
#pragma once
#include "../../config.h"
#include <stddef.h>
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;
}

View file

@ -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);

View file

@ -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")