[Nagiosplug-checkins] SF.net SVN: nagiosplug:[2138] nagiosplug/trunk

dermoth at users.sourceforge.net dermoth at users.sourceforge.net
Thu Jan 22 08:08:25 CET 2009


Revision: 2138
          http://nagiosplug.svn.sourceforge.net/nagiosplug/?rev=2138&view=rev
Author:   dermoth
Date:     2009-01-22 07:08:24 +0000 (Thu, 22 Jan 2009)

Log Message:
-----------
Move check_ntp's extract_value to utils_base.c.

This function can also be used to parse performance data strings which
could be useful in the future.

From: Thomas Guyot-Sionnest <dermoth at aei.ca>

Modified Paths:
--------------
    nagiosplug/trunk/lib/tests/test_utils.c
    nagiosplug/trunk/lib/utils_base.c
    nagiosplug/trunk/lib/utils_base.h
    nagiosplug/trunk/plugins/check_ntp_peer.c

Modified: nagiosplug/trunk/lib/tests/test_utils.c
===================================================================
--- nagiosplug/trunk/lib/tests/test_utils.c	2009-01-21 06:27:23 UTC (rev 2137)
+++ nagiosplug/trunk/lib/tests/test_utils.c	2009-01-22 07:08:24 UTC (rev 2138)
@@ -29,7 +29,7 @@
 	thresholds *thresholds = NULL;
 	int	rc;
 
-	plan_tests(81);
+	plan_tests(81+23);
 
 	range = parse_range_string("6");
 	ok( range != NULL, "'6' is valid range");
@@ -172,5 +172,84 @@
 	test = np_escaped_string("everything");
 	ok( strcmp(test, "everything") == 0, "everything okay");
 
+	/* np_extract_value tests (23) */
+	test=np_extract_value("foo=bar, bar=foo, foobar=barfoo\n", "foo");
+	ok(test && !strcmp(test, "bar"), "1st test as expected");
+	free(test);
+
+	test=np_extract_value("foo=bar,bar=foo,foobar=barfoo\n", "bar");
+	ok(test && !strcmp(test, "foo"), "2nd test as expected");
+	free(test);
+
+	test=np_extract_value("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
+	ok(test && !strcmp(test, "barfoo"), "3rd test as expected");
+	free(test);
+
+	test=np_extract_value("foo=bar\n", "foo");
+	ok(test && !strcmp(test, "bar"), "Single test as expected");
+	free(test);
+
+	test=np_extract_value("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
+	ok(!test, "Key not found 1");
+
+	test=np_extract_value("foo=bar\n", "abcd");
+	ok(!test, "Key not found 2");
+
+	test=np_extract_value("foo=bar=foobar", "foo");
+	ok(test && !strcmp(test, "bar=foobar"), "Strange string 1");
+	free(test);
+
+	test=np_extract_value("foo", "foo");
+	ok(!test, "Malformed string 1");
+
+	test=np_extract_value("foo,", "foo");
+	ok(!test, "Malformed string 2");
+
+	test=np_extract_value("foo=", "foo");
+	ok(!test, "Malformed string 3");
+
+	test=np_extract_value("foo=,bar=foo", "foo");
+	ok(!test, "Malformed string 4");
+
+	test=np_extract_value(",foo", "foo");
+	ok(!test, "Malformed string 5");
+
+	test=np_extract_value("=foo", "foo");
+	ok(!test, "Malformed string 6");
+
+	test=np_extract_value("=foo,", "foo");
+	ok(!test, "Malformed string 7");
+
+	test=np_extract_value(",,,", "foo");
+	ok(!test, "Malformed string 8");
+
+	test=np_extract_value("===", "foo");
+	ok(!test, "Malformed string 9");
+
+	test=np_extract_value(",=,=,", "foo");
+	ok(!test, "Malformed string 10");
+
+	test=np_extract_value("=,=,=", "foo");
+	ok(!test, "Malformed string 11");
+
+	test=np_extract_value("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "foo");
+	ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1");
+	free(test);
+
+	test=np_extract_value("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "bar");
+	ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2");
+	free(test);
+
+	test=np_extract_value("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "foobar");
+	ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3");
+	free(test);
+
+	test=np_extract_value("  foo=bar  ,\n bar\n \n= \n foo\n , foobar=barfoo  \n  ", "bar");
+	ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4");
+	free(test);
+
+	test=np_extract_value("", "foo");
+	ok(!test, "Empty string return NULL");
+
 	return exit_status();
 }

Modified: nagiosplug/trunk/lib/utils_base.c
===================================================================
--- nagiosplug/trunk/lib/utils_base.c	2009-01-21 06:27:23 UTC (rev 2137)
+++ nagiosplug/trunk/lib/utils_base.c	2009-01-22 07:08:24 UTC (rev 2138)
@@ -251,3 +251,60 @@
 	}
 	return status;
 }
+
+/*
+ * Extract the value from key/value pairs, or return NULL. The value returned
+ * can be free()ed.
+ * This function can be used to parse NTP control packet data and performance
+ * data strings.
+ */
+char *np_extract_value(const char *varlist, const char *name) {
+	char *tmp=NULL, *value=NULL;
+	int i;
+
+	while (1) {
+		/* Strip any leading space */
+		for (varlist; isspace(varlist[0]); varlist++);
+
+		if (strncmp(name, varlist, strlen(name)) == 0) {
+			varlist += strlen(name);
+			/* strip trailing spaces */
+			for (varlist; isspace(varlist[0]); varlist++);
+
+			if (varlist[0] == '=') {
+				/* We matched the key, go past the = sign */
+				varlist++;
+				/* strip leading spaces */
+				for (varlist; isspace(varlist[0]); varlist++);
+
+				if (tmp = index(varlist, ',')) {
+					/* Value is delimited by a comma */
+					if (tmp-varlist == 0) continue;
+					value = (char *)malloc(tmp-varlist+1);
+					strncpy(value, varlist, tmp-varlist);
+					value[tmp-varlist] = '\0';
+				} else {
+					/* Value is delimited by a \0 */
+					if (strlen(varlist) == 0) continue;
+					value = (char *)malloc(strlen(varlist) + 1);
+					strncpy(value, varlist, strlen(varlist));
+					value[strlen(varlist)] = '\0';
+				}
+				break;
+			}
+		}
+		if (tmp = index(varlist, ',')) {
+			/* More keys, keep going... */
+			varlist = tmp + 1;
+		} else {
+			/* We're done */
+			break;
+		}
+	}
+
+	/* Clean-up trailing spaces/newlines */
+	if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
+
+	return value;
+}
+

Modified: nagiosplug/trunk/lib/utils_base.h
===================================================================
--- nagiosplug/trunk/lib/utils_base.h	2009-01-21 06:27:23 UTC (rev 2137)
+++ nagiosplug/trunk/lib/utils_base.h	2009-01-22 07:08:24 UTC (rev 2138)
@@ -50,4 +50,12 @@
  * code from the above function, in case it's helpful for testing */
 int np_warn_if_not_root(void);
 
+/*
+ * Extract the value from key/value pairs, or return NULL. The value returned
+ * can be free()ed.
+ * This function can be used to parse NTP control packet data and performance
+ * data strings.
+ */
+char *np_extract_value(const char*, const char*);
+
 #endif /* _UTILS_BASE_ */

Modified: nagiosplug/trunk/plugins/check_ntp_peer.c
===================================================================
--- nagiosplug/trunk/plugins/check_ntp_peer.c	2009-01-21 06:27:23 UTC (rev 2137)
+++ nagiosplug/trunk/plugins/check_ntp_peer.c	2009-01-22 07:08:24 UTC (rev 2138)
@@ -172,60 +172,6 @@
 	}
 }
 
