statschannel.c 80 KB
Newer Older
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2008-2014  Internet Systems Consortium, Inc. ("ISC")
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

17
/* $Id: statschannel.c,v 1.28.224.1 2011/12/22 07:48:27 marka Exp $ */
18
19
20
21
22
23
24
25

/*! \file */

#include <config.h>

#include <isc/buffer.h>
#include <isc/httpd.h>
#include <isc/mem.h>
26
#include <isc/once.h>
27
28
#include <isc/print.h>
#include <isc/socket.h>
29
#include <isc/stats.h>
Evan Hunt's avatar
Evan Hunt committed
30
#include <isc/string.h>
31
32
#include <isc/task.h>

33
#include <dns/cache.h>
34
35
#include <dns/db.h>
#include <dns/opcode.h>
36
#include <dns/resolver.h>
37
38
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
39
40
#include <dns/stats.h>
#include <dns/view.h>
41
#include <dns/zt.h>
42
43
44
45
46

#include <named/log.h>
#include <named/server.h>
#include <named/statschannel.h>

47
48
49
50
#ifdef HAVE_JSON_H
#include <json/json.h>
#endif

51
52
#include "bind9.xsl.h"

53
54
55
56
57
58
59
struct ns_statschannel {
	/* Unlocked */
	isc_httpdmgr_t				*httpdmgr;
	isc_sockaddr_t				address;
	isc_mem_t				*mctx;

	/*
Francis Dupont's avatar
Francis Dupont committed
60
	 * Locked by channel lock: can be referenced and modified by both
Automatic Updater's avatar
Automatic Updater committed
61
	 * the server task and the channel task.
62
63
64
65
66
67
68
69
	 */
	isc_mutex_t				lock;
	dns_acl_t				*acl;

	/* Locked by server task */
	ISC_LINK(struct ns_statschannel)	link;
};

70
71
typedef struct
stats_dumparg {
72
73
74
75
76
77
	isc_statsformat_t	type;
	void			*arg;		/* type dependent argument */
	int			ncounters;	/* for general statistics */
	int			*counterindices; /* for general statistics */
	isc_uint64_t		*countervalues;	 /* for general statistics */
	isc_result_t		result;
78
79
} stats_dumparg_t;

80
81
static isc_once_t once = ISC_ONCE_INIT;

82
83
84
85
86
87
88
/*%
 * Statistics descriptions.  These could be statistically initialized at
 * compile time, but we configure them run time in the init_desc() function
 * below so that they'll be less susceptible to counter name changes.
 */
static const char *nsstats_desc[dns_nsstatscounter_max];
static const char *resstats_desc[dns_resstatscounter_max];
89
static const char *adbstats_desc[dns_adbstats_max];
90
static const char *zonestats_desc[dns_zonestatscounter_max];
91
static const char *sockstats_desc[isc_sockstatscounter_max];
92
static const char *dnssecstats_desc[dns_dnssecstats_max];
93
94
95
#ifdef HAVE_LIBXML2
static const char *nsstats_xmldesc[dns_nsstatscounter_max];
static const char *resstats_xmldesc[dns_resstatscounter_max];
96
static const char *adbstats_xmldesc[dns_adbstats_max];
97
static const char *zonestats_xmldesc[dns_zonestatscounter_max];
98
static const char *sockstats_xmldesc[isc_sockstatscounter_max];
99
static const char *dnssecstats_xmldesc[dns_dnssecstats_max];
100
101
102
#else
#define nsstats_xmldesc NULL
#define resstats_xmldesc NULL
103
#define adbstats_xmldesc NULL
104
#define zonestats_xmldesc NULL
105
#define sockstats_xmldesc NULL
106
#define dnssecstats_xmldesc NULL
107
108
#endif	/* HAVE_LIBXML2 */

109
110
#define TRY0(a) do { xmlrc = (a); if (xmlrc < 0) goto error; } while(0)

111
112
113
114
115
116
117
/*%
 * Mapping arrays to represent statistics counters in the order of our
 * preference, regardless of the order of counter indices.  For example,
 * nsstats_desc[nsstats_index[0]] will be the description that is shown first.
 */
static int nsstats_index[dns_nsstatscounter_max];
static int resstats_index[dns_resstatscounter_max];
118
static int adbstats_index[dns_adbstats_max];
119
120
static int zonestats_index[dns_zonestatscounter_max];
static int sockstats_index[isc_sockstatscounter_max];
121
static int dnssecstats_index[dns_dnssecstats_max];
122

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
static inline void
set_desc(int counter, int maxcounter, const char *fdesc, const char **fdescs,
	 const char *xdesc, const char **xdescs)
{
	REQUIRE(counter < maxcounter);
	REQUIRE(fdescs[counter] == NULL);
#ifdef HAVE_LIBXML2
	REQUIRE(xdescs[counter] == NULL);
#endif

	fdescs[counter] = fdesc;
#ifdef HAVE_LIBXML2
	xdescs[counter] = xdesc;
#else
	UNUSED(xdesc);
	UNUSED(xdescs);
#endif
}

