summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/tests/test_utils.c56
-rw-r--r--lib/utils_base.c81
-rw-r--r--lib/utils_base.h18
3 files changed, 126 insertions, 29 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index 763392a..aae358f 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -33,9 +33,19 @@ main (int argc, char **argv)
33 state_data *temp_state_data; 33 state_data *temp_state_data;
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 37
37 plan_tests(81+23); 38 plan_tests(81+23);
38 39
40 _get_nagios_plugin( &temp_nagios_plugin );
41 ok( temp_nagios_plugin==NULL, "nagios_plugin not initialised");
42
43 np_init( "check_test" );
44 _get_nagios_plugin( &temp_nagios_plugin );
45 ok( temp_nagios_plugin!=NULL, "nagios_plugin now initialised");
46 ok( !strcmp(temp_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" );
47
48
39 range = parse_range_string("6"); 49 range = parse_range_string("6");
40 ok( range != NULL, "'6' is valid range"); 50 ok( range != NULL, "'6' is valid range");
41 ok( range->start == 0, "Start correct"); 51 ok( range->start == 0, "Start correct");
@@ -257,33 +267,63 @@ main (int argc, char **argv)
257 ok(!test, "Empty string return NULL"); 267 ok(!test, "Empty string return NULL");
258 268
259 269
260 temp_string = np_state_generate_key(argv); 270 temp_string = (char *) _np_state_generate_key();
261 ok(!strcmp(temp_string, "Ahash"), "Got hash" ); 271 ok(!strcmp(temp_string, "Ahash"), "Got hash" );
262 272
263 temp_string = _np_state_calculate_location_prefix(); 273
274
275 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
276 temp_string = (char *) _np_state_calculate_location_prefix();
264 ok(!strcmp(temp_string, NP_SHAREDSTATE_DIR), "Got default directory" ); 277 ok(!strcmp(temp_string, NP_SHAREDSTATE_DIR), "Got default directory" );
265 278
279 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1);
280 temp_string = (char *) _np_state_calculate_location_prefix();
281 ok(!strcmp(temp_string, NP_SHAREDSTATE_DIR), "Got default directory even with empty string" );
282
283 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1);
284 temp_string = (char *) _np_state_calculate_location_prefix();
285 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
286
287
288
266 ok(temp_state_key==NULL, "temp_state_key initially empty"); 289 ok(temp_state_key==NULL, "temp_state_key initially empty");
267 temp_state_key = np_state_init("check_test", temp_string, 54); 290
291 np_state_init(NULL, 51);
292 temp_state_key = temp_nagios_plugin->state;
268 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 293 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
269 ok( !strcmp(temp_state_key->name, temp_string), "Got key name" ); 294 ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" );
295
296
297 printf("Filename=%s\n", temp_state_key->_filename);
298 np_state_init("funnykeyname", 54);
299 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->name, "funnykeyname"), "Got key name" );
302
270 printf("Filename=%s\n", temp_state_key->_filename); 303 printf("Filename=%s\n", temp_state_key->_filename);
271 304
272 ok( !strcmp(temp_state_key->_filename, "Tobedone"), "Got internal filename" ); 305
306 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" );
273 ok( temp_state_key->data_version==54, "Version set" ); 307 ok( temp_state_key->data_version==54, "Version set" );
274 308
275 temp_state_data = np_state_read(temp_state_key); 309 temp_state_data = np_state_read(temp_state_key);
276 ok( temp_state_data==NULL, "Got state data" ); 310 ok( temp_state_data==NULL, "Got no state data as file does not exist" );
311
312 temp_state_key->_filename="var/statefile";
313 temp_state_data = np_state_read(temp_state_key);
314 ok( temp_state_data!=NULL, "Got state data now" );
315
277 316
278 time(&current_time); 317 time(&current_time);
279 np_state_write_string(temp_state_key, NULL, "New data"); 318 np_state_write_string(NULL, "New data");
280 319
281 temp_state_data = np_state_read(temp_state_key); 320 temp_state_data = np_state_read(temp_state_key);
282 /* Check time is set to current_time */ 321 /* Check time is set to current_time */
283 322
284 323
285 np_state_cleanup(temp_state_key); 324 np_cleanup();
286 ok(temp_state_key==NULL, "temp_state_key cleared"); 325 ok(temp_state_key==NULL, "temp_state_key cleared");
326 ok( temp_nagios_plugin==NULL, "Reset" );
287 327
288 return exit_status(); 328 return exit_status();
289} 329}
diff --git a/lib/utils_base.c b/lib/utils_base.c
index b717a99..fba383f 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -27,9 +27,39 @@
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>
30 31
31#define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } } 32#define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } }
32 33
34nagios_plugin *this_nagios_plugin=NULL;
35
36void np_init( char *plugin_name ) {
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\n"),
41 strerror(errno));
42 }
43 this_nagios_plugin->plugin_name = strdup(plugin_name);
44 }
45}
46
47void np_cleanup() {
48 if (this_nagios_plugin!=NULL) {
49 if(this_nagios_plugin->state!=NULL) {
50 np_free(this_nagios_plugin->state);
51 }
52 np_free(this_nagios_plugin->plugin_name);
53 np_free(this_nagios_plugin);
54 }
55 this_nagios_plugin=NULL;
56}
57
58/* Hidden function to get a pointer to this_nagios_plugin for testing */
59void _get_nagios_plugin( nagios_plugin **pointer ){
60 *pointer = this_nagios_plugin;
61}
62
33void 63void
34die (int result, const char *fmt, ...) 64die (int result, const char *fmt, ...)
35{ 65{
@@ -37,6 +67,9 @@ die (int result, const char *fmt, ...)
37 va_start (ap, fmt); 67 va_start (ap, fmt);
38 vprintf (fmt, ap); 68 vprintf (fmt, ap);
39 va_end (ap); 69 va_end (ap);
70 if(this_nagios_plugin!=NULL) {
71 np_cleanup();
72 }
40 exit (result); 73 exit (result);
41} 74}
42 75
@@ -317,11 +350,21 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
317 * hopefully a unique key per service/plugin invocation. Use the extra-opts 350 * hopefully a unique key per service/plugin invocation. Use the extra-opts
318 * parse of argv, so that uniqueness in parameters are reflected there. 351 * parse of argv, so that uniqueness in parameters are reflected there.
319 */ 352 */
320char *np_state_generate_key(char **argv) { 353char *_np_state_generate_key() {
321 return "Ahash"; 354 return "Ahash";
322} 355}
323 356
357/*
358 * Internal function. Returns either:
359 * envvar NAGIOS_PLUGIN_STATE_DIRECTORY
360 * statically compiled shared state directory
361 */
324char* _np_state_calculate_location_prefix(){ 362char* _np_state_calculate_location_prefix(){
363 char *env_dir;
364
365 env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
366 if(env_dir && env_dir[0] != '\0')
367 return env_dir;
325 return NP_SHAREDSTATE_DIR; 368 return NP_SHAREDSTATE_DIR;
326} 369}
327 370
@@ -330,24 +373,30 @@ char* _np_state_calculate_location_prefix(){
330 * Sets variables. Generates filename. Returns np_state_key. die with 373 * Sets variables. Generates filename. Returns np_state_key. die with
331 * UNKNOWN if exception 374 * UNKNOWN if exception
332 */ 375 */
333state_key *np_state_init(char *plugin_name, char *keyname, int expected_data_version) { 376void np_state_init(char *keyname, int expected_data_version) {
334 state_key *this_state = NULL; 377 state_key *this_state = NULL;
335 char *temp_filename = NULL; 378 char *temp_filename = NULL;
336 379
380 if(this_nagios_plugin==NULL)
381 die(STATE_UNKNOWN, _("This requires np_init to be called"));
382
337 this_state = (state_key *) malloc(sizeof(state_key)); 383 this_state = (state_key *) malloc(sizeof(state_key));
338 384
339 if(this_state==NULL) 385 if(this_state==NULL)
340 die(STATE_UNKNOWN, _("Cannot allocate memory for state key")); 386 die(STATE_UNKNOWN, _("Cannot allocate memory for state key"));
341 387
388 if(keyname==NULL) {
389 keyname = _np_state_generate_key();
390 }
342 this_state->name=keyname; 391 this_state->name=keyname;
343 this_state->plugin_name=plugin_name; 392 this_state->plugin_name=this_nagios_plugin->plugin_name;
344 this_state->data_version=expected_data_version; 393 this_state->data_version=expected_data_version;
345 394
346 /* Calculate filename */ 395 /* Calculate filename */
347 asprintf(&temp_filename, "%s/%s", _np_state_calculate_location_prefix(), plugin_name); 396 asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, keyname);
348 this_state->_filename=temp_filename; 397 this_state->_filename=temp_filename;
349 398
350 return this_state; 399 this_nagios_plugin->state = this_state;
351} 400}
352 401
353/* 402/*
@@ -357,11 +406,19 @@ state_key *np_state_init(char *plugin_name, char *keyname, int expected_data_ver
357 * If numerically lower, then return as no previous state. die with UNKNOWN 406 * If numerically lower, then return as no previous state. die with UNKNOWN
358 * if exceptional error. 407 * if exceptional error.
359 */ 408 */
360state_data *np_state_read(state_key *my_state_key) { 409state_data *np_state_read() {
410 state_key *my_state_key;
361 state_data *this_state_data=NULL; 411 state_data *this_state_data=NULL;
412 int statefile=0;
413
414 my_state_key = this_nagios_plugin->state;
362 my_state_key->state_data = this_state_data; 415 my_state_key->state_data = this_state_data;
363 416
364 /* Open file */ 417 /* Open file. If this fails, no previous state found */
418 statefile = open( my_state_key->_filename, O_RDONLY );
419 if(statefile<0) {
420 return NULL;
421 }
365 422
366 return this_state_data; 423 return this_state_data;
367} 424}
@@ -373,14 +430,6 @@ state_data *np_state_read(state_key *my_state_key) {
373 * two things writing to same key at same time. 430 * two things writing to same key at same time.
374 * Will die with UNKNOWN if errors 431 * Will die with UNKNOWN if errors
375 */ 432 */
376void np_state_write_string(state_key *my_state_key, time_t *data_time, char *data_string) { 433void np_state_write_string(time_t *data_time, char *data_string) {
377}
378
379/*
380 * Cleanup
381 */
382void np_state_cleanup(state_key *my_state_key) {
383 free(my_state_key);
384 my_state_key=NULL;
385} 434}
386 435
diff --git a/lib/utils_base.h b/lib/utils_base.h
index b6eb57e..6a8af19 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -2,6 +2,7 @@
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
5/* This file holds header information for thresholds - use this in preference to 6/* This file holds header information for thresholds - use this in preference to
6 individual plugin logic */ 7 individual plugin logic */
7 8
@@ -46,6 +47,12 @@ typedef struct state_key_struct {
46 state_data *state_data; 47 state_data *state_data;
47 } state_key; 48 } state_key;
48 49
50typedef struct np_struct {
51 char *plugin_name;
52 state_key *state;
53 char **expanded_argv;
54 } nagios_plugin;
55
49range *parse_range_string (char *); 56range *parse_range_string (char *);
50int _set_thresholds(thresholds **, char *, char *); 57int _set_thresholds(thresholds **, char *, char *);
51void set_thresholds(thresholds **, char *, char *); 58void set_thresholds(thresholds **, char *, char *);
@@ -86,10 +93,11 @@ char *np_extract_value(const char*, const char*, char);
86#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',') 93#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
87 94
88 95
89char *np_state_generate_key(char **); 96void np_state_init(char *, int);
90state_key *np_state_init(char *, char *, int); 97state_data *np_state_read();
91state_data *np_state_read(state_key *); 98void np_state_write_string(time_t *, char *);
92void np_state_write_string(state_key *, time_t *, char *); 99
93void np_state_cleanup(state_key *); 100void np_init(char *);
101void np_cleanup();
94 102
95#endif /* _UTILS_BASE_ */ 103#endif /* _UTILS_BASE_ */