diff options
Diffstat (limited to 'lib/utils_base.c')
-rw-r--r-- | lib/utils_base.c | 112 |
1 files changed, 65 insertions, 47 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c index c458cf61..f86efbeb 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
@@ -6,21 +6,21 @@ | |||
6 | * Copyright (c) 2006 Monitoring Plugins Development Team | 6 | * Copyright (c) 2006 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Library of useful functions for plugins | 8 | * Library of useful functions for plugins |
9 | * | 9 | * |
10 | * | 10 | * |
11 | * This program is free software: you can redistribute it and/or modify | 11 | * This program is free software: you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License as published by | 12 | * it under the terms of the GNU General Public License as published by |
13 | * the Free Software Foundation, either version 3 of the License, or | 13 | * the Free Software Foundation, either version 3 of the License, or |
14 | * (at your option) any later version. | 14 | * (at your option) any later version. |
15 | * | 15 | * |
16 | * This program is distributed in the hope that it will be useful, | 16 | * This program is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU General Public License for more details. | 19 | * GNU General Public License for more details. |
20 | * | 20 | * |
21 | * You should have received a copy of the GNU General Public License | 21 | * You should have received a copy of the GNU General Public License |
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
23 | * | 23 | * |
24 | * | 24 | * |
25 | *****************************************************************************/ | 25 | *****************************************************************************/ |
26 | 26 | ||
@@ -40,7 +40,7 @@ monitoring_plugin *this_monitoring_plugin=NULL; | |||
40 | unsigned int timeout_state = STATE_CRITICAL; | 40 | unsigned int timeout_state = STATE_CRITICAL; |
41 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; | 41 | unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; |
42 | 42 | ||
43 | int _np_state_read_file(FILE *); | 43 | bool _np_state_read_file(FILE *); |
44 | 44 | ||
45 | void np_init( char *plugin_name, int argc, char **argv ) { | 45 | void np_init( char *plugin_name, int argc, char **argv ) { |
46 | if (this_monitoring_plugin==NULL) { | 46 | if (this_monitoring_plugin==NULL) { |
@@ -105,12 +105,12 @@ die (int result, const char *fmt, ...) | |||
105 | 105 | ||
106 | void set_range_start (range *this, double value) { | 106 | void set_range_start (range *this, double value) { |
107 | this->start = value; | 107 | this->start = value; |
108 | this->start_infinity = FALSE; | 108 | this->start_infinity = false; |
109 | } | 109 | } |
110 | 110 | ||
111 | void set_range_end (range *this, double value) { | 111 | void set_range_end (range *this, double value) { |
112 | this->end = value; | 112 | this->end = value; |
113 | this->end_infinity = FALSE; | 113 | this->end_infinity = false; |
114 | } | 114 | } |
115 | 115 | ||
116 | range | 116 | range |
@@ -124,9 +124,9 @@ range | |||
124 | 124 | ||
125 | /* Set defaults */ | 125 | /* Set defaults */ |
126 | temp_range->start = 0; | 126 | temp_range->start = 0; |
127 | temp_range->start_infinity = FALSE; | 127 | temp_range->start_infinity = false; |
128 | temp_range->end = 0; | 128 | temp_range->end = 0; |
129 | temp_range->end_infinity = TRUE; | 129 | temp_range->end_infinity = true; |
130 | temp_range->alert_on = OUTSIDE; | 130 | temp_range->alert_on = OUTSIDE; |
131 | temp_range->text = strdup(str); | 131 | temp_range->text = strdup(str); |
132 | 132 | ||
@@ -138,7 +138,7 @@ range | |||
138 | end_str = index(str, ':'); | 138 | end_str = index(str, ':'); |
139 | if (end_str != NULL) { | 139 | if (end_str != NULL) { |
140 | if (str[0] == '~') { | 140 | if (str[0] == '~') { |
141 | temp_range->start_infinity = TRUE; | 141 | temp_range->start_infinity = true; |
142 | } else { | 142 | } else { |
143 | start = strtod(str, NULL); /* Will stop at the ':' */ | 143 | start = strtod(str, NULL); /* Will stop at the ':' */ |
144 | set_range_start(temp_range, start); | 144 | set_range_start(temp_range, start); |
@@ -152,8 +152,8 @@ range | |||
152 | set_range_end(temp_range, end); | 152 | set_range_end(temp_range, end); |
153 | } | 153 | } |
154 | 154 | ||
155 | if (temp_range->start_infinity == TRUE || | 155 | if (temp_range->start_infinity == true || |
156 | temp_range->end_infinity == TRUE || | 156 | temp_range->end_infinity == true || |
157 | temp_range->start <= temp_range->end) { | 157 | temp_range->start <= temp_range->end) { |
158 | return temp_range; | 158 | return temp_range; |
159 | } | 159 | } |
@@ -223,31 +223,30 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) { | |||
223 | printf("\n"); | 223 | printf("\n"); |
224 | } | 224 | } |
225 | 225 | ||
226 | /* Returns TRUE if alert should be raised based on the range */ | 226 | /* Returns true if alert should be raised based on the range */ |
227 | int | 227 | bool check_range(double value, range *my_range) |
228 | check_range(double value, range *my_range) | ||
229 | { | 228 | { |
230 | int no = FALSE; | 229 | bool no = false; |
231 | int yes = TRUE; | 230 | bool yes = true; |
232 | 231 | ||
233 | if (my_range->alert_on == INSIDE) { | 232 | if (my_range->alert_on == INSIDE) { |
234 | no = TRUE; | 233 | no = true; |
235 | yes = FALSE; | 234 | yes = false; |
236 | } | 235 | } |
237 | 236 | ||
238 | if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) { | 237 | if (my_range->end_infinity == false && my_range->start_infinity == false) { |
239 | if ((my_range->start <= value) && (value <= my_range->end)) { | 238 | if ((my_range->start <= value) && (value <= my_range->end)) { |
240 | return no; | 239 | return no; |
241 | } else { | 240 | } else { |
242 | return yes; | 241 | return yes; |
243 | } | 242 | } |
244 | } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) { | 243 | } else if (my_range->start_infinity == false && my_range->end_infinity == true) { |
245 | if (my_range->start <= value) { | 244 | if (my_range->start <= value) { |
246 | return no; | 245 | return no; |
247 | } else { | 246 | } else { |
248 | return yes; | 247 | return yes; |
249 | } | 248 | } |
250 | } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) { | 249 | } else if (my_range->start_infinity == true && my_range->end_infinity == false) { |
251 | if (value <= my_range->end) { | 250 | if (value <= my_range->end) { |
252 | return no; | 251 | return no; |
253 | } else { | 252 | } else { |
@@ -263,12 +262,12 @@ int | |||
263 | get_status(double value, thresholds *my_thresholds) | 262 | get_status(double value, thresholds *my_thresholds) |
264 | { | 263 | { |
265 | if (my_thresholds->critical != NULL) { | 264 | if (my_thresholds->critical != NULL) { |
266 | if (check_range(value, my_thresholds->critical) == TRUE) { | 265 | if (check_range(value, my_thresholds->critical) == true) { |
267 | return STATE_CRITICAL; | 266 | return STATE_CRITICAL; |
268 | } | 267 | } |
269 | } | 268 | } |
270 | if (my_thresholds->warning != NULL) { | 269 | if (my_thresholds->warning != NULL) { |
271 | if (check_range(value, my_thresholds->warning) == TRUE) { | 270 | if (check_range(value, my_thresholds->warning) == true) { |
272 | return STATE_WARNING; | 271 | return STATE_WARNING; |
273 | } | 272 | } |
274 | } | 273 | } |
@@ -332,7 +331,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
332 | /* strip leading spaces */ | 331 | /* strip leading spaces */ |
333 | for (; isspace(varlist[0]); varlist++); | 332 | for (; isspace(varlist[0]); varlist++); |
334 | 333 | ||
335 | if (tmp = index(varlist, sep)) { | 334 | if ((tmp = index(varlist, sep))) { |
336 | /* Value is delimited by a comma */ | 335 | /* Value is delimited by a comma */ |
337 | if (tmp-varlist == 0) continue; | 336 | if (tmp-varlist == 0) continue; |
338 | value = (char *)calloc(1, tmp-varlist+1); | 337 | value = (char *)calloc(1, tmp-varlist+1); |
@@ -348,7 +347,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
348 | break; | 347 | break; |
349 | } | 348 | } |
350 | } | 349 | } |
351 | if (tmp = index(varlist, sep)) { | 350 | if ((tmp = index(varlist, sep))) { |
352 | /* More keys, keep going... */ | 351 | /* More keys, keep going... */ |
353 | varlist = tmp + 1; | 352 | varlist = tmp + 1; |
354 | } else { | 353 | } else { |
@@ -402,26 +401,45 @@ int mp_translate_state (char *state_text) { | |||
402 | * parse of argv, so that uniqueness in parameters are reflected there. | 401 | * parse of argv, so that uniqueness in parameters are reflected there. |
403 | */ | 402 | */ |
404 | char *_np_state_generate_key() { | 403 | char *_np_state_generate_key() { |
405 | struct sha256_ctx ctx; | ||
406 | int i; | 404 | int i; |
407 | char **argv = this_monitoring_plugin->argv; | 405 | char **argv = this_monitoring_plugin->argv; |
408 | unsigned char result[20]; | ||
409 | char keyname[41]; | 406 | char keyname[41]; |
410 | char *p=NULL; | 407 | char *p=NULL; |
411 | 408 | ||
412 | sha256_init_ctx(&ctx); | 409 | unsigned char result[256]; |
413 | 410 | ||
411 | #ifdef USE_OPENSSL | ||
412 | /* | ||
413 | * This code path is chosen if openssl is available (which should be the most common | ||
414 | * scenario). Alternatively, the gnulib implementation/ | ||
415 | * | ||
416 | */ | ||
417 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | ||
418 | |||
419 | EVP_DigestInit(ctx, EVP_sha256()); | ||
420 | |||
421 | for(i=0; i<this_monitoring_plugin->argc; i++) { | ||
422 | EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); | ||
423 | } | ||
424 | |||
425 | EVP_DigestFinal(ctx, result, NULL); | ||
426 | #else | ||
427 | |||
428 | struct sha256_ctx ctx; | ||
429 | |||
414 | for(i=0; i<this_monitoring_plugin->argc; i++) { | 430 | for(i=0; i<this_monitoring_plugin->argc; i++) { |
415 | sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); | 431 | sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); |
416 | } | 432 | } |
417 | 433 | ||
418 | sha256_finish_ctx(&ctx, &result); | 434 | sha256_finish_ctx(&ctx, result); |
419 | 435 | #endif // FOUNDOPENSSL | |
436 | |||
420 | for (i=0; i<20; ++i) { | 437 | for (i=0; i<20; ++i) { |
421 | sprintf(&keyname[2*i], "%02x", result[i]); | 438 | sprintf(&keyname[2*i], "%02x", result[i]); |
422 | } | 439 | } |
440 | |||
423 | keyname[40]='\0'; | 441 | keyname[40]='\0'; |
424 | 442 | ||
425 | p = strdup(keyname); | 443 | p = strdup(keyname); |
426 | if(p==NULL) { | 444 | if(p==NULL) { |
427 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | 445 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); |
@@ -446,7 +464,7 @@ char* _np_state_calculate_location_prefix(){ | |||
446 | 464 | ||
447 | /* Do not allow passing MP_STATE_PATH in setuid plugins | 465 | /* Do not allow passing MP_STATE_PATH in setuid plugins |
448 | * for security reasons */ | 466 | * for security reasons */ |
449 | if (mp_suid() == FALSE) { | 467 | if (!mp_suid()) { |
450 | env_dir = getenv("MP_STATE_PATH"); | 468 | env_dir = getenv("MP_STATE_PATH"); |
451 | if(env_dir && env_dir[0] != '\0') | 469 | if(env_dir && env_dir[0] != '\0') |
452 | return env_dir; | 470 | return env_dir; |
@@ -522,7 +540,7 @@ void np_enable_state(char *keyname, int expected_data_version) { | |||
522 | state_data *np_state_read() { | 540 | state_data *np_state_read() { |
523 | state_data *this_state_data=NULL; | 541 | state_data *this_state_data=NULL; |
524 | FILE *statefile; | 542 | FILE *statefile; |
525 | int rc = FALSE; | 543 | bool rc = false; |
526 | 544 | ||
527 | if(this_monitoring_plugin==NULL) | 545 | if(this_monitoring_plugin==NULL) |
528 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | 546 | die(STATE_UNKNOWN, _("This requires np_init to be called")); |
@@ -544,7 +562,7 @@ state_data *np_state_read() { | |||
544 | fclose(statefile); | 562 | fclose(statefile); |
545 | } | 563 | } |
546 | 564 | ||
547 | if(rc==FALSE) { | 565 | if(!rc) { |
548 | _cleanup_state_data(); | 566 | _cleanup_state_data(); |
549 | } | 567 | } |
550 | 568 | ||
@@ -554,8 +572,8 @@ state_data *np_state_read() { | |||
554 | /* | 572 | /* |
555 | * Read the state file | 573 | * Read the state file |
556 | */ | 574 | */ |
557 | int _np_state_read_file(FILE *f) { | 575 | bool _np_state_read_file(FILE *f) { |
558 | int status=FALSE; | 576 | bool status = false; |
559 | size_t pos; | 577 | size_t pos; |
560 | char *line; | 578 | char *line; |
561 | int i; | 579 | int i; |
@@ -609,7 +627,7 @@ int _np_state_read_file(FILE *f) { | |||
609 | if(this_monitoring_plugin->state->state_data->data==NULL) | 627 | if(this_monitoring_plugin->state->state_data->data==NULL) |
610 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | 628 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); |
611 | expected=STATE_DATA_END; | 629 | expected=STATE_DATA_END; |
612 | status=TRUE; | 630 | status=true; |
613 | break; | 631 | break; |
614 | case STATE_DATA_END: | 632 | case STATE_DATA_END: |
615 | ; | 633 | ; |
@@ -621,10 +639,10 @@ int _np_state_read_file(FILE *f) { | |||
621 | } | 639 | } |
622 | 640 | ||
623 | /* | 641 | /* |
624 | * If time=NULL, use current time. Create state file, with state format | 642 | * If time=NULL, use current time. Create state file, with state format |
625 | * version, default text. Writes version, time, and data. Avoid locking | 643 | * version, default text. Writes version, time, and data. Avoid locking |
626 | * problems - use mv to write and then swap. Possible loss of state data if | 644 | * problems - use mv to write and then swap. Possible loss of state data if |
627 | * two things writing to same key at same time. | 645 | * two things writing to same key at same time. |
628 | * Will die with UNKNOWN if errors | 646 | * Will die with UNKNOWN if errors |
629 | */ | 647 | */ |
630 | void np_state_write_string(time_t data_time, char *data_string) { | 648 | void np_state_write_string(time_t data_time, char *data_string) { |
@@ -639,7 +657,7 @@ void np_state_write_string(time_t data_time, char *data_string) { | |||
639 | time(¤t_time); | 657 | time(¤t_time); |
640 | else | 658 | else |
641 | current_time=data_time; | 659 | current_time=data_time; |
642 | 660 | ||
643 | /* If file doesn't currently exist, create directories */ | 661 | /* If file doesn't currently exist, create directories */ |
644 | if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) { | 662 | if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) { |
645 | result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); | 663 | result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); |
@@ -678,15 +696,15 @@ void np_state_write_string(time_t data_time, char *data_string) { | |||
678 | np_free(temp_file); | 696 | np_free(temp_file); |
679 | die(STATE_UNKNOWN, _("Unable to open temporary state file")); | 697 | die(STATE_UNKNOWN, _("Unable to open temporary state file")); |
680 | } | 698 | } |
681 | 699 | ||
682 | fprintf(fp,"# NP State file\n"); | 700 | fprintf(fp,"# NP State file\n"); |
683 | fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION); | 701 | fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION); |
684 | fprintf(fp,"%d\n",this_monitoring_plugin->state->data_version); | 702 | fprintf(fp,"%d\n",this_monitoring_plugin->state->data_version); |
685 | fprintf(fp,"%lu\n",current_time); | 703 | fprintf(fp,"%lu\n",current_time); |
686 | fprintf(fp,"%s\n",data_string); | 704 | fprintf(fp,"%s\n",data_string); |
687 | 705 | ||
688 | fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP); | 706 | fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP); |
689 | 707 | ||
690 | fflush(fp); | 708 | fflush(fp); |
691 | 709 | ||
692 | result=fclose(fp); | 710 | result=fclose(fp); |