static void
143
init_desc(void) {
144
145
146
	int i;

	/* Initialize name server statistics */
147
148
	for (i = 0; i < dns_nsstatscounter_max; i++)
		nsstats_desc[i] = NULL;
149
#ifdef HAVE_LIBXML2
150
151
	for (i = 0; i < dns_nsstatscounter_max; i++)
		nsstats_xmldesc[i] = NULL;
152
#endif
153
154
155
156
157

#define SET_NSSTATDESC(counterid, desc, xmldesc) \
	do { \
		set_desc(dns_nsstatscounter_ ## counterid, \
			 dns_nsstatscounter_max, \
Automatic Updater's avatar
Automatic Updater committed
158
			 desc, nsstats_desc, xmldesc, nsstats_xmldesc); \
159
160
161
162
163
164
165
166
167
168
169
170
171
172
		nsstats_index[i++] = dns_nsstatscounter_ ## counterid; \
	} while (0)

	i = 0;
	SET_NSSTATDESC(requestv4, "IPv4 requests received", "Requestv4");
	SET_NSSTATDESC(requestv6, "IPv6 requests received", "Requestv6");
	SET_NSSTATDESC(edns0in, "requests with EDNS(0) received", "ReqEdns0");
	SET_NSSTATDESC(badednsver,
		       "requests with unsupported EDNS version received",
		       "ReqBadEDNSVer");
	SET_NSSTATDESC(tsigin, "requests with TSIG received", "ReqTSIG");
	SET_NSSTATDESC(sig0in, "requests with SIG(0) received", "ReqSIG0");
	SET_NSSTATDESC(invalidsig, "requests with invalid signature",
		       "ReqBadSIG");
173
	SET_NSSTATDESC(requesttcp, "TCP requests received", "ReqTCP");
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
	SET_NSSTATDESC(authrej, "auth queries rejected", "AuthQryRej");
	SET_NSSTATDESC(recurserej, "recursive queries rejected", "RecQryRej");
	SET_NSSTATDESC(xfrrej, "transfer requests rejected", "XfrRej");
	SET_NSSTATDESC(updaterej, "update requests rejected", "UpdateRej");
	SET_NSSTATDESC(response, "responses sent", "Response");
	SET_NSSTATDESC(truncatedresp, "truncated responses sent",
		       "TruncatedResp");
	SET_NSSTATDESC(edns0out, "responses with EDNS(0) sent", "RespEDNS0");
	SET_NSSTATDESC(tsigout, "responses with TSIG sent", "RespTSIG");
	SET_NSSTATDESC(sig0out, "responses with SIG(0) sent", "RespSIG0");
	SET_NSSTATDESC(success, "queries resulted in successful answer",
		       "QrySuccess");
	SET_NSSTATDESC(authans, "queries resulted in authoritative answer",
		       "QryAuthAns");
	SET_NSSTATDESC(nonauthans,
		       "queries resulted in non authoritative answer",
		       "QryNoauthAns");
	SET_NSSTATDESC(referral, "queries resulted in referral answer",
		       "QryReferral");
	SET_NSSTATDESC(nxrrset, "queries resulted in nxrrset", "QryNxrrset");
	SET_NSSTATDESC(servfail, "queries resulted in SERVFAIL", "QrySERVFAIL");
	SET_NSSTATDESC(formerr, "queries resulted in FORMERR", "QryFORMERR");
	SET_NSSTATDESC(nxdomain, "queries resulted in NXDOMAIN", "QryNXDOMAIN");
197
	SET_NSSTATDESC(recursion, "queries caused recursion", "QryRecursion");
198
199
200
201
202
203
204
205
206
207
208
209
210
211
	SET_NSSTATDESC(duplicate, "duplicate queries received", "QryDuplicate");
	SET_NSSTATDESC(dropped, "queries dropped", "QryDropped");
	SET_NSSTATDESC(failure, "other query failures", "QryFailure");
	SET_NSSTATDESC(xfrdone, "requested transfers completed", "XfrReqDone");
	SET_NSSTATDESC(updatereqfwd, "update requests forwarded",
		       "UpdateReqFwd");
	SET_NSSTATDESC(updaterespfwd, "update responses forwarded",
		       "UpdateRespFwd");
	SET_NSSTATDESC(updatefwdfail, "update forward failed", "UpdateFwdFail");
	SET_NSSTATDESC(updatedone, "updates completed", "UpdateDone");
	SET_NSSTATDESC(updatefail, "updates failed", "UpdateFail");
	SET_NSSTATDESC(updatebadprereq,
		       "updates rejected due to prerequisite failure",
		       "UpdateBadPrereq");
212
213
	SET_NSSTATDESC(recursclients, "recursing clients",
			"RecursClients");
214
	SET_NSSTATDESC(dns64, "queries answered by DNS64", "DNS64");
Evan Hunt's avatar
Evan Hunt committed
215
216
217
218
	SET_NSSTATDESC(ratedropped, "responses dropped for rate limits",
		       "RateDropped");
	SET_NSSTATDESC(rateslipped, "responses truncated for rate limits",
		       "RateSlipped");
219
220
	SET_NSSTATDESC(rpz_rewrites, "response policy zone rewrites",
		       "RPZRewrites");
221
222
	SET_NSSTATDESC(udp, "UDP queries received", "QryUDP");
	SET_NSSTATDESC(tcp, "TCP queries received", "QryTCP");
223
224
225
	SET_NSSTATDESC(nsidopt, "NSID option received", "NSIDOpt");
	SET_NSSTATDESC(expireopt, "Expire option recieved", "ExpireOpt");
	SET_NSSTATDESC(otheropt, "Other EDNS option recieved", "OtherOpt");
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifdef ISC_PLATFORM_USESIT
	SET_NSSTATDESC(sitopt, "source identity token option received",
		       "SitOpt");
	SET_NSSTATDESC(sitnew, "new source identity token requested",
		       "SitNew");
	SET_NSSTATDESC(sitbadsize, "source identity token - bad size",
		       "SitBadSize");
	SET_NSSTATDESC(sitbadtime, "source identity token - bad time",
		       "SitBadTime");
	SET_NSSTATDESC(sitnomatch, "source identity token - no match",
		       "SitNoMatch");
	SET_NSSTATDESC(sitmatch, "source identity token - match", "SitMatch");
#endif
239
	INSIST(i == dns_nsstatscounter_max);
240
241

	/* Initialize resolver statistics */
242
243
	for (i = 0; i < dns_resstatscounter_max; i++)
		resstats_desc[i] = NULL;
244
#ifdef  HAVE_LIBXML2
245
246
	for (i = 0; i < dns_resstatscounter_max; i++)
		resstats_xmldesc[i] = NULL;
247
248
#endif

249
250
251
252
#define SET_RESSTATDESC(counterid, desc, xmldesc) \
	do { \
		set_desc(dns_resstatscounter_ ## counterid, \
			 dns_resstatscounter_max, \
Automatic Updater's avatar
Automatic Updater committed
253
			 desc, resstats_desc, xmldesc, resstats_xmldesc); \
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
		resstats_index[i++] = dns_resstatscounter_ ## counterid; \
	} while (0)

	i = 0;
	SET_RESSTATDESC(queryv4, "IPv4 queries sent", "Queryv4");
	SET_RESSTATDESC(queryv6, "IPv6 queries sent", "Queryv6");
	SET_RESSTATDESC(responsev4, "IPv4 responses received", "Responsev4");
	SET_RESSTATDESC(responsev6, "IPv6 responses received", "Responsev6");
	SET_RESSTATDESC(nxdomain, "NXDOMAIN received", "NXDOMAIN");
	SET_RESSTATDESC(servfail, "SERVFAIL received", "SERVFAIL");
	SET_RESSTATDESC(formerr, "FORMERR received", "FORMERR");
	SET_RESSTATDESC(othererror, "other errors received", "OtherError");
	SET_RESSTATDESC(edns0fail, "EDNS(0) query failures", "EDNS0Fail");
	SET_RESSTATDESC(mismatch, "mismatch responses received", "Mismatch");
	SET_RESSTATDESC(truncated, "truncated responses received", "Truncated");
	SET_RESSTATDESC(lame, "lame delegations received", "Lame");
	SET_RESSTATDESC(retry, "query retries", "Retry");
	SET_RESSTATDESC(dispabort, "queries aborted due to quota",
			"QueryAbort");
	SET_RESSTATDESC(dispsockfail, "failures in opening query sockets",
			"QuerySockFail");
275
276
	SET_RESSTATDESC(disprequdp, "UDP queries in progress", "QueryCurUDP");
	SET_RESSTATDESC(dispreqtcp, "TCP queries in progress", "QueryCurTCP");
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
	SET_RESSTATDESC(querytimeout, "query timeouts", "QueryTimeout");
	SET_RESSTATDESC(gluefetchv4, "IPv4 NS address fetches", "GlueFetchv4");
	SET_RESSTATDESC(gluefetchv6, "IPv6 NS address fetches", "GlueFetchv6");
	SET_RESSTATDESC(gluefetchv4fail, "IPv4 NS address fetch failed",
			"GlueFetchv4Fail");
	SET_RESSTATDESC(gluefetchv6fail, "IPv6 NS address fetch failed",
			"GlueFetchv6Fail");
	SET_RESSTATDESC(val, "DNSSEC validation attempted", "ValAttempt");
	SET_RESSTATDESC(valsuccess, "DNSSEC validation succeeded", "ValOk");
	SET_RESSTATDESC(valnegsuccess, "DNSSEC NX validation succeeded",
			"ValNegOk");
	SET_RESSTATDESC(valfail, "DNSSEC validation failed", "ValFail");
	SET_RESSTATDESC(queryrtt0, "queries with RTT < "
			DNS_RESOLVER_QRYRTTCLASS0STR "ms",
			"QryRTT" DNS_RESOLVER_QRYRTTCLASS0STR);
	SET_RESSTATDESC(queryrtt1, "queries with RTT "
			DNS_RESOLVER_QRYRTTCLASS0STR "-"
			DNS_RESOLVER_QRYRTTCLASS1STR "ms",
			"QryRTT" DNS_RESOLVER_QRYRTTCLASS1STR);
	SET_RESSTATDESC(queryrtt2, "queries with RTT "
			DNS_RESOLVER_QRYRTTCLASS1STR "-"
			DNS_RESOLVER_QRYRTTCLASS2STR "ms",
			"QryRTT" DNS_RESOLVER_QRYRTTCLASS2STR);
	SET_RESSTATDESC(queryrtt3, "queries with RTT "
			DNS_RESOLVER_QRYRTTCLASS2STR "-"
			DNS_RESOLVER_QRYRTTCLASS3STR "ms",
			"QryRTT" DNS_RESOLVER_QRYRTTCLASS3STR);
	SET_RESSTATDESC(queryrtt4, "queries with RTT "
			DNS_RESOLVER_QRYRTTCLASS3STR "-"
			DNS_RESOLVER_QRYRTTCLASS4STR "ms",
			"QryRTT" DNS_RESOLVER_QRYRTTCLASS4STR);
	SET_RESSTATDESC(queryrtt5, "queries with RTT > "
			DNS_RESOLVER_QRYRTTCLASS4STR "ms",
			"QryRTT" DNS_RESOLVER_QRYRTTCLASS4STR "+");
311
312
	SET_RESSTATDESC(nfetch, "active fetches", "NumFetch");
	SET_RESSTATDESC(buckets, "bucket size", "BucketSize");
313
	SET_RESSTATDESC(refused, "REFUSED received", "REFUSED");
314
315
316
317
318
319
320
321
#ifdef ISC_PLATFORM_USESIT
	SET_RESSTATDESC(sitcc, "SIT sent client cookie only",
			"SitClientOut");
	SET_RESSTATDESC(sitout, "SIT sent with client and server cookie",
			"SitOut");
	SET_RESSTATDESC(sitin, "SIT replies received", "SitIn");
	SET_RESSTATDESC(sitok, "SIT client cookie ok", "SitClientOk");
#endif
Tinderbox User's avatar
Tinderbox User committed
322

323
	INSIST(i == dns_resstatscounter_max);
Automatic Updater's avatar
Automatic Updater committed
324

325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
	/* Initialize adb statistics */
	for (i = 0; i < dns_adbstats_max; i++)
		adbstats_desc[i] = NULL;
#ifdef  HAVE_LIBXML2
	for (i = 0; i < dns_adbstats_max; i++)
		adbstats_xmldesc[i] = NULL;
#endif

#define SET_ADBSTATDESC(id, desc, xmldesc) \
	do { \
		set_desc(dns_adbstats_ ## id, dns_adbstats_max, \
			 desc, adbstats_desc, xmldesc, adbstats_xmldesc); \
		adbstats_index[i++] = dns_adbstats_ ## id; \
	} while (0)
	i = 0;
	SET_ADBSTATDESC(nentries, "Address hash table size", "nentries");
	SET_ADBSTATDESC(entriescnt, "Addresses in hash table", "entriescnt");
	SET_ADBSTATDESC(nnames, "Name hash table size", "nnames");
	SET_ADBSTATDESC(namescnt, "Names in hash table", "namescnt");

	INSIST(i == dns_adbstats_max);

347
	/* Initialize zone statistics */
348
349
	for (i = 0; i < dns_zonestatscounter_max; i++)
		zonestats_desc[i] = NULL;
350
#ifdef  HAVE_LIBXML2
351
352
	for (i = 0; i < dns_zonestatscounter_max; i++)
		zonestats_xmldesc[i] = NULL;
353
#endif
354
355
356
357
358

#define SET_ZONESTATDESC(counterid, desc, xmldesc) \
	do { \
		set_desc(dns_zonestatscounter_ ## counterid, \
			 dns_zonestatscounter_max, \
Automatic Updater's avatar
Automatic Updater committed
359
			 desc, zonestats_desc, xmldesc, zonestats_xmldesc); \
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
		zonestats_index[i++] = dns_zonestatscounter_ ## counterid; \
	} while (0)

	i = 0;
	SET_ZONESTATDESC(notifyoutv4, "IPv4 notifies sent", "NotifyOutv4");
	SET_ZONESTATDESC(notifyoutv6, "IPv6 notifies sent", "NotifyOutv6");
	SET_ZONESTATDESC(notifyinv4, "IPv4 notifies received", "NotifyInv4");
	SET_ZONESTATDESC(notifyinv6, "IPv6 notifies received", "NotifyInv6");
	SET_ZONESTATDESC(notifyrej, "notifies rejected", "NotifyRej");
	SET_ZONESTATDESC(soaoutv4, "IPv4 SOA queries sent", "SOAOutv4");
	SET_ZONESTATDESC(soaoutv6, "IPv6 SOA queries sent", "SOAOutv6");
	SET_ZONESTATDESC(axfrreqv4, "IPv4 AXFR requested", "AXFRReqv4");
	SET_ZONESTATDESC(axfrreqv6, "IPv6 AXFR requested", "AXFRReqv6");
	SET_ZONESTATDESC(ixfrreqv4, "IPv4 IXFR requested", "IXFRReqv4");
	SET_ZONESTATDESC(ixfrreqv6, "IPv6 IXFR requested", "IXFRReqv6");
375
376
	SET_ZONESTATDESC(xfrsuccess, "transfer requests succeeded",
			 "XfrSuccess");
377
378
379
380
	SET_ZONESTATDESC(xfrfail, "transfer requests failed", "XfrFail");
	INSIST(i == dns_zonestatscounter_max);

	/* Initialize socket statistics */
381
382
	for (i = 0; i < isc_sockstatscounter_max; i++)
		sockstats_desc[i] = NULL;
383
#ifdef  HAVE_LIBXML2
384
385
	for (i = 0; i < isc_sockstatscounter_max; i++)
		sockstats_xmldesc[i] = NULL;
386
387
388
389
390
391
#endif

#define SET_SOCKSTATDESC(counterid, desc, xmldesc) \
	do { \
		set_desc(isc_sockstatscounter_ ## counterid, \
			 isc_sockstatscounter_max, \
Automatic Updater's avatar
Automatic Updater committed
392
			 desc, sockstats_desc, xmldesc, sockstats_xmldesc); \
393
394
395
396
		sockstats_index[i++] = isc_sockstatscounter_ ## counterid; \
	} while (0)

	i = 0;
Automatic Updater's avatar
Automatic Updater committed
397
398
	SET_SOCKSTATDESC(udp4open, "UDP/IPv4 sockets opened", "UDP4Open");
	SET_SOCKSTATDESC(udp6open, "UDP/IPv6 sockets opened", "UDP6Open");
399
400
401
	SET_SOCKSTATDESC(tcp4open, "TCP/IPv4 sockets opened", "TCP4Open");
	SET_SOCKSTATDESC(tcp6open, "TCP/IPv6 sockets opened", "TCP6Open");
	SET_SOCKSTATDESC(unixopen, "Unix domain sockets opened", "UnixOpen");
402
	SET_SOCKSTATDESC(rawopen, "Raw sockets opened", "RawOpen");
403
	SET_SOCKSTATDESC(udp4openfail, "UDP/IPv4 socket open failures",
Automatic Updater's avatar
Automatic Updater committed
404
			 "UDP4OpenFail");
405
	SET_SOCKSTATDESC(udp6openfail, "UDP/IPv6 socket open failures",
Automatic Updater's avatar
Automatic Updater committed
406
			 "UDP6OpenFail");
407
408
409
410
411
412
	SET_SOCKSTATDESC(tcp4openfail, "TCP/IPv4 socket open failures",
			 "TCP4OpenFail");
	SET_SOCKSTATDESC(tcp6openfail, "TCP/IPv6 socket open failures",
			 "TCP6OpenFail");
	SET_SOCKSTATDESC(unixopenfail, "Unix domain socket open failures",
			 "UnixOpenFail");
413
414
	SET_SOCKSTATDESC(rawopenfail, "Raw socket open failures",
			 "RawOpenFail");
415
416
417
418
419
420
421
	SET_SOCKSTATDESC(udp4close, "UDP/IPv4 sockets closed", "UDP4Close");
	SET_SOCKSTATDESC(udp6close, "UDP/IPv6 sockets closed", "UDP6Close");
	SET_SOCKSTATDESC(tcp4close, "TCP/IPv4 sockets closed", "TCP4Close");
	SET_SOCKSTATDESC(tcp6close, "TCP/IPv6 sockets closed", "TCP6Close");
	SET_SOCKSTATDESC(unixclose, "Unix domain sockets closed", "UnixClose");
	SET_SOCKSTATDESC(fdwatchclose, "FDwatch sockets closed",
			 "FDWatchClose");
422
	SET_SOCKSTATDESC(rawclose, "Raw sockets closed", "RawClose");
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
	SET_SOCKSTATDESC(udp4bindfail, "UDP/IPv4 socket bind failures",
			 "UDP4BindFail");
	SET_SOCKSTATDESC(udp6bindfail, "UDP/IPv6 socket bind failures",
			 "UDP6BindFail");
	SET_SOCKSTATDESC(tcp4bindfail, "TCP/IPv4 socket bind failures",
			 "TCP4BindFail");
	SET_SOCKSTATDESC(tcp6bindfail, "TCP/IPv6 socket bind failures",
			 "TCP6BindFail");
	SET_SOCKSTATDESC(unixbindfail, "Unix domain socket bind failures",
			 "UnixBindFail");
	SET_SOCKSTATDESC(fdwatchbindfail, "FDwatch socket bind failures",
			 "FdwatchBindFail");
	SET_SOCKSTATDESC(udp4connectfail, "UDP/IPv4 socket connect failures",
			 "UDP4ConnFail");
	SET_SOCKSTATDESC(udp6connectfail, "UDP/IPv6 socket connect failures",
			 "UDP6ConnFail");
	SET_SOCKSTATDESC(tcp4connectfail, "TCP/IPv4 socket connect failures",
			 "TCP4ConnFail");
	SET_SOCKSTATDESC(tcp6connectfail, "TCP/IPv6 socket connect failures",
			 "TCP6ConnFail");
	SET_SOCKSTATDESC(unixconnectfail, "Unix domain socket connect failures",
			 "UnixConnFail");
	SET_SOCKSTATDESC(fdwatchconnectfail, "FDwatch socket connect failures",
			 "FDwatchConnFail");
	SET_SOCKSTATDESC(udp4connect, "UDP/IPv4 connections established",
			 "UDP4Conn");
	SET_SOCKSTATDESC(udp6connect, "UDP/IPv6 connections established",
			 "UDP6Conn");
	SET_SOCKSTATDESC(tcp4connect, "TCP/IPv4 connections established",
			 "TCP4Conn");
	SET_SOCKSTATDESC(tcp6connect, "TCP/IPv6 connections established",
			 "TCP6Conn");
	SET_SOCKSTATDESC(unixconnect, "Unix domain connections established",
			 "UnixConn");
	SET_SOCKSTATDESC(fdwatchconnect,
			 "FDwatch domain connections established",
			 "FDwatchConn");
	SET_SOCKSTATDESC(tcp4acceptfail, "TCP/IPv4 connection accept failures",
			 "TCP4AcceptFail");
	SET_SOCKSTATDESC(tcp6acceptfail, "TCP/IPv6 connection accept failures",
			 "TCP6AcceptFail");
	SET_SOCKSTATDESC(unixacceptfail,
			 "Unix domain connection accept failures",
			 "UnixAcceptFail");
	SET_SOCKSTATDESC(tcp4accept, "TCP/IPv4 connections accepted",
			 "TCP4Accept");
	SET_SOCKSTATDESC(tcp6accept, "TCP/IPv6 connections accepted",
			 "TCP6Accept");
	SET_SOCKSTATDESC(unixaccept, "Unix domain connections accepted",
			 "UnixAccept");
	SET_SOCKSTATDESC(udp4sendfail, "UDP/IPv4 send errors", "UDP4SendErr");
	SET_SOCKSTATDESC(udp6sendfail, "UDP/IPv6 send errors", "UDP6SendErr");
	SET_SOCKSTATDESC(tcp4sendfail, "TCP/IPv4 send errors", "TCP4SendErr");
	SET_SOCKSTATDESC(tcp6sendfail, "TCP/IPv6 send errors", "TCP6SendErr");
	SET_SOCKSTATDESC(unixsendfail, "Unix domain send errors",
			 "UnixSendErr");
	SET_SOCKSTATDESC(fdwatchsendfail, "FDwatch send errors",
			 "FDwatchSendErr");
	SET_SOCKSTATDESC(udp4recvfail, "UDP/IPv4 recv errors", "UDP4RecvErr");
	SET_SOCKSTATDESC(udp6recvfail, "UDP/IPv6 recv errors", "UDP6RecvErr");
	SET_SOCKSTATDESC(tcp4recvfail, "TCP/IPv4 recv errors", "TCP4RecvErr");
	SET_SOCKSTATDESC(tcp6recvfail, "TCP/IPv6 recv errors", "TCP6RecvErr");
	SET_SOCKSTATDESC(unixrecvfail, "Unix domain recv errors",
			 "UnixRecvErr");
	SET_SOCKSTATDESC(fdwatchrecvfail, "FDwatch recv errors",
			 "FDwatchRecvErr");
489
	SET_SOCKSTATDESC(rawrecvfail, "Raw recv errors", "RawRecvErr");
490
491
492
493
494
495
	SET_SOCKSTATDESC(udp4active, "UDP/IPv4 sockets active", "UDP4Active");
	SET_SOCKSTATDESC(udp6active, "UDP/IPv6 sockets active", "UDP6Active");
	SET_SOCKSTATDESC(tcp4active, "TCP/IPv4 sockets active", "TCP4Active");
	SET_SOCKSTATDESC(tcp6active, "TCP/IPv6 sockets active", "TCP6Active");
	SET_SOCKSTATDESC(unixactive, "Unix domain sockets active",
			 "UnixActive");
496
	SET_SOCKSTATDESC(rawactive, "Raw sockets active", "RawActive");
497
	INSIST(i == isc_sockstatscounter_max);
498

499
500
501
502
503
504
505
506
507
508
509
510
	/* Initialize DNSSEC statistics */
	for (i = 0; i < dns_dnssecstats_max; i++)
		dnssecstats_desc[i] = NULL;
#ifdef  HAVE_LIBXML2
	for (i = 0; i < dns_dnssecstats_max; i++)
		dnssecstats_xmldesc[i] = NULL;
#endif

#define SET_DNSSECSTATDESC(counterid, desc, xmldesc) \
	do { \
		set_desc(dns_dnssecstats_ ## counterid, \
			 dns_dnssecstats_max, \
511
			 desc, dnssecstats_desc, \
512
513
514
515
516
517
518
519
520
521
522
523
524
525
			 xmldesc, dnssecstats_xmldesc); \
		dnssecstats_index[i++] = dns_dnssecstats_ ## counterid; \
	} while (0)

	i = 0;
	SET_DNSSECSTATDESC(asis, "dnssec validation success with signer "
			   "\"as is\"", "DNSSECasis");
	SET_DNSSECSTATDESC(downcase, "dnssec validation success with signer "
			   "lower cased", "DNSSECdowncase");
	SET_DNSSECSTATDESC(wildcard, "dnssec validation of wildcard signature",
			   "DNSSECwild");
	SET_DNSSECSTATDESC(fail, "dnssec validation failures", "DNSSECfail");
	INSIST(i == dns_dnssecstats_max);

526
527
528
529
530
	/* Sanity check */
	for (i = 0; i < dns_nsstatscounter_max; i++)
		INSIST(nsstats_desc[i] != NULL);
	for (i = 0; i < dns_resstatscounter_max; i++)
		INSIST(resstats_desc[i] != NULL);
531
532
	for (i = 0; i < dns_adbstats_max; i++)
		INSIST(adbstats_desc[i] != NULL);
533
534
	for (i = 0; i < dns_zonestatscounter_max; i++)
		INSIST(zonestats_desc[i] != NULL);
535
536
	for (i = 0; i < isc_sockstatscounter_max; i++)
		INSIST(sockstats_desc[i] != NULL);
537
538
	for (i = 0; i < dns_dnssecstats_max; i++)
		INSIST(dnssecstats_desc[i] != NULL);
539
540
541
542
543
#ifdef  HAVE_LIBXML2
	for (i = 0; i < dns_nsstatscounter_max; i++)
		INSIST(nsstats_xmldesc[i] != NULL);
	for (i = 0; i < dns_resstatscounter_max; i++)
		INSIST(resstats_xmldesc[i] != NULL);
544
545
	for (i = 0; i < dns_adbstats_max; i++)
		INSIST(adbstats_xmldesc[i] != NULL);
546
547
	for (i = 0; i < dns_zonestatscounter_max; i++)
		INSIST(zonestats_xmldesc[i] != NULL);
548
549
	for (i = 0; i < isc_sockstatscounter_max; i++)
		INSIST(sockstats_xmldesc[i] != NULL);
550
551
	for (i = 0; i < dns_dnssecstats_max; i++)
		INSIST(dnssecstats_xmldesc[i] != NULL);
552
553
554
555
556
557
#endif
}

