summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am1
-rw-r--r--lib/tests/Makefile.am2
-rw-r--r--lib/tests/test_utils.c182
-rw-r--r--lib/tests/var/.gitignore2
-rw-r--r--lib/tests/var/baddate5
-rw-r--r--lib/tests/var/missingdataline4
-rw-r--r--lib/tests/var/oldformat5
-rw-r--r--lib/tests/var/statefile5
-rw-r--r--lib/utils_base.c354
-rw-r--r--lib/utils_base.h35
10 files changed, 593 insertions, 2 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 17e4c1b..99fa591 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -4,6 +4,7 @@ SUBDIRS = . tests
4 4
5noinst_LIBRARIES = libnagiosplug.a 5noinst_LIBRARIES = libnagiosplug.a
6 6
7AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\"
7 8
8libnagiosplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c 9libnagiosplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c
9EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h 10EXTRA_DIST = utils_base.h utils_disk.h utils_tcp.h utils_cmd.h parse_ini.h extra_opts.h
diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am
index 694baf0..06a4b42 100644
--- a/lib/tests/Makefile.am
+++ b/lib/tests/Makefile.am
@@ -5,6 +5,8 @@ noinst_PROGRAMS = @EXTRA_TEST@
5TESTS = @EXTRA_TEST@ 5TESTS = @EXTRA_TEST@
6check_PROGRAMS = @EXTRA_TEST@ 6check_PROGRAMS = @EXTRA_TEST@
7 7
8AM_CPPFLAGS = -DNP_STATE_DIR_PREFIX=\"$(localstatedir)\"
9
8INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins 10INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins
9 11
10EXTRA_PROGRAMS = test_utils test_disk test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3 12EXTRA_PROGRAMS = test_utils test_disk test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index 3a45944..5336907 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -21,6 +21,11 @@
21 21
22#include "tap.h" 22#include "tap.h"
23 23
24#include <sys/types.h>
25#include <sys/stat.h>
26
27#include "utils_base.c"
28
24int 29int
25main (int argc, char **argv) 30main (int argc, char **argv)
26{ 31{
@@ -28,8 +33,31 @@ main (int argc, char **argv)
28 double temp; 33 double temp;
29 thresholds *thresholds = NULL; 34 thresholds *thresholds = NULL;
30 int rc; 35 int rc;
36 char *temp_string;
37 state_key *temp_state_key = NULL;
38 state_data *temp_state_data;
39 time_t current_time;
40 char *temp_filename;
41 FILE *temp_fp;
42
43 plan_tests(141);
44
45 ok( this_nagios_plugin==NULL, "nagios_plugin not initialised");
46
47 np_init( "check_test", argc, argv );
48
49 ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised");
50 ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" );
51
52 ok( this_nagios_plugin->argc==argc, "Argc set" );
53 ok( this_nagios_plugin->argv==argv, "Argv set" );
31 54
32 plan_tests(81+23); 55 np_set_args(0,0);
56
57 ok( this_nagios_plugin->argc==0, "argc changed" );
58 ok( this_nagios_plugin->argv==0, "argv changed" );
59
60 np_set_args(argc, argv);
33 61
34 range = parse_range_string("6"); 62 range = parse_range_string("6");
35 ok( range != NULL, "'6' is valid range"); 63 ok( range != NULL, "'6' is valid range");
@@ -251,5 +279,157 @@ main (int argc, char **argv)
251 test=np_extract_ntpvar("", "foo"); 279 test=np_extract_ntpvar("", "foo");
252 ok(!test, "Empty string return NULL"); 280 ok(!test, "Empty string return NULL");
253 281
282
283 /* This is the result of running ./test_utils */
284 temp_string = (char *) _np_state_generate_key();
285 ok(!strcmp(temp_string, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got hash with exe and no parameters" ) ||
286 diag( "You are probably running in wrong directory. Must run as ./test_utils" );
287
288
289 this_nagios_plugin->argc=4;
290 this_nagios_plugin->argv[0] = "./test_utils";
291 this_nagios_plugin->argv[1] = "here";
292 this_nagios_plugin->argv[2] = "--and";
293 this_nagios_plugin->argv[3] = "now";
294 temp_string = (char *) _np_state_generate_key();
295 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
296
297 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
298 temp_string = (char *) _np_state_calculate_location_prefix();
299 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
300
301 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1);
302 temp_string = (char *) _np_state_calculate_location_prefix();
303 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
304
305 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1);
306 temp_string = (char *) _np_state_calculate_location_prefix();
307 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
308
309
310
311 ok(temp_state_key==NULL, "temp_state_key initially empty");
312
313 this_nagios_plugin->argc=1;
314 this_nagios_plugin->argv[0] = "./test_utils";
315 np_enable_state(NULL, 51);
316 temp_state_key = this_nagios_plugin->state;
317 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
318 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
319
320
321 np_enable_state("allowedchars_in_keyname", 77);
322 temp_state_key = this_nagios_plugin->state;
323 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
324 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" );
325 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" );
326
327
328 /* Don't do this test just yet. Will die */
329 /*
330 np_enable_state("bad^chars$in@here", 77);
331 temp_state_key = this_nagios_plugin->state;
332 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
333 */
334
335 np_enable_state("funnykeyname", 54);
336 temp_state_key = this_nagios_plugin->state;
337 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
338 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
339
340
341
342 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" );
343 ok( temp_state_key->data_version==54, "Version set" );
344
345 temp_state_data = np_state_read(temp_state_key);
346 ok( temp_state_data==NULL, "Got no state data as file does not exist" );
347
348
349/*
350 temp_fp = fopen("var/statefile", "r");
351 if (temp_fp==NULL)
352 printf("Error opening. errno=%d\n", errno);
353 printf("temp_fp=%s\n", temp_fp);
354 ok( _np_state_read_file(temp_fp) == TRUE, "Can read state file" );
355 fclose(temp_fp);
356*/
357
358 temp_state_key->_filename="var/statefile";
359 temp_state_data = np_state_read(temp_state_key);
360 ok( this_nagios_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not");
361 ok( this_nagios_plugin->state->state_data->time==1234567890, "Got time" );
362 ok( !strcmp((char *)this_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" );
363
364 temp_state_key->data_version=53;
365 temp_state_data = np_state_read(temp_state_key);
366 ok( temp_state_data==NULL, "Older data version gives NULL" );
367 temp_state_key->data_version=54;
368
369 temp_state_key->_filename="var/nonexistant";
370 temp_state_data = np_state_read(temp_state_key);
371 ok( temp_state_data==NULL, "Missing file gives NULL" );
372 ok( this_nagios_plugin->state->state_data==NULL, "No state information" );
373
374 temp_state_key->_filename="var/oldformat";
375 temp_state_data = np_state_read(temp_state_key);
376 ok( temp_state_data==NULL, "Old file format gives NULL" );
377
378 temp_state_key->_filename="var/baddate";
379 temp_state_data = np_state_read(temp_state_key);
380 ok( temp_state_data==NULL, "Bad date gives NULL" );
381
382 temp_state_key->_filename="var/missingdataline";
383 temp_state_data = np_state_read(temp_state_key);
384 ok( temp_state_data==NULL, "Missing data line gives NULL" );
385
386
387
388
389 unlink("var/generated");
390 temp_state_key->_filename="var/generated";
391 current_time=1234567890;
392 np_state_write_string(current_time, "String to read");
393 ok(system("cmp var/generated var/statefile")==0, "Generated file same as expected");
394
395
396
397
398 unlink("var/generated_directory/statefile");
399 unlink("var/generated_directory");
400 temp_state_key->_filename="var/generated_directory/statefile";
401 current_time=1234567890;
402 np_state_write_string(current_time, "String to read");
403 ok(system("cmp var/generated_directory/statefile var/statefile")==0, "Have created directory");
404
405 /* This test to check cannot write to dir - can't automate yet */
406 /*
407 unlink("var/generated_bad_dir");
408 mkdir("var/generated_bad_dir", S_IRUSR);
409 np_state_write_string(current_time, "String to read");
410 */
411
412
413 temp_state_key->_filename="var/generated";
414 time(&current_time);
415 np_state_write_string(0, "String to read");
416 temp_state_data = np_state_read(temp_state_key);
417 /* Check time is set to current_time */
418 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
419 ok(this_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
420
421
422 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
423 /*
424 temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write";
425 np_state_write_string(0, "Bad file");
426 */
427
428
429 np_cleanup();
430
431 ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" );
432
254 return exit_status(); 433 return exit_status();
255} 434}
435
diff --git a/lib/tests/var/.gitignore b/lib/tests/var/.gitignore
new file mode 100644
index 0000000..95a59c1
--- /dev/null
+++ b/lib/tests/var/.gitignore
@@ -0,0 +1,2 @@
1generated
2generated_directory/
diff --git a/lib/tests/var/baddate b/lib/tests/var/baddate
new file mode 100644
index 0000000..2fda432
--- /dev/null
+++ b/lib/tests/var/baddate
@@ -0,0 +1,5 @@
1# NP State file
21
354
42147483647
5Date in future!!!!
diff --git a/lib/tests/var/missingdataline b/lib/tests/var/missingdataline
new file mode 100644
index 0000000..d87f73a
--- /dev/null
+++ b/lib/tests/var/missingdataline
@@ -0,0 +1,4 @@
1# NP State file
21
354
41234567890
diff --git a/lib/tests/var/oldformat b/lib/tests/var/oldformat
new file mode 100644
index 0000000..102f8c9
--- /dev/null
+++ b/lib/tests/var/oldformat
@@ -0,0 +1,5 @@
1# NP State file
20
354
41234567890
5String to read
diff --git a/lib/tests/var/statefile b/lib/tests/var/statefile
new file mode 100644
index 0000000..5002139
--- /dev/null
+++ b/lib/tests/var/statefile
@@ -0,0 +1,5 @@
1# NP State file
21
354
41234567890
5String to read
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 4303e15..6de92cb 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -27,6 +27,56 @@
27#include "common.h" 27#include "common.h"
28#include <stdarg.h> 28#include <stdarg.h>
29#include "utils_base.h" 29#include "utils_base.h"
30#include <fcntl.h>
31
32#define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } }
33
34nagios_plugin *this_nagios_plugin=NULL;
35
36void np_init( char *plugin_name, int argc, char **argv ) {
37 if (this_nagios_plugin==NULL) {
38 this_nagios_plugin = malloc(sizeof(nagios_plugin));
39 if (this_nagios_plugin==NULL) {
40 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
41 strerror(errno));
42 }
43 this_nagios_plugin->plugin_name = strdup(plugin_name);
44 if (this_nagios_plugin->plugin_name==NULL)
45 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
46 this_nagios_plugin->argc = argc;
47 this_nagios_plugin->argv = argv;
48 }
49}
50
51void np_set_args( int argc, char **argv ) {
52 if (this_nagios_plugin==NULL)
53 die(STATE_UNKNOWN, _("This requires np_init to be called"));
54
55 this_nagios_plugin->argc = argc;
56 this_nagios_plugin->argv = argv;
57}
58
59
60void np_cleanup() {
61 if (this_nagios_plugin!=NULL) {
62 if(this_nagios_plugin->state!=NULL) {
63 if(this_nagios_plugin->state->state_data) {
64 np_free(this_nagios_plugin->state->state_data->data);
65 np_free(this_nagios_plugin->state->state_data);
66 }
67 np_free(this_nagios_plugin->state->name);
68 np_free(this_nagios_plugin->state);
69 }
70 np_free(this_nagios_plugin->plugin_name);
71 np_free(this_nagios_plugin);
72 }
73 this_nagios_plugin=NULL;
74}
75
76/* Hidden function to get a pointer to this_nagios_plugin for testing */
77void _get_nagios_plugin( nagios_plugin **pointer ){
78 *pointer = this_nagios_plugin;
79}
30 80
31void 81void
32die (int result, const char *fmt, ...) 82die (int result, const char *fmt, ...)
@@ -35,6 +85,9 @@ die (int result, const char *fmt, ...)
35 va_start (ap, fmt); 85 va_start (ap, fmt);
36 vprintf (fmt, ap); 86 vprintf (fmt, ap);
37 va_end (ap); 87 va_end (ap);
88 if(this_nagios_plugin!=NULL) {
89 np_cleanup();
90 }
38 exit (result); 91 exit (result);
39} 92}
40 93
@@ -102,7 +155,7 @@ _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_st
102 thresholds *temp_thresholds = NULL; 155 thresholds *temp_thresholds = NULL;
103 156
104 if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL) 157 if ((temp_thresholds = malloc(sizeof(thresholds))) == NULL)
105 die(STATE_UNKNOWN, _("Cannot allocate memory: %s\n"), 158 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
106 strerror(errno)); 159 strerror(errno));
107 160
108 temp_thresholds->warning = NULL; 161 temp_thresholds->warning = NULL;
@@ -310,3 +363,302 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
310 return value; 363 return value;
311} 364}
312 365
366/*
367 * Returns a string to use as a keyname, based on an md5 hash of argv, thus
368 * hopefully a unique key per service/plugin invocation. Use the extra-opts
369 * parse of argv, so that uniqueness in parameters are reflected there.
370 */
371char *_np_state_generate_key() {
372 struct sha1_ctx ctx;
373 int i;
374 char **argv = this_nagios_plugin->argv;
375 unsigned char result[20];
376 char keyname[41];
377 char *p=NULL;
378
379 sha1_init_ctx(&ctx);
380
381 for(i=0; i<this_nagios_plugin->argc; i++) {
382 sha1_process_bytes(argv[i], strlen(argv[i]), &ctx);
383 }
384
385 sha1_finish_ctx(&ctx, &result);
386
387 for (i=0; i<20; ++i) {
388 sprintf(&keyname[2*i], "%02x", result[i]);
389 }
390 keyname[40]='\0';
391
392 p = strdup(keyname);
393 if(p==NULL) {
394 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
395 }
396 return p;
397}
398
399void _cleanup_state_data() {
400 if (this_nagios_plugin->state->state_data!=NULL) {
401 np_free(this_nagios_plugin->state->state_data->data);
402 np_free(this_nagios_plugin->state->state_data);
403 }
404}
405
406/*
407 * Internal function. Returns either:
408 * envvar NAGIOS_PLUGIN_STATE_DIRECTORY
409 * statically compiled shared state directory
410 */
411char* _np_state_calculate_location_prefix(){
412 char *env_dir;
413
414 env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
415 if(env_dir && env_dir[0] != '\0')
416 return env_dir;
417 return NP_STATE_DIR_PREFIX;
418}
419
420/*
421 * Initiatializer for state routines.
422 * Sets variables. Generates filename. Returns np_state_key. die with
423 * UNKNOWN if exception
424 */
425void np_enable_state(char *keyname, int expected_data_version) {
426 state_key *this_state = NULL;
427 char *temp_filename = NULL;
428 char *temp_keyname = NULL;
429 char *p=NULL;
430
431 if(this_nagios_plugin==NULL)
432 die(STATE_UNKNOWN, _("This requires np_init to be called"));
433
434 this_state = (state_key *) malloc(sizeof(state_key));
435 if(this_state==NULL)
436 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
437 strerror(errno));
438
439 if(keyname==NULL) {
440 temp_keyname = _np_state_generate_key();
441 } else {
442 temp_keyname = strdup(keyname);
443 if(temp_keyname==NULL)
444 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
445 }
446 /* Die if invalid characters used for keyname */
447 p = temp_keyname;
448 while(*p!='\0') {
449 if(! (isalnum(*p) || *p == '_')) {
450 die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'"));
451 }
452 p++;
453 }
454 this_state->name=temp_keyname;
455 this_state->plugin_name=this_nagios_plugin->plugin_name;
456 this_state->data_version=expected_data_version;
457 this_state->state_data=NULL;
458
459 /* Calculate filename */
460 asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name);
461 this_state->_filename=temp_filename;
462
463 this_nagios_plugin->state = this_state;
464}
465
466/*
467 * Will return NULL if no data is available (first run). If key currently
468 * exists, read data. If state file format version is not expected, return
469 * as if no data. Get state data version number and compares to expected.
470 * If numerically lower, then return as no previous state. die with UNKNOWN
471 * if exceptional error.
472 */
473state_data *np_state_read() {
474 state_key *my_state_key;
475 state_data *this_state_data=NULL;
476 FILE *statefile;
477 int c;
478 int rc = FALSE;
479
480 if(this_nagios_plugin==NULL)
481 die(STATE_UNKNOWN, _("This requires np_init to be called"));
482
483 /* Open file. If this fails, no previous state found */
484 statefile = fopen( this_nagios_plugin->state->_filename, "r" );
485 if(statefile!=NULL) {
486
487 this_state_data = (state_data *) malloc(sizeof(state_data));
488 if(this_state_data==NULL)
489 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
490 strerror(errno));
491
492 this_state_data->data=NULL;
493 this_nagios_plugin->state->state_data = this_state_data;
494
495 rc = _np_state_read_file(statefile);
496
497 fclose(statefile);
498 }
499
500 if(rc==FALSE) {
501 _cleanup_state_data();
502 }
503
504 return this_nagios_plugin->state->state_data;
505}
506
507/*
508 * Read the state file
509 */
510int _np_state_read_file(FILE *f) {
511 int c, status=FALSE;
512 size_t pos;
513 char *line;
514 int i;
515 int failure=0;
516 time_t current_time, data_time;
517 enum { STATE_FILE_VERSION, STATE_DATA_VERSION, STATE_DATA_TIME, STATE_DATA_TEXT, STATE_DATA_END } expected=STATE_FILE_VERSION;
518
519 time(&current_time);
520
521 /* Note: This introduces a limit of 1024 bytes in the string data */
522 line = (char *) malloc(1024);
523 if(line==NULL)
524 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
525 strerror(errno));
526
527 while(!failure && (fgets(line,1024,f))!=NULL){
528 pos=strlen(line);
529 if(line[pos-1]=='\n') {
530 line[pos-1]='\0';
531 }
532
533 if(line[0] == '#') continue;
534
535 switch(expected) {
536 case STATE_FILE_VERSION:
537 i=atoi(line);
538 if(i!=NP_STATE_FORMAT_VERSION)
539 failure++;
540 else
541 expected=STATE_DATA_VERSION;
542 break;
543 case STATE_DATA_VERSION:
544 i=atoi(line);
545 if(i != this_nagios_plugin->state->data_version)
546 failure++;
547 else
548 expected=STATE_DATA_TIME;
549 break;
550 case STATE_DATA_TIME:
551 /* If time > now, error */
552 data_time=strtoul(line,NULL,10);
553 if(data_time > current_time)
554 failure++;
555 else {
556 this_nagios_plugin->state->state_data->time = data_time;
557 expected=STATE_DATA_TEXT;
558 }
559 break;
560 case STATE_DATA_TEXT:
561 this_nagios_plugin->state->state_data->data = strdup(line);
562 if(this_nagios_plugin->state->state_data->data==NULL)
563 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
564 expected=STATE_DATA_END;
565 status=TRUE;
566 break;
567 case STATE_DATA_END:
568 ;
569 }
570 }
571
572 np_free(line);
573 return status;
574}
575
576/*
577 * If time=NULL, use current time. Create state file, with state format
578 * version, default text. Writes version, time, and data. Avoid locking
579 * problems - use mv to write and then swap. Possible loss of state data if
580 * two things writing to same key at same time.
581 * Will die with UNKNOWN if errors
582 */
583void np_state_write_string(time_t data_time, char *data_string) {
584 FILE *fp;
585 char *temp_file=NULL;
586 int fd=0, result=0;
587 time_t current_time;
588 size_t len;
589 char *directories=NULL;
590 char *p=NULL;
591
592 if(data_time==0)
593 time(&current_time);
594 else
595 current_time=data_time;
596
597 /* If file doesn't currently exist, create directories */
598 if(access(this_nagios_plugin->state->_filename,F_OK)!=0) {
599 asprintf(&directories, "%s", this_nagios_plugin->state->_filename);
600 if(directories==NULL)
601 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
602 strerror(errno));
603
604 for(p=directories+1; *p; p++) {
605 if(*p=='/') {
606 *p='\0';
607 if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) {
608 /* Can't free this! Otherwise error message is wrong! */
609 /* np_free(directories); */
610 die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories);
611 }
612 *p='/';
613 }
614 }
615 np_free(directories);
616 }
617
618 asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename);
619 if(temp_file==NULL)
620 die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
621 strerror(errno));
622
623 if((fd=mkstemp(temp_file))==-1) {
624 np_free(temp_file);
625 die(STATE_UNKNOWN, _("Cannot create temporary filename"));
626 }
627
628 fp=(FILE *)fdopen(fd,"w");
629 if(fp==NULL) {
630 close(fd);
631 unlink(temp_file);
632 np_free(temp_file);
633 die(STATE_UNKNOWN, _("Unable to open temporary state file"));
634 }
635
636 fprintf(fp,"# NP State file\n");
637 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION);
638 fprintf(fp,"%d\n",this_nagios_plugin->state->data_version);
639 fprintf(fp,"%lu\n",current_time);
640 fprintf(fp,"%s\n",data_string);
641
642 fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP);
643
644 fflush(fp);
645
646 result=fclose(fp);
647
648 fsync(fd);
649
650 if(result!=0) {
651 unlink(temp_file);
652 np_free(temp_file);
653 die(STATE_UNKNOWN, _("Error writing temp file"));
654 }
655
656 if(rename(temp_file, this_nagios_plugin->state->_filename)!=0) {
657 unlink(temp_file);
658 np_free(temp_file);
659 die(STATE_UNKNOWN, _("Cannot rename state temp file"));
660 }
661
662 np_free(temp_file);
663}
664
diff --git a/lib/utils_base.h b/lib/utils_base.h
index 49e40a6..0cb371f 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -2,6 +2,8 @@
2#define _UTILS_BASE_ 2#define _UTILS_BASE_
3/* Header file for nagios plugins utils_base.c */ 3/* Header file for nagios plugins utils_base.c */
4 4
5#include "sha1.h"
6
5/* This file holds header information for thresholds - use this in preference to 7/* This file holds header information for thresholds - use this in preference to
6 individual plugin logic */ 8 individual plugin logic */
7 9
@@ -28,6 +30,30 @@ typedef struct thresholds_struct {
28 range *critical; 30 range *critical;
29 } thresholds; 31 } thresholds;
30 32
33#define NP_STATE_FORMAT_VERSION 1
34
35typedef struct state_data_struct {
36 time_t time;
37 void *data;
38 int length; /* Of binary data */
39 } state_data;
40
41
42typedef struct state_key_struct {
43 char *name;
44 char *plugin_name;
45 int data_version;
46 char *_filename;
47 state_data *state_data;
48 } state_key;
49
50typedef struct np_struct {
51 char *plugin_name;
52 state_key *state;
53 int argc;
54 char **argv;
55 } nagios_plugin;
56
31range *parse_range_string (char *); 57range *parse_range_string (char *);
32int _set_thresholds(thresholds **, char *, char *); 58int _set_thresholds(thresholds **, char *, char *);
33void set_thresholds(thresholds **, char *, char *); 59void set_thresholds(thresholds **, char *, char *);
@@ -67,4 +93,13 @@ char *np_extract_value(const char*, const char*, char);
67 */ 93 */
68#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',') 94#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
69 95
96
97void np_enable_state(char *, int);
98state_data *np_state_read();
99void np_state_write_string(time_t, char *);
100
101void np_init(char *, int argc, char **argv);
102void np_set_args(int argc, char **argv);
103void np_cleanup();
104
70#endif /* _UTILS_BASE_ */ 105#endif /* _UTILS_BASE_ */