summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTon Voon <ton.voon@opsera.com>2010-06-21 20:43:33 +0100
committerTon Voon <ton.voon@opsera.com>2010-06-21 20:43:33 +0100
commit9de4938ba85d5afd53e08f6a8e90fa4c477704fc (patch)
tree55da51fbe681f1d130a2242a6f9adc59577f537c
parentbc9c95bdf4b47ab5d39dbdf35165837a4c4140c1 (diff)
downloadmonitoring-plugins-9de4938ba85d5afd53e08f6a8e90fa4c477704fc.tar.gz
Use NP_STATE_DIR_PREFIX as compile variable
-rw-r--r--lib/Makefile.am2
-rw-r--r--lib/tests/Makefile.am3
-rw-r--r--lib/tests/test_utils.c52
-rw-r--r--lib/utils_base.c2
-rw-r--r--lib/utils_base.h1
5 files changed, 32 insertions, 28 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 17e4c1b0..1c7e7e7b 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -4,6 +4,8 @@ SUBDIRS = . tests
4 4
5noinst_LIBRARIES = libnagiosplug.a 5noinst_LIBRARIES = libnagiosplug.a
6 6
7# Not sure how to set this in config.h
8DEFS = -DNP_STATE_DIR_PREFIX='"$(localstatedir)"'
7 9
8libnagiosplug_a_SOURCES = utils_base.c utils_disk.c utils_tcp.c utils_cmd.c 10libnagiosplug_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 11EXTRA_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 694baf00..3112020c 100644
--- a/lib/tests/Makefile.am
+++ b/lib/tests/Makefile.am
@@ -5,6 +5,9 @@ noinst_PROGRAMS = @EXTRA_TEST@
5TESTS = @EXTRA_TEST@ 5TESTS = @EXTRA_TEST@
6check_PROGRAMS = @EXTRA_TEST@ 6check_PROGRAMS = @EXTRA_TEST@
7 7
8# See lib/Makefile.am
9DEFS = -DNP_STATE_DIR_PREFIX='"$(localstatedir)"'
10
8INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins 11INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/gl -I$(top_srcdir)/intl -I$(top_srcdir)/plugins
9 12
10EXTRA_PROGRAMS = test_utils test_disk test_tcp test_cmd test_base64 test_ini1 test_ini3 test_opts1 test_opts2 test_opts3 13EXTRA_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 ccc4a52c..ce79fc64 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -24,6 +24,8 @@
24#include <sys/types.h> 24#include <sys/types.h>
25#include <sys/stat.h> 25#include <sys/stat.h>
26 26
27#include "utils_base.c"
28
27int 29int
28main (int argc, char **argv) 30main (int argc, char **argv)
29{ 31{
@@ -36,19 +38,16 @@ main (int argc, char **argv)
36 state_data *temp_state_data; 38 state_data *temp_state_data;
37 time_t current_time; 39 time_t current_time;
38 char *temp_filename; 40 char *temp_filename;
39 nagios_plugin *temp_nagios_plugin;
40 FILE *temp_fp; 41 FILE *temp_fp;
41 42
42 plan_tests(138); 43 plan_tests(137);
43 44
44 _get_nagios_plugin( &temp_nagios_plugin ); 45 ok( this_nagios_plugin==NULL, "nagios_plugin not initialised");
45 ok( temp_nagios_plugin==NULL, "nagios_plugin not initialised");
46 46
47 np_init( "check_test", argc, argv ); 47 np_init( "check_test", argc, argv );
48 48
49 _get_nagios_plugin( &temp_nagios_plugin ); 49 ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised");
50 ok( temp_nagios_plugin!=NULL, "nagios_plugin now initialised"); 50 ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" );
51 ok( !strcmp(temp_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" );
52 51
53 52
54 range = parse_range_string("6"); 53 range = parse_range_string("6");
@@ -277,21 +276,21 @@ main (int argc, char **argv)
277 ok(!strcmp(temp_string, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got hash with exe and no parameters" ); 276 ok(!strcmp(temp_string, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got hash with exe and no parameters" );
278 277
279 278
280 temp_nagios_plugin->argc=4; 279 this_nagios_plugin->argc=4;
281 temp_nagios_plugin->argv[0] = "./test_utils"; 280 this_nagios_plugin->argv[0] = "./test_utils";
282 temp_nagios_plugin->argv[1] = "here"; 281 this_nagios_plugin->argv[1] = "here";
283 temp_nagios_plugin->argv[2] = "--and"; 282 this_nagios_plugin->argv[2] = "--and";
284 temp_nagios_plugin->argv[3] = "now"; 283 this_nagios_plugin->argv[3] = "now";
285 temp_string = (char *) _np_state_generate_key(); 284 temp_string = (char *) _np_state_generate_key();
286 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" ); 285 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
287 286
288 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); 287 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
289 temp_string = (char *) _np_state_calculate_location_prefix(); 288 temp_string = (char *) _np_state_calculate_location_prefix();
290 ok(!strcmp(temp_string, NP_SHAREDSTATE_DIR), "Got default directory" ); 289 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
291 290
292 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1); 291 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1);
293 temp_string = (char *) _np_state_calculate_location_prefix(); 292 temp_string = (char *) _np_state_calculate_location_prefix();
294 ok(!strcmp(temp_string, NP_SHAREDSTATE_DIR), "Got default directory even with empty string" ); 293 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
295 294
296 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1); 295 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1);
297 temp_string = (char *) _np_state_calculate_location_prefix(); 296 temp_string = (char *) _np_state_calculate_location_prefix();
@@ -301,22 +300,22 @@ main (int argc, char **argv)
301 300
302 ok(temp_state_key==NULL, "temp_state_key initially empty"); 301 ok(temp_state_key==NULL, "temp_state_key initially empty");
303 302
304 temp_nagios_plugin->argc=1; 303 this_nagios_plugin->argc=1;
305 temp_nagios_plugin->argv[0] = "./test_utils"; 304 this_nagios_plugin->argv[0] = "./test_utils";
306 np_enable_state(NULL, 51); 305 np_enable_state(NULL, 51);
307 temp_state_key = temp_nagios_plugin->state; 306 temp_state_key = this_nagios_plugin->state;
308 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 307 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
309 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" ); 308 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
310 309
311 310
312 np_enable_state("bad^chars$in@here", 77); 311 np_enable_state("bad^chars$in@here", 77);
313 temp_state_key = temp_nagios_plugin->state; 312 temp_state_key = this_nagios_plugin->state;
314 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 313 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
315 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" ); 314 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
316 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/bad_chars_in_here"), "Got internal filename" ); 315 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/bad_chars_in_here"), "Got internal filename" );
317 316
318 np_enable_state("funnykeyname", 54); 317 np_enable_state("funnykeyname", 54);
319 temp_state_key = temp_nagios_plugin->state; 318 temp_state_key = this_nagios_plugin->state;
320 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 319 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
321 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); 320 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
322 321
@@ -340,9 +339,9 @@ main (int argc, char **argv)
340 339
341 temp_state_key->_filename="var/statefile"; 340 temp_state_key->_filename="var/statefile";
342 temp_state_data = np_state_read(temp_state_key); 341 temp_state_data = np_state_read(temp_state_key);
343 ok( temp_nagios_plugin->state->state_data!=NULL, "Got state data now" ); 342 ok( this_nagios_plugin->state->state_data!=NULL, "Got state data now" );
344 ok( temp_nagios_plugin->state->state_data->time==1234567890, "Got time" ); 343 ok( this_nagios_plugin->state->state_data->time==1234567890, "Got time" );
345 ok( !strcmp((char *)temp_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" ); 344 ok( !strcmp((char *)this_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" );
346 345
347 temp_state_key->data_version=53; 346 temp_state_key->data_version=53;
348 temp_state_data = np_state_read(temp_state_key); 347 temp_state_data = np_state_read(temp_state_key);
@@ -352,7 +351,7 @@ main (int argc, char **argv)
352 temp_state_key->_filename="var/nonexistant"; 351 temp_state_key->_filename="var/nonexistant";
353 temp_state_data = np_state_read(temp_state_key); 352 temp_state_data = np_state_read(temp_state_key);
354 ok( temp_state_data==NULL, "Missing file gives NULL" ); 353 ok( temp_state_data==NULL, "Missing file gives NULL" );
355 ok( temp_nagios_plugin->state->state_data==NULL, "No state information" ); 354 ok( this_nagios_plugin->state->state_data==NULL, "No state information" );
356 355
357 temp_state_key->_filename="var/oldformat"; 356 temp_state_key->_filename="var/oldformat";
358 temp_state_data = np_state_read(temp_state_key); 357 temp_state_data = np_state_read(temp_state_key);
@@ -399,7 +398,7 @@ main (int argc, char **argv)
399 temp_state_data = np_state_read(temp_state_key); 398 temp_state_data = np_state_read(temp_state_key);
400 /* Check time is set to current_time */ 399 /* Check time is set to current_time */
401 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time"); 400 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
402 ok(temp_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time"); 401 ok(this_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
403 402
404 403
405 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ 404 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
@@ -410,8 +409,9 @@ main (int argc, char **argv)
410 409
411 410
412 np_cleanup(); 411 np_cleanup();
413 ok(temp_state_key==NULL, "temp_state_key cleared"); 412
414 ok( temp_nagios_plugin==NULL, "Reset" ); 413 ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" );
415 414
416 return exit_status(); 415 return exit_status();
417} 416}
417
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 1234e25a..70279178 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -403,7 +403,7 @@ char* _np_state_calculate_location_prefix(){
403 env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); 403 env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
404 if(env_dir && env_dir[0] != '\0') 404 if(env_dir && env_dir[0] != '\0')
405 return env_dir; 405 return env_dir;
406 return NP_SHAREDSTATE_DIR; 406 return NP_STATE_DIR_PREFIX;
407} 407}
408 408
409/* 409/*
diff --git a/lib/utils_base.h b/lib/utils_base.h
index 0037b7b5..2fbdfbe8 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -30,7 +30,6 @@ typedef struct thresholds_struct {
30 range *critical; 30 range *critical;
31 } thresholds; 31 } thresholds;
32 32
33#define NP_SHAREDSTATE_DIR "/tmp"
34#define NP_STATE_FORMAT_VERSION 1 33#define NP_STATE_FORMAT_VERSION 1
35 34
36typedef struct state_data_struct { 35typedef struct state_data_struct {