/*%
 * Dump callback functions.
 */
558
static void
559
generalstat_dump(isc_statscounter_t counter, isc_uint64_t val, void *arg) {
560
	stats_dumparg_t *dumparg = arg;
561
562
563
564
565

	REQUIRE(counter < dumparg->ncounters);
	dumparg->countervalues[counter] = val;
}

566
static isc_result_t
567
dump_counters(isc_stats_t *stats, isc_statsformat_t type, void *arg,
568
569
570
571
572
573
	      const char *category, const char **desc, int ncounters,
	      int *indices, isc_uint64_t *values, int options)
{
	int i, index;
	isc_uint64_t value;
	stats_dumparg_t dumparg;
574
575
576
	FILE *fp;
#ifdef HAVE_LIBXML2
	xmlTextWriterPtr writer;
577
	int xmlrc;
578
#endif
579
580
581
#ifdef HAVE_JSON
	json_object *job, *cat, *counter;
#endif
582

583
#if !defined(HAVE_LIBXML2) && !defined(HAVE_JSON)
584
585
586
	UNUSED(category);
#endif

587
588
589
590
	dumparg.type = type;
	dumparg.ncounters = ncounters;
	dumparg.counterindices = indices;
	dumparg.countervalues = values;
591

592
593
	memset(values, 0, sizeof(values[0]) * ncounters);
	isc_stats_dump(stats, generalstat_dump, &dumparg, options);
594

595
596
597
598
599
600
601
602
603
604
605
606
#ifdef HAVE_JSON
	cat = job = (json_object *) arg;
	if (ncounters > 0 && type == isc_statsformat_json) {
		if (category != NULL) {
			cat = json_object_new_object();
			if (cat == NULL)
				return (ISC_R_NOMEMORY);
			json_object_object_add(job, category, cat);
		}
	}
#endif

607
608
609
610
611
612
613
614
	for (i = 0; i < ncounters; i++) {
		index = indices[i];
		value = values[index];

		if (value == 0 && (options & ISC_STATSDUMP_VERBOSE) == 0)
			continue;

		switch (dumparg.type) {
615
		case isc_statsformat_file:
616
617
618
619
			fp = arg;
			fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n",
				value, desc[index]);
			break;
620
		case isc_statsformat_xml:
621
#ifdef HAVE_LIBXML2
622
			writer = (xmlTextWriterPtr) arg;
623
624

			if (category != NULL) {
625
				/* <NameOfCategory> */
626
627
628
				TRY0(xmlTextWriterStartElement(writer,
							       ISC_XMLCHAR
							       category));
629
630

				/* <name> inside category */
631
632
633
634
635
636
				TRY0(xmlTextWriterStartElement(writer,
							       ISC_XMLCHAR
							       "name"));
				TRY0(xmlTextWriterWriteString(writer,
							      ISC_XMLCHAR
							      desc[index]));
637
638
				TRY0(xmlTextWriterEndElement(writer));
				/* </name> */
639

640
				/* <counter> */
641
642
643
				TRY0(xmlTextWriterStartElement(writer,
							       ISC_XMLCHAR
							       "counter"));
644
645
646
647
648
649
650
651
				TRY0(xmlTextWriterWriteFormatString(writer,
					"%" ISC_PRINT_QUADFORMAT "u", value));

				TRY0(xmlTextWriterEndElement(writer));
				/* </counter> */
				TRY0(xmlTextWriterEndElement(writer));
				/* </NameOfCategory> */

652
			} else {
653
654
				TRY0(xmlTextWriterStartElement(writer,
							       ISC_XMLCHAR
655
656
657
658
659
660
661
662
663
664
							       "counter"));
				TRY0(xmlTextWriterWriteAttribute(writer,
								 ISC_XMLCHAR
								 "name",
								 ISC_XMLCHAR
								 desc[index]));
				TRY0(xmlTextWriterWriteFormatString(writer,
					"%" ISC_PRINT_QUADFORMAT "u", value));
				TRY0(xmlTextWriterEndElement(writer));
				/* counter */
665
			}
666

667
#endif
668
			break;
669
670
671
672
673
674
675
		case isc_statsformat_json:
#ifdef HAVE_JSON
			counter = json_object_new_int64(value);
			if (counter == NULL)
				return (ISC_R_NOMEMORY);
			json_object_object_add(cat, desc[index], counter);
#endif
Mark Andrews's avatar
Mark Andrews committed
676
			break;
677
		}
678
	}
