unbound.chart.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # -*- coding: utf-8 -*-
  2. # Description: unbound netdata python.d module
  3. # Author: Austin S. Hemmelgarn (Ferroin)
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. import os
  6. import sys
  7. from copy import deepcopy
  8. from bases.FrameworkServices.SocketService import SocketService
  9. from bases.loaders import load_config
  10. PRECISION = 1000
  11. ORDER = [
  12. 'queries',
  13. 'recursion',
  14. 'reqlist',
  15. ]
  16. CHARTS = {
  17. 'queries': {
  18. 'options': [None, 'Queries Processed', 'queries', 'Unbound', 'unbound.queries', 'line'],
  19. 'lines': [
  20. ['ratelimit', 'ratelimited', 'absolute', 1, 1],
  21. ['cachemiss', 'cache_miss', 'absolute', 1, 1],
  22. ['cachehit', 'cache_hit', 'absolute', 1, 1],
  23. ['expired', 'expired', 'absolute', 1, 1],
  24. ['prefetch', 'prefetched', 'absolute', 1, 1],
  25. ['recursive', 'recursive', 'absolute', 1, 1]
  26. ]
  27. },
  28. 'recursion': {
  29. 'options': [None, 'Recursion Timings', 'seconds', 'Unbound', 'unbound.recursion', 'line'],
  30. 'lines': [
  31. ['recursive_avg', 'average', 'absolute', 1, PRECISION],
  32. ['recursive_med', 'median', 'absolute', 1, PRECISION]
  33. ]
  34. },
  35. 'reqlist': {
  36. 'options': [None, 'Request List', 'items', 'Unbound', 'unbound.reqlist', 'line'],
  37. 'lines': [
  38. ['reqlist_avg', 'average_size', 'absolute', 1, 1],
  39. ['reqlist_max', 'maximum_size', 'absolute', 1, 1],
  40. ['reqlist_overwritten', 'overwritten_requests', 'absolute', 1, 1],
  41. ['reqlist_exceeded', 'overruns', 'absolute', 1, 1],
  42. ['reqlist_current', 'current_size', 'absolute', 1, 1],
  43. ['reqlist_user', 'user_requests', 'absolute', 1, 1]
  44. ]
  45. }
  46. }
  47. # These get added too if we are told to use extended stats.
  48. EXTENDED_ORDER = ['cache']
  49. EXTENDED_CHARTS = {
  50. 'cache': {
  51. 'options': [None, 'Cache Sizes', 'items', 'Unbound', 'unbound.cache', 'stacked'],
  52. 'lines': [
  53. ['cache_message', 'message_cache', 'absolute', 1, 1],
  54. ['cache_rrset', 'rrset_cache', 'absolute', 1, 1],
  55. ['cache_infra', 'infra_cache', 'absolute', 1, 1],
  56. ['cache_key', 'dnssec_key_cache', 'absolute', 1, 1],
  57. ['cache_dnscss', 'dnscrypt_Shared_Secret_cache', 'absolute', 1, 1],
  58. ['cache_dnscn', 'dnscrypt_Nonce_cache', 'absolute', 1, 1]
  59. ]
  60. }
  61. }
  62. # This is used as a templates for the per-thread charts.
  63. PER_THREAD_CHARTS = {
  64. '_queries': {
  65. 'options': [None, '{longname} Queries Processed', 'queries', 'Queries Processed',
  66. 'unbound.threads.queries', 'line'],
  67. 'lines': [
  68. ['{shortname}_ratelimit', 'ratelimited', 'absolute', 1, 1],
  69. ['{shortname}_cachemiss', 'cache_miss', 'absolute', 1, 1],
  70. ['{shortname}_cachehit', 'cache_hit', 'absolute', 1, 1],
  71. ['{shortname}_expired', 'expired', 'absolute', 1, 1],
  72. ['{shortname}_prefetch', 'prefetched', 'absolute', 1, 1],
  73. ['{shortname}_recursive', 'recursive', 'absolute', 1, 1]
  74. ]
  75. },
  76. '_recursion': {
  77. 'options': [None, '{longname} Recursion Timings', 'seconds', 'Recursive Timings',
  78. 'unbound.threads.recursion', 'line'],
  79. 'lines': [
  80. ['{shortname}_recursive_avg', 'average', 'absolute', 1, PRECISION],
  81. ['{shortname}_recursive_med', 'median', 'absolute', 1, PRECISION]
  82. ]
  83. },
  84. '_reqlist': {
  85. 'options': [None, '{longname} Request List', 'items', 'Request List', 'unbound.threads.reqlist', 'line'],
  86. 'lines': [
  87. ['{shortname}_reqlist_avg', 'average_size', 'absolute', 1, 1],
  88. ['{shortname}_reqlist_max', 'maximum_size', 'absolute', 1, 1],
  89. ['{shortname}_reqlist_overwritten', 'overwritten_requests', 'absolute', 1, 1],
  90. ['{shortname}_reqlist_exceeded', 'overruns', 'absolute', 1, 1],
  91. ['{shortname}_reqlist_current', 'current_size', 'absolute', 1, 1],
  92. ['{shortname}_reqlist_user', 'user_requests', 'absolute', 1, 1]
  93. ]
  94. }
  95. }
  96. # This maps the Unbound stat names to our names and precision requiremnets.
  97. STAT_MAP = {
  98. 'total.num.queries_ip_ratelimited': ('ratelimit', 1),
  99. 'total.num.cachehits': ('cachehit', 1),
  100. 'total.num.cachemiss': ('cachemiss', 1),
  101. 'total.num.zero_ttl': ('expired', 1),
  102. 'total.num.prefetch': ('prefetch', 1),
  103. 'total.num.recursivereplies': ('recursive', 1),
  104. 'total.requestlist.avg': ('reqlist_avg', 1),
  105. 'total.requestlist.max': ('reqlist_max', 1),
  106. 'total.requestlist.overwritten': ('reqlist_overwritten', 1),
  107. 'total.requestlist.exceeded': ('reqlist_exceeded', 1),
  108. 'total.requestlist.current.all': ('reqlist_current', 1),
  109. 'total.requestlist.current.user': ('reqlist_user', 1),
  110. 'total.recursion.time.avg': ('recursive_avg', PRECISION),
  111. 'total.recursion.time.median': ('recursive_med', PRECISION),
  112. 'msg.cache.count': ('cache_message', 1),
  113. 'rrset.cache.count': ('cache_rrset', 1),
  114. 'infra.cache.count': ('cache_infra', 1),
  115. 'key.cache.count': ('cache_key', 1),
  116. 'dnscrypt_shared_secret.cache.count': ('cache_dnscss', 1),
  117. 'dnscrypt_nonce.cache.count': ('cache_dnscn', 1)
  118. }
  119. # Same as above, but for per-thread stats.
  120. PER_THREAD_STAT_MAP = {
  121. '{shortname}.num.queries_ip_ratelimited': ('{shortname}_ratelimit', 1),
  122. '{shortname}.num.cachehits': ('{shortname}_cachehit', 1),
  123. '{shortname}.num.cachemiss': ('{shortname}_cachemiss', 1),
  124. '{shortname}.num.zero_ttl': ('{shortname}_expired', 1),
  125. '{shortname}.num.prefetch': ('{shortname}_prefetch', 1),
  126. '{shortname}.num.recursivereplies': ('{shortname}_recursive', 1),
  127. '{shortname}.requestlist.avg': ('{shortname}_reqlist_avg', 1),
  128. '{shortname}.requestlist.max': ('{shortname}_reqlist_max', 1),
  129. '{shortname}.requestlist.overwritten': ('{shortname}_reqlist_overwritten', 1),
  130. '{shortname}.requestlist.exceeded': ('{shortname}_reqlist_exceeded', 1),
  131. '{shortname}.requestlist.current.all': ('{shortname}_reqlist_current', 1),
  132. '{shortname}.requestlist.current.user': ('{shortname}_reqlist_user', 1),
  133. '{shortname}.recursion.time.avg': ('{shortname}_recursive_avg', PRECISION),
  134. '{shortname}.recursion.time.median': ('{shortname}_recursive_med', PRECISION)
  135. }
  136. # Used to actually generate per-thread charts.
  137. def _get_perthread_info(thread):
  138. sname = 'thread{0}'.format(thread)
  139. lname = 'Thread {0}'.format(thread)
  140. charts = dict()
  141. order = []
  142. statmap = dict()
  143. for item in PER_THREAD_CHARTS:
  144. cname = '{0}{1}'.format(sname, item)
  145. chart = deepcopy(PER_THREAD_CHARTS[item])
  146. chart['options'][1] = chart['options'][1].format(longname=lname)
  147. for index, line in enumerate(chart['lines']):
  148. chart['lines'][index][0] = line[0].format(shortname=sname)
  149. order.append(cname)
  150. charts[cname] = chart
  151. for key, value in PER_THREAD_STAT_MAP.items():
  152. statmap[key.format(shortname=sname)] = (value[0].format(shortname=sname), value[1])
  153. return charts, order, statmap
  154. class Service(SocketService):
  155. def __init__(self, configuration=None, name=None):
  156. # The unbound control protocol is always TLS encapsulated
  157. # unless it's used over a UNIX socket, so enable TLS _before_
  158. # doing the normal SocketService initialization.
  159. configuration['tls'] = True
  160. self.port = 8935
  161. SocketService.__init__(self, configuration, name)
  162. self.ext = self.configuration.get('extended', None)
  163. self.ubconf = self.configuration.get('ubconf', None)
  164. self.perthread = self.configuration.get('per_thread', False)
  165. self.threads = None
  166. self.order = deepcopy(ORDER)
  167. self.definitions = deepcopy(CHARTS)
  168. self.request = 'UBCT1 stats\n'
  169. self.statmap = deepcopy(STAT_MAP)
  170. self._parse_config()
  171. self._auto_config()
  172. self.debug('Extended stats: {0}'.format(self.ext))
  173. self.debug('Per-thread stats: {0}'.format(self.perthread))
  174. if self.ext:
  175. self.order = self.order + EXTENDED_ORDER
  176. self.definitions.update(EXTENDED_CHARTS)
  177. if self.unix_socket:
  178. self.debug('Using unix socket: {0}'.format(self.unix_socket))
  179. else:
  180. self.debug('Connecting to: {0}:{1}'.format(self.host, self.port))
  181. self.debug('Using key: {0}'.format(self.key))
  182. self.debug('Using certificate: {0}'.format(self.cert))
  183. def _auto_config(self):
  184. if self.ubconf and os.access(self.ubconf, os.R_OK):
  185. self.debug('Unbound config: {0}'.format(self.ubconf))
  186. conf = dict()
  187. try:
  188. conf = load_config(self.ubconf)
  189. except Exception as error:
  190. self.error("error on loading '{0}' : {1}".format(self.ubconf, error))
  191. if self.ext is None:
  192. if 'extended-statistics' in conf['server']:
  193. self.ext = conf['server']['extended-statistics']
  194. if 'remote-control' in conf:
  195. if conf['remote-control'].get('control-use-cert', False):
  196. self.key = self.key or conf['remote-control'].get('control-key-file')
  197. self.cert = self.cert or conf['remote-control'].get('control-cert-file')
  198. self.port = self.port or conf['remote-control'].get('control-port')
  199. else:
  200. self.unix_socket = self.unix_socket or conf['remote-control'].get('control-interface')
  201. else:
  202. self.debug('Unbound configuration not found.')
  203. if not self.key:
  204. self.key = '/etc/unbound/unbound_control.key'
  205. if not self.cert:
  206. self.cert = '/etc/unbound/unbound_control.pem'
  207. if not self.port:
  208. self.port = 8953
  209. def _generate_perthread_charts(self):
  210. tmporder = list()
  211. for thread in range(0, self.threads):
  212. charts, order, statmap = _get_perthread_info(thread)
  213. tmporder.extend(order)
  214. self.definitions.update(charts)
  215. self.statmap.update(statmap)
  216. self.order.extend(sorted(tmporder))
  217. def check(self):
  218. # Check if authentication is working.
  219. self._connect()
  220. result = bool(self._sock)
  221. self._disconnect()
  222. # If auth works, and we need per-thread charts, query the server
  223. # to see how many threads it's using. This somewhat abuses the
  224. # SocketService API to get the data we need.
  225. if result and self.perthread:
  226. tmp = self.request
  227. if sys.version_info[0] < 3:
  228. self.request = 'UBCT1 status\n'
  229. else:
  230. self.request = b'UBCT1 status\n'
  231. raw = self._get_raw_data()
  232. for line in raw.splitlines():
  233. if line.startswith('threads'):
  234. self.threads = int(line.split()[1])
  235. self._generate_perthread_charts()
  236. break
  237. if self.threads is None:
  238. self.info('Unable to auto-detect thread counts, disabling per-thread stats.')
  239. self.perthread = False
  240. self.request = tmp
  241. return result
  242. @staticmethod
  243. def _check_raw_data(data):
  244. # The server will close the connection when it's done sending
  245. # data, so just keep looping until that happens.
  246. return False
  247. def _get_data(self):
  248. raw = self._get_raw_data()
  249. data = dict()
  250. tmp = dict()
  251. for line in raw.splitlines():
  252. stat = line.split('=')
  253. tmp[stat[0]] = stat[1]
  254. for item in self.statmap:
  255. if item in tmp:
  256. data[self.statmap[item][0]] = float(tmp[item]) * self.statmap[item][1]
  257. return data