mirror of
https://github.com/monitoring-plugins/monitoring-plugins.git
synced 2026-05-28 04:35:40 -04:00
Merge pull request #2094 from RincewindsHat/refactor/check_nt
Refactor/check nt
This commit is contained in:
commit
494cd44090
3 changed files with 311 additions and 268 deletions
|
|
@ -56,6 +56,7 @@ EXTRA_DIST = t \
|
|||
check_nagios.d \
|
||||
check_dbi.d \
|
||||
check_ssh.d \
|
||||
check_nt.d \
|
||||
check_dns.d \
|
||||
check_mrtgtraf.d \
|
||||
check_mysql_query.d \
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
* This plugin collects data from the NSClient service running on a
|
||||
* Windows NT/2000/XP/2003 server.
|
||||
* This plugin requires NSClient software to run on NT
|
||||
* (http://nsclient.ready2run.nl/)
|
||||
* (https://nsclient.org/)
|
||||
*
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
|
|
@ -39,82 +39,28 @@ const char *email = "devel@monitoring-plugins.org";
|
|||
#include "common.h"
|
||||
#include "netutils.h"
|
||||
#include "utils.h"
|
||||
|
||||
enum checkvars {
|
||||
CHECK_NONE,
|
||||
CHECK_CLIENTVERSION,
|
||||
CHECK_CPULOAD,
|
||||
CHECK_UPTIME,
|
||||
CHECK_USEDDISKSPACE,
|
||||
CHECK_SERVICESTATE,
|
||||
CHECK_PROCSTATE,
|
||||
CHECK_MEMUSE,
|
||||
CHECK_COUNTER,
|
||||
CHECK_FILEAGE,
|
||||
CHECK_INSTANCES
|
||||
};
|
||||
#include "check_nt.d/config.h"
|
||||
|
||||
enum {
|
||||
MAX_VALUE_LIST = 30,
|
||||
PORT = 1248
|
||||
};
|
||||
|
||||
static char *server_address = NULL;
|
||||
static int server_port = PORT;
|
||||
static char *value_list = NULL;
|
||||
static char *req_password = NULL;
|
||||
static unsigned long lvalue_list[MAX_VALUE_LIST];
|
||||
static unsigned long warning_value = 0L;
|
||||
static unsigned long critical_value = 0L;
|
||||
static bool check_warning_value = false;
|
||||
static bool check_critical_value = false;
|
||||
static enum checkvars vars_to_check = CHECK_NONE;
|
||||
static bool show_all = false;
|
||||
|
||||
static char recv_buffer[MAX_INPUT_BUFFER];
|
||||
|
||||
static void fetch_data(const char *address, int port, const char *sendb);
|
||||
static int process_arguments(int /*argc*/, char ** /*argv*/);
|
||||
|
||||
typedef struct {
|
||||
int errorcode;
|
||||
check_nt_config config;
|
||||
} check_nt_config_wrapper;
|
||||
static check_nt_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
|
||||
|
||||
static void preparelist(char *string);
|
||||
static bool strtoularray(unsigned long *array, char *string, const char *delim);
|
||||
static void print_help(void);
|
||||
void print_usage(void);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
/* should be int result = STATE_UNKNOWN; */
|
||||
|
||||
int return_code = STATE_UNKNOWN;
|
||||
char *send_buffer = NULL;
|
||||
char *output_message = NULL;
|
||||
char *perfdata = NULL;
|
||||
char *temp_string = NULL;
|
||||
char *temp_string_perf = NULL;
|
||||
char *description = NULL, *counter_unit = NULL;
|
||||
char *minval = NULL, *maxval = NULL, *errcvt = NULL;
|
||||
char *fds = NULL, *tds = NULL;
|
||||
char *numstr;
|
||||
|
||||
double total_disk_space = 0;
|
||||
double free_disk_space = 0;
|
||||
double percent_used_space = 0;
|
||||
double warning_used_space = 0;
|
||||
double critical_used_space = 0;
|
||||
double mem_commitLimit = 0;
|
||||
double mem_commitByte = 0;
|
||||
double fminval = 0, fmaxval = 0;
|
||||
unsigned long utilization;
|
||||
unsigned long uptime;
|
||||
unsigned long age_in_minutes;
|
||||
double counter_value = 0.0;
|
||||
int offset = 0;
|
||||
int updays = 0;
|
||||
int uphours = 0;
|
||||
int upminutes = 0;
|
||||
|
||||
bool isPercent = false;
|
||||
bool allRight = false;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
bindtextdomain(PACKAGE, LOCALEDIR);
|
||||
textdomain(PACKAGE);
|
||||
|
|
@ -122,8 +68,12 @@ 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_nt_config_wrapper tmp_config = process_arguments(argc, argv);
|
||||
if (tmp_config.errorcode == ERROR) {
|
||||
usage4(_("Could not parse arguments"));
|
||||
}
|
||||
|
||||
const check_nt_config config = tmp_config.config;
|
||||
|
||||
/* initialize alarm signal handling */
|
||||
signal(SIGALRM, socket_timeout_alarm_handler);
|
||||
|
|
@ -131,49 +81,57 @@ int main(int argc, char **argv) {
|
|||
/* set socket timeout */
|
||||
alarm(socket_timeout);
|
||||
|
||||
switch (vars_to_check) {
|
||||
|
||||
int return_code = STATE_UNKNOWN;
|
||||
char *send_buffer = NULL;
|
||||
char *output_message = NULL;
|
||||
char *perfdata = NULL;
|
||||
char *temp_string = NULL;
|
||||
char *temp_string_perf = NULL;
|
||||
char *description = NULL;
|
||||
char *counter_unit = NULL;
|
||||
char *errcvt = NULL;
|
||||
unsigned long lvalue_list[MAX_VALUE_LIST];
|
||||
switch (config.vars_to_check) {
|
||||
case CHECK_CLIENTVERSION:
|
||||
|
||||
xasprintf(&send_buffer, "%s&1", req_password);
|
||||
fetch_data(server_address, server_port, send_buffer);
|
||||
if (value_list != NULL && strcmp(recv_buffer, value_list) != 0) {
|
||||
xasprintf(&output_message, _("Wrong client version - running: %s, required: %s"), recv_buffer, value_list);
|
||||
xasprintf(&send_buffer, "%s&1", config.req_password);
|
||||
fetch_data(config.server_address, config.server_port, send_buffer);
|
||||
if (config.value_list != NULL && strcmp(recv_buffer, config.value_list) != 0) {
|
||||
xasprintf(&output_message, _("Wrong client version - running: %s, required: %s"), recv_buffer, config.value_list);
|
||||
return_code = STATE_WARNING;
|
||||
} else {
|
||||
xasprintf(&output_message, "%s", recv_buffer);
|
||||
return_code = STATE_OK;
|
||||
}
|
||||
break;
|
||||
|
||||
case CHECK_CPULOAD:
|
||||
|
||||
if (value_list == NULL)
|
||||
if (config.value_list == NULL) {
|
||||
output_message = strdup(_("missing -l parameters"));
|
||||
else if (!strtoularray(lvalue_list, value_list, ","))
|
||||
} else if (!strtoularray(lvalue_list, config.value_list, ",")) {
|
||||
output_message = strdup(_("wrong -l parameter."));
|
||||
else {
|
||||
} else {
|
||||
/* -l parameters is present with only integers */
|
||||
return_code = STATE_OK;
|
||||
temp_string = strdup(_("CPU Load"));
|
||||
temp_string_perf = strdup(" ");
|
||||
|
||||
/* loop until one of the parameters is wrong or not present */
|
||||
int offset = 0;
|
||||
while (lvalue_list[0 + offset] > (unsigned long)0 && lvalue_list[0 + offset] <= (unsigned long)17280 &&
|
||||
lvalue_list[1 + offset] > (unsigned long)0 && lvalue_list[1 + offset] <= (unsigned long)100 &&
|
||||
lvalue_list[2 + offset] > (unsigned long)0 && lvalue_list[2 + offset] <= (unsigned long)100) {
|
||||
|
||||
/* Send request and retrieve data */
|
||||
xasprintf(&send_buffer, "%s&2&%lu", req_password, lvalue_list[0 + offset]);
|
||||
fetch_data(server_address, server_port, send_buffer);
|
||||
xasprintf(&send_buffer, "%s&2&%lu", config.req_password, lvalue_list[0 + offset]);
|
||||
fetch_data(config.server_address, config.server_port, send_buffer);
|
||||
|
||||
utilization = strtoul(recv_buffer, NULL, 10);
|
||||
unsigned long utilization = strtoul(recv_buffer, NULL, 10);
|
||||
|
||||
/* Check if any of the request is in a warning or critical state */
|
||||
if (utilization >= lvalue_list[2 + offset])
|
||||
if (utilization >= lvalue_list[2 + offset]) {
|
||||
return_code = STATE_CRITICAL;
|
||||
else if (utilization >= lvalue_list[1 + offset] && return_code < STATE_WARNING)
|
||||
} else if (utilization >= lvalue_list[1 + offset] && return_code < STATE_WARNING) {
|
||||
return_code = STATE_WARNING;
|
||||
}
|
||||
|
||||
xasprintf(&output_message, _(" %lu%% (%lu min average)"), utilization, lvalue_list[0 + offset]);
|
||||
xasprintf(&temp_string, "%s%s", temp_string, output_message);
|
||||
|
|
@ -186,82 +144,87 @@ int main(int argc, char **argv) {
|
|||
if (strlen(temp_string) > 10) { /* we had at least one loop */
|
||||
output_message = strdup(temp_string);
|
||||
perfdata = temp_string_perf;
|
||||
} else
|
||||
} else {
|
||||
output_message = strdup(_("not enough values for -l parameters"));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case CHECK_UPTIME:
|
||||
|
||||
if (value_list == NULL) {
|
||||
value_list = "minutes";
|
||||
case CHECK_UPTIME: {
|
||||
char *tmp_value_list = config.value_list;
|
||||
if (config.value_list == NULL) {
|
||||
tmp_value_list = "minutes";
|
||||
}
|
||||
if (strncmp(value_list, "seconds", strlen("seconds") + 1) && strncmp(value_list, "minutes", strlen("minutes") + 1) &&
|
||||
strncmp(value_list, "hours", strlen("hours") + 1) && strncmp(value_list, "days", strlen("days") + 1)) {
|
||||
if (strncmp(tmp_value_list, "seconds", strlen("seconds") + 1) && strncmp(tmp_value_list, "minutes", strlen("minutes") + 1) &&
|
||||
strncmp(config.value_list, "hours", strlen("hours") + 1) && strncmp(tmp_value_list, "days", strlen("days") + 1)) {
|
||||
|
||||
output_message = strdup(_("wrong -l argument"));
|
||||
} else {
|
||||
xasprintf(&send_buffer, "%s&3", req_password);
|
||||
fetch_data(server_address, server_port, send_buffer);
|
||||
uptime = strtoul(recv_buffer, NULL, 10);
|
||||
updays = uptime / 86400;
|
||||
uphours = (uptime % 86400) / 3600;
|
||||
upminutes = ((uptime % 86400) % 3600) / 60;
|
||||
xasprintf(&send_buffer, "%s&3", config.req_password);
|
||||
fetch_data(config.server_address, config.server_port, send_buffer);
|
||||
unsigned long uptime = strtoul(recv_buffer, NULL, 10);
|
||||
int updays = uptime / 86400;
|
||||
int uphours = (uptime % 86400) / 3600;
|
||||
int upminutes = ((uptime % 86400) % 3600) / 60;
|
||||
|
||||
if (!strncmp(value_list, "minutes", strlen("minutes")))
|
||||
if (!strncmp(tmp_value_list, "minutes", strlen("minutes"))) {
|
||||
uptime = uptime / 60;
|
||||
else if (!strncmp(value_list, "hours", strlen("hours")))
|
||||
} else if (!strncmp(tmp_value_list, "hours", strlen("hours"))) {
|
||||
uptime = uptime / 3600;
|
||||
else if (!strncmp(value_list, "days", strlen("days")))
|
||||
} else if (!strncmp(tmp_value_list, "days", strlen("days"))) {
|
||||
uptime = uptime / 86400;
|
||||
}
|
||||
/* else uptime in seconds, nothing to do */
|
||||
|
||||
xasprintf(&output_message, _("System Uptime - %u day(s) %u hour(s) %u minute(s) |uptime=%lu"), updays, uphours, upminutes,
|
||||
uptime);
|
||||
|
||||
if (check_critical_value && uptime <= critical_value)
|
||||
if (config.check_critical_value && uptime <= config.critical_value) {
|
||||
return_code = STATE_CRITICAL;
|
||||
else if (check_warning_value && uptime <= warning_value)
|
||||
} else if (config.check_warning_value && uptime <= config.warning_value) {
|
||||
return_code = STATE_WARNING;
|
||||
else
|
||||
} else {
|
||||
return_code = STATE_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
} break;
|
||||
case CHECK_USEDDISKSPACE:
|
||||
|
||||
if (value_list == NULL)
|
||||
if (config.value_list == NULL) {
|
||||
output_message = strdup(_("missing -l parameters"));
|
||||
else if (strlen(value_list) != 1)
|
||||
} else if (strlen(config.value_list) != 1) {
|
||||
output_message = strdup(_("wrong -l argument"));
|
||||
else {
|
||||
xasprintf(&send_buffer, "%s&4&%s", req_password, value_list);
|
||||
fetch_data(server_address, server_port, send_buffer);
|
||||
fds = strtok(recv_buffer, "&");
|
||||
tds = strtok(NULL, "&");
|
||||
if (fds != NULL)
|
||||
} else {
|
||||
xasprintf(&send_buffer, "%s&4&%s", config.req_password, config.value_list);
|
||||
fetch_data(config.server_address, config.server_port, send_buffer);
|
||||
char *fds = strtok(recv_buffer, "&");
|
||||
char *tds = strtok(NULL, "&");
|
||||
double total_disk_space = 0;
|
||||
double free_disk_space = 0;
|
||||
if (fds != NULL) {
|
||||
free_disk_space = atof(fds);
|
||||
if (tds != NULL)
|
||||
}
|
||||
if (tds != NULL) {
|
||||
total_disk_space = atof(tds);
|
||||
}
|
||||
|
||||
if (total_disk_space > 0 && free_disk_space >= 0) {
|
||||
percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
|
||||
warning_used_space = ((float)warning_value / 100) * total_disk_space;
|
||||
critical_used_space = ((float)critical_value / 100) * total_disk_space;
|
||||
double percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
|
||||
double warning_used_space = ((float)config.warning_value / 100) * total_disk_space;
|
||||
double critical_used_space = ((float)config.critical_value / 100) * total_disk_space;
|
||||
|
||||
xasprintf(&temp_string, _("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), value_list,
|
||||
xasprintf(&temp_string, _("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), config.value_list,
|
||||
total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
|
||||
free_disk_space / 1073741824, (free_disk_space / total_disk_space) * 100);
|
||||
xasprintf(&temp_string_perf, _("'%s:\\ Used Space'=%.2fGb;%.2f;%.2f;0.00;%.2f"), value_list,
|
||||
xasprintf(&temp_string_perf, _("'%s:\\ Used Space'=%.2fGb;%.2f;%.2f;0.00;%.2f"), config.value_list,
|
||||
(total_disk_space - free_disk_space) / 1073741824, warning_used_space / 1073741824,
|
||||
critical_used_space / 1073741824, total_disk_space / 1073741824);
|
||||
|
||||
if (check_critical_value && percent_used_space >= critical_value)
|
||||
if (config.check_critical_value && percent_used_space >= config.critical_value) {
|
||||
return_code = STATE_CRITICAL;
|
||||
else if (check_warning_value && percent_used_space >= warning_value)
|
||||
} else if (config.check_warning_value && percent_used_space >= config.warning_value) {
|
||||
return_code = STATE_WARNING;
|
||||
else
|
||||
} else {
|
||||
return_code = STATE_OK;
|
||||
}
|
||||
|
||||
output_message = strdup(temp_string);
|
||||
perfdata = temp_string_perf;
|
||||
|
|
@ -271,41 +234,40 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case CHECK_SERVICESTATE:
|
||||
case CHECK_PROCSTATE:
|
||||
|
||||
if (value_list == NULL)
|
||||
if (config.value_list == NULL) {
|
||||
output_message = strdup(_("No service/process specified"));
|
||||
else {
|
||||
preparelist(value_list); /* replace , between services with & to send the request */
|
||||
xasprintf(&send_buffer, "%s&%u&%s&%s", req_password, (vars_to_check == CHECK_SERVICESTATE) ? 5 : 6,
|
||||
(show_all) ? "ShowAll" : "ShowFail", value_list);
|
||||
fetch_data(server_address, server_port, send_buffer);
|
||||
numstr = strtok(recv_buffer, "&");
|
||||
if (numstr == NULL)
|
||||
} else {
|
||||
preparelist(config.value_list); /* replace , between services with & to send the request */
|
||||
xasprintf(&send_buffer, "%s&%u&%s&%s", config.req_password, (config.vars_to_check == CHECK_SERVICESTATE) ? 5 : 6,
|
||||
(config.show_all) ? "ShowAll" : "ShowFail", config.value_list);
|
||||
fetch_data(config.server_address, config.server_port, send_buffer);
|
||||
char *numstr = strtok(recv_buffer, "&");
|
||||
if (numstr == NULL) {
|
||||
die(STATE_UNKNOWN, _("could not fetch information from server\n"));
|
||||
}
|
||||
return_code = atoi(numstr);
|
||||
temp_string = strtok(NULL, "&");
|
||||
output_message = strdup(temp_string);
|
||||
}
|
||||
break;
|
||||
|
||||
case CHECK_MEMUSE:
|
||||
|
||||
xasprintf(&send_buffer, "%s&7", req_password);
|
||||
fetch_data(server_address, server_port, send_buffer);
|
||||
numstr = strtok(recv_buffer, "&");
|
||||
if (numstr == NULL)
|
||||
xasprintf(&send_buffer, "%s&7", config.req_password);
|
||||
fetch_data(config.server_address, config.server_port, send_buffer);
|
||||
char *numstr = strtok(recv_buffer, "&");
|
||||
if (numstr == NULL) {
|
||||
die(STATE_UNKNOWN, _("could not fetch information from server\n"));
|
||||
mem_commitLimit = atof(numstr);
|
||||
}
|
||||
double mem_commitLimit = atof(numstr);
|
||||
numstr = strtok(NULL, "&");
|
||||
if (numstr == NULL)
|
||||
if (numstr == NULL) {
|
||||
die(STATE_UNKNOWN, _("could not fetch information from server\n"));
|
||||
mem_commitByte = atof(numstr);
|
||||
percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
|
||||
warning_used_space = ((float)warning_value / 100) * mem_commitLimit;
|
||||
critical_used_space = ((float)critical_value / 100) * mem_commitLimit;
|
||||
}
|
||||
double mem_commitByte = atof(numstr);
|
||||
double percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
|
||||
double warning_used_space = ((float)config.warning_value / 100) * mem_commitLimit;
|
||||
double critical_used_space = ((float)config.critical_value / 100) * mem_commitLimit;
|
||||
|
||||
/* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here,
|
||||
which equals RAM + Pagefiles. */
|
||||
|
|
@ -316,15 +278,14 @@ int main(int argc, char **argv) {
|
|||
critical_used_space / 1048567, mem_commitLimit / 1048567);
|
||||
|
||||
return_code = STATE_OK;
|
||||
if (check_critical_value && percent_used_space >= critical_value)
|
||||
if (config.check_critical_value && percent_used_space >= config.critical_value) {
|
||||
return_code = STATE_CRITICAL;
|
||||
else if (check_warning_value && percent_used_space >= warning_value)
|
||||
} else if (config.check_warning_value && percent_used_space >= config.warning_value) {
|
||||
return_code = STATE_WARNING;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case CHECK_COUNTER:
|
||||
|
||||
case CHECK_COUNTER: {
|
||||
/*
|
||||
CHECK_COUNTER has been modified to provide extensive perfdata information.
|
||||
In order to do this, some modifications have been done to the code
|
||||
|
|
@ -346,26 +307,32 @@ int main(int argc, char **argv) {
|
|||
strange things will happen when you make graphs of your data.
|
||||
*/
|
||||
|
||||
if (value_list == NULL)
|
||||
double counter_value = 0.0;
|
||||
if (config.value_list == NULL) {
|
||||
output_message = strdup(_("No counter specified"));
|
||||
else {
|
||||
preparelist(value_list); /* replace , between services with & to send the request */
|
||||
isPercent = (strchr(value_list, '%') != NULL);
|
||||
} else {
|
||||
preparelist(config.value_list); /* replace , between services with & to send the request */
|
||||
bool isPercent = (strchr(config.value_list, '%') != NULL);
|
||||
|
||||
strtok(value_list, "&"); /* burn the first parameters */
|
||||
strtok(config.value_list, "&"); /* burn the first parameters */
|
||||
description = strtok(NULL, "&");
|
||||
counter_unit = strtok(NULL, "&");
|
||||
xasprintf(&send_buffer, "%s&8&%s", req_password, value_list);
|
||||
fetch_data(server_address, server_port, send_buffer);
|
||||
xasprintf(&send_buffer, "%s&8&%s", config.req_password, config.value_list);
|
||||
fetch_data(config.server_address, config.server_port, send_buffer);
|
||||
counter_value = atof(recv_buffer);
|
||||
|
||||
if (description == NULL)
|
||||
bool allRight = false;
|
||||
if (description == NULL) {
|
||||
xasprintf(&output_message, "%.f", counter_value);
|
||||
else if (isPercent) {
|
||||
} else if (isPercent) {
|
||||
counter_unit = strdup("%");
|
||||
allRight = true;
|
||||
}
|
||||
|
||||
char *minval = NULL;
|
||||
char *maxval = NULL;
|
||||
double fminval = 0;
|
||||
double fmaxval = 0;
|
||||
if ((counter_unit != NULL) && (!allRight)) {
|
||||
minval = strtok(NULL, "&");
|
||||
maxval = strtok(NULL, "&");
|
||||
|
|
@ -375,16 +342,18 @@ int main(int argc, char **argv) {
|
|||
fminval = (minval != NULL) ? strtod(minval, &errcvt) : -1;
|
||||
fmaxval = (minval != NULL) ? strtod(maxval, &errcvt) : -1;
|
||||
|
||||
if ((fminval == 0) && (minval == errcvt))
|
||||
if ((fminval == 0) && (minval == errcvt)) {
|
||||
output_message = strdup(_("Minimum value contains non-numbers"));
|
||||
else {
|
||||
if ((fmaxval == 0) && (maxval == errcvt))
|
||||
} else {
|
||||
if ((fmaxval == 0) && (maxval == errcvt)) {
|
||||
output_message = strdup(_("Maximum value contains non-numbers"));
|
||||
else
|
||||
} else {
|
||||
allRight = true; /* Everything is OK. */
|
||||
}
|
||||
}
|
||||
} else if ((counter_unit == NULL) && (description != NULL))
|
||||
} else if ((counter_unit == NULL) && (description != NULL)) {
|
||||
output_message = strdup(_("No unit counter specified"));
|
||||
}
|
||||
|
||||
if (allRight) {
|
||||
/* Let's format the output string, finally... */
|
||||
|
|
@ -396,63 +365,65 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
xasprintf(&output_message, "%s |", output_message);
|
||||
xasprintf(&output_message, "%s %s", output_message,
|
||||
fperfdata(description, counter_value, counter_unit, 1, warning_value, 1, critical_value,
|
||||
fperfdata(description, counter_value, counter_unit, 1, config.warning_value, 1, config.critical_value,
|
||||
(!(isPercent) && (minval != NULL)), fminval, (!(isPercent) && (minval != NULL)), fmaxval));
|
||||
}
|
||||
}
|
||||
|
||||
if (critical_value > warning_value) { /* Normal thresholds */
|
||||
if (check_critical_value && counter_value >= critical_value)
|
||||
if (config.critical_value > config.warning_value) { /* Normal thresholds */
|
||||
if (config.check_critical_value && counter_value >= config.critical_value) {
|
||||
return_code = STATE_CRITICAL;
|
||||
else if (check_warning_value && counter_value >= warning_value)
|
||||
} else if (config.check_warning_value && counter_value >= config.warning_value) {
|
||||
return_code = STATE_WARNING;
|
||||
else
|
||||
} else {
|
||||
return_code = STATE_OK;
|
||||
}
|
||||
} else { /* inverse thresholds */
|
||||
return_code = STATE_OK;
|
||||
if (check_critical_value && counter_value <= critical_value)
|
||||
if (config.check_critical_value && counter_value <= config.critical_value) {
|
||||
return_code = STATE_CRITICAL;
|
||||
else if (check_warning_value && counter_value <= warning_value)
|
||||
} else if (config.check_warning_value && counter_value <= config.warning_value) {
|
||||
return_code = STATE_WARNING;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
} break;
|
||||
case CHECK_FILEAGE:
|
||||
|
||||
if (value_list == NULL)
|
||||
if (config.value_list == NULL) {
|
||||
output_message = strdup(_("No counter specified"));
|
||||
else {
|
||||
preparelist(value_list); /* replace , between services with & to send the request */
|
||||
xasprintf(&send_buffer, "%s&9&%s", req_password, value_list);
|
||||
fetch_data(server_address, server_port, send_buffer);
|
||||
age_in_minutes = atoi(strtok(recv_buffer, "&"));
|
||||
} else {
|
||||
preparelist(config.value_list); /* replace , between services with & to send the request */
|
||||
xasprintf(&send_buffer, "%s&9&%s", config.req_password, config.value_list);
|
||||
fetch_data(config.server_address, config.server_port, send_buffer);
|
||||
unsigned long age_in_minutes = atoi(strtok(recv_buffer, "&"));
|
||||
description = strtok(NULL, "&");
|
||||
output_message = strdup(description);
|
||||
|
||||
if (critical_value > warning_value) { /* Normal thresholds */
|
||||
if (check_critical_value && age_in_minutes >= critical_value)
|
||||
if (config.critical_value > config.warning_value) { /* Normal thresholds */
|
||||
if (config.check_critical_value && age_in_minutes >= config.critical_value) {
|
||||
return_code = STATE_CRITICAL;
|
||||
else if (check_warning_value && age_in_minutes >= warning_value)
|
||||
} else if (config.check_warning_value && age_in_minutes >= config.warning_value) {
|
||||
return_code = STATE_WARNING;
|
||||
else
|
||||
} else {
|
||||
return_code = STATE_OK;
|
||||
}
|
||||
} else { /* inverse thresholds */
|
||||
if (check_critical_value && age_in_minutes <= critical_value)
|
||||
if (config.check_critical_value && age_in_minutes <= config.critical_value) {
|
||||
return_code = STATE_CRITICAL;
|
||||
else if (check_warning_value && age_in_minutes <= warning_value)
|
||||
} else if (config.check_warning_value && age_in_minutes <= config.warning_value) {
|
||||
return_code = STATE_WARNING;
|
||||
else
|
||||
} else {
|
||||
return_code = STATE_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case CHECK_INSTANCES:
|
||||
if (value_list == NULL)
|
||||
if (config.value_list == NULL) {
|
||||
output_message = strdup(_("No counter specified"));
|
||||
else {
|
||||
xasprintf(&send_buffer, "%s&10&%s", req_password, value_list);
|
||||
fetch_data(server_address, server_port, send_buffer);
|
||||
} else {
|
||||
xasprintf(&send_buffer, "%s&10&%s", config.req_password, config.value_list);
|
||||
fetch_data(config.server_address, config.server_port, send_buffer);
|
||||
if (!strncmp(recv_buffer, "ERROR", 5)) {
|
||||
printf("NSClient - %s\n", recv_buffer);
|
||||
exit(STATE_UNKNOWN);
|
||||
|
|
@ -471,18 +442,16 @@ int main(int argc, char **argv) {
|
|||
/* reset timeout */
|
||||
alarm(0);
|
||||
|
||||
if (perfdata == NULL)
|
||||
if (perfdata == NULL) {
|
||||
printf("%s\n", output_message);
|
||||
else
|
||||
} else {
|
||||
printf("%s | %s\n", output_message, perfdata);
|
||||
}
|
||||
return return_code;
|
||||
}
|
||||
|
||||
/* process command-line arguments */
|
||||
int process_arguments(int argc, char **argv) {
|
||||
int c;
|
||||
|
||||
int option = 0;
|
||||
check_nt_config_wrapper process_arguments(int argc, char **argv) {
|
||||
static struct option longopts[] = {{"port", required_argument, 0, 'p'},
|
||||
{"timeout", required_argument, 0, 't'},
|
||||
{"critical", required_argument, 0, 'c'},
|
||||
|
|
@ -497,34 +466,44 @@ int process_arguments(int argc, char **argv) {
|
|||
{"help", no_argument, 0, 'h'},
|
||||
{0, 0, 0, 0}};
|
||||
|
||||
check_nt_config_wrapper result = {
|
||||
.errorcode = OK,
|
||||
.config = check_nt_config_init(),
|
||||
};
|
||||
|
||||
/* no options were supplied */
|
||||
if (argc < 2)
|
||||
return ERROR;
|
||||
if (argc < 2) {
|
||||
result.errorcode = ERROR;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* backwards compatibility */
|
||||
if (!is_option(argv[1])) {
|
||||
server_address = strdup(argv[1]);
|
||||
result.config.server_address = strdup(argv[1]);
|
||||
argv[1] = argv[0];
|
||||
argv = &argv[1];
|
||||
argc--;
|
||||
}
|
||||
|
||||
for (c = 1; c < argc; c++) {
|
||||
if (strcmp("-to", argv[c]) == 0)
|
||||
strcpy(argv[c], "-t");
|
||||
else if (strcmp("-wv", argv[c]) == 0)
|
||||
strcpy(argv[c], "-w");
|
||||
else if (strcmp("-cv", argv[c]) == 0)
|
||||
strcpy(argv[c], "-c");
|
||||
for (int index = 1; index < argc; index++) {
|
||||
if (strcmp("-to", argv[index]) == 0) {
|
||||
strcpy(argv[index], "-t");
|
||||
} else if (strcmp("-wv", argv[index]) == 0) {
|
||||
strcpy(argv[index], "-w");
|
||||
} else if (strcmp("-cv", argv[index]) == 0) {
|
||||
strcpy(argv[index], "-c");
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
c = getopt_long(argc, argv, "+hVH:t:c:w:p:v:l:s:d:u", longopts, &option);
|
||||
int option = 0;
|
||||
while (true) {
|
||||
int option_index = getopt_long(argc, argv, "+hVH:t:c:w:p:v:l:s:d:u", longopts, &option);
|
||||
|
||||
if (c == -1 || c == EOF || c == 1)
|
||||
if (option_index == -1 || option_index == EOF || option_index == 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
switch (option_index) {
|
||||
case '?': /* print short usage statement if args not parsable */
|
||||
usage5();
|
||||
case 'h': /* help */
|
||||
|
|
@ -534,118 +513,128 @@ int process_arguments(int argc, char **argv) {
|
|||
print_revision(progname, NP_VERSION);
|
||||
exit(STATE_UNKNOWN);
|
||||
case 'H': /* hostname */
|
||||
server_address = optarg;
|
||||
result.config.server_address = optarg;
|
||||
break;
|
||||
case 's': /* password */
|
||||
req_password = optarg;
|
||||
result.config.req_password = optarg;
|
||||
break;
|
||||
case 'p': /* port */
|
||||
if (is_intnonneg(optarg))
|
||||
server_port = atoi(optarg);
|
||||
else
|
||||
if (is_intnonneg(optarg)) {
|
||||
result.config.server_port = atoi(optarg);
|
||||
} else {
|
||||
die(STATE_UNKNOWN, _("Server port must be an integer\n"));
|
||||
}
|
||||
break;
|
||||
case 'v':
|
||||
if (strlen(optarg) < 4)
|
||||
return ERROR;
|
||||
if (!strcmp(optarg, "CLIENTVERSION"))
|
||||
vars_to_check = CHECK_CLIENTVERSION;
|
||||
else if (!strcmp(optarg, "CPULOAD"))
|
||||
vars_to_check = CHECK_CPULOAD;
|
||||
else if (!strcmp(optarg, "UPTIME"))
|
||||
vars_to_check = CHECK_UPTIME;
|
||||
else if (!strcmp(optarg, "USEDDISKSPACE"))
|
||||
vars_to_check = CHECK_USEDDISKSPACE;
|
||||
else if (!strcmp(optarg, "SERVICESTATE"))
|
||||
vars_to_check = CHECK_SERVICESTATE;
|
||||
else if (!strcmp(optarg, "PROCSTATE"))
|
||||
vars_to_check = CHECK_PROCSTATE;
|
||||
else if (!strcmp(optarg, "MEMUSE"))
|
||||
vars_to_check = CHECK_MEMUSE;
|
||||
else if (!strcmp(optarg, "COUNTER"))
|
||||
vars_to_check = CHECK_COUNTER;
|
||||
else if (!strcmp(optarg, "FILEAGE"))
|
||||
vars_to_check = CHECK_FILEAGE;
|
||||
else if (!strcmp(optarg, "INSTANCES"))
|
||||
vars_to_check = CHECK_INSTANCES;
|
||||
else
|
||||
return ERROR;
|
||||
if (strlen(optarg) < 4) {
|
||||
result.errorcode = ERROR;
|
||||
return result;
|
||||
}
|
||||
if (!strcmp(optarg, "CLIENTVERSION")) {
|
||||
result.config.vars_to_check = CHECK_CLIENTVERSION;
|
||||
} else if (!strcmp(optarg, "CPULOAD")) {
|
||||
result.config.vars_to_check = CHECK_CPULOAD;
|
||||
} else if (!strcmp(optarg, "UPTIME")) {
|
||||
result.config.vars_to_check = CHECK_UPTIME;
|
||||
} else if (!strcmp(optarg, "USEDDISKSPACE")) {
|
||||
result.config.vars_to_check = CHECK_USEDDISKSPACE;
|
||||
} else if (!strcmp(optarg, "SERVICESTATE")) {
|
||||
result.config.vars_to_check = CHECK_SERVICESTATE;
|
||||
} else if (!strcmp(optarg, "PROCSTATE")) {
|
||||
result.config.vars_to_check = CHECK_PROCSTATE;
|
||||
} else if (!strcmp(optarg, "MEMUSE")) {
|
||||
result.config.vars_to_check = CHECK_MEMUSE;
|
||||
} else if (!strcmp(optarg, "COUNTER")) {
|
||||
result.config.vars_to_check = CHECK_COUNTER;
|
||||
} else if (!strcmp(optarg, "FILEAGE")) {
|
||||
result.config.vars_to_check = CHECK_FILEAGE;
|
||||
} else if (!strcmp(optarg, "INSTANCES")) {
|
||||
result.config.vars_to_check = CHECK_INSTANCES;
|
||||
} else {
|
||||
result.errorcode = ERROR;
|
||||
return result;
|
||||
}
|
||||
break;
|
||||
case 'l': /* value list */
|
||||
value_list = optarg;
|
||||
result.config.value_list = optarg;
|
||||
break;
|
||||
case 'w': /* warning threshold */
|
||||
warning_value = strtoul(optarg, NULL, 10);
|
||||
check_warning_value = true;
|
||||
result.config.warning_value = strtoul(optarg, NULL, 10);
|
||||
result.config.check_warning_value = true;
|
||||
break;
|
||||
case 'c': /* critical threshold */
|
||||
critical_value = strtoul(optarg, NULL, 10);
|
||||
check_critical_value = true;
|
||||
result.config.critical_value = strtoul(optarg, NULL, 10);
|
||||
result.config.check_critical_value = true;
|
||||
break;
|
||||
case 'd': /* Display select for services */
|
||||
if (!strcmp(optarg, "SHOWALL"))
|
||||
show_all = true;
|
||||
if (!strcmp(optarg, "SHOWALL")) {
|
||||
result.config.show_all = true;
|
||||
}
|
||||
break;
|
||||
case 'u':
|
||||
socket_timeout_state = STATE_UNKNOWN;
|
||||
break;
|
||||
case 't': /* timeout */
|
||||
socket_timeout = atoi(optarg);
|
||||
if (socket_timeout <= 0)
|
||||
return ERROR;
|
||||
if (socket_timeout <= 0) {
|
||||
result.errorcode = ERROR;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (server_address == NULL)
|
||||
if (result.config.server_address == NULL) {
|
||||
usage4(_("You must provide a server address or host name"));
|
||||
}
|
||||
|
||||
if (vars_to_check == CHECK_NONE)
|
||||
return ERROR;
|
||||
if (result.config.vars_to_check == CHECK_NONE) {
|
||||
result.errorcode = ERROR;
|
||||
return result;
|
||||
}
|
||||
|
||||
if (req_password == NULL)
|
||||
req_password = strdup(_("None"));
|
||||
if (result.config.req_password == NULL) {
|
||||
result.config.req_password = strdup(_("None"));
|
||||
}
|
||||
|
||||
return OK;
|
||||
return result;
|
||||
}
|
||||
|
||||
void fetch_data(const char *address, int port, const char *sendb) {
|
||||
int result;
|
||||
int result = process_tcp_request(address, port, sendb, recv_buffer, sizeof(recv_buffer));
|
||||
|
||||
result = process_tcp_request(address, port, sendb, recv_buffer, sizeof(recv_buffer));
|
||||
|
||||
if (result != STATE_OK)
|
||||
if (result != STATE_OK) {
|
||||
die(result, _("could not fetch information from server\n"));
|
||||
}
|
||||
|
||||
if (!strncmp(recv_buffer, "ERROR", 5))
|
||||
if (!strncmp(recv_buffer, "ERROR", 5)) {
|
||||
die(STATE_UNKNOWN, "NSClient - %s\n", recv_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
bool strtoularray(unsigned long *array, char *string, const char *delim) {
|
||||
/* split a <delim> delimited string into a long array */
|
||||
int idx = 0;
|
||||
char *t1;
|
||||
|
||||
for (idx = 0; idx < MAX_VALUE_LIST; idx++)
|
||||
for (int idx = 0; idx < MAX_VALUE_LIST; idx++) {
|
||||
array[idx] = 0;
|
||||
}
|
||||
|
||||
idx = 0;
|
||||
for (t1 = strtok(string, delim); t1 != NULL; t1 = strtok(NULL, delim)) {
|
||||
int idx = 0;
|
||||
for (char *t1 = strtok(string, delim); t1 != NULL; t1 = strtok(NULL, delim)) {
|
||||
if (is_numeric(t1) && idx < MAX_VALUE_LIST) {
|
||||
array[idx] = strtoul(t1, NULL, 10);
|
||||
idx++;
|
||||
} else
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void preparelist(char *string) {
|
||||
/* Replace all , with & which is the delimiter for the request */
|
||||
int i;
|
||||
|
||||
for (i = 0; (size_t)i < strlen(string); i++)
|
||||
for (int i = 0; (size_t)i < strlen(string); i++) {
|
||||
if (string[i] == ',') {
|
||||
string[i] = '&';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_help(void) {
|
||||
|
|
|
|||
53
plugins/check_nt.d/config.h
Normal file
53
plugins/check_nt.d/config.h
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#pragma once
|
||||
|
||||
#include "../../config.h"
|
||||
#include <stddef.h>
|
||||
|
||||
enum {
|
||||
PORT = 1248,
|
||||
};
|
||||
|
||||
enum checkvars {
|
||||
CHECK_NONE,
|
||||
CHECK_CLIENTVERSION,
|
||||
CHECK_CPULOAD,
|
||||
CHECK_UPTIME,
|
||||
CHECK_USEDDISKSPACE,
|
||||
CHECK_SERVICESTATE,
|
||||
CHECK_PROCSTATE,
|
||||
CHECK_MEMUSE,
|
||||
CHECK_COUNTER,
|
||||
CHECK_FILEAGE,
|
||||
CHECK_INSTANCES
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
char *server_address;
|
||||
int server_port;
|
||||
char *req_password;
|
||||
enum checkvars vars_to_check;
|
||||
bool show_all;
|
||||
char *value_list;
|
||||
bool check_warning_value;
|
||||
unsigned long warning_value;
|
||||
bool check_critical_value;
|
||||
unsigned long critical_value;
|
||||
} check_nt_config;
|
||||
|
||||
check_nt_config check_nt_config_init() {
|
||||
check_nt_config tmp = {
|
||||
.server_address = NULL,
|
||||
.server_port = PORT,
|
||||
.req_password = NULL,
|
||||
|
||||
.vars_to_check = CHECK_NONE,
|
||||
.show_all = false,
|
||||
.value_list = NULL,
|
||||
|
||||
.check_warning_value = false,
|
||||
.warning_value = 0,
|
||||
.check_critical_value = false,
|
||||
.critical_value = 0,
|
||||
};
|
||||
return tmp;
|
||||
}
|
||||
Loading…
Reference in a new issue