679
680
681
	return (ISC_R_SUCCESS);
#ifdef HAVE_LIBXML2
 error:
682
683
	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
		      ISC_LOG_ERROR, "failed at dump_counters()");
684
685
	return (ISC_R_FAILURE);
#endif
686
687
688
689
690
691
692
693
694
695
}

static void
rdtypestat_dump(dns_rdatastatstype_t type, isc_uint64_t val, void *arg) {
	char typebuf[64];
	const char *typestr;
	stats_dumparg_t *dumparg = arg;
	FILE *fp;
#ifdef HAVE_LIBXML2
	xmlTextWriterPtr writer;
696
	int xmlrc;
697
#endif
698
699
700
#ifdef HAVE_JSON
	json_object *zoneobj, *obj;
#endif
701
702
703
704
705
706
707
708
709
710

	if ((DNS_RDATASTATSTYPE_ATTR(type) & DNS_RDATASTATSTYPE_ATTR_OTHERTYPE)
	    == 0) {
		dns_rdatatype_format(DNS_RDATASTATSTYPE_BASE(type), typebuf,
				     sizeof(typebuf));
		typestr = typebuf;
	} else
		typestr = "Others";

	switch (dumparg->type) {
711
	case isc_statsformat_file:
712
713
714
		fp = dumparg->arg;
		fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n", val, typestr);
		break;
715
	case isc_statsformat_xml:
716
717
718
719
#ifdef HAVE_LIBXML2
		writer = dumparg->arg;


720
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counter"));
721
722
723
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "name",
						 ISC_XMLCHAR typestr));