-/*
- * Extract the value from NTP key/value pairs, or return NULL.
- * The value returned can be free()ed.
- */
-char *extract_value(const char *varlist, const char *name){
-	char *tmp=NULL, *value=NULL;
-	int i;
-
-	while (1) {
-		/* Strip any leading space */
-		for (varlist; isspace(varlist[0]); varlist++);
-
-		if (strncmp(name, varlist, strlen(name)) == 0) {
-			varlist += strlen(name);
-			/* strip trailing spaces */
-			for (varlist; isspace(varlist[0]); varlist++);
-
-			if (varlist[0] == '=') {
-				/* We matched the key, go past the = sign */
-				varlist++;
-				/* strip leading spaces */
-				for (varlist; isspace(varlist[0]); varlist++);
-
-				if (tmp = index(varlist, ',')) {
-					/* Value is delimited by a comma */
-					if (tmp-varlist == 0) continue;
-					value = (char *)malloc(tmp-varlist+1);
-					strncpy(value, varlist, tmp-varlist);
-					value[tmp-varlist] = '\0';
-				} else {
-					/* Value is delimited by a \0 */
-					if (strlen(varlist) == 0) continue;
-					value = (char *)malloc(strlen(varlist) + 1);
-					strncpy(value, varlist, strlen(varlist));
-					value[strlen(varlist)] = '\0';
-				}
-				break;
-			}
-		}
-		if (tmp = index(varlist, ',')) {
-			/* More keys, keep going... */
-			varlist = tmp + 1;
-		} else {
-			/* We're done */
-			break;
-		}
-	}
-
-	/* Clean-up trailing spaces/newlines */
-	if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
-
-	return value;
-}
-
 void
 setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
 	memset(p, 0, sizeof(ntp_control_message));
@@ -387,7 +333,7 @@
 			if(verbose)
 				printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
 
-			value = extract_value(data, "offset");
+			value = np_extract_value(data, "offset");
 			nptr=NULL;
 			/* Convert the value if we have one */
 			if(value != NULL)
@@ -411,7 +357,7 @@
 				if(verbose) {
 					printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
 				}
-				value = extract_value(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
+				value = np_extract_value(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
 				nptr=NULL;
 				/* Convert the value if we have one */
 				if(value != NULL)
@@ -430,7 +376,7 @@
 				if(verbose) {
 					printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
 				}
-				value = extract_value(data, "stratum");
+				value = np_extract_value(data, "stratum");
 				nptr=NULL;
 				/* Convert the value if we have one */
 				if(value != NULL)


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Commits mailing list