3 * Queries the stats of a nmdb server.
4 * Alberto Bertogli (albertito@blitiri.com.ar)
7 #include <stdio.h> /* printf() */
8 #include <stdint.h> /* uint64_t */
9 #include <string.h> /* strcmp() */
10 #include <stdlib.h> /* atoi() */
11 #include <arpa/inet.h> /* htonl() and friends */
16 /* ntohll() is not standard, so we define it using an UGLY trick because there
17 * is no standard way to check for endianness at runtime! (this is the same as
18 * the one in nmdb/parse.c, the infraestructure to keep these common is not
20 static uint64_t ntohll(uint64_t x
)
22 static int endianness
= 0;
24 /* determine the endianness by checking how htonl() behaves; use -1
25 * for little endian and 1 for big endian */
26 if (endianness
== 0) {
33 if (endianness
== 1) {
39 return ( ntohl( (x
>> 32) & 0xFFFFFFFF ) | \
40 ( (uint64_t) ntohl(x
& 0xFFFFFFFF) ) << 32 );
43 #define MAX_STATS_SIZE 64
45 static void help(void)
47 printf("Use: nmdb-stats [ tipc port | [tcp|udp|sctp] host port ]\n");
50 int main(int argc
, char **argv
)
54 uint64_t stats
[MAX_STATS_SIZE
];
55 unsigned int nservers
= 0, nstats
= 0;
65 if (strcmp("tipc", argv
[1]) == 0) {
66 rv
= nmdb_add_tipc_server(db
, atoi(argv
[2]));
73 if (strcmp("tcp", argv
[1]) == 0) {
74 rv
= nmdb_add_tcp_server(db
, argv
[2], atoi(argv
[3]));
75 } else if (strcmp("udp", argv
[1]) == 0) {
76 rv
= nmdb_add_udp_server(db
, argv
[2], atoi(argv
[3]));
77 } else if (strcmp("sctp", argv
[1]) == 0) {
78 rv
= nmdb_add_sctp_server(db
, argv
[2], atoi(argv
[3]));
86 perror("Error adding server");
90 rv
= nmdb_stats(db
, (unsigned char *) stats
, sizeof(stats
),
93 printf("Error %d\n", rv
);
97 /* Macro to simplify showing the fields */
98 #define shst(s, pos) \
100 printf("\t%ju\t%s\n", ntohll(stats[j + pos]), s); \
103 /* The following assumes it can be more than one server. This can
104 * never happen with the current code, but it can be useful as an
105 * example in the future. */
106 for (i
= 0; i
< nservers
; i
++) {
107 printf("stats for server %d:\n", i
);
111 /* note they are not necessarily in numerical order */
112 shst("cache get", 0);
113 shst("cache set", 1);
114 shst("cache del", 2);
115 shst("cache cas", 3);
116 shst("cache incr", 4);
123 shst("db firstkey", 21);
124 shst("db nextkey", 22);
126 shst("cache hits", 10);
127 shst("cache misses", 11);
130 shst("db misses", 13);
132 shst("msg tipc", 14);
135 shst("msg sctp", 17);
137 shst("version mismatch", 18);
138 shst("broken requests", 19);
139 shst("unknown requests", 20);
141 /* if there are any fields we don't know, show them anyway */
142 for (k
= 23; k
< nstats
; k
++) {
143 shst("unknown field", k
);