724
		TRY0(xmlTextWriterWriteFormatString(writer,
725
					       "%" ISC_PRINT_QUADFORMAT "u",
726
					       val));
727

728
		TRY0(xmlTextWriterEndElement(writer)); /* type */
729
730
731
732
733
734
735
736
737
#endif
		break;
	case isc_statsformat_json:
#ifdef HAVE_JSON
		zoneobj = (json_object *) dumparg->arg;
		obj = json_object_new_int64(val);
		if (obj == NULL)
			return;
		json_object_object_add(zoneobj, typestr, obj);
738
739
740
#endif
		break;
	}
741
742
743
	return;
#ifdef HAVE_LIBXML2
 error:
744
745
	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
		      ISC_LOG_ERROR, "failed at rdtypestat_dump()");
746
747
748
	dumparg->result = ISC_R_FAILURE;
	return;
#endif
749
750
751
752
753
754
755
756
757
}

static void
rdatasetstats_dump(dns_rdatastatstype_t type, isc_uint64_t val, void *arg) {
	stats_dumparg_t *dumparg = arg;
	FILE *fp;
	char typebuf[64];
	const char *typestr;
	isc_boolean_t nxrrset = ISC_FALSE;
758
	isc_boolean_t stale = ISC_FALSE;
759
760
#ifdef HAVE_LIBXML2
	xmlTextWriterPtr writer;
761
	int xmlrc;
762
#endif
763
764
765
766
#ifdef HAVE_JSON
	json_object *zoneobj, *obj;
	char buf[1024];
#endif
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783

	if ((DNS_RDATASTATSTYPE_ATTR(type) & DNS_RDATASTATSTYPE_ATTR_NXDOMAIN)
	    != 0) {
		typestr = "NXDOMAIN";
	} else if ((DNS_RDATASTATSTYPE_ATTR(type) &
		    DNS_RDATASTATSTYPE_ATTR_OTHERTYPE) != 0) {
		typestr = "Others";
	} else {
		dns_rdatatype_format(DNS_RDATASTATSTYPE_BASE(type), typebuf,
				     sizeof(typebuf));
		typestr = typebuf;
	}

	if ((DNS_RDATASTATSTYPE_ATTR(type) & DNS_RDATASTATSTYPE_ATTR_NXRRSET)
	    != 0)
		nxrrset = ISC_TRUE;

784
785
786
787
	if ((DNS_RDATASTATSTYPE_ATTR(type) & DNS_RDATASTATSTYPE_ATTR_STALE)
	    != 0)
		stale = ISC_TRUE;

788
	switch (dumparg->type) {
789
	case isc_statsformat_file:
790
		fp = dumparg->arg;
791
792
		fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s%s%s\n", val,
			stale ? "#" : "", nxrrset ? "!" : "", typestr);
793
		break;
794
	case isc_statsformat_xml:
795
796
797
#ifdef HAVE_LIBXML2
		writer = dumparg->arg;

798
799
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "rrset"));
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"));
800
801
		TRY0(xmlTextWriterWriteFormatString(writer, "%s%s%s",
					       stale ? "#" : "",
802
803
					       nxrrset ? "!" : "", typestr));
		TRY0(xmlTextWriterEndElement(writer)); /* name */
