summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-12 00:36:53 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-12 00:36:53 +0100
commit47b484eb454b0a487f7d32406ee93f85c94f5895 (patch)
treea4ac9fa42c63ed8fa2fe5f4a7dbf11d32b3e4704 /plugins
parentdeac7f99ef409cf1299ab8da25a930db200e1dba (diff)
downloadmonitoring-plugins-47b484eb454b0a487f7d32406ee93f85c94f5895.tar.gz
Refactor check_nt
Diffstat (limited to 'plugins')
-rw-r--r--plugins/Makefile.am1
-rw-r--r--plugins/check_nt.c254
-rw-r--r--plugins/check_nt.d/config.h53
3 files changed, 178 insertions, 130 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 5d4449bf..097763fc 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -56,6 +56,7 @@ EXTRA_DIST = t \
56 check_nagios.d \ 56 check_nagios.d \
57 check_dbi.d \ 57 check_dbi.d \
58 check_ssh.d \ 58 check_ssh.d \
59 check_nt.d \
59 check_dns.d \ 60 check_dns.d \
60 check_mrtgtraf.d \ 61 check_mrtgtraf.d \
61 check_mysql_query.d \ 62 check_mysql_query.d \
diff --git a/plugins/check_nt.c b/plugins/check_nt.c
index 7897558b..7dd23e5c 100644
--- a/plugins/check_nt.c
+++ b/plugins/check_nt.c
@@ -39,42 +39,22 @@ const char *email = "devel@monitoring-plugins.org";
39#include "common.h" 39#include "common.h"
40#include "netutils.h" 40#include "netutils.h"
41#include "utils.h" 41#include "utils.h"
42 42#include "check_nt.d/config.h"
43enum checkvars {
44 CHECK_NONE,
45 CHECK_CLIENTVERSION,
46 CHECK_CPULOAD,
47 CHECK_UPTIME,
48 CHECK_USEDDISKSPACE,
49 CHECK_SERVICESTATE,
50 CHECK_PROCSTATE,
51 CHECK_MEMUSE,
52 CHECK_COUNTER,
53 CHECK_FILEAGE,
54 CHECK_INSTANCES
55};
56 43
57enum { 44enum {
58 MAX_VALUE_LIST = 30, 45 MAX_VALUE_LIST = 30,
59 PORT = 1248
60}; 46};
61 47
62static char *server_address = NULL;
63static int server_port = PORT;
64static char *value_list = NULL;
65static char *req_password = NULL;
66static unsigned long lvalue_list[MAX_VALUE_LIST];
67static unsigned long warning_value = 0L;
68static unsigned long critical_value = 0L;
69static bool check_warning_value = false;
70static bool check_critical_value = false;
71static enum checkvars vars_to_check = CHECK_NONE;
72static bool show_all = false;
73
74static char recv_buffer[MAX_INPUT_BUFFER]; 48static char recv_buffer[MAX_INPUT_BUFFER];
75 49
76static void fetch_data(const char *address, int port, const char *sendb); 50static void fetch_data(const char *address, int port, const char *sendb);
77static int process_arguments(int /*argc*/, char ** /*argv*/); 51
52typedef struct {
53 int errorcode;
54 check_nt_config config;
55} check_nt_config_wrapper;
56static check_nt_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
57
78static void preparelist(char *string); 58static void preparelist(char *string);
79static bool strtoularray(unsigned long *array, char *string, const char *delim); 59static bool strtoularray(unsigned long *array, char *string, const char *delim);
80static void print_help(void); 60static void print_help(void);
@@ -88,10 +68,13 @@ int main(int argc, char **argv) {
88 /* Parse extra opts if any */ 68 /* Parse extra opts if any */
89 argv = np_extra_opts(&argc, argv, progname); 69 argv = np_extra_opts(&argc, argv, progname);
90 70
91 if (process_arguments(argc, argv) == ERROR) { 71 check_nt_config_wrapper tmp_config = process_arguments(argc, argv);
72 if (tmp_config.errorcode == ERROR) {
92 usage4(_("Could not parse arguments")); 73 usage4(_("Could not parse arguments"));
93 } 74 }
94 75
76 const check_nt_config config = tmp_config.config;
77
95 /* initialize alarm signal handling */ 78 /* initialize alarm signal handling */
96 signal(SIGALRM, socket_timeout_alarm_handler); 79 signal(SIGALRM, socket_timeout_alarm_handler);
97 80
@@ -107,12 +90,13 @@ int main(int argc, char **argv) {
107 char *description = NULL; 90 char *description = NULL;
108 char *counter_unit = NULL; 91 char *counter_unit = NULL;
109 char *errcvt = NULL; 92 char *errcvt = NULL;
110 switch (vars_to_check) { 93 unsigned long lvalue_list[MAX_VALUE_LIST];
94 switch (config.vars_to_check) {
111 case CHECK_CLIENTVERSION: 95 case CHECK_CLIENTVERSION:
112 xasprintf(&send_buffer, "%s&1", req_password); 96 xasprintf(&send_buffer, "%s&1", config.req_password);
113 fetch_data(server_address, server_port, send_buffer); 97 fetch_data(config.server_address, config.server_port, send_buffer);
114 if (value_list != NULL && strcmp(recv_buffer, value_list) != 0) { 98 if (config.value_list != NULL && strcmp(recv_buffer, config.value_list) != 0) {
115 xasprintf(&output_message, _("Wrong client version - running: %s, required: %s"), recv_buffer, value_list); 99 xasprintf(&output_message, _("Wrong client version - running: %s, required: %s"), recv_buffer, config.value_list);
116 return_code = STATE_WARNING; 100 return_code = STATE_WARNING;
117 } else { 101 } else {
118 xasprintf(&output_message, "%s", recv_buffer); 102 xasprintf(&output_message, "%s", recv_buffer);
@@ -120,9 +104,9 @@ int main(int argc, char **argv) {
120 } 104 }
121 break; 105 break;
122 case CHECK_CPULOAD: 106 case CHECK_CPULOAD:
123 if (value_list == NULL) { 107 if (config.value_list == NULL) {
124 output_message = strdup(_("missing -l parameters")); 108 output_message = strdup(_("missing -l parameters"));
125 } else if (!strtoularray(lvalue_list, value_list, ",")) { 109 } else if (!strtoularray(lvalue_list, config.value_list, ",")) {
126 output_message = strdup(_("wrong -l parameter.")); 110 output_message = strdup(_("wrong -l parameter."));
127 } else { 111 } else {
128 /* -l parameters is present with only integers */ 112 /* -l parameters is present with only integers */
@@ -137,8 +121,8 @@ int main(int argc, char **argv) {
137 lvalue_list[2 + offset] > (unsigned long)0 && lvalue_list[2 + offset] <= (unsigned long)100) { 121 lvalue_list[2 + offset] > (unsigned long)0 && lvalue_list[2 + offset] <= (unsigned long)100) {
138 122
139 /* Send request and retrieve data */ 123 /* Send request and retrieve data */
140 xasprintf(&send_buffer, "%s&2&%lu", req_password, lvalue_list[0 + offset]); 124 xasprintf(&send_buffer, "%s&2&%lu", config.req_password, lvalue_list[0 + offset]);
141 fetch_data(server_address, server_port, send_buffer); 125 fetch_data(config.server_address, config.server_port, send_buffer);
142 126
143 unsigned long utilization = strtoul(recv_buffer, NULL, 10); 127 unsigned long utilization = strtoul(recv_buffer, NULL, 10);
144 128
@@ -165,27 +149,28 @@ int main(int argc, char **argv) {
165 } 149 }
166 } 150 }
167 break; 151 break;
168 case CHECK_UPTIME: 152 case CHECK_UPTIME: {
169 if (value_list == NULL) { 153 char *tmp_value_list = config.value_list;
170 value_list = "minutes"; 154 if (config.value_list == NULL) {
155 tmp_value_list = "minutes";
171 } 156 }
172 if (strncmp(value_list, "seconds", strlen("seconds") + 1) && strncmp(value_list, "minutes", strlen("minutes") + 1) && 157 if (strncmp(tmp_value_list, "seconds", strlen("seconds") + 1) && strncmp(tmp_value_list, "minutes", strlen("minutes") + 1) &&
173 strncmp(value_list, "hours", strlen("hours") + 1) && strncmp(value_list, "days", strlen("days") + 1)) { 158 strncmp(config.value_list, "hours", strlen("hours") + 1) && strncmp(tmp_value_list, "days", strlen("days") + 1)) {
174 159
175 output_message = strdup(_("wrong -l argument")); 160 output_message = strdup(_("wrong -l argument"));
176 } else { 161 } else {
177 xasprintf(&send_buffer, "%s&3", req_password); 162 xasprintf(&send_buffer, "%s&3", config.req_password);
178 fetch_data(server_address, server_port, send_buffer); 163 fetch_data(config.server_address, config.server_port, send_buffer);
179 unsigned long uptime = strtoul(recv_buffer, NULL, 10); 164 unsigned long uptime = strtoul(recv_buffer, NULL, 10);
180 int updays = uptime / 86400; 165 int updays = uptime / 86400;
181 int uphours = (uptime % 86400) / 3600; 166 int uphours = (uptime % 86400) / 3600;
182 int upminutes = ((uptime % 86400) % 3600) / 60; 167 int upminutes = ((uptime % 86400) % 3600) / 60;
183 168
184 if (!strncmp(value_list, "minutes", strlen("minutes"))) { 169 if (!strncmp(tmp_value_list, "minutes", strlen("minutes"))) {
185 uptime = uptime / 60; 170 uptime = uptime / 60;
186 } else if (!strncmp(value_list, "hours", strlen("hours"))) { 171 } else if (!strncmp(tmp_value_list, "hours", strlen("hours"))) {
187 uptime = uptime / 3600; 172 uptime = uptime / 3600;
188 } else if (!strncmp(value_list, "days", strlen("days"))) { 173 } else if (!strncmp(tmp_value_list, "days", strlen("days"))) {
189 uptime = uptime / 86400; 174 uptime = uptime / 86400;
190 } 175 }
191 /* else uptime in seconds, nothing to do */ 176 /* else uptime in seconds, nothing to do */
@@ -193,23 +178,23 @@ int main(int argc, char **argv) {
193 xasprintf(&output_message, _("System Uptime - %u day(s) %u hour(s) %u minute(s) |uptime=%lu"), updays, uphours, upminutes, 178 xasprintf(&output_message, _("System Uptime - %u day(s) %u hour(s) %u minute(s) |uptime=%lu"), updays, uphours, upminutes,
194 uptime); 179 uptime);
195 180
196 if (check_critical_value && uptime <= critical_value) { 181 if (config.check_critical_value && uptime <= config.critical_value) {
197 return_code = STATE_CRITICAL; 182 return_code = STATE_CRITICAL;
198 } else if (check_warning_value && uptime <= warning_value) { 183 } else if (config.check_warning_value && uptime <= config.warning_value) {
199 return_code = STATE_WARNING; 184 return_code = STATE_WARNING;
200 } else { 185 } else {
201 return_code = STATE_OK; 186 return_code = STATE_OK;
202 } 187 }
203 } 188 }
204 break; 189 } break;
205 case CHECK_USEDDISKSPACE: 190 case CHECK_USEDDISKSPACE:
206 if (value_list == NULL) { 191 if (config.value_list == NULL) {
207 output_message = strdup(_("missing -l parameters")); 192 output_message = strdup(_("missing -l parameters"));
208 } else if (strlen(value_list) != 1) { 193 } else if (strlen(config.value_list) != 1) {
209 output_message = strdup(_("wrong -l argument")); 194 output_message = strdup(_("wrong -l argument"));
210 } else { 195 } else {
211 xasprintf(&send_buffer, "%s&4&%s", req_password, value_list); 196 xasprintf(&send_buffer, "%s&4&%s", config.req_password, config.value_list);
212 fetch_data(server_address, server_port, send_buffer); 197 fetch_data(config.server_address, config.server_port, send_buffer);
213 char *fds = strtok(recv_buffer, "&"); 198 char *fds = strtok(recv_buffer, "&");
214 char *tds = strtok(NULL, "&"); 199 char *tds = strtok(NULL, "&");
215 double total_disk_space = 0; 200 double total_disk_space = 0;
@@ -223,19 +208,19 @@ int main(int argc, char **argv) {
223 208
224 if (total_disk_space > 0 && free_disk_space >= 0) { 209 if (total_disk_space > 0 && free_disk_space >= 0) {
225 double percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100; 210 double percent_used_space = ((total_disk_space - free_disk_space) / total_disk_space) * 100;
226 double warning_used_space = ((float)warning_value / 100) * total_disk_space; 211 double warning_used_space = ((float)config.warning_value / 100) * total_disk_space;
227 double critical_used_space = ((float)critical_value / 100) * total_disk_space; 212 double critical_used_space = ((float)config.critical_value / 100) * total_disk_space;
228 213
229 xasprintf(&temp_string, _("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), value_list, 214 xasprintf(&temp_string, _("%s:\\ - total: %.2f Gb - used: %.2f Gb (%.0f%%) - free %.2f Gb (%.0f%%)"), config.value_list,
230 total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space, 215 total_disk_space / 1073741824, (total_disk_space - free_disk_space) / 1073741824, percent_used_space,
231 free_disk_space / 1073741824, (free_disk_space / total_disk_space) * 100); 216 free_disk_space / 1073741824, (free_disk_space / total_disk_space) * 100);
232 xasprintf(&temp_string_perf, _("'%s:\\ Used Space'=%.2fGb;%.2f;%.2f;0.00;%.2f"), value_list, 217 xasprintf(&temp_string_perf, _("'%s:\\ Used Space'=%.2fGb;%.2f;%.2f;0.00;%.2f"), config.value_list,
233 (total_disk_space - free_disk_space) / 1073741824, warning_used_space / 1073741824, 218 (total_disk_space - free_disk_space) / 1073741824, warning_used_space / 1073741824,
234 critical_used_space / 1073741824, total_disk_space / 1073741824); 219 critical_used_space / 1073741824, total_disk_space / 1073741824);
235 220
236 if (check_critical_value && percent_used_space >= critical_value) { 221 if (config.check_critical_value && percent_used_space >= config.critical_value) {
237 return_code = STATE_CRITICAL; 222 return_code = STATE_CRITICAL;
238 } else if (check_warning_value && percent_used_space >= warning_value) { 223 } else if (config.check_warning_value && percent_used_space >= config.warning_value) {
239 return_code = STATE_WARNING; 224 return_code = STATE_WARNING;
240 } else { 225 } else {
241 return_code = STATE_OK; 226 return_code = STATE_OK;
@@ -251,13 +236,13 @@ int main(int argc, char **argv) {
251 break; 236 break;
252 case CHECK_SERVICESTATE: 237 case CHECK_SERVICESTATE:
253 case CHECK_PROCSTATE: 238 case CHECK_PROCSTATE:
254 if (value_list == NULL) { 239 if (config.value_list == NULL) {
255 output_message = strdup(_("No service/process specified")); 240 output_message = strdup(_("No service/process specified"));
256 } else { 241 } else {
257 preparelist(value_list); /* replace , between services with & to send the request */ 242 preparelist(config.value_list); /* replace , between services with & to send the request */
258 xasprintf(&send_buffer, "%s&%u&%s&%s", req_password, (vars_to_check == CHECK_SERVICESTATE) ? 5 : 6, 243 xasprintf(&send_buffer, "%s&%u&%s&%s", config.req_password, (config.vars_to_check == CHECK_SERVICESTATE) ? 5 : 6,
259 (show_all) ? "ShowAll" : "ShowFail", value_list); 244 (config.show_all) ? "ShowAll" : "ShowFail", config.value_list);
260 fetch_data(server_address, server_port, send_buffer); 245 fetch_data(config.server_address, config.server_port, send_buffer);
261 char *numstr = strtok(recv_buffer, "&"); 246 char *numstr = strtok(recv_buffer, "&");
262 if (numstr == NULL) { 247 if (numstr == NULL) {
263 die(STATE_UNKNOWN, _("could not fetch information from server\n")); 248 die(STATE_UNKNOWN, _("could not fetch information from server\n"));
@@ -268,8 +253,8 @@ int main(int argc, char **argv) {
268 } 253 }
269 break; 254 break;
270 case CHECK_MEMUSE: 255 case CHECK_MEMUSE:
271 xasprintf(&send_buffer, "%s&7", req_password); 256 xasprintf(&send_buffer, "%s&7", config.req_password);
272 fetch_data(server_address, server_port, send_buffer); 257 fetch_data(config.server_address, config.server_port, send_buffer);
273 char *numstr = strtok(recv_buffer, "&"); 258 char *numstr = strtok(recv_buffer, "&");
274 if (numstr == NULL) { 259 if (numstr == NULL) {
275 die(STATE_UNKNOWN, _("could not fetch information from server\n")); 260 die(STATE_UNKNOWN, _("could not fetch information from server\n"));
@@ -281,8 +266,8 @@ int main(int argc, char **argv) {
281 } 266 }
282 double mem_commitByte = atof(numstr); 267 double mem_commitByte = atof(numstr);
283 double percent_used_space = (mem_commitByte / mem_commitLimit) * 100; 268 double percent_used_space = (mem_commitByte / mem_commitLimit) * 100;
284 double warning_used_space = ((float)warning_value / 100) * mem_commitLimit; 269 double warning_used_space = ((float)config.warning_value / 100) * mem_commitLimit;
285 double critical_used_space = ((float)critical_value / 100) * mem_commitLimit; 270 double critical_used_space = ((float)config.critical_value / 100) * mem_commitLimit;
286 271
287 /* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here, 272 /* Divisor should be 1048567, not 3044515, as we are measuring "Commit Charge" here,
288 which equals RAM + Pagefiles. */ 273 which equals RAM + Pagefiles. */
@@ -293,14 +278,14 @@ int main(int argc, char **argv) {
293 critical_used_space / 1048567, mem_commitLimit / 1048567); 278 critical_used_space / 1048567, mem_commitLimit / 1048567);
294 279
295 return_code = STATE_OK; 280 return_code = STATE_OK;
296 if (check_critical_value && percent_used_space >= critical_value) { 281 if (config.check_critical_value && percent_used_space >= config.critical_value) {
297 return_code = STATE_CRITICAL; 282 return_code = STATE_CRITICAL;
298 } else if (check_warning_value && percent_used_space >= warning_value) { 283 } else if (config.check_warning_value && percent_used_space >= config.warning_value) {
299 return_code = STATE_WARNING; 284 return_code = STATE_WARNING;
300 } 285 }
301 286
302 break; 287 break;
303 case CHECK_COUNTER: 288 case CHECK_COUNTER: {
304 /* 289 /*
305 CHECK_COUNTER has been modified to provide extensive perfdata information. 290 CHECK_COUNTER has been modified to provide extensive perfdata information.
306 In order to do this, some modifications have been done to the code 291 In order to do this, some modifications have been done to the code
@@ -323,17 +308,17 @@ int main(int argc, char **argv) {
323 */ 308 */
324 309
325 double counter_value = 0.0; 310 double counter_value = 0.0;
326 if (value_list == NULL) { 311 if (config.value_list == NULL) {
327 output_message = strdup(_("No counter specified")); 312 output_message = strdup(_("No counter specified"));
328 } else { 313 } else {
329 preparelist(value_list); /* replace , between services with & to send the request */ 314 preparelist(config.value_list); /* replace , between services with & to send the request */
330 bool isPercent = (strchr(value_list, '%') != NULL); 315 bool isPercent = (strchr(config.value_list, '%') != NULL);
331 316
332 strtok(value_list, "&"); /* burn the first parameters */ 317 strtok(config.value_list, "&"); /* burn the first parameters */
333 description = strtok(NULL, "&"); 318 description = strtok(NULL, "&");
334 counter_unit = strtok(NULL, "&"); 319 counter_unit = strtok(NULL, "&");
335 xasprintf(&send_buffer, "%s&8&%s", req_password, value_list); 320 xasprintf(&send_buffer, "%s&8&%s", config.req_password, config.value_list);
336 fetch_data(server_address, server_port, send_buffer); 321 fetch_data(config.server_address, config.server_port, send_buffer);
337 counter_value = atof(recv_buffer); 322 counter_value = atof(recv_buffer);
338 323
339 bool allRight = false; 324 bool allRight = false;
@@ -380,52 +365,51 @@ int main(int argc, char **argv) {
380 } 365 }
381 xasprintf(&output_message, "%s |", output_message); 366 xasprintf(&output_message, "%s |", output_message);
382 xasprintf(&output_message, "%s %s", output_message, 367 xasprintf(&output_message, "%s %s", output_message,
383 fperfdata(description, counter_value, counter_unit, 1, warning_value, 1, critical_value, 368 fperfdata(description, counter_value, counter_unit, 1, config.warning_value, 1, config.critical_value,
384 (!(isPercent) && (minval != NULL)), fminval, (!(isPercent) && (minval != NULL)), fmaxval)); 369 (!(isPercent) && (minval != NULL)), fminval, (!(isPercent) && (minval != NULL)), fmaxval));
385 } 370 }
386 } 371 }
387 372
388 if (critical_value > warning_value) { /* Normal thresholds */ 373 if (config.critical_value > config.warning_value) { /* Normal thresholds */
389 if (check_critical_value && counter_value >= critical_value) { 374 if (config.check_critical_value && counter_value >= config.critical_value) {
390 return_code = STATE_CRITICAL; 375 return_code = STATE_CRITICAL;
391 } else if (check_warning_value && counter_value >= warning_value) { 376 } else if (config.check_warning_value && counter_value >= config.warning_value) {
392 return_code = STATE_WARNING; 377 return_code = STATE_WARNING;
393 } else { 378 } else {
394 return_code = STATE_OK; 379 return_code = STATE_OK;
395 } 380 }
396 } else { /* inverse thresholds */ 381 } else { /* inverse thresholds */
397 return_code = STATE_OK; 382 return_code = STATE_OK;
398 if (check_critical_value && counter_value <= critical_value) { 383 if (config.check_critical_value && counter_value <= config.critical_value) {
399 return_code = STATE_CRITICAL; 384 return_code = STATE_CRITICAL;
400 } else if (check_warning_value && counter_value <= warning_value) { 385 } else if (config.check_warning_value && counter_value <= config.warning_value) {
401 return_code = STATE_WARNING; 386 return_code = STATE_WARNING;
402 } 387 }
403 } 388 }
404 break; 389 } break;
405
406 case CHECK_FILEAGE: 390 case CHECK_FILEAGE:
407 if (value_list == NULL) { 391 if (config.value_list == NULL) {
408 output_message = strdup(_("No counter specified")); 392 output_message = strdup(_("No counter specified"));
409 } else { 393 } else {
410 preparelist(value_list); /* replace , between services with & to send the request */ 394 preparelist(config.value_list); /* replace , between services with & to send the request */
411 xasprintf(&send_buffer, "%s&9&%s", req_password, value_list); 395 xasprintf(&send_buffer, "%s&9&%s", config.req_password, config.value_list);
412 fetch_data(server_address, server_port, send_buffer); 396 fetch_data(config.server_address, config.server_port, send_buffer);
413 unsigned long age_in_minutes = atoi(strtok(recv_buffer, "&")); 397 unsigned long age_in_minutes = atoi(strtok(recv_buffer, "&"));
414 description = strtok(NULL, "&"); 398 description = strtok(NULL, "&");
415 output_message = strdup(description); 399 output_message = strdup(description);
416 400
417 if (critical_value > warning_value) { /* Normal thresholds */ 401 if (config.critical_value > config.warning_value) { /* Normal thresholds */
418 if (check_critical_value && age_in_minutes >= critical_value) { 402 if (config.check_critical_value && age_in_minutes >= config.critical_value) {
419 return_code = STATE_CRITICAL; 403 return_code = STATE_CRITICAL;
420 } else if (check_warning_value && age_in_minutes >= warning_value) { 404 } else if (config.check_warning_value && age_in_minutes >= config.warning_value) {
421 return_code = STATE_WARNING; 405 return_code = STATE_WARNING;
422 } else { 406 } else {
423 return_code = STATE_OK; 407 return_code = STATE_OK;
424 } 408 }
425 } else { /* inverse thresholds */ 409 } else { /* inverse thresholds */
426 if (check_critical_value && age_in_minutes <= critical_value) { 410 if (config.check_critical_value && age_in_minutes <= config.critical_value) {
427 return_code = STATE_CRITICAL; 411 return_code = STATE_CRITICAL;
428 } else if (check_warning_value && age_in_minutes <= warning_value) { 412 } else if (config.check_warning_value && age_in_minutes <= config.warning_value) {
429 return_code = STATE_WARNING; 413 return_code = STATE_WARNING;
430 } else { 414 } else {
431 return_code = STATE_OK; 415 return_code = STATE_OK;
@@ -435,11 +419,11 @@ int main(int argc, char **argv) {
435 break; 419 break;
436 420
437 case CHECK_INSTANCES: 421 case CHECK_INSTANCES:
438 if (value_list == NULL) { 422 if (config.value_list == NULL) {
439 output_message = strdup(_("No counter specified")); 423 output_message = strdup(_("No counter specified"));
440 } else { 424 } else {
441 xasprintf(&send_buffer, "%s&10&%s", req_password, value_list); 425 xasprintf(&send_buffer, "%s&10&%s", config.req_password, config.value_list);
442 fetch_data(server_address, server_port, send_buffer); 426 fetch_data(config.server_address, config.server_port, send_buffer);
443 if (!strncmp(recv_buffer, "ERROR", 5)) { 427 if (!strncmp(recv_buffer, "ERROR", 5)) {
444 printf("NSClient - %s\n", recv_buffer); 428 printf("NSClient - %s\n", recv_buffer);
445 exit(STATE_UNKNOWN); 429 exit(STATE_UNKNOWN);
@@ -467,7 +451,7 @@ int main(int argc, char **argv) {
467} 451}
468 452
469/* process command-line arguments */ 453/* process command-line arguments */
470int process_arguments(int argc, char **argv) { 454check_nt_config_wrapper process_arguments(int argc, char **argv) {
471 static struct option longopts[] = {{"port", required_argument, 0, 'p'}, 455 static struct option longopts[] = {{"port", required_argument, 0, 'p'},
472 {"timeout", required_argument, 0, 't'}, 456 {"timeout", required_argument, 0, 't'},
473 {"critical", required_argument, 0, 'c'}, 457 {"critical", required_argument, 0, 'c'},
@@ -482,14 +466,20 @@ int process_arguments(int argc, char **argv) {
482 {"help", no_argument, 0, 'h'}, 466 {"help", no_argument, 0, 'h'},
483 {0, 0, 0, 0}}; 467 {0, 0, 0, 0}};
484 468
469 check_nt_config_wrapper result = {
470 .errorcode = OK,
471 .config = check_nt_config_init(),
472 };
473
485 /* no options were supplied */ 474 /* no options were supplied */
486 if (argc < 2) { 475 if (argc < 2) {
487 return ERROR; 476 result.errorcode = ERROR;
477 return result;
488 } 478 }
489 479
490 /* backwards compatibility */ 480 /* backwards compatibility */
491 if (!is_option(argv[1])) { 481 if (!is_option(argv[1])) {
492 server_address = strdup(argv[1]); 482 result.config.server_address = strdup(argv[1]);
493 argv[1] = argv[0]; 483 argv[1] = argv[0];
494 argv = &argv[1]; 484 argv = &argv[1];
495 argc--; 485 argc--;
@@ -523,60 +513,62 @@ int process_arguments(int argc, char **argv) {
523 print_revision(progname, NP_VERSION); 513 print_revision(progname, NP_VERSION);
524 exit(STATE_UNKNOWN); 514 exit(STATE_UNKNOWN);
525 case 'H': /* hostname */ 515 case 'H': /* hostname */
526 server_address = optarg; 516 result.config.server_address = optarg;
527 break; 517 break;
528 case 's': /* password */ 518 case 's': /* password */
529 req_password = optarg; 519 result.config.req_password = optarg;
530 break; 520 break;
531 case 'p': /* port */ 521 case 'p': /* port */
532 if (is_intnonneg(optarg)) { 522 if (is_intnonneg(optarg)) {
533 server_port = atoi(optarg); 523 result.config.server_port = atoi(optarg);
534 } else { 524 } else {
535 die(STATE_UNKNOWN, _("Server port must be an integer\n")); 525 die(STATE_UNKNOWN, _("Server port must be an integer\n"));
536 } 526 }
537 break; 527 break;
538 case 'v': 528 case 'v':
539 if (strlen(optarg) < 4) { 529 if (strlen(optarg) < 4) {
540 return ERROR; 530 result.errorcode = ERROR;
531 return result;
541 } 532 }
542 if (!strcmp(optarg, "CLIENTVERSION")) { 533 if (!strcmp(optarg, "CLIENTVERSION")) {
543 vars_to_check = CHECK_CLIENTVERSION; 534 result.config.vars_to_check = CHECK_CLIENTVERSION;
544 } else if (!strcmp(optarg, "CPULOAD")) { 535 } else if (!strcmp(optarg, "CPULOAD")) {
545 vars_to_check = CHECK_CPULOAD; 536 result.config.vars_to_check = CHECK_CPULOAD;
546 } else if (!strcmp(optarg, "UPTIME")) { 537 } else if (!strcmp(optarg, "UPTIME")) {
547 vars_to_check = CHECK_UPTIME; 538 result.config.vars_to_check = CHECK_UPTIME;
548 } else if (!strcmp(optarg, "USEDDISKSPACE")) { 539 } else if (!strcmp(optarg, "USEDDISKSPACE")) {
549 vars_to_check = CHECK_USEDDISKSPACE; 540 result.config.vars_to_check = CHECK_USEDDISKSPACE;
550 } else if (!strcmp(optarg, "SERVICESTATE")) { 541 } else if (!strcmp(optarg, "SERVICESTATE")) {
551 vars_to_check = CHECK_SERVICESTATE; 542 result.config.vars_to_check = CHECK_SERVICESTATE;
552 } else if (!strcmp(optarg, "PROCSTATE")) { 543 } else if (!strcmp(optarg, "PROCSTATE")) {
553 vars_to_check = CHECK_PROCSTATE; 544 result.config.vars_to_check = CHECK_PROCSTATE;
554 } else if (!strcmp(optarg, "MEMUSE")) { 545 } else if (!strcmp(optarg, "MEMUSE")) {
555 vars_to_check = CHECK_MEMUSE; 546 result.config.vars_to_check = CHECK_MEMUSE;
556 } else if (!strcmp(optarg, "COUNTER")) { 547 } else if (!strcmp(optarg, "COUNTER")) {
557 vars_to_check = CHECK_COUNTER; 548 result.config.vars_to_check = CHECK_COUNTER;
558 } else if (!strcmp(optarg, "FILEAGE")) { 549 } else if (!strcmp(optarg, "FILEAGE")) {
559 vars_to_check = CHECK_FILEAGE; 550 result.config.vars_to_check = CHECK_FILEAGE;
560 } else if (!strcmp(optarg, "INSTANCES")) { 551 } else if (!strcmp(optarg, "INSTANCES")) {
561 vars_to_check = CHECK_INSTANCES; 552 result.config.vars_to_check = CHECK_INSTANCES;
562 } else { 553 } else {
563 return ERROR; 554 result.errorcode = ERROR;
555 return result;
564 } 556 }
565 break; 557 break;
566 case 'l': /* value list */ 558 case 'l': /* value list */
567 value_list = optarg; 559 result.config.value_list = optarg;
568 break; 560 break;
569 case 'w': /* warning threshold */ 561 case 'w': /* warning threshold */
570 warning_value = strtoul(optarg, NULL, 10); 562 result.config.warning_value = strtoul(optarg, NULL, 10);
571 check_warning_value = true; 563 result.config.check_warning_value = true;
572 break; 564 break;
573 case 'c': /* critical threshold */ 565 case 'c': /* critical threshold */
574 critical_value = strtoul(optarg, NULL, 10); 566 result.config.critical_value = strtoul(optarg, NULL, 10);
575 check_critical_value = true; 567 result.config.check_critical_value = true;
576 break; 568 break;
577 case 'd': /* Display select for services */ 569 case 'd': /* Display select for services */
578 if (!strcmp(optarg, "SHOWALL")) { 570 if (!strcmp(optarg, "SHOWALL")) {
579 show_all = true; 571 result.config.show_all = true;
580 } 572 }
581 break; 573 break;
582 case 'u': 574 case 'u':
@@ -585,23 +577,25 @@ int process_arguments(int argc, char **argv) {
585 case 't': /* timeout */ 577 case 't': /* timeout */
586 socket_timeout = atoi(optarg); 578 socket_timeout = atoi(optarg);
587 if (socket_timeout <= 0) { 579 if (socket_timeout <= 0) {
588 return ERROR; 580 result.errorcode = ERROR;
581 return result;
589 } 582 }
590 } 583 }
591 } 584 }
592 if (server_address == NULL) { 585 if (result.config.server_address == NULL) {
593 usage4(_("You must provide a server address or host name")); 586 usage4(_("You must provide a server address or host name"));
594 } 587 }
595 588
596 if (vars_to_check == CHECK_NONE) { 589 if (result.config.vars_to_check == CHECK_NONE) {
597 return ERROR; 590 result.errorcode = ERROR;
591 return result;
598 } 592 }
599 593
600 if (req_password == NULL) { 594 if (result.config.req_password == NULL) {
601 req_password = strdup(_("None")); 595 result.config.req_password = strdup(_("None"));
602 } 596 }
603 597
604 return OK; 598 return result;
605} 599}
606 600
607void fetch_data(const char *address, int port, const char *sendb) { 601void fetch_data(const char *address, int port, const char *sendb) {
diff --git a/plugins/check_nt.d/config.h b/plugins/check_nt.d/config.h
new file mode 100644
index 00000000..431889cb
--- /dev/null
+++ b/plugins/check_nt.d/config.h
@@ -0,0 +1,53 @@
1#pragma once
2
3#include "../../config.h"
4#include <stddef.h>
5
6enum {
7 PORT = 1248,
8};
9
10enum checkvars {
11 CHECK_NONE,
12 CHECK_CLIENTVERSION,
13 CHECK_CPULOAD,
14 CHECK_UPTIME,
15 CHECK_USEDDISKSPACE,
16 CHECK_SERVICESTATE,
17 CHECK_PROCSTATE,
18 CHECK_MEMUSE,
19 CHECK_COUNTER,
20 CHECK_FILEAGE,
21 CHECK_INSTANCES
22};
23
24typedef struct {
25 char *server_address;
26 int server_port;
27 char *req_password;
28 enum checkvars vars_to_check;
29 bool show_all;
30 char *value_list;
31 bool check_warning_value;
32 unsigned long warning_value;
33 bool check_critical_value;
34 unsigned long critical_value;
35} check_nt_config;
36
37check_nt_config check_nt_config_init() {
38 check_nt_config tmp = {
39 .server_address = NULL,
40 .server_port = PORT,
41 .req_password = NULL,
42
43 .vars_to_check = CHECK_NONE,
44 .show_all = false,
45 .value_list = NULL,
46
47 .check_warning_value = false,
48 .warning_value = 0,
49 .check_critical_value = false,
50 .critical_value = 0,
51 };
52 return tmp;
53}