[nagiosplug] More tests

"Ton Voon" <[email protected]>
Newsgroups gmane.network.nagios.plugins.cvs
Message-ID <[email protected]>
 Module: nagiosplug
 Branch: ton/state
 Commit: 68eb836e527bdead1eea7fc281c95c86eab3164d
 Author: Ton Voon <[email protected]>
   Date: Fri Jun 18 14:45:02 2010 +0100
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=68eb836

More tests

---

 lib/tests/test_utils.c |   25 +++++++++-
 lib/utils_base.c       |  111 ++++++++++++++++++++++++++++++++++++++++++++---
 lib/utils_base.h       |    4 +-
 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)
 	time_t	current_time;
 	char 	*temp_filename;
 	nagios_plugin *temp_nagios_plugin;
+	FILE    *temp_fp;
 
 	plan_tests(81+23);
 
@@ -288,14 +289,14 @@ main (int argc, char **argv)
 
 	ok(temp_state_key==NULL, "temp_state_key initially empty");
 
-	np_state_init(NULL, 51);
+	np_enable_state(NULL, 51);
 	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, "Ahash"), "Got key name" );
 
 
 	printf("Filename=%s\n", temp_state_key->_filename);
-	np_state_init("funnykeyname", 54);
+	np_enable_state("funnykeyname", 54);
 	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, "funnykeyname"), "Got key name" );
@@ -309,11 +310,29 @@ main (int argc, char **argv)
 	temp_state_data = np_state_read(temp_state_key);
 	ok( temp_state_data==NULL, "Got no state data as file does not exist" );
 
+
+/*
+	temp_fp = fopen("var/statefile", "r");
+	if (temp_fp==NULL) 
+		printf("Error opening. errno=%d\n", errno);
+	printf("temp_fp=%s\n", temp_fp);
+	ok( _np_state_read_file(temp_fp) == TRUE, "Can read state file" );
+	fclose(temp_fp);
+*/
+	
 	temp_state_key->_filename="var/statefile";
 	temp_state_data = np_state_read(temp_state_key);