804

805
806
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counter"));
		TRY0(xmlTextWriterWriteFormatString(writer,
807
					       "%" ISC_PRINT_QUADFORMAT "u",
808
809
					       val));
		TRY0(xmlTextWriterEndElement(writer)); /* counter */
810

811
		TRY0(xmlTextWriterEndElement(writer)); /* rrset */
812
813
814
815
816
817
818
819
820
821
822
#endif
		break;
	case isc_statsformat_json:
#ifdef HAVE_JSON
		zoneobj = (json_object *) dumparg->arg;
		sprintf(buf, "%s%s%s", stale ? "#" : "",
				       nxrrset ? "!" : "", typestr);
		obj = json_object_new_int64(val);
		if (obj == NULL)
			return;
		json_object_object_add(zoneobj, buf, obj);
823
824
825
#endif
		break;
	}
826
827
828
	return;
#ifdef HAVE_LIBXML2
 error:
829
830
	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
		      ISC_LOG_ERROR, "failed at rdatasetstats_dump()");
831
832
833
	dumparg->result = ISC_R_FAILURE;
#endif

834
835
836
837
}

static void
opcodestat_dump(dns_opcode_t code, isc_uint64_t val, void *arg) {
838
	FILE *fp;
839
840
841
842
843
	isc_buffer_t b;
	char codebuf[64];
	stats_dumparg_t *dumparg = arg;
#ifdef HAVE_LIBXML2
	xmlTextWriterPtr writer;
844
	int xmlrc;
845
#endif
846
847
848
#ifdef HAVE_JSON
	json_object *zoneobj, *obj;
#endif
849
850
851
852
853
854

	isc_buffer_init(&b, codebuf, sizeof(codebuf) - 1);
	dns_opcode_totext(code, &b);
	codebuf[isc_buffer_usedlength(&b)] = '\0';

	switch (dumparg->type) {
855
	case isc_statsformat_file:
856
857
858
		fp = dumparg->arg;
		fprintf(fp, "%20" ISC_PRINT_QUADFORMAT "u %s\n", val, codebuf);
		break;
859
	case isc_statsformat_xml:
860
861
#ifdef HAVE_LIBXML2
		writer = dumparg->arg;
862
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counter"));
863
864
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "name",
						 ISC_XMLCHAR codebuf ));
865
		TRY0(xmlTextWriterWriteFormatString(writer,
866
867
						       "%" ISC_PRINT_QUADFORMAT "u",
						       val));
868
		TRY0(xmlTextWriterEndElement(writer)); /* counter */
869
870
871
872
873
874
875
876
877
#endif
		break;
	case isc_statsformat_json:
#ifdef HAVE_JSON
		zoneobj = (json_object *) dumparg->arg;
		obj = json_object_new_int64(val);
		if (obj == NULL)
			return;
		json_object_object_add(zoneobj, codebuf, obj);
878
879
880
#endif
		break;
	}
881
882
883
884
	return;

#ifdef HAVE_LIBXML2
 error:
885
886
	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
		      ISC_LOG_ERROR, "failed at opcodestat_dump()");
887
888
889
	dumparg->result = ISC_R_FAILURE;
	return;
#endif
890
891
}

892
#ifdef HAVE_LIBXML2
Evan Hunt's avatar
Evan Hunt committed
893
894
895
896
897
898
899
900
901
902
/*
 * Which statistics to include when rendering to XML
 */
