diff options
Diffstat (limited to 'lib/utils_base.c')
| -rw-r--r-- | lib/utils_base.c | 117 |
1 files changed, 64 insertions, 53 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c index 54463e92..f5f75067 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | * utils_base.c | 3 | * utils_base.c |
| 4 | * | 4 | * |
| 5 | * License: GPL | 5 | * License: GPL |
| 6 | * Copyright (c) 2006 Nagios 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 | * |
| @@ -30,56 +30,58 @@ | |||
| 30 | #include <ctype.h> | 30 | #include <ctype.h> |
| 31 | #include <fcntl.h> | 31 | #include <fcntl.h> |
| 32 | #include <sys/stat.h> | 32 | #include <sys/stat.h> |
| 33 | #include <unistd.h> | ||
| 34 | #include <sys/types.h> | ||
| 33 | 35 | ||
| 34 | #define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } } | 36 | #define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } } |
| 35 | 37 | ||
| 36 | nagios_plugin *this_nagios_plugin=NULL; | 38 | monitoring_plugin *this_monitoring_plugin=NULL; |
| 37 | 39 | ||
| 38 | int _np_state_read_file(FILE *); | 40 | int _np_state_read_file(FILE *); |
| 39 | 41 | ||
| 40 | void np_init( char *plugin_name, int argc, char **argv ) { | 42 | void np_init( char *plugin_name, int argc, char **argv ) { |
| 41 | if (this_nagios_plugin==NULL) { | 43 | if (this_monitoring_plugin==NULL) { |
| 42 | this_nagios_plugin = calloc(1, sizeof(nagios_plugin)); | 44 | this_monitoring_plugin = calloc(1, sizeof(monitoring_plugin)); |
| 43 | if (this_nagios_plugin==NULL) { | 45 | if (this_monitoring_plugin==NULL) { |
| 44 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), | 46 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), |
| 45 | strerror(errno)); | 47 | strerror(errno)); |
| 46 | } | 48 | } |
| 47 | this_nagios_plugin->plugin_name = strdup(plugin_name); | 49 | this_monitoring_plugin->plugin_name = strdup(plugin_name); |
| 48 | if (this_nagios_plugin->plugin_name==NULL) | 50 | if (this_monitoring_plugin->plugin_name==NULL) |
| 49 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | 51 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); |
| 50 | this_nagios_plugin->argc = argc; | 52 | this_monitoring_plugin->argc = argc; |
| 51 | this_nagios_plugin->argv = argv; | 53 | this_monitoring_plugin->argv = argv; |
| 52 | } | 54 | } |
| 53 | } | 55 | } |
| 54 | 56 | ||
| 55 | void np_set_args( int argc, char **argv ) { | 57 | void np_set_args( int argc, char **argv ) { |
| 56 | if (this_nagios_plugin==NULL) | 58 | if (this_monitoring_plugin==NULL) |
| 57 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | 59 | die(STATE_UNKNOWN, _("This requires np_init to be called")); |
| 58 | 60 | ||
| 59 | this_nagios_plugin->argc = argc; | 61 | this_monitoring_plugin->argc = argc; |
| 60 | this_nagios_plugin->argv = argv; | 62 | this_monitoring_plugin->argv = argv; |
| 61 | } | 63 | } |
| 62 | 64 | ||
| 63 | 65 | ||
| 64 | void np_cleanup() { | 66 | void np_cleanup() { |
| 65 | if (this_nagios_plugin!=NULL) { | 67 | if (this_monitoring_plugin!=NULL) { |
| 66 | if(this_nagios_plugin->state!=NULL) { | 68 | if(this_monitoring_plugin->state!=NULL) { |
| 67 | if(this_nagios_plugin->state->state_data) { | 69 | if(this_monitoring_plugin->state->state_data) { |
| 68 | np_free(this_nagios_plugin->state->state_data->data); | 70 | np_free(this_monitoring_plugin->state->state_data->data); |
| 69 | np_free(this_nagios_plugin->state->state_data); | 71 | np_free(this_monitoring_plugin->state->state_data); |
| 70 | } | 72 | } |
| 71 | np_free(this_nagios_plugin->state->name); | 73 | np_free(this_monitoring_plugin->state->name); |
| 72 | np_free(this_nagios_plugin->state); | 74 | np_free(this_monitoring_plugin->state); |
| 73 | } | 75 | } |
| 74 | np_free(this_nagios_plugin->plugin_name); | 76 | np_free(this_monitoring_plugin->plugin_name); |
| 75 | np_free(this_nagios_plugin); | 77 | np_free(this_monitoring_plugin); |
| 76 | } | 78 | } |
| 77 | this_nagios_plugin=NULL; | 79 | this_monitoring_plugin=NULL; |
| 78 | } | 80 | } |
| 79 | 81 | ||
| 80 | /* Hidden function to get a pointer to this_nagios_plugin for testing */ | 82 | /* Hidden function to get a pointer to this_monitoring_plugin for testing */ |
| 81 | void _get_nagios_plugin( nagios_plugin **pointer ){ | 83 | void _get_monitoring_plugin( monitoring_plugin **pointer ){ |
| 82 | *pointer = this_nagios_plugin; | 84 | *pointer = this_monitoring_plugin; |
| 83 | } | 85 | } |
| 84 | 86 | ||
| 85 | void | 87 | void |
| @@ -89,7 +91,7 @@ die (int result, const char *fmt, ...) | |||
| 89 | va_start (ap, fmt); | 91 | va_start (ap, fmt); |
| 90 | vprintf (fmt, ap); | 92 | vprintf (fmt, ap); |
| 91 | va_end (ap); | 93 | va_end (ap); |
| 92 | if(this_nagios_plugin!=NULL) { | 94 | if(this_monitoring_plugin!=NULL) { |
| 93 | np_cleanup(); | 95 | np_cleanup(); |
| 94 | } | 96 | } |
| 95 | exit (result); | 97 | exit (result); |
| @@ -375,14 +377,14 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
| 375 | char *_np_state_generate_key() { | 377 | char *_np_state_generate_key() { |
| 376 | struct sha1_ctx ctx; | 378 | struct sha1_ctx ctx; |
| 377 | int i; | 379 | int i; |
| 378 | char **argv = this_nagios_plugin->argv; | 380 | char **argv = this_monitoring_plugin->argv; |
| 379 | unsigned char result[20]; | 381 | unsigned char result[20]; |
| 380 | char keyname[41]; | 382 | char keyname[41]; |
| 381 | char *p=NULL; | 383 | char *p=NULL; |
| 382 | 384 | ||
| 383 | sha1_init_ctx(&ctx); | 385 | sha1_init_ctx(&ctx); |
| 384 | 386 | ||
| 385 | for(i=0; i<this_nagios_plugin->argc; i++) { | 387 | for(i=0; i<this_monitoring_plugin->argc; i++) { |
| 386 | sha1_process_bytes(argv[i], strlen(argv[i]), &ctx); | 388 | sha1_process_bytes(argv[i], strlen(argv[i]), &ctx); |
| 387 | } | 389 | } |
| 388 | 390 | ||
| @@ -401,9 +403,9 @@ char *_np_state_generate_key() { | |||
| 401 | } | 403 | } |
| 402 | 404 | ||
| 403 | void _cleanup_state_data() { | 405 | void _cleanup_state_data() { |
| 404 | if (this_nagios_plugin->state->state_data!=NULL) { | 406 | if (this_monitoring_plugin->state->state_data!=NULL) { |
| 405 | np_free(this_nagios_plugin->state->state_data->data); | 407 | np_free(this_monitoring_plugin->state->state_data->data); |
| 406 | np_free(this_nagios_plugin->state->state_data); | 408 | np_free(this_monitoring_plugin->state->state_data); |
| 407 | } | 409 | } |
| 408 | } | 410 | } |
| 409 | 411 | ||
| @@ -415,9 +417,18 @@ void _cleanup_state_data() { | |||
| 415 | char* _np_state_calculate_location_prefix(){ | 417 | char* _np_state_calculate_location_prefix(){ |
| 416 | char *env_dir; | 418 | char *env_dir; |
| 417 | 419 | ||
| 418 | env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); | 420 | /* Do not allow passing MP_STATE_PATH in setuid plugins |
| 419 | if(env_dir && env_dir[0] != '\0') | 421 | * for security reasons */ |
| 420 | return env_dir; | 422 | if (mp_suid() == FALSE) { |
| 423 | env_dir = getenv("MP_STATE_PATH"); | ||
| 424 | if(env_dir && env_dir[0] != '\0') | ||
| 425 | return env_dir; | ||
| 426 | /* This is the former ENV, for backward-compatibility */ | ||
| 427 | env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); | ||
| 428 | if(env_dir && env_dir[0] != '\0') | ||
| 429 | return env_dir; | ||
| 430 | } | ||
| 431 | |||
| 421 | return NP_STATE_DIR_PREFIX; | 432 | return NP_STATE_DIR_PREFIX; |
| 422 | } | 433 | } |
| 423 | 434 | ||
| @@ -432,7 +443,7 @@ void np_enable_state(char *keyname, int expected_data_version) { | |||
| 432 | char *temp_keyname = NULL; | 443 | char *temp_keyname = NULL; |
| 433 | char *p=NULL; | 444 | char *p=NULL; |
| 434 | 445 | ||
| 435 | if(this_nagios_plugin==NULL) | 446 | if(this_monitoring_plugin==NULL) |
| 436 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | 447 | die(STATE_UNKNOWN, _("This requires np_init to be called")); |
| 437 | 448 | ||
| 438 | this_state = (state_key *) calloc(1, sizeof(state_key)); | 449 | this_state = (state_key *) calloc(1, sizeof(state_key)); |
| @@ -456,15 +467,15 @@ void np_enable_state(char *keyname, int expected_data_version) { | |||
| 456 | p++; | 467 | p++; |
| 457 | } | 468 | } |
| 458 | this_state->name=temp_keyname; | 469 | this_state->name=temp_keyname; |
| 459 | this_state->plugin_name=this_nagios_plugin->plugin_name; | 470 | this_state->plugin_name=this_monitoring_plugin->plugin_name; |
| 460 | this_state->data_version=expected_data_version; | 471 | this_state->data_version=expected_data_version; |
| 461 | this_state->state_data=NULL; | 472 | this_state->state_data=NULL; |
| 462 | 473 | ||
| 463 | /* Calculate filename */ | 474 | /* Calculate filename */ |
| 464 | asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name); | 475 | asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_monitoring_plugin->plugin_name, this_state->name); |
| 465 | this_state->_filename=temp_filename; | 476 | this_state->_filename=temp_filename; |
| 466 | 477 | ||
| 467 | this_nagios_plugin->state = this_state; | 478 | this_monitoring_plugin->state = this_state; |
| 468 | } | 479 | } |
| 469 | 480 | ||
| 470 | /* | 481 | /* |
| @@ -479,11 +490,11 @@ state_data *np_state_read() { | |||
| 479 | FILE *statefile; | 490 | FILE *statefile; |
| 480 | int rc = FALSE; | 491 | int rc = FALSE; |
| 481 | 492 | ||
| 482 | if(this_nagios_plugin==NULL) | 493 | if(this_monitoring_plugin==NULL) |
| 483 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | 494 | die(STATE_UNKNOWN, _("This requires np_init to be called")); |
| 484 | 495 | ||
| 485 | /* Open file. If this fails, no previous state found */ | 496 | /* Open file. If this fails, no previous state found */ |
| 486 | statefile = fopen( this_nagios_plugin->state->_filename, "r" ); | 497 | statefile = fopen( this_monitoring_plugin->state->_filename, "r" ); |
| 487 | if(statefile!=NULL) { | 498 | if(statefile!=NULL) { |
| 488 | 499 | ||
| 489 | this_state_data = (state_data *) calloc(1, sizeof(state_data)); | 500 | this_state_data = (state_data *) calloc(1, sizeof(state_data)); |
| @@ -492,7 +503,7 @@ state_data *np_state_read() { | |||
| 492 | strerror(errno)); | 503 | strerror(errno)); |
| 493 | 504 | ||
| 494 | this_state_data->data=NULL; | 505 | this_state_data->data=NULL; |
| 495 | this_nagios_plugin->state->state_data = this_state_data; | 506 | this_monitoring_plugin->state->state_data = this_state_data; |
| 496 | 507 | ||
| 497 | rc = _np_state_read_file(statefile); | 508 | rc = _np_state_read_file(statefile); |
| 498 | 509 | ||
| @@ -503,10 +514,10 @@ state_data *np_state_read() { | |||
| 503 | _cleanup_state_data(); | 514 | _cleanup_state_data(); |
| 504 | } | 515 | } |
| 505 | 516 | ||
| 506 | return this_nagios_plugin->state->state_data; | 517 | return this_monitoring_plugin->state->state_data; |
| 507 | } | 518 | } |
| 508 | 519 | ||
| 509 | /* | 520 | /* |
| 510 | * Read the state file | 521 | * Read the state file |
| 511 | */ | 522 | */ |
| 512 | int _np_state_read_file(FILE *f) { | 523 | int _np_state_read_file(FILE *f) { |
| @@ -544,7 +555,7 @@ int _np_state_read_file(FILE *f) { | |||
| 544 | break; | 555 | break; |
| 545 | case STATE_DATA_VERSION: | 556 | case STATE_DATA_VERSION: |
| 546 | i=atoi(line); | 557 | i=atoi(line); |
| 547 | if(i != this_nagios_plugin->state->data_version) | 558 | if(i != this_monitoring_plugin->state->data_version) |
| 548 | failure++; | 559 | failure++; |
| 549 | else | 560 | else |
| 550 | expected=STATE_DATA_TIME; | 561 | expected=STATE_DATA_TIME; |
| @@ -555,13 +566,13 @@ int _np_state_read_file(FILE *f) { | |||
| 555 | if(data_time > current_time) | 566 | if(data_time > current_time) |
| 556 | failure++; | 567 | failure++; |
| 557 | else { | 568 | else { |
| 558 | this_nagios_plugin->state->state_data->time = data_time; | 569 | this_monitoring_plugin->state->state_data->time = data_time; |
| 559 | expected=STATE_DATA_TEXT; | 570 | expected=STATE_DATA_TEXT; |
| 560 | } | 571 | } |
| 561 | break; | 572 | break; |
| 562 | case STATE_DATA_TEXT: | 573 | case STATE_DATA_TEXT: |
| 563 | this_nagios_plugin->state->state_data->data = strdup(line); | 574 | this_monitoring_plugin->state->state_data->data = strdup(line); |
| 564 | if(this_nagios_plugin->state->state_data->data==NULL) | 575 | if(this_monitoring_plugin->state->state_data->data==NULL) |
| 565 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | 576 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); |
| 566 | expected=STATE_DATA_END; | 577 | expected=STATE_DATA_END; |
| 567 | status=TRUE; | 578 | status=TRUE; |
| @@ -596,8 +607,8 @@ void np_state_write_string(time_t data_time, char *data_string) { | |||
| 596 | current_time=data_time; | 607 | current_time=data_time; |
| 597 | 608 | ||
| 598 | /* If file doesn't currently exist, create directories */ | 609 | /* If file doesn't currently exist, create directories */ |
| 599 | if(access(this_nagios_plugin->state->_filename,F_OK)!=0) { | 610 | if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) { |
| 600 | asprintf(&directories, "%s", this_nagios_plugin->state->_filename); | 611 | asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); |
| 601 | if(directories==NULL) | 612 | if(directories==NULL) |
| 602 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), | 613 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), |
| 603 | strerror(errno)); | 614 | strerror(errno)); |
| @@ -607,7 +618,7 @@ void np_state_write_string(time_t data_time, char *data_string) { | |||
| 607 | *p='\0'; | 618 | *p='\0'; |
| 608 | if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) { | 619 | if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) { |
| 609 | /* Can't free this! Otherwise error message is wrong! */ | 620 | /* Can't free this! Otherwise error message is wrong! */ |
| 610 | /* np_free(directories); */ | 621 | /* np_free(directories); */ |
| 611 | die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories); | 622 | die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories); |
| 612 | } | 623 | } |
| 613 | *p='/'; | 624 | *p='/'; |
| @@ -616,7 +627,7 @@ void np_state_write_string(time_t data_time, char *data_string) { | |||
| 616 | np_free(directories); | 627 | np_free(directories); |
| 617 | } | 628 | } |
| 618 | 629 | ||
| 619 | asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename); | 630 | asprintf(&temp_file,"%s.XXXXXX",this_monitoring_plugin->state->_filename); |
| 620 | if(temp_file==NULL) | 631 | if(temp_file==NULL) |
| 621 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), | 632 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), |
| 622 | strerror(errno)); | 633 | strerror(errno)); |
| @@ -636,7 +647,7 @@ void np_state_write_string(time_t data_time, char *data_string) { | |||
| 636 | 647 | ||
| 637 | fprintf(fp,"# NP State file\n"); | 648 | fprintf(fp,"# NP State file\n"); |
| 638 | fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION); | 649 | fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION); |
| 639 | fprintf(fp,"%d\n",this_nagios_plugin->state->data_version); | 650 | fprintf(fp,"%d\n",this_monitoring_plugin->state->data_version); |
| 640 | fprintf(fp,"%lu\n",current_time); | 651 | fprintf(fp,"%lu\n",current_time); |
| 641 | fprintf(fp,"%s\n",data_string); | 652 | fprintf(fp,"%s\n",data_string); |
| 642 | 653 | ||
| @@ -654,7 +665,7 @@ void np_state_write_string(time_t data_time, char *data_string) { | |||
| 654 | die(STATE_UNKNOWN, _("Error writing temp file")); | 665 | die(STATE_UNKNOWN, _("Error writing temp file")); |
| 655 | } | 666 | } |
| 656 | 667 | ||
| 657 | if(rename(temp_file, this_nagios_plugin->state->_filename)!=0) { | 668 | if(rename(temp_file, this_monitoring_plugin->state->_filename)!=0) { |
| 658 | unlink(temp_file); | 669 | unlink(temp_file); |
| 659 | np_free(temp_file); | 670 | np_free(temp_file); |
| 660 | die(STATE_UNKNOWN, _("Cannot rename state temp file")); | 671 | die(STATE_UNKNOWN, _("Cannot rename state temp file")); |
