diff options
| author | Ton Voon <ton.voon@opsera.com> | 2010-06-21 13:31:21 +0100 |
|---|---|---|
| committer | Ton Voon <ton.voon@opsera.com> | 2010-06-21 13:31:21 +0100 |
| commit | 29cf8ff2fea1a8f0e1f56b9f8a07fd7ee68c1770 (patch) | |
| tree | 4ef9aeb91cbdad8b8e5623b50e94b0a98507ba2d | |
| parent | 602896277c76298bcbc152312d915dec306eda27 (diff) | |
| download | monitoring-plugins-29cf8ff2fea1a8f0e1f56b9f8a07fd7ee68c1770.tar.gz | |
Convert bad chars to underscore
| -rw-r--r-- | lib/tests/test_utils.c | 6 | ||||
| -rw-r--r-- | lib/utils_base.c | 23 |
2 files changed, 25 insertions, 4 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c index e90d4fb5..ffab7486 100644 --- a/lib/tests/test_utils.c +++ b/lib/tests/test_utils.c | |||
| @@ -298,6 +298,12 @@ main (int argc, char **argv) | |||
| 298 | ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" ); | 298 | ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" ); |
| 299 | 299 | ||
| 300 | 300 | ||
| 301 | np_enable_state("bad^chars$in@here", 77); | ||
| 302 | temp_state_key = temp_nagios_plugin->state; | ||
| 303 | ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); | ||
| 304 | ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" ); | ||
| 305 | ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/bad_chars_in_here"), "Got internal filename" ); | ||
| 306 | |||
| 301 | np_enable_state("funnykeyname", 54); | 307 | np_enable_state("funnykeyname", 54); |
| 302 | temp_state_key = temp_nagios_plugin->state; | 308 | temp_state_key = temp_nagios_plugin->state; |
| 303 | ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); | 309 | ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); |
diff --git a/lib/utils_base.c b/lib/utils_base.c index e6b20c87..b86ba737 100644 --- a/lib/utils_base.c +++ b/lib/utils_base.c | |||
| @@ -47,6 +47,9 @@ void np_init( char *plugin_name ) { | |||
| 47 | void np_cleanup() { | 47 | void np_cleanup() { |
| 48 | if (this_nagios_plugin!=NULL) { | 48 | if (this_nagios_plugin!=NULL) { |
| 49 | if(this_nagios_plugin->state!=NULL) { | 49 | if(this_nagios_plugin->state!=NULL) { |
| 50 | np_free(this_nagios_plugin->state->state_data->data); | ||
| 51 | np_free(this_nagios_plugin->state->state_data); | ||
| 52 | np_free(this_nagios_plugin->state->name); | ||
| 50 | np_free(this_nagios_plugin->state); | 53 | np_free(this_nagios_plugin->state); |
| 51 | } | 54 | } |
| 52 | np_free(this_nagios_plugin->plugin_name); | 55 | np_free(this_nagios_plugin->plugin_name); |
| @@ -351,7 +354,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) { | |||
| 351 | * parse of argv, so that uniqueness in parameters are reflected there. | 354 | * parse of argv, so that uniqueness in parameters are reflected there. |
| 352 | */ | 355 | */ |
| 353 | char *_np_state_generate_key() { | 356 | char *_np_state_generate_key() { |
| 354 | return "Ahash"; | 357 | return strdup("Ahash"); |
| 355 | } | 358 | } |
| 356 | 359 | ||
| 357 | void _cleanup_state_data() { | 360 | void _cleanup_state_data() { |
| @@ -383,6 +386,8 @@ char* _np_state_calculate_location_prefix(){ | |||
| 383 | void np_enable_state(char *keyname, int expected_data_version) { | 386 | void np_enable_state(char *keyname, int expected_data_version) { |
| 384 | state_key *this_state = NULL; | 387 | state_key *this_state = NULL; |
| 385 | char *temp_filename = NULL; | 388 | char *temp_filename = NULL; |
| 389 | char *temp_keyname = NULL; | ||
| 390 | char *p=NULL; | ||
| 386 | 391 | ||
| 387 | if(this_nagios_plugin==NULL) | 392 | if(this_nagios_plugin==NULL) |
| 388 | die(STATE_UNKNOWN, _("This requires np_init to be called")); | 393 | die(STATE_UNKNOWN, _("This requires np_init to be called")); |
| @@ -393,15 +398,25 @@ void np_enable_state(char *keyname, int expected_data_version) { | |||
| 393 | die(STATE_UNKNOWN, _("Cannot allocate memory for state key")); | 398 | die(STATE_UNKNOWN, _("Cannot allocate memory for state key")); |
| 394 | 399 | ||
| 395 | if(keyname==NULL) { | 400 | if(keyname==NULL) { |
| 396 | keyname = _np_state_generate_key(); | 401 | temp_keyname = _np_state_generate_key(); |
| 402 | } else { | ||
| 403 | temp_keyname = strdup(keyname); | ||
| 404 | } | ||
| 405 | /* Convert all non-alphanumerics to _ */ | ||
| 406 | p = temp_keyname; | ||
| 407 | while(*p!='\0') { | ||
| 408 | if(! isalnum(*p)) { | ||
| 409 | *p='_'; | ||
| 410 | } | ||
| 411 | p++; | ||
| 397 | } | 412 | } |
| 398 | this_state->name=keyname; | 413 | this_state->name=temp_keyname; |
| 399 | this_state->plugin_name=this_nagios_plugin->plugin_name; | 414 | this_state->plugin_name=this_nagios_plugin->plugin_name; |
| 400 | this_state->data_version=expected_data_version; | 415 | this_state->data_version=expected_data_version; |
| 401 | this_state->state_data=NULL; | 416 | this_state->state_data=NULL; |
| 402 | 417 | ||
| 403 | /* Calculate filename */ | 418 | /* Calculate filename */ |
| 404 | asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, keyname); | 419 | asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name); |
| 405 | this_state->_filename=temp_filename; | 420 | this_state->_filename=temp_filename; |
| 406 | 421 | ||
| 407 | this_nagios_plugin->state = this_state; | 422 | this_nagios_plugin->state = this_state; |