#define STATS_XML_STATUS	0x00	/* display only common statistics */
#define STATS_XML_SERVER	0x01
#define STATS_XML_ZONES		0x02
#define STATS_XML_TASKS		0x04
#define STATS_XML_NET		0x08
#define STATS_XML_MEM		0x10
#define STATS_XML_ALL		0xff
903

904
905
static isc_result_t
zone_xmlrender(dns_zone_t *zone, void *arg) {
906
	isc_result_t result;
907
	char buf[1024 + 32];	/* sufficiently large for zone name and class */
908
	char *zone_name_only = NULL;
909
910
911
	dns_rdataclass_t rdclass;
	isc_uint32_t serial;
	xmlTextWriterPtr writer = arg;
912
	isc_stats_t *zonestats;
913
	dns_stats_t *rcvquerystats;
914
	dns_zonestat_level_t statlevel;
915
	isc_uint64_t nsstat_values[dns_nsstatscounter_max];
916
	int xmlrc;
Mark Andrews's avatar
Mark Andrews committed
917
	stats_dumparg_t dumparg;
918
919
920
921

	statlevel = dns_zone_getstatlevel(zone);
	if (statlevel == dns_zonestat_none)
		return (ISC_R_SUCCESS);
922

923
924
925
926
	dumparg.type = isc_statsformat_xml;
	dumparg.arg = writer;

	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "zone"));
927
	dns_zone_name(zone, buf, sizeof(buf));
928
	zone_name_only = strtok(buf, "/");
929
	if(zone_name_only == NULL)
930
		zone_name_only = buf;
931

932
933
	TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "name",
					 ISC_XMLCHAR zone_name_only));
934
935
	rdclass = dns_zone_getclass(zone);
	dns_rdataclass_format(rdclass, buf, sizeof(buf));
936
937
	TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "rdataclass",
					 ISC_XMLCHAR buf));
938

939
	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "serial"));
940
	if (dns_zone_getserial2(zone, &serial) == ISC_R_SUCCESS)
941
		TRY0(xmlTextWriterWriteFormatString(writer, "%u", serial));
942
	else
943
		TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR "-"));
944
	TRY0(xmlTextWriterEndElement(writer)); /* serial */
945
946

	zonestats = dns_zone_getrequeststats(zone);
947
	rcvquerystats = dns_zone_getrcvquerystats(zone);
948
	if (statlevel == dns_zonestat_full && zonestats != NULL) {
949
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
950
951
952
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "rcode"));

953
954
955
956
		result = dump_counters(zonestats, isc_statsformat_xml, writer,
				       NULL, nsstats_xmldesc,
				       dns_nsstatscounter_max, nsstats_index,
				       nsstat_values, ISC_STATSDUMP_VERBOSE);
957
958
		if (result != ISC_R_SUCCESS)
			goto error;
959
960
961
962
		/* counters type="rcode"*/
		TRY0(xmlTextWriterEndElement(writer));
	}

963
	if (statlevel == dns_zonestat_full && rcvquerystats != NULL) {
964
965
966
967
968
969
970
971
972
973
974
975
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "qtype"));

		dumparg.result = ISC_R_SUCCESS;
		dns_rdatatypestats_dump(rcvquerystats, rdtypestat_dump,
					&dumparg, 0);
		if(dumparg.result != ISC_R_SUCCESS)
			goto error;

		/* counters type="qtype"*/
		TRY0(xmlTextWriterEndElement(writer));
976
977
	}

978
	TRY0(xmlTextWriterEndElement(writer)); /* zone */
979
980

	return (ISC_R_SUCCESS);
981
 error:
982
983
	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
		      ISC_LOG_ERROR, "Failed at zone_xmlrender()");
984
	return (ISC_R_FAILURE);
985
}
986

987
static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
988
989
990
generatexml(ns_server_t *server, isc_uint32_t flags,
	    int *buflen, xmlChar **buf)
{
991
	char boottime[sizeof "yyyy-mm-ddThh:mm:ssZ"];
992
	char configtime[sizeof "yyyy-mm-ddThh:mm:ssZ"];
993
994
	char nowstr[sizeof "yyyy-mm-ddThh:mm:ssZ"];
	isc_time_t now;
995
996
	xmlTextWriterPtr writer = NULL;
	xmlDocPtr doc = NULL;
997
998
	int xmlrc;
	dns_view_t *view;
999
	stats_dumparg_t dumparg;
1000
	dns_stats_t *cacherrstats;
1001
1002
	isc_uint64_t nsstat_values[dns_nsstatscounter_max];
	isc_uint64_t resstat_values[dns_resstatscounter_max];
1003
	isc_uint64_t adbstat_values[dns_adbstats_max];
1004
1005
	isc_uint64_t zonestat_values[dns_zonestatscounter_max];
	isc_uint64_t sockstat_values[isc_sockstatscounter_max];
1006
	isc_result_t result;
1007
1008
1009

	isc_time_now(&now);
	isc_time_formatISO8601(&ns_g_boottime, boottime, sizeof boottime);
1010
	isc_time_formatISO8601(&ns_g_configtime, configtime, sizeof configtime);
1011
1012
1013
	isc_time_formatISO8601(&now, nowstr, sizeof nowstr);

	writer = xmlNewTextWriterDoc(&doc, 0);
1014
1015
	if (writer == NULL)
		goto error;
1016
1017
1018
1019
1020
	TRY0(xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL));
	TRY0(xmlTextWriterWritePI(writer, ISC_XMLCHAR "xml-stylesheet",
			ISC_XMLCHAR "type=\"text/xsl\" href=\"/bind9.xsl\""));
	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "statistics"));
	TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "version",
1021
					 ISC_XMLCHAR "3.5"));
1022

1023
	/* Set common fields for statistics dump */
1024
	dumparg.type = isc_statsformat_xml;
1025
1026
	dumparg.arg = writer;

Evan Hunt's avatar
Evan Hunt committed
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
	/* Render server information */
	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "server"));
	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "boot-time"));
	TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR boottime));
	TRY0(xmlTextWriterEndElement(writer)); /* boot-time */
	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "config-time"));
	TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR configtime));
	TRY0(xmlTextWriterEndElement(writer)); /* config-time */
	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "current-time"));
	TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR nowstr));
	TRY0(xmlTextWriterEndElement(writer));  /* current-time */

	if ((flags & STATS_XML_SERVER) != 0) {
		dumparg.result = ISC_R_SUCCESS;

		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "opcode"));

		dns_opcodestats_dump(server->opcodestats, opcodestat_dump,
				     &dumparg, ISC_STATSDUMP_VERBOSE);
		if (dumparg.result != ISC_R_SUCCESS)
			goto error;

		TRY0(xmlTextWriterEndElement(writer));

		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "qtype"));

		dumparg.result = ISC_R_SUCCESS;
		dns_rdatatypestats_dump(server->rcvquerystats, rdtypestat_dump,
					&dumparg, 0);
		if (dumparg.result != ISC_R_SUCCESS)
			goto error;
		TRY0(xmlTextWriterEndElement(writer)); /* counters */

		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "nsstat"));

		result = dump_counters(server->nsstats, isc_statsformat_xml,
				       writer, NULL, nsstats_xmldesc,
				       dns_nsstatscounter_max,
				       nsstats_index, nsstat_values,
				       ISC_STATSDUMP_VERBOSE);
		if (result != ISC_R_SUCCESS)
			goto error;

		TRY0(xmlTextWriterEndElement(writer)); /* /nsstat */

		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "zonestat"));

		result = dump_counters(server->zonestats, isc_statsformat_xml,
				       writer, NULL, zonestats_xmldesc,
				       dns_zonestatscounter_max,
				       zonestats_index, zonestat_values,
				       ISC_STATSDUMP_VERBOSE);
		if (result != ISC_R_SUCCESS)
			goto error;

		TRY0(xmlTextWriterEndElement(writer)); /* /zonestat */

		/*
		 * Most of the common resolver statistics entries are 0, so
		 * we don't use the verbose dump here.
		 */
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "resstat"));
		result = dump_counters(server->resolverstats,
				       isc_statsformat_xml, writer,
				       NULL, resstats_xmldesc,
				       dns_resstatscounter_max,
				       resstats_index, resstat_values, 0);
		if (result != ISC_R_SUCCESS)
			goto error;
		TRY0(xmlTextWriterEndElement(writer)); /* resstat */
	}

	if ((flags & STATS_XML_NET) != 0) {
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "sockstat"));

		result = dump_counters(server->sockstats, isc_statsformat_xml,
				       writer, NULL, sockstats_xmldesc,
				       isc_sockstatscounter_max,
				       sockstats_index, sockstat_values,
				       ISC_STATSDUMP_VERBOSE);
		if (result != ISC_R_SUCCESS)
			goto error;

		TRY0(xmlTextWriterEndElement(writer)); /* /sockstat */
	}
	TRY0(xmlTextWriterEndElement(writer)); /* /server */

