diff options
Diffstat (limited to 'lib/utils_base.c')
| -rw-r--r-- | lib/utils_base.c | 111 |
1 files changed, 103 insertions, 8 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c index fba383f7..85505779 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
| @@ -354,6 +354,14 @@ char *_np_state_generate_key() { | |||
| 354 | return "Ahash"; | 354 | return "Ahash"; |
| 355 | } | 355 | } |
| 356 | 356 | ||
| 357 | void _cleanup_state_data() { | ||
| 358 | if (this_nagios_plugin->state->state_data!=NULL) { | ||
| 359 | np_free(this_nagios_plugin->state->state_data->data); | ||
| 360 | printf("***Setting\n"); | ||
| 361 | np_free(this_nagios_plugin->state->state_data); | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 357 | /* | 365 | /* |
| 358 | * Internal function. Returns either: | 366 | * Internal function. Returns either: |
| 359 | * envvar NAGIOS_PLUGIN_STATE_DIRECTORY | 367 | * envvar NAGIOS_PLUGIN_STATE_DIRECTORY |
| @@ -373,7 +381,7 @@ char* _np_state_calculate_location_prefix(){ | |||
| 373 | * Sets variables. Generates filename. Returns np_state_key. die with | 381 | * Sets variables. Generates filename. Returns np_state_key. die with |
| 374 | * UNKNOWN if exception | 382 | * UNKNOWN if exception |
| 375 | */ | 383 | */ |
| 376 | void np_state_init(char *keyname, int expected_data_version) { | 384 | void np_enable_state(char *keyname, int expected_data_version) { |
| 377 | state_key *this_state = NULL; | 385 | state_key *this_state = NULL; |
| 378 | char *temp_filename = NULL; | 386 | char *temp_filename = NULL; |
| 379 | 387 | ||
| @@ -409,18 +417,105 @@ void np_state_init(char *keyname, int expected_data_version) { | |||
| 409 | state_data *np_state_read() { | 417 | state_data *np_state_read() { |
| 410 | state_key *my_state_key; | 418 | state_key *my_state_key; |
| 411 | state_data *this_state_data=NULL; | 419 | state_data *this_state_data=NULL; |
| 412 | int statefile=0; | 420 | FILE *statefile; |
| 421 | int c; | ||
| 422 | int rc = FALSE; | ||
| 413 | 423 | ||
| 414 | my_state_key = this_nagios_plugin->state; | 424 | if(this_nagios_plugin==NULL) |
| 415 | my_state_key->state_data = this_state_data; | 425 | die(STATE_UNKNOWN, _("This requires np_init to be called")); |
| 416 | 426 | ||
| 417 | /* Open file. If this fails, no previous state found */ | 427 | /* Open file. If this fails, no previous state found */ |
| 418 | statefile = open( my_state_key->_filename, O_RDONLY ); | 428 | statefile = fopen( this_nagios_plugin->state->_filename, "r" ); |
| 419 | if(statefile<0) { | 429 | if(statefile!=NULL) { |
| 420 | return NULL; | 430 | |
| 431 | this_state_data = (state_data *) malloc(sizeof(state_data)); | ||
| 432 | |||
| 433 | if(this_state_data==NULL) | ||
| 434 | die(STATE_UNKNOWN, _("Cannot allocate memory for state data")); | ||
| 435 | |||
| 436 | this_nagios_plugin->state->state_data = this_state_data; | ||
| 437 | |||
| 438 | rc = _np_state_read_file(statefile); | ||
| 439 | |||
| 440 | fclose(statefile); | ||
| 441 | } | ||
| 442 | |||
| 443 | if(rc==FALSE) { | ||
| 444 | printf("Called\n"); | ||
| 445 | _cleanup_state_data(); | ||
| 446 | } | ||
| 447 | |||
| 448 | return this_nagios_plugin->state->state_data; | ||
| 449 | } | ||
| 450 | |||
| 451 | /* | ||
| 452 | * Read the state file | ||
| 453 | */ | ||
| 454 | int _np_state_read_file(FILE *f) { | ||
| 455 | int c, status=FALSE; | ||
| 456 | size_t pos; | ||
| 457 | char *line; | ||
| 458 | int i; | ||
| 459 | int failure=0; | ||
| 460 | time_t current_time, data_time; | ||
| 461 | enum { STATE_FILE_VERSION, STATE_DATA_VERSION, STATE_DATA_TIME, STATE_DATA_TEXT, STATE_DATA_END } expected=STATE_FILE_VERSION; | ||
| 462 | |||
| 463 | time(¤t_time); | ||
| 464 | |||
| 465 | /* Note: This introduces a limit of 1024 bytes in the string data */ | ||
| 466 | line = (char *) malloc(1024); | ||
| 467 | |||
| 468 | printf("Here\n"); | ||
| 469 | while(!failure && (fgets(line,1024,f))!=NULL){ | ||
| 470 | pos=strlen(line); | ||
| 471 | if(line[pos-1]=='\n') { | ||
| 472 | line[pos-1]='\0'; | ||
| 473 | } | ||
| 474 | printf("Reading line |%s|\n", line); | ||
| 475 | |||
| 476 | if(line[0] == '#') continue; | ||
| 477 | |||
| 478 | switch(expected) { | ||
| 479 | case STATE_FILE_VERSION: | ||
| 480 | i=atoi(line); | ||
| 481 | //printf("line=|%d|, exp=|%d|\n", i, NP_STATE_FORMAT_VERSION); | ||
| 482 | //if(!strcmp(NP_STATE_FORMAT_VERSION, line)) { | ||
| 483 | if(i!=NP_STATE_FORMAT_VERSION) | ||
| 484 | failure++; | ||
| 485 | else | ||
| 486 | expected=STATE_DATA_VERSION; | ||
| 487 | break; | ||
| 488 | case STATE_DATA_VERSION: | ||
| 489 | i=atoi(line); | ||
| 490 | printf("i=%d, exp=%d\n", i, this_nagios_plugin->state->data_version); | ||
| 491 | if(i != this_nagios_plugin->state->data_version) | ||
| 492 | failure++; | ||
| 493 | else | ||
| 494 | expected=STATE_DATA_TIME; | ||
| 495 | break; | ||
| 496 | case STATE_DATA_TIME: | ||
| 497 | /* If time > now, error */ | ||
| 498 | data_time=strtoul(line,NULL,10); | ||
| 499 | if(data_time > current_time) | ||
| 500 | failure++; | ||
| 501 | else { | ||
| 502 | this_nagios_plugin->state->state_data->time = data_time; | ||
| 503 | expected=STATE_DATA_TEXT; | ||
| 504 | } | ||
| 505 | break; | ||
| 506 | case STATE_DATA_TEXT: | ||
| 507 | this_nagios_plugin->state->state_data->data = strdup(line); | ||
| 508 | expected=STATE_DATA_END; | ||
| 509 | status=TRUE; | ||
| 510 | break; | ||
| 511 | case STATE_DATA_END: | ||
| 512 | ; | ||
| 513 | } | ||
| 421 | } | 514 | } |
| 422 | 515 | ||
| 423 | return this_state_data; | 516 | np_free(line); |
| 517 | printf("Returning status=%d\n", status); | ||
| 518 | return status; | ||
| 424 | } | 519 | } |
| 425 | 520 | ||
| 426 | /* | 521 | /* |
