summaryrefslogtreecommitdiffstats
path: root/lib/utils_base.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/utils_base.c')
-rw-r--r--lib/utils_base.c87
1 files changed, 79 insertions, 8 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 8550577..5eaf57b 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -357,7 +357,6 @@ char *_np_state_generate_key() {
357void _cleanup_state_data() { 357void _cleanup_state_data() {
358 if (this_nagios_plugin->state->state_data!=NULL) { 358 if (this_nagios_plugin->state->state_data!=NULL) {
359 np_free(this_nagios_plugin->state->state_data->data); 359 np_free(this_nagios_plugin->state->state_data->data);
360 printf("***Setting\n");
361 np_free(this_nagios_plugin->state->state_data); 360 np_free(this_nagios_plugin->state->state_data);
362 } 361 }
363} 362}
@@ -399,6 +398,7 @@ void np_enable_state(char *keyname, int expected_data_version) {
399 this_state->name=keyname; 398 this_state->name=keyname;
400 this_state->plugin_name=this_nagios_plugin->plugin_name; 399 this_state->plugin_name=this_nagios_plugin->plugin_name;
401 this_state->data_version=expected_data_version; 400 this_state->data_version=expected_data_version;
401 this_state->state_data=NULL;
402 402
403 /* Calculate filename */ 403 /* Calculate filename */
404 asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, keyname); 404 asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, keyname);
@@ -433,6 +433,7 @@ state_data *np_state_read() {
433 if(this_state_data==NULL) 433 if(this_state_data==NULL)
434 die(STATE_UNKNOWN, _("Cannot allocate memory for state data")); 434 die(STATE_UNKNOWN, _("Cannot allocate memory for state data"));
435 435
436 this_state_data->data=NULL;
436 this_nagios_plugin->state->state_data = this_state_data; 437 this_nagios_plugin->state->state_data = this_state_data;
437 438
438 rc = _np_state_read_file(statefile); 439 rc = _np_state_read_file(statefile);
@@ -441,7 +442,6 @@ state_data *np_state_read() {
441 } 442 }
442 443
443 if(rc==FALSE) { 444 if(rc==FALSE) {
444 printf("Called\n");
445 _cleanup_state_data(); 445 _cleanup_state_data();
446 } 446 }
447 447
@@ -465,21 +465,17 @@ int _np_state_read_file(FILE *f) {
465 /* Note: This introduces a limit of 1024 bytes in the string data */ 465 /* Note: This introduces a limit of 1024 bytes in the string data */
466 line = (char *) malloc(1024); 466 line = (char *) malloc(1024);
467 467
468 printf("Here\n");
469 while(!failure && (fgets(line,1024,f))!=NULL){ 468 while(!failure && (fgets(line,1024,f))!=NULL){
470 pos=strlen(line); 469 pos=strlen(line);
471 if(line[pos-1]=='\n') { 470 if(line[pos-1]=='\n') {
472 line[pos-1]='\0'; 471 line[pos-1]='\0';
473 } 472 }
474 printf("Reading line |%s|\n", line);
475 473
476 if(line[0] == '#') continue; 474 if(line[0] == '#') continue;
477 475
478 switch(expected) { 476 switch(expected) {
479 case STATE_FILE_VERSION: 477 case STATE_FILE_VERSION:
480 i=atoi(line); 478 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) 479 if(i!=NP_STATE_FORMAT_VERSION)
484 failure++; 480 failure++;
485 else 481 else
@@ -487,7 +483,6 @@ int _np_state_read_file(FILE *f) {
487 break; 483 break;
488 case STATE_DATA_VERSION: 484 case STATE_DATA_VERSION:
489 i=atoi(line); 485 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) 486 if(i != this_nagios_plugin->state->data_version)
492 failure++; 487 failure++;
493 else 488 else
@@ -514,7 +509,6 @@ int _np_state_read_file(FILE *f) {
514 } 509 }
515 510
516 np_free(line); 511 np_free(line);
517 printf("Returning status=%d\n", status);
518 return status; 512 return status;
519} 513}
520 514
@@ -526,5 +520,82 @@ int _np_state_read_file(FILE *f) {
526 * Will die with UNKNOWN if errors 520 * Will die with UNKNOWN if errors
527 */ 521 */
528void np_state_write_string(time_t *data_time, char *data_string) { 522void np_state_write_string(time_t *data_time, char *data_string) {
523 FILE *fp;
524 char *temp_file=NULL;
525 int fd=0, result=0;
526 time_t current_time;
527 size_t len;
528 char *directories=NULL;
529 char *p=NULL;
530
531 if(data_time==NULL)
532 time(&current_time);
533 else
534 current_time=*data_time;
535
536 /* If file doesn't currently exist, create directories */
537 if(access(this_nagios_plugin->state->_filename,F_OK)!=0) {
538 asprintf(&directories, "%s", this_nagios_plugin->state->_filename);
539 if(directories==NULL)
540 die(STATE_UNKNOWN, _("Cannot malloc"));
541
542 for(p=directories+1; *p; p++) {
543 if(*p=='/') {
544 *p='\0';
545 if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) {
546 /* Can't free this! Otherwise error message is wrong! */
547 /* np_free(directories); */
548 die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories);
549 }
550 *p='/';
551 }
552 }
553 np_free(directories);
554 }
555
556 asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename);
557 if(temp_file==NULL)
558 die(STATE_UNKNOWN, _("Cannot malloc temporary state file"));
559
560 if((fd=mkstemp(temp_file))==-1) {
561 np_free(temp_file);
562 die(STATE_UNKNOWN, _("Cannot create temporary filename"));
563 }
564
565 fp=(FILE *)fdopen(fd,"w");
566 if(fp==NULL) {
567 close(fd);
568 unlink(temp_file);
569 np_free(temp_file);
570 die(STATE_UNKNOWN, _("Unable to open temporary state file"));
571 }
572
573 fprintf(fp,"# NP State file\n");
574 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION);
575 fprintf(fp,"%d\n",this_nagios_plugin->state->data_version);
576 fprintf(fp,"%lu\n",current_time);
577 fprintf(fp,"%s\n",data_string);
578
579 fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP);
580
581 fflush(fp);
582
583 result=fclose(fp);
584
585 fsync(fd);
586
587 if(result!=0) {
588 unlink(temp_file);
589 np_free(temp_file);
590 die(STATE_UNKNOWN, _("Error writing temp file"));
591 }
592
593 if(rename(temp_file, this_nagios_plugin->state->_filename)!=0) {
594 unlink(temp_file);
595 np_free(temp_file);
596 die(STATE_UNKNOWN, _("Cannot rename state temp file"));
597 }
598
599 np_free(temp_file);
529} 600}
530 601