[nagiosplug] np_init() with this_nagios_plugin

Ton Voon tonvoon at users.sourceforge.net
Mon Jun 21 14:08:12 CEST 2010


 Module: nagiosplug
 Branch: ton/state
 Commit: 62c95256699bf476207b92ae521577f339d5134d
 Author: Ton Voon <ton.voon at opsera.com>
   Date: Thu Jun 17 23:18:17 2010 +0100
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=62c9525

np_init() with this_nagios_plugin

---

 lib/tests/test_utils.c |   56 ++++++++++++++++++++++++++++-----
 lib/utils_base.c       |   81 ++++++++++++++++++++++++++++++++++++++---------
 lib/utils_base.h       |   18 ++++++++---
 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)
 	state_data *temp_state_data;
 	time_t	current_time;
 	char 	*temp_filename;
+	nagios_plugin *temp_nagios_plugin;
 
 	plan_tests(81+23);
 
+	_get_nagios_plugin( &temp_nagios_plugin );
+	ok( temp_nagios_plugin==NULL, "nagios_plugin not initialised");
+
+	np_init( "check_test" );
+	_get_nagios_plugin( &temp_nagios_plugin );
+	ok( temp_nagios_plugin!=NULL, "nagios_plugin now initialised");
+	ok( !strcmp(temp_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" );
+
+
 	range = parse_range_string("6");
 	ok( range != NULL, "'6' is valid range");
 	ok( range->start == 0, "Start correct");
@@ -257,33 +267,63 @@ main (int argc, char **argv)
 	ok(!test, "Empty string return NULL");
 
 
-	temp_string = np_state_generate_key(argv);
+	temp_string = (char *) _np_state_generate_key();
 	ok(!strcmp(temp_string, "Ahash"), "Got hash" );
 	
-	temp_string = _np_state_calculate_location_prefix();
+
+
+	unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
+	temp_string = (char *) _np_state_calculate_location_prefix();
 	ok(!strcmp(temp_string, NP_SHAREDSTATE_DIR), "Got default directory" );
 
+	setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1);
+	temp_string = (char *) _np_state_calculate_location_prefix();
+	ok(!strcmp(temp_string, NP_SHAREDSTATE_DIR), "Got default directory even with empty string" );
+
+	setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1);
+	temp_string = (char *) _np_state_calculate_location_prefix();
+	ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
+
+
+
 	ok(temp_state_key==NULL, "temp_state_key initially empty");
-	temp_state_key = np_state_init("check_test", temp_string, 54);
+
+	np_state_init(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, temp_string), "Got key name" );
+	ok( !strcmp(temp_state_key->name, "Ahash"), "Got key name" );
+
+
+	printf("Filename=%s\n", temp_state_key->_filename);
+	np_state_init("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" );
+
 	printf("Filename=%s\n", temp_state_key->_filename);
 
-	ok( !strcmp(temp_state_key->_filename, "Tobedone"), "Got internal filename" );
+
+	ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/funnykeyname"), "Got internal filename" );
 	ok( temp_state_key->data_version==54, "Version set" );
 
 	temp_state_data = np_state_read(temp_state_key);
-	ok( temp_state_data==NULL, "Got state data" );
+	ok( temp_state_data==NULL, "Got no state data as file does not exist" );
+
+	temp_state_key->_filename="var/statefile";
+	temp_state_data = np_state_read(temp_state_key);
+	ok( temp_state_data!=NULL, "Got state data now" );
+
 
 	time(&current_time);
-	np_state_write_string(temp_state_key, NULL, "New data");
+	np_state_write_string(NULL, "New data");
 
 	temp_state_data = np_state_read(temp_state_key);
 	/* Check time is set to current_time */
 
 
-	np_state_cleanup(temp_state_key);
+	np_cleanup();
 	ok(temp_state_key==NULL, "temp_state_key cleared");
+	ok( temp_nagios_plugin==NULL, "Reset" );
 
 	return exit_status();
 }
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 @@
 #include "common.h"
 #include <stdarg.h>
 #include "utils_base.h"
+#include <fcntl.h>
 
 #define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } }
 
+nagios_plugin *this_nagios_plugin=NULL;
+
+void np_init( char *plugin_name ) {
+	if (this_nagios_plugin==NULL) {
+		this_nagios_plugin = malloc(sizeof(nagios_plugin));
+		if (this_nagios_plugin==NULL) {
+			die(STATE_UNKNOWN, _("Cannot allocate memory: %s\n"),
+			    strerror(errno));
+		}
+		this_nagios_plugin->plugin_name = strdup(plugin_name);
+	}
+}
+
+void np_cleanup() {
+	if (this_nagios_plugin!=NULL) {
+		if(this_nagios_plugin->state!=NULL) {
+			np_free(this_nagios_plugin->state);
+		}
+		np_free(this_nagios_plugin->plugin_name);
+		np_free(this_nagios_plugin);
+	}
+	this_nagios_plugin=NULL;
+}
+
+/* Hidden function to get a pointer to this_nagios_plugin for testing */
+void _get_nagios_plugin( nagios_plugin **pointer ){
+	*pointer = this_nagios_plugin;
+}
+
 void
 die (int result, const char *fmt, ...)
 {
@@ -37,6 +67,9 @@ die (int result, const char *fmt, ...)
 	va_start (ap, fmt);
 	vprintf (fmt, ap);
 	va_end (ap);
+	if(this_nagios_plugin!=NULL) {
+		np_cleanup();
+	}
 	exit (result);
 }
 
