diff options
Diffstat (limited to 'lib/utils_base.c')
| -rw-r--r-- | lib/utils_base.c | 81 |
1 files changed, 65 insertions, 16 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c index b717a99a..fba383f7 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 | ||
| 34 | nagios_plugin *this_nagios_plugin=NULL; | ||
| 35 | |||
| 36 | void 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 | |||
| 47 | void 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 */ | ||
| 59 | void _get_nagios_plugin( nagios_plugin **pointer ){ | ||
| 60 | *pointer = this_nagios_plugin; | ||
| 61 | } | ||
| 62 | |||
| 33 | void | 63 | void |
| 34 | die (int result, const char *fmt, ...) | 64 | die (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 | */ |
| 320 | char *np_state_generate_key(char **argv) { | 353 | char *_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 | */ | ||
| 324 | char* _np_state_calculate_location_prefix(){ | 362 | char* _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 | */ |
| 333 | state_key *np_state_init(char *plugin_name, char *keyname, int expected_data_version) { | 376 | void 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 | */ |
| 360 | state_data *np_state_read(state_key *my_state_key) { | 409 | state_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 | */ |
| 376 | void np_state_write_string(state_key *my_state_key, time_t *data_time, char *data_string) { | 433 | void np_state_write_string(time_t *data_time, char *data_string) { |
| 377 | } | ||
| 378 | |||
| 379 | /* | ||
| 380 | * Cleanup | ||
| 381 | */ | ||
| 382 | void np_state_cleanup(state_key *my_state_key) { | ||
| 383 | free(my_state_key); | ||
| 384 | my_state_key=NULL; | ||
| 385 | } | 434 | } |
| 386 | 435 | ||
