diff options
author | Ton Voon <ton.voon@opsera.com> | 2010-06-18 17:34:24 +0100 |
---|---|---|
committer | Ton Voon <ton.voon@opsera.com> | 2010-06-18 17:34:24 +0100 |
commit | f58aa8e66bbb4ecf23cf6add7efc574abb733d3a (patch) | |
tree | b68d5933921da5c462ec83e78b3b3589f54b5cd1 /lib | |
parent | 68eb836e527bdead1eea7fc281c95c86eab3164d (diff) | |
download | monitoring-plugins-f58aa8e66bbb4ecf23cf6add7efc574abb733d3a.tar.gz |
Implemented writes to state file
Diffstat (limited to 'lib')
-rw-r--r-- | lib/tests/test_utils.c | 64 | ||||
-rw-r--r-- | lib/utils_base.c | 87 |
2 files changed, 137 insertions, 14 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index d7cdc336..6e04bfc7 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c | |||
@@ -21,6 +21,9 @@ | |||
21 | 21 | ||
22 | #include "tap.h" | 22 | #include "tap.h" |
23 | 23 | ||
24 | #include <sys/types.h> | ||
25 | #include <sys/stat.h> | ||
26 | |||
24 | int | 27 | int |
25 | main (int argc, char **argv) | 28 | main (int argc, char **argv) |
26 | { | 29 | { |
@@ -36,7 +39,7 @@ main (int argc, char **argv) | |||
36 | nagios_plugin *temp_nagios_plugin; | 39 | nagios_plugin *temp_nagios_plugin; |
37 | FILE *temp_fp; | 40 | FILE *temp_fp; |
38 | 41 | ||
39 | plan_tests(81+23); | 42 | plan_tests(134); |
40 | 43 | ||
41 | _get_nagios_plugin( &temp_nagios_plugin ); | 44 | _get_nagios_plugin( &temp_nagios_plugin ); |
42 | ok( temp_nagios_plugin==NULL, "nagios_plugin not initialised"); | 45 | ok( temp_nagios_plugin==NULL, "nagios_plugin not initialised"); |
@@ -295,13 +298,11 @@ main (int argc, char **argv) | |||
295 | ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" ); | 298 | ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" ); |
296 | 299 | ||
297 | 300 | ||
298 | printf("Filename=%s\n", temp_state_key->_filename); | ||
299 | np_enable_state("funnykeyname", 54); | 301 | np_enable_state("funnykeyname", 54); |
300 | temp_state_key = temp_nagios_plugin->state; | 302 | temp_state_key = temp_nagios_plugin->state; |
301 | ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); | 303 | ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); |
302 | ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); | 304 | ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); |
303 | 305 | ||
304 | printf("Filename=%s\n", temp_state_key->_filename); | ||
305 | 306 | ||
306 | 307 | ||
307 | ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" ); | 308 | ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" ); |
@@ -325,20 +326,71 @@ main (int argc, char **argv) | |||
325 | ok( temp_nagios_plugin->state->state_data!=NULL, "Got state data now" ); | 326 | ok( temp_nagios_plugin->state->state_data!=NULL, "Got state data now" ); |
326 | ok( temp_nagios_plugin->state->state_data->time==1234567890, "Got time" ); | 327 | ok( temp_nagios_plugin->state->state_data->time==1234567890, "Got time" ); |
327 | ok( !strcmp((char *)temp_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" ); | 328 | ok( !strcmp((char *)temp_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" ); |
328 | printf("state_data=%s|\n", temp_nagios_plugin->state->state_data->data); | ||
329 | 329 | ||
330 | temp_state_key->data_version=53; | ||
331 | temp_state_data = np_state_read(temp_state_key); | ||
332 | ok( temp_state_data==NULL, "Older data version gives NULL" ); | ||
333 | temp_state_key->data_version=54; | ||
330 | 334 | ||
331 | temp_state_key->_filename="var/nonexistant"; | 335 | temp_state_key->_filename="var/nonexistant"; |
332 | temp_state_data = np_state_read(temp_state_key); | 336 | temp_state_data = np_state_read(temp_state_key); |
333 | ok( temp_state_data==NULL, "Missing file gives NULL" ); | 337 | ok( temp_state_data==NULL, "Missing file gives NULL" ); |
334 | ok( temp_nagios_plugin->state->state_data==NULL, "No state information" ); | 338 | ok( temp_nagios_plugin->state->state_data==NULL, "No state information" ); |
335 | 339 | ||
336 | time(¤t_time); | 340 | temp_state_key->_filename="var/oldformat"; |
337 | np_state_write_string(NULL, "New data"); | 341 | temp_state_data = np_state_read(temp_state_key); |
342 | ok( temp_state_data==NULL, "Old file format gives NULL" ); | ||
343 | |||
344 | temp_state_key->_filename="var/baddate"; | ||
345 | temp_state_data = np_state_read(temp_state_key); | ||
346 | ok( temp_state_data==NULL, "Bad date gives NULL" ); | ||
338 | 347 | ||
348 | temp_state_key->_filename="var/missingdataline"; | ||
349 | temp_state_data = np_state_read(temp_state_key); | ||
350 | ok( temp_state_data==NULL, "Missing data line gives NULL" ); | ||
351 | |||
352 | |||
353 | |||
354 | |||
355 | unlink("var/generated"); | ||
356 | temp_state_key->_filename="var/generated"; | ||
357 | current_time=1234567890; | ||
358 | np_state_write_string(¤t_time, "String to read"); | ||
359 | ok(system("cmp var/generated var/statefile")==0, "Generated file same as expected"); | ||
360 | |||
361 | |||
362 | |||
363 | |||
364 | unlink("var/generated_directory/statefile"); | ||
365 | unlink("var/generated_directory"); | ||
366 | temp_state_key->_filename="var/generated_directory/statefile"; | ||
367 | current_time=1234567890; | ||
368 | np_state_write_string(¤t_time, "String to read"); | ||
369 | ok(system("cmp var/generated_directory/statefile var/statefile")==0, "Have created directory"); | ||
370 | |||
371 | /* This test to check cannot write to dir - can't automate yet */ | ||
372 | /* | ||
373 | unlink("var/generated_bad_dir"); | ||
374 | mkdir("var/generated_bad_dir", S_IRUSR); | ||
375 | np_state_write_string(¤t_time, "String to read"); | ||
376 | */ | ||
377 | |||
378 | |||
379 | temp_state_key->_filename="var/generated"; | ||
380 | time(¤t_time); | ||
381 | np_state_write_string(NULL, "String to read"); | ||
339 | temp_state_data = np_state_read(temp_state_key); | 382 | temp_state_data = np_state_read(temp_state_key); |
340 | /* Check time is set to current_time */ | 383 | /* Check time is set to current_time */ |
384 | ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time"); | ||
385 | ok(temp_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time"); | ||
386 | |||
341 | 387 | ||
388 | /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ | ||
389 | /* | ||
390 | temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write"; | ||
391 | np_state_write_string(NULL, "Bad file"); | ||
392 | */ | ||
393 | |||
342 | 394 | ||
343 | np_cleanup(); | 395 | np_cleanup(); |
344 | ok(temp_state_key==NULL, "temp_state_key cleared"); | 396 | ok(temp_state_key==NULL, "temp_state_key cleared"); |
diff --git a/lib/utils_base.c b/lib/utils_base.c index 85505779..5eaf57bd 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
@@ -357,7 +357,6 @@ char *_np_state_generate_key() { | |||
357 | void _cleanup_state_data() { | 357 | void _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 | */ |
528 | void np_state_write_string(time_t *data_time, char *data_string) { | 522 | void 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(¤t_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 | ||