-	ok( temp_state_data!=NULL, "Got state data now" );
+	ok( temp_nagios_plugin->state->state_data!=NULL, "Got state data now" );
+	ok( temp_nagios_plugin->state->state_data->time==1234567890, "Got time" );
+	ok( !strcmp((char *)temp_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" );
+	printf("state_data=%s|\n", temp_nagios_plugin->state->state_data->data);
 
 
+	temp_state_key->_filename="var/nonexistant";
+	temp_state_data = np_state_read(temp_state_key);
+	ok( temp_state_data==NULL, "Missing file gives NULL" );
+	ok( temp_nagios_plugin->state->state_data==NULL, "No state information" );
+
 	time(&current_time);
 	np_state_write_string(NULL, "New data");
 
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() {
 	return "Ahash";
 }
 
+void _cleanup_state_data() {
+	if (this_nagios_plugin->state->state_data!=NULL) {
+		np_free(this_nagios_plugin->state->state_data->data);
+		printf("***Setting\n");
+		np_free(this_nagios_plugin->state->state_data);
+	}
+}
+
 /*
  * Internal function. Returns either:
  *   envvar NAGIOS_PLUGIN_STATE_DIRECTORY
@@ -373,7 +381,7 @@ char* _np_state_calculate_location_prefix(){
  * Sets variables. Generates filename. Returns np_state_key. die with
  * UNKNOWN if exception
  */
-void np_state_init(char *keyname, int expected_data_version) {
+void np_enable_state(char *keyname, int expected_data_version) {
 	state_key *this_state = NULL;
 	char *temp_filename = NULL;
 
@@ -409,18 +417,105 @@ void np_state_init(char *keyname, int expected_data_version) {
 state_data *np_state_read() {
 	state_key *my_state_key;
 	state_data *this_state_data=NULL;
-	int statefile=0;
+	FILE *statefile;
+	int c;
+	int rc = FALSE;
 
-	my_state_key = this_nagios_plugin->state;
-	my_state_key->state_data = this_state_data;
+	if(this_nagios_plugin==NULL)
+		die(STATE_UNKNOWN, _("This requires np_init to be called"));
 
 	/* Open file. If this fails, no previous state found */
-	statefile = open( my_state_key->_filename, O_RDONLY );
-	if(statefile<0) {
-		return NULL;
+	statefile = fopen( this_nagios_plugin->state->_filename, "r" );
+	if(statefile!=NULL) {
+
+		this_state_data = (state_data *) malloc(sizeof(state_data));
+		
+		if(this_state_data==NULL)
+			die(STATE_UNKNOWN, _("Cannot allocate memory for state data"));
+
+		this_nagios_plugin->state->state_data = this_state_data;
+
+		rc = _np_state_read_file(statefile);
+
+		fclose(statefile);
+	}
+
+	if(rc==FALSE) {
+		printf("Called\n");
+		_cleanup_state_data();
+	}
+
+	return this_nagios_plugin->state->state_data;
+}
+
+/* 
+ * Read the state file
+ */
+int _np_state_read_file(FILE *f) {
+	int c, status=FALSE;
+	size_t pos;
+	char *line;
+	int i;
+	int failure=0;
+	time_t current_time, data_time;
+	enum { STATE_FILE_VERSION, STATE_DATA_VERSION, STATE_DATA_TIME, STATE_DATA_TEXT, STATE_DATA_END } expected=STATE_FILE_VERSION;
+
+	time(&current_time);
+
+	/* Note: This introduces a limit of 1024 bytes in the string data */
+	line = (char *) malloc(1024);
+
+	printf("Here\n");
+	while(!failure && (fgets(line,1024,f))!=NULL){
+		pos=strlen(line);
+		if(line[pos-1]=='\n') {
+			line[pos-1]='\0';
+		}
+		printf("Reading line |%s|\n", line);
+
+		if(line[0] == '#') continue;
+
+		switch(expected) {
+			case STATE_FILE_VERSION:
+				i=atoi(line);
+				//printf("line=|%d|, exp=|%d|\n", i, NP_STATE_FORMAT_VERSION);
+				//if(!strcmp(NP_STATE_FORMAT_VERSION, line)) {
+				if(i!=NP_STATE_FORMAT_VERSION)
+					failure++;
+				else
+					expected=STATE_DATA_VERSION;
+				break;
+			case STATE_DATA_VERSION:
+				i=atoi(line);
+				printf("i=%d, exp=%d\n", i, this_nagios_plugin->state->data_version);
+				if(i != this_nagios_plugin->state->data_version)
+					failure++;
+				else
+					expected=STATE_DATA_TIME;
+				break;
+			case STATE_DATA_TIME:
+				/* If time > now, error */
+				data_time=strtoul(line,NULL,10);
+				if(data_time > current_time)
+					failure++;
+				else {
+					this_nagios_plugin->state->state_data->time = data_time;
+					expected=STATE_DATA_TEXT;
+				}
+				break;
+			case STATE_DATA_TEXT:
+				this_nagios_plugin->state->state_data->data = strdup(line);
+				expected=STATE_DATA_END;
+				status=TRUE;
+				break;
+			case STATE_DATA_END:
+				;
+		}
 	}
 
-	return this_state_data;
+	np_free(line);
+	printf("Returning status=%d\n", status);
+	return status;
 }
 
 /*
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 {
 	} thresholds;
 
 #define NP_SHAREDSTATE_DIR "/tmp"
-#define STATE_FORMAT_VERSION "1"
+#define NP_STATE_FORMAT_VERSION 1
 
 typedef struct state_data_struct {
 	time_t	time;
@@ -93,7 +93,7 @@ char *np_extract_value(const char*, const char*, char);
 #define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
 
 
-void np_state_init(char *, int);
+void np_enable_state(char *, int);
 state_data *np_state_read();
 void np_state_write_string(time_t *, char *);
 


------------------------------------------------------------------------------
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.