Browse code

core:dns Added failed DNS requests statistics when dns cache is not used

DNS statistics can be seen with:

sercmd> cnt.get dns failed_dns_request

Still TODO: failed statistics for DNS cache

Marius Zbihlei authored on 11/08/2010 14:54:27
Showing 2 changed files
... ...
@@ -77,7 +77,13 @@
77 77
 #include "dns_cache.h"
78 78
 #endif
79 79
 
80
-
80
+/* counters framework */
81
+struct dns_counters_h dns_cnts_h;
82
+counter_def_t dns_cnt_defs[] =  {
83
+	{&dns_cnts_h.failed_dns_req, "failed_dns_request", 0, 0, 0,
84
+		"incremented each time a DNS request has failed."},
85
+	{0, 0, 0, 0, 0, 0 }
86
+};
81 87
 
82 88
 /* mallocs for local stuff */
83 89
 #define local_malloc pkg_malloc
... ...
@@ -119,6 +125,18 @@ int register_resolv_reinit_cb(on_resolv_reinit cb)
119 119
 }
120 120
 #endif
121 121
 
122
+/* counter init function
123
+  must be called before fork
124
+*/
125
+static int stat_init()
126
+{
127
+	if (counter_register_array("dns", dns_cnt_defs) < 0)
128
+		goto error;
129
+	return 0;
130
+error:
131
+	return -1;
132
+}
133
+
122 134
 /* init. the resolver
123 135
  * params: retr_time  - time before retransmitting (must be >0)
124 136
  *         retr_no    - retransmissions number
... ...
@@ -157,12 +175,17 @@ static int _resolv_init()
157 157
 /* wrapper function to initialize the resolver at startup */
158 158
 int resolv_init()
159 159
 {
160
+	int res = -1;
160 161
 	_resolv_init();
161 162
 
162 163
 #ifdef USE_NAPTR
163 164
 	init_naptr_proto_prefs();
164 165
 #endif
165
-	return 0;
166
+	/* init counter API only at startup
167
+	 * This function must be called before DNS cache init method (if available)
168
+	 */
169
+	res = stat_init();
170
+	return res;
166 171
 }
167 172
 
168 173
 /* wrapper function to reinitialize the resolver
... ...
@@ -1486,11 +1509,17 @@ end:
1486 1486
  */
1487 1487
 struct hostent* _sip_resolvehost(str* name, unsigned short* port, char* proto)
1488 1488
 {
1489
+	struct hostent* res = NULL;
1489 1490
 #ifdef USE_NAPTR
1490 1491
 	if (cfg_get(core, core_cfg, dns_try_naptr))
1491
-		return naptr_sip_resolvehost(name, port, proto);
1492
+		res = naptr_sip_resolvehost(name, port, proto);
1492 1493
 #endif
1493
-	return srv_sip_resolvehost(name, 0, port, proto, 0, 0);
1494
+	res = srv_sip_resolvehost(name, 0, port, proto, 0, 0);
1495
+	if( unlikely(!res) ){
1496
+		/* failed DNS request */
1497
+		counter_inc(dns_cnts_h.failed_dns_req);
1498
+	}
1499
+	return res;
1494 1500
 }
1495 1501
 
1496 1502
 
... ...
@@ -47,6 +47,7 @@
47 47
 #include <netdb.h>
48 48
 #include <arpa/nameser.h>
49 49
 #include <resolv.h>
50
+#include "counters.h"
50 51
 
51 52
 #ifdef __OS_darwin
52 53
 #include <arpa/nameser_compat.h>
... ...
@@ -78,6 +79,14 @@
78 78
 #define RES_ONLY_TYPE 1   /* return only the specified type records */
79 79
 #define RES_AR		  2   /* return also the additional records */
80 80
 
81
+/* counter for failed DNS requests
82
+*/
83
+struct dns_counters_h {
84
+    counter_handle_t failed_dns_req;
85
+};
86
+
87
+extern struct dns_counters_h dns_cnts_h;
88
+
81 89
 /* query union*/
82 90
 union dns_query{
83 91
 	HEADER hdr;