git » nmdb » commit 7d3cc60

Add a nmdb-stats.

author Alberto Bertogli
2007-10-11 08:05:25 UTC
committer Alberto Bertogli
2007-10-11 08:05:25 UTC
parent e1c28f7e9304ab77cd9c7ba972585587ac72e23a

Add a nmdb-stats.

It is a small tool that can be used to query the stats of a given server.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

.gitignore +1 -0
Makefile +7 -2
utils/Makefile +51 -0
utils/nmdb-stats.1 +34 -0
utils/nmdb-stats.c +150 -0

diff --git a/.gitignore b/.gitignore
index d6bb4ff..e1533f2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,6 +9,7 @@
 *.gcda
 # *~
 /nmdb/nmdb
+/utils/nmdb-stats
 tests/c/*-*-cache
 tests/c/*-*-normal
 tests/c/*-*-sync
diff --git a/Makefile b/Makefile
index 3fcab55..dee63c9 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 
 all: default
 
-default: nmdb libnmdb
+default: nmdb libnmdb utils
 
 nmdb:
 	make -C nmdb
@@ -9,13 +9,18 @@ nmdb:
 libnmdb:
 	make -C libnmdb
 
+utils:
+	make -C utils
+
 install:
 	make -C nmdb install
 	make -C libnmdb install
+	make -C utils install
 
 clean:
 	make -C nmdb clean
 	make -C libnmdb clean
+	make -C utils clean
 
 
 python:
@@ -28,6 +33,6 @@ python_clean:
 	cd bindings/python && rm -rf build/
 
 
-.PHONY: default all clean nmdb libnmdb python python_install python_clean
+.PHONY: default all clean nmdb libnmdb utils python python_install python_clean
 
 
diff --git a/utils/Makefile b/utils/Makefile
new file mode 100644
index 0000000..3b4c6bd
--- /dev/null
+++ b/utils/Makefile
@@ -0,0 +1,51 @@
+
+CFLAGS += -std=c99 -pedantic -Wall -O3
+ALL_CFLAGS = -D_XOPEN_SOURCE=500 $(CFLAGS)
+
+ifdef DEBUG
+ALL_CFLAGS += -g
+endif
+
+ifdef PROFILE
+ALL_CFLAGS += -g -pg -fprofile-arcs -ftest-coverage
+endif
+
+
+# prefix for installing the binaries
+PREFIX=/usr/local
+
+
+ifneq ($(V), 1)
+	NICE_CC = @echo "  CC  $@"; $(CC)
+else
+	NICE_CC = $(CC)
+endif
+
+
+default: all
+
+all: nmdb-stats
+
+nmdb-stats: nmdb-stats.o
+	$(NICE_CC) $(ALL_CFLAGS) -lnmdb nmdb-stats.o -o nmdb-stats
+
+.c.o:
+	$(NICE_CC) $(ALL_CFLAGS) -c $< -o $@
+
+install-bin: nmdb-stats
+	install -d $(PREFIX)/bin
+	install -m 0755 nmdb-stats $(PREFIX)/bin
+
+install-man:
+	install -d $(PREFIX)/man/man1
+	install -m 0644 nmdb-stats.1 $(PREFIX)/man/man1/
+
+install: install-bin install-man
+
+clean:
+	rm -f nmdb-stats.o nmdb-stats
+	rm -f *.bb *.bbg *.da *.gcov *.gcda *.gcno gmon.out
+
+.PHONY: default all clean install-bin install-man install
+
+
diff --git a/utils/nmdb-stats.1 b/utils/nmdb-stats.1
new file mode 100644
index 0000000..7978771
--- /dev/null
+++ b/utils/nmdb-stats.1
@@ -0,0 +1,34 @@
+.TH nmdb-stats 1 "11/Oct/2007"
+.SH NAME
+nmdb-stats - Get the stats of a nmdb server.
+.SH SYNOPSYS
+nmdb-stats [ tipc
+.B port
+| [tcp|udp|sctp]
+.B host
+.B port
+]
+
+.SH DESCRIPTION
+
+This small application is used to query an nmdb server in order to get its
+statistics.
+
+It takes the protocol as the first parameter (can be "tipc", "tcp", "udp", or
+"sctp"), and then the server address.
+
+.SH INVOCATION EXAMPLE
+.B "nmdb-stats tcp localhost 26010"
+
+.SH SEE ALSO
+.BR nmdb (1).
+
+.SH AUTHORS
+Created by Alberto Bertogli (albertito@gmail.com).
+
+.SH CONTACT
+
+To get in touch with developers and users, join the mailing list at
+http://lists.auriga.wearlab.de/cgi-bin/mailman/listinfo/nmdb-devel or just
+send an email to nmdb-devel@lists.auriga.wearlab.de.
+
diff --git a/utils/nmdb-stats.c b/utils/nmdb-stats.c
new file mode 100644
index 0000000..73e5db4
--- /dev/null
+++ b/utils/nmdb-stats.c
@@ -0,0 +1,150 @@
+
+/* nmdb-stats.c
+ * Queries the stats of a nmdb server.
+ * Alberto Bertogli (albertito@gmail.com)
+ */
+
+#include <stdio.h>		/* printf() */
+#include <stdint.h>		/* uint64_t */
+#include <string.h>		/* strcmp() */
+#include <stdlib.h>		/* atoi() */
+
+#include "nmdb.h"
+
+
+/* ntohll() is not standard, so we define it using an UGLY trick because there
+ * is no standard way to check for endianness at runtime! (this is the same as
+ * the one in nmdb/parse.c, the infraestructure to keep these common is not
+ * worth it)*/
+static uint64_t ntohll(uint64_t x)
+{
+	static int endianness = 0;
+
+	/* determine the endianness by checking how htonl() behaves; use -1
+	 * for little endian and 1 for big endian */
+	if (endianness == 0) {
+		if (htonl(1) == 1)
+			endianness = 1;
+		else
+			endianness = -1;
+	}
+
+	if (endianness == 1) {
+		/* big endian */
+		return x;
+	}
+
+	/* little endian */
+	return ( ntohl( (x >> 32) & 0xFFFFFFFF ) | \
+			( (uint64_t) ntohl(x & 0xFFFFFFFF) ) << 32 );
+}
+
+#define MAX_STATS_SIZE 64
+
+void help(void)
+{
+	printf("Use: nmdb-stats [ tipc port | [tcp|udp|sctp] host port ]\n");
+}
+
+int main(int argc, char **argv)
+{
+	int i, j, k;
+	int rv;
+	uint64_t stats[MAX_STATS_SIZE];
+	unsigned int nservers = 0, nstats = 0;
+	nmdb_t *db;
+
+	db = nmdb_init();
+
+	if (argc < 3) {
+		help();
+		return 1;
+	}
+
+	if (strcmp("tipc", argv[1]) == 0) {
+		rv = nmdb_add_tipc_server(db, atoi(argv[2]));
+	} else {
+		if (argc != 4) {
+			help();
+			return 1;
+		}
+
+		if (strcmp("tcp", argv[1]) == 0) {
+			rv = nmdb_add_tcp_server(db, argv[2], atoi(argv[3]));
+		} else if (strcmp("udp", argv[1]) == 0) {
+			rv = nmdb_add_udp_server(db, argv[2], atoi(argv[3]));
+		} else if  (strcmp("sctp", argv[1]) == 0) {
+			rv = nmdb_add_sctp_server(db, argv[2], atoi(argv[3]));
+		} else {
+			help();
+			return 1;
+		}
+	}
+
+	if (!rv) {
+		perror("Error adding server");
+		return 1;
+	}
+
+	rv = nmdb_stats(db, (unsigned char *) stats, sizeof(stats),
+			&nservers, &nstats);
+	if (rv <= 0) {
+		printf("Error %d\n", rv);
+		return 1;
+	}
+
+	/* Macro to simplify showing the fields */
+	#define shst(s, pos) \
+		do { \
+			printf("\t%ju\t%s\n", ntohll(stats[j + pos]), s); \
+		} while(0)
+
+	/* The following assumes it can be more than one server. This can
+	 * never happen with the current code, but it can be useful as an
+	 * example in the future. */
+	j = 0;
+	for (i = 0; i < nservers; i++) {
+		printf("stats for server %d:\n", i);
+
+		j = nstats * i;
+
+		shst("cache get", 0);
+		shst("cache set", 1);
+		shst("cache del", 2);
+		shst("cache cas", 3);
+		shst("cache incr", 4);
+
+		shst("db get", 5);
+		shst("db set", 6);
+		shst("db del", 7);
+		shst("db cas", 8);
+		shst("db incr", 9);
+
+		shst("cache hits", 10);
+		shst("cache misses", 11);
+
+		shst("db hits", 12);
+		shst("db misses", 13);
+
+		shst("msg tipc", 14);
+		shst("msg tcp", 15);
+		shst("msg udp", 16);
+		shst("msg sctp", 17);
+
+		shst("version mismatch", 18);
+		shst("broken requests", 19);
+		shst("unknown requests", 20);
+
+		/* if there are any fields we don't know, show them anyway */
+		for (k = 21; k < nstats; k++) {
+			shst("unknown field", k);
+		}
+
+		printf("\n");
+	}
+
+	nmdb_free(db);
+
+	return 0;
+}
+