@@ -317,11 +350,21 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
  * hopefully a unique key per service/plugin invocation. Use the extra-opts
  * parse of argv, so that uniqueness in parameters are reflected there.
  */
-char *np_state_generate_key(char **argv) {
+char *_np_state_generate_key() {
 	return "Ahash";
 }
 
+/*
+ * Internal function. Returns either:
+ *   envvar NAGIOS_PLUGIN_STATE_DIRECTORY
+ *   statically compiled shared state directory
+ */
 char* _np_state_calculate_location_prefix(){
+	char *env_dir;
+
+	env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
+	if(env_dir && env_dir[0] != '\0')
+		return env_dir;
 	return NP_SHAREDSTATE_DIR;
 }
 
@@ -330,24 +373,30 @@ char* _np_state_calculate_location_prefix(){
  * Sets variables. Generates filename. Returns np_state_key. die with
  * UNKNOWN if exception
  */
-state_key *np_state_init(char *plugin_name, char *keyname, int expected_data_version) {
+void np_state_init(char *keyname, int expected_data_version) {
 	state_key *this_state = NULL;
 	char *temp_filename = NULL;
 
+	if(this_nagios_plugin==NULL)
+		die(STATE_UNKNOWN, _("This requires np_init to be called"));
+
 	this_state = (state_key *) malloc(sizeof(state_key));
 	
 	if(this_state==NULL)
 		die(STATE_UNKNOWN, _("Cannot allocate memory for state key"));
 
+	if(keyname==NULL) {
+		keyname = _np_state_generate_key();
+	}
 	this_state->name=keyname;
-	this_state->plugin_name=plugin_name;
+	this_state->plugin_name=this_nagios_plugin->plugin_name;
 	this_state->data_version=expected_data_version;
 
 	/* Calculate filename */
-	asprintf(&temp_filename, "%s/%s", _np_state_calculate_location_prefix(), plugin_name);
+	asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, keyname);
 	this_state->_filename=temp_filename;
 
-	return this_state;
+	this_nagios_plugin->state = this_state;
 }
 
 /*
@@ -357,11 +406,19 @@ state_key *np_state_init(char *plugin_name, char *keyname, int expected_data_ver
  * If numerically lower, then return as no previous state. die with UNKNOWN
  * if exceptional error.
  */
-state_data *np_state_read(state_key *my_state_key) {
+state_data *np_state_read() {
+	state_key *my_state_key;
 	state_data *this_state_data=NULL;
+	int statefile=0;
+
+	my_state_key = this_nagios_plugin->state;
 	my_state_key->state_data = this_state_data;
 
-	/* Open file */
+	/* Open file. If this fails, no previous state found */
+	statefile = open( my_state_key->_filename, O_RDONLY );
+	if(statefile<0) {
+		return NULL;
+	}
 
 	return this_state_data;
 }
@@ -373,14 +430,6 @@ state_data *np_state_read(state_key *my_state_key) {
  * two things writing to same key at same time. 
  * Will die with UNKNOWN if errors
  */
-void np_state_write_string(state_key *my_state_key, time_t *data_time, char *data_string) {
-}
-
-/*
- * Cleanup
- */
-void np_state_cleanup(state_key *my_state_key) {
-	free(my_state_key);
-	my_state_key=NULL;
+void np_state_write_string(time_t *data_time, char *data_string) {
 }
 
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 @@
 #define _UTILS_BASE_
 /* Header file for nagios plugins utils_base.c */
 
+
 /* This file holds header information for thresholds - use this in preference to 
    individual plugin logic */
 
@@ -46,6 +47,12 @@ typedef struct state_key_struct {
 	state_data *state_data;
 	} state_key;
 
+typedef struct np_struct {
+	char      *plugin_name;
+	state_key *state;
+	char      **expanded_argv;
+	} nagios_plugin;
+
 range *parse_range_string (char *);
 int _set_thresholds(thresholds **, char *, char *);
 void set_thresholds(thresholds **, char *, char *);
@@ -86,10 +93,11 @@ char *np_extract_value(const char*, const char*, char);
 #define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
 
 
-char *np_state_generate_key(char **);
-state_key *np_state_init(char *, char *, int);
-state_data *np_state_read(state_key *);
-void np_state_write_string(state_key *, time_t *, char *);
-void np_state_cleanup(state_key *);
+void np_state_init(char *, int);
+state_data *np_state_read();
+void np_state_write_string(time_t *, char *);
+
+void np_init(char *);
+void np_cleanup();
 
 #endif /* _UTILS_BASE_ */





More information about the Commits mailing list