[nagiosplug] Convert bad chars to underscore

"Ton Voon" <[email protected]>
Newsgroups gmane.network.nagios.plugins.cvs
Message-ID <[email protected]>
 Module: nagiosplug
 Branch: ton/state
 Commit: 29cf8ff2fea1a8f0e1f56b9f8a07fd7ee68c1770
 Author: Ton Voon <[email protected]>
   Date: Mon Jun 21 13:31:21 2010 +0100
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=29cf8ff

Convert bad chars to underscore

---

 lib/tests/test_utils.c |    6 ++++++
 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 e90d4fb..ffab748 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -298,6 +298,12 @@ main (int argc, char **argv)
 	ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" );
 
 
+	np_enable_state("bad^chars$in@here", 77);
+	temp_state_key = temp_nagios_plugin->state;
+	ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
+	ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
+	ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/bad_chars_in_here"), "Got internal filename" );
+
 	np_enable_state("funnykeyname", 54);
 	temp_state_key = temp_nagios_plugin->state;
 	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 e6b20c8..b86ba73 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -47,6 +47,9 @@ void np_init( char *plugin_name ) {
 void np_cleanup() {
 	if (this_nagios_plugin!=NULL) {
 		if(this_nagios_plugin->state!=NULL) {
+			np_free(this_nagios_plugin->state->state_data->data);
+			np_free(this_nagios_plugin->state->state_data);
+			np_free(this_nagios_plugin->state->name);
 			np_free(this_nagios_plugin->state);
 		}
 		np_free(this_nagios_plugin->plugin_name);
@@ -351,7 +354,7 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
  * parse of argv, so that uniqueness in parameters are reflected there.
  */
 char *_np_state_generate_key() {
-	return "Ahash";
+	return strdup("Ahash");
 }
 
 void _cleanup_state_data() {
@@ -383,6 +386,8 @@ char* _np_state_calculate_location_prefix(){
 void np_enable_state(char *keyname, int expected_data_version) {
 	state_key *this_state = NULL;
 	char *temp_filename = NULL;
+	char *temp_keyname = NULL;
+	char *p=NULL;
 
 	if(this_nagios_plugin==NULL)
 		die(STATE_UNKNOWN, _("This requires np_init to be called"));
@@ -393,15 +398,25 @@ void np_enable_state(char *keyname, int expected_data_version) {
 		die(STATE_UNKNOWN, _("Cannot allocate memory for state key"));
 
 	if(keyname==NULL) {
-		keyname = _np_state_generate_key();
+		temp_keyname = _np_state_generate_key();
+	} else {
+		temp_keyname = strdup(keyname);
+	}
+	/* Convert all non-alphanumerics to _ */
+	p = temp_keyname;
+	while(*p!='\0') {
+		if(! isalnum(*p)) {
+			*p='_';
+		}
+		p++;
 	}
-	this_state->name=keyname;
+	this_state->name=temp_keyname;
 	this_state->plugin_name=this_nagios_plugin->plugin_name;
 	this_state->data_version=expected_data_version;
 	this_state->state_data=NULL;
 
 	/* Calculate filename */
-	asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, keyname);
+	asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name);
 	this_state->_filename=temp_filename;
 
 	this_nagios_plugin->state = this_state;


------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.