1126
	/*
Evan Hunt's avatar
Evan Hunt committed
1127
1128
	 * Render views.  For each view we know of, call its
	 * rendering function.
1129
1130
1131
	 */
	view = ISC_LIST_HEAD(server->viewlist);
	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "views"));
Evan Hunt's avatar
Evan Hunt committed
1132
1133
1134
	while (view != NULL &&
	       ((flags & (STATS_XML_SERVER | STATS_XML_ZONES)) != 0))
	{
1135
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "view"));
1136
1137
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "name",
						 ISC_XMLCHAR view->name));
1138

Evan Hunt's avatar
Evan Hunt committed
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
		if ((flags & STATS_XML_ZONES) != 0) {
			TRY0(xmlTextWriterStartElement(writer,
						       ISC_XMLCHAR "zones"));
			result = dns_zt_apply(view->zonetable, ISC_TRUE,
					      zone_xmlrender, writer);
			if (result != ISC_R_SUCCESS)
				goto error;
			TRY0(xmlTextWriterEndElement(writer)); /* /zones */
		}

		if ((flags & STATS_XML_SERVER) == 0) {
			TRY0(xmlTextWriterEndElement(writer)); /* /view */
			view = ISC_LIST_NEXT(view, link);
			continue;
		}
1154
1155
1156
1157
1158

		TRY0(xmlTextWriterStartElement(writer,
					       ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "resqtype"));
1159
1160

		if (view->resquerystats != NULL) {
1161
			dumparg.result = ISC_R_SUCCESS;
1162
1163
			dns_rdatatypestats_dump(view->resquerystats,
						rdtypestat_dump, &dumparg, 0);
1164
1165
			if (dumparg.result != ISC_R_SUCCESS)
				goto error;
1166
		}
1167
		TRY0(xmlTextWriterEndElement(writer));
1168

1169
1170
1171
1172
		/* <resstats> */
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "resstats"));
1173
		if (view->resstats != NULL) {
1174
1175
			result = dump_counters(view->resstats,
					       isc_statsformat_xml, writer,
1176
					       NULL, resstats_xmldesc,
1177
1178
1179
1180
1181
					       dns_resstatscounter_max,
					       resstats_index, resstat_values,
					       ISC_STATSDUMP_VERBOSE);
			if (result != ISC_R_SUCCESS)
				goto error;
1182
		}
1183
		TRY0(xmlTextWriterEndElement(writer)); /* </resstats> */
1184

1185
1186
		cacherrstats = dns_db_getrrsetstats(view->cachedb);
		if (cacherrstats != NULL) {
1187
1188
1189
1190
1191
1192
			TRY0(xmlTextWriterStartElement(writer,
						       ISC_XMLCHAR "cache"));
			TRY0(xmlTextWriterWriteAttribute(writer,
					 ISC_XMLCHAR "name",
					 ISC_XMLCHAR
					 dns_cache_getname(view->cache)));
1193
			dumparg.result = ISC_R_SUCCESS;
1194
			dns_rdatasetstats_dump(cacherrstats, rdatasetstats_dump,
1195
					       &dumparg, 0);
1196
1197
			if (dumparg.result != ISC_R_SUCCESS)
				goto error;
1198
			TRY0(xmlTextWriterEndElement(writer)); /* cache */
1199
1200
		}

1201
1202
1203
1204
		/* <adbstats> */
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "adbstat"));
1205
		if (view->adbstats != NULL) {
1206
1207
			result = dump_counters(view->adbstats,
					       isc_statsformat_xml, writer,
1208
					       NULL, adbstats_xmldesc,
1209
1210
1211
1212
1213
1214
					       dns_adbstats_max,
					       adbstats_index, adbstat_values,
					       ISC_STATSDUMP_VERBOSE);
			if (result != ISC_R_SUCCESS)
				goto error;
		}
1215
1216
1217
1218
1219
1220
1221
1222
		TRY0(xmlTextWriterEndElement(writer)); /* </adbstats> */

		/* <cachestats> */
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counters"));
		TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "type",
						 ISC_XMLCHAR "cachestats"));
		TRY0(dns_cache_renderxml(view->cache, writer));
		TRY0(xmlTextWriterEndElement(writer)); /* </cachestats> */
1223

1224
		TRY0(xmlTextWriterEndElement(writer)); /* view */
1225

1226
1227
		view = ISC_LIST_NEXT(view, link);
	}
Evan Hunt's avatar
Evan Hunt committed
1228
	TRY0(xmlTextWriterEndElement(writer)); /* /views */
1229

Evan Hunt's avatar
Evan Hunt committed
1230
1231
1232
1233
1234
1235
	if ((flags & STATS_XML_NET) != 0) {
		TRY0(xmlTextWriterStartElement(writer,
					       ISC_XMLCHAR "socketmgr"));
		TRY0(isc_socketmgr_renderxml(ns_g_socketmgr, writer));
		TRY0(xmlTextWriterEndElement(writer)); /* /socketmgr */
	}
1236

Evan Hunt's avatar
Evan Hunt committed
1237
1238
1239
1240
1241
	if ((flags & STATS_XML_TASKS) != 0) {
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "taskmgr"));
		TRY0(isc_taskmgr_renderxml(ns_g_taskmgr, writer));
		TRY0(xmlTextWriterEndElement(writer)); /* /taskmgr */
	}
1242

Evan Hunt's avatar
Evan Hunt committed
1243
1244
1245
1246
1247
	if ((flags & STATS_XML_MEM) != 0) {
		TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "memory"));
		TRY0(isc_mem_renderxml(writer));
		TRY0(xmlTextWriterEndElement(writer)); /* /memory */
	}
1248

Evan Hunt's avatar
Evan Hunt committed
1249
	TRY0(xmlTextWriterEndElement(writer)); /* /statistics */
1250
1251
1252
1253
	TRY0(xmlTextWriterEndDocument(writer));

	xmlFreeTextWriter(writer);

1254
	xmlDocDumpFormatMemoryEnc(doc, buf, buflen, "UTF-8", 0);