summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTon Voon <ton.voon@opsera.com>2010-06-18 13:45:02 (GMT)
committerTon Voon <ton.voon@opsera.com>2010-06-18 13:45:02 (GMT)
commit68eb836e527bdead1eea7fc281c95c86eab3164d (patch)
treef70ea0fc62671c05548172ebbe8a8c7d3db46a80
parent62c95256699bf476207b92ae521577f339d5134d (diff)
downloadmonitoring-plugins-68eb836e527bdead1eea7fc281c95c86eab3164d.tar.gz
More tests
-rw-r--r--lib/tests/test_utils.c25
-rw-r--r--lib/utils_base.c111
-rw-r--r--lib/utils_base.h4
3 files changed, 127 insertions, 13 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index aae358f..d7cdc33 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -34,6 +34,7 @@ main (int argc, char **argv)
34 time_t current_time; 34 time_t current_time;
35 char *temp_filename; 35 char *temp_filename;
36 nagios_plugin *temp_nagios_plugin; 36 nagios_plugin *temp_nagios_plugin;
37 FILE *temp_fp;
37 38
38 plan_tests(81+23); 39 plan_tests(81+23);
39 40
@@ -288,14 +289,14 @@ main (int argc, char **argv)
288 289
289 ok(temp_state_key==NULL, "temp_state_key initially empty"); 290 ok(temp_state_key==NULL, "temp_state_key initially empty");
290 291
291 np_state_init(NULL, 51); 292 np_enable_state(NULL, 51);
292 temp_state_key = temp_nagios_plugin->state; 293 temp_state_key = temp_nagios_plugin->state;
293 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 294 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
294 ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" ); 295 ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" );
295 296
296 297
297 printf("Filename=%s\n", temp_state_key->_filename); 298 printf("Filename=%s\n", temp_state_key->_filename);
298 np_state_init("funnykeyname", 54); 299 np_enable_state("funnykeyname", 54);
299 temp_state_key = temp_nagios_plugin->state; 300 temp_state_key = temp_nagios_plugin->state;
300 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 301 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
301 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); 302 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
@@ -309,11 +310,29 @@ main (int argc, char **argv)
309 temp_state_data = np_state_read(temp_state_key); 310 temp_state_data = np_state_read(temp_state_key);
310 ok( temp_state_data==NULL, "Got no state data as file does not exist" ); 311 ok( temp_state_data==NULL, "Got no state data as file does not exist" );
311 312
313
314/*
315 temp_fp = fopen("var/statefile", "r");
316 if (temp_fp==NULL)
317 printf("Error opening. errno=%d\n", errno);
318 printf("temp_fp=%s\n", temp_fp);
319 ok( _np_state_read_file(temp_fp) == TRUE, "Can read state file" );
320 fclose(temp_fp);
321*/
322
312 temp_state_key->_filename="var/statefile"; 323 temp_state_key->_filename="var/statefile";
313 temp_state_data = np_state_read(temp_state_key); 324 temp_state_data = np_state_read(temp_state_key);
314 ok( temp_state_data!=NULL, "Got state data now" ); 325 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( !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);
315 329
316 330
331 temp_state_key->_filename="var/nonexistant";
332 temp_state_data = np_state_read(temp_state_key);
333 ok( temp_state_data==NULL, "Missing file gives NULL" );
334 ok( temp_nagios_plugin->state->state_data==NULL, "No state information" );
335
317 time(&current_time); 336 time(&current_time);
318 np_state_write_string(NULL, "New data"); 337 np_state_write_string(NULL, "New data");
319 338
diff --git a/lib/utils_base.c b/lib/utils_base.c
index fba383f..8550577 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
357void _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 */
376void np_state_init(char *keyname, int expected_data_version) { 384void 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) {
409state_data *np_state_read() { 417state_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 */
454int _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(&current_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/*
diff --git a/lib/utils_base.h b/lib/utils_base.h
index 6a8af19..12576d7 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -30,7 +30,7 @@ typedef struct thresholds_struct {
30 } thresholds; 30 } thresholds;
31 31
32#define NP_SHAREDSTATE_DIR "/tmp" 32#define NP_SHAREDSTATE_DIR "/tmp"
33#define STATE_FORMAT_VERSION "1" 33#define NP_STATE_FORMAT_VERSION 1
34 34
35typedef struct state_data_struct { 35typedef struct state_data_struct {
36 time_t time; 36 time_t time;
@@ -93,7 +93,7 @@ char *np_extract_value(const char*, const char*, char);
93#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',') 93#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
94 94
95 95
96void np_state_init(char *, int); 96void np_enable_state(char *, int);
97state_data *np_state_read(); 97state_data *np_state_read();
98void np_state_write_string(time_t *, char *); 98void np_state_write_string(time_t *, char *);
99 99