couchdb.chart.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. # -*- coding: utf-8 -*-
  2. # Description: couchdb netdata python.d module
  3. # Author: wohali <wohali@apache.org>
  4. # Thanks to ilyam8 for good examples :)
  5. # SPDX-License-Identifier: GPL-3.0-or-later
  6. from collections import namedtuple, defaultdict
  7. from json import loads
  8. from socket import gethostbyname, gaierror
  9. from threading import Thread
  10. try:
  11. from queue import Queue
  12. except ImportError:
  13. from Queue import Queue
  14. from bases.FrameworkServices.UrlService import UrlService
  15. update_every = 1
  16. METHODS = namedtuple('METHODS', ['get_data', 'url', 'stats'])
  17. OVERVIEW_STATS = [
  18. 'couchdb.database_reads.value',
  19. 'couchdb.database_writes.value',
  20. 'couchdb.httpd.view_reads.value',
  21. 'couchdb.httpd_request_methods.COPY.value',
  22. 'couchdb.httpd_request_methods.DELETE.value',
  23. 'couchdb.httpd_request_methods.GET.value',
  24. 'couchdb.httpd_request_methods.HEAD.value',
  25. 'couchdb.httpd_request_methods.OPTIONS.value',
  26. 'couchdb.httpd_request_methods.POST.value',
  27. 'couchdb.httpd_request_methods.PUT.value',
  28. 'couchdb.httpd_status_codes.200.value',
  29. 'couchdb.httpd_status_codes.201.value',
  30. 'couchdb.httpd_status_codes.202.value',
  31. 'couchdb.httpd_status_codes.204.value',
  32. 'couchdb.httpd_status_codes.206.value',
  33. 'couchdb.httpd_status_codes.301.value',
  34. 'couchdb.httpd_status_codes.302.value',
  35. 'couchdb.httpd_status_codes.304.value',
  36. 'couchdb.httpd_status_codes.400.value',
  37. 'couchdb.httpd_status_codes.401.value',
  38. 'couchdb.httpd_status_codes.403.value',
  39. 'couchdb.httpd_status_codes.404.value',
  40. 'couchdb.httpd_status_codes.405.value',
  41. 'couchdb.httpd_status_codes.406.value',
  42. 'couchdb.httpd_status_codes.409.value',
  43. 'couchdb.httpd_status_codes.412.value',
  44. 'couchdb.httpd_status_codes.413.value',
  45. 'couchdb.httpd_status_codes.414.value',
  46. 'couchdb.httpd_status_codes.415.value',
  47. 'couchdb.httpd_status_codes.416.value',
  48. 'couchdb.httpd_status_codes.417.value',
  49. 'couchdb.httpd_status_codes.500.value',
  50. 'couchdb.httpd_status_codes.501.value',
  51. 'couchdb.open_os_files.value',
  52. 'couch_replicator.jobs.running.value',
  53. 'couch_replicator.jobs.pending.value',
  54. 'couch_replicator.jobs.crashed.value',
  55. ]
  56. SYSTEM_STATS = [
  57. 'context_switches',
  58. 'run_queue',
  59. 'ets_table_count',
  60. 'reductions',
  61. 'memory.atom',
  62. 'memory.atom_used',
  63. 'memory.binary',
  64. 'memory.code',
  65. 'memory.ets',
  66. 'memory.other',
  67. 'memory.processes',
  68. 'io_input',
  69. 'io_output',
  70. 'os_proc_count',
  71. 'process_count',
  72. 'internal_replication_jobs'
  73. ]
  74. DB_STATS = [
  75. 'doc_count',
  76. 'doc_del_count',
  77. 'sizes.file',
  78. 'sizes.external',
  79. 'sizes.active'
  80. ]
  81. ORDER = [
  82. 'activity',
  83. 'request_methods',
  84. 'response_codes',
  85. 'active_tasks',
  86. 'replicator_jobs',
  87. 'open_files',
  88. 'db_sizes_file',
  89. 'db_sizes_external',
  90. 'db_sizes_active',
  91. 'db_doc_counts',
  92. 'db_doc_del_counts',
  93. 'erlang_memory',
  94. 'erlang_proc_counts',
  95. 'erlang_peak_msg_queue',
  96. 'erlang_reductions'
  97. ]
  98. CHARTS = {
  99. 'activity': {
  100. 'options': [None, 'Overall Activity', 'requests/s',
  101. 'dbactivity', 'couchdb.activity', 'stacked'],
  102. 'lines': [
  103. ['couchdb_database_reads', 'DB reads', 'incremental'],
  104. ['couchdb_database_writes', 'DB writes', 'incremental'],
  105. ['couchdb_httpd_view_reads', 'View reads', 'incremental']
  106. ]
  107. },
  108. 'request_methods': {
  109. 'options': [None, 'HTTP request methods', 'requests/s',
  110. 'httptraffic', 'couchdb.request_methods',
  111. 'stacked'],
  112. 'lines': [
  113. ['couchdb_httpd_request_methods_COPY', 'COPY', 'incremental'],
  114. ['couchdb_httpd_request_methods_DELETE', 'DELETE', 'incremental'],
  115. ['couchdb_httpd_request_methods_GET', 'GET', 'incremental'],
  116. ['couchdb_httpd_request_methods_HEAD', 'HEAD', 'incremental'],
  117. ['couchdb_httpd_request_methods_OPTIONS', 'OPTIONS',
  118. 'incremental'],
  119. ['couchdb_httpd_request_methods_POST', 'POST', 'incremental'],
  120. ['couchdb_httpd_request_methods_PUT', 'PUT', 'incremental']
  121. ]
  122. },
  123. 'response_codes': {
  124. 'options': [None, 'HTTP response status codes', 'responses/s',
  125. 'httptraffic', 'couchdb.response_codes',
  126. 'stacked'],
  127. 'lines': [
  128. ['couchdb_httpd_status_codes_200', '200 OK', 'incremental'],
  129. ['couchdb_httpd_status_codes_201', '201 Created', 'incremental'],
  130. ['couchdb_httpd_status_codes_202', '202 Accepted', 'incremental'],
  131. ['couchdb_httpd_status_codes_2xx', 'Other 2xx Success',
  132. 'incremental'],
  133. ['couchdb_httpd_status_codes_3xx', '3xx Redirection',
  134. 'incremental'],
  135. ['couchdb_httpd_status_codes_4xx', '4xx Client error',
  136. 'incremental'],
  137. ['couchdb_httpd_status_codes_5xx', '5xx Server error',
  138. 'incremental']
  139. ]
  140. },
  141. 'open_files': {
  142. 'options': [None, 'Open files', 'files', 'ops', 'couchdb.open_files', 'line'],
  143. 'lines': [
  144. ['couchdb_open_os_files', '# files', 'absolute']
  145. ]
  146. },
  147. 'active_tasks': {
  148. 'options': [None, 'Active task breakdown', 'tasks', 'ops', 'couchdb.active_tasks', 'stacked'],
  149. 'lines': [
  150. ['activetasks_indexer', 'Indexer', 'absolute'],
  151. ['activetasks_database_compaction', 'DB Compaction', 'absolute'],
  152. ['activetasks_replication', 'Replication', 'absolute'],
  153. ['activetasks_view_compaction', 'View Compaction', 'absolute']
  154. ]
  155. },
  156. 'replicator_jobs': {
  157. 'options': [None, 'Replicator job breakdown', 'jobs', 'ops', 'couchdb.replicator_jobs', 'stacked'],
  158. 'lines': [
  159. ['couch_replicator_jobs_running', 'Running', 'absolute'],
  160. ['couch_replicator_jobs_pending', 'Pending', 'absolute'],
  161. ['couch_replicator_jobs_crashed', 'Crashed', 'absolute'],
  162. ['internal_replication_jobs', 'Internal replication jobs',
  163. 'absolute']
  164. ]
  165. },
  166. 'erlang_memory': {
  167. 'options': [None, 'Erlang VM memory usage', 'B', 'erlang', 'couchdb.erlang_vm_memory', 'stacked'],
  168. 'lines': [
  169. ['memory_atom', 'atom', 'absolute'],
  170. ['memory_binary', 'binaries', 'absolute'],
  171. ['memory_code', 'code', 'absolute'],
  172. ['memory_ets', 'ets', 'absolute'],
  173. ['memory_processes', 'procs', 'absolute'],
  174. ['memory_other', 'other', 'absolute']
  175. ]
  176. },
  177. 'erlang_reductions': {
  178. 'options': [None, 'Erlang reductions', 'count', 'erlang', 'couchdb.reductions', 'line'],
  179. 'lines': [
  180. ['reductions', 'reductions', 'incremental']
  181. ]
  182. },
  183. 'erlang_proc_counts': {
  184. 'options': [None, 'Process counts', 'count', 'erlang', 'couchdb.proccounts', 'line'],
  185. 'lines': [
  186. ['os_proc_count', 'OS procs', 'absolute'],
  187. ['process_count', 'erl procs', 'absolute']
  188. ]
  189. },
  190. 'erlang_peak_msg_queue': {
  191. 'options': [None, 'Peak message queue size', 'count', 'erlang', 'couchdb.peakmsgqueue',
  192. 'line'],
  193. 'lines': [
  194. ['peak_msg_queue', 'peak size', 'absolute']
  195. ]
  196. },
  197. # Lines for the following are added as part of check()
  198. 'db_sizes_file': {
  199. 'options': [None, 'Database sizes (file)', 'KiB', 'perdbstats', 'couchdb.db_sizes_file', 'line'],
  200. 'lines': []
  201. },
  202. 'db_sizes_external': {
  203. 'options': [None, 'Database sizes (external)', 'KiB', 'perdbstats', 'couchdb.db_sizes_external', 'line'],
  204. 'lines': []
  205. },
  206. 'db_sizes_active': {
  207. 'options': [None, 'Database sizes (active)', 'KiB', 'perdbstats', 'couchdb.db_sizes_active', 'line'],
  208. 'lines': []
  209. },
  210. 'db_doc_counts': {
  211. 'options': [None, 'Database # of docs', 'docs',
  212. 'perdbstats', 'couchdb_db_doc_count', 'line'],
  213. 'lines': []
  214. },
  215. 'db_doc_del_counts': {
  216. 'options': [None, 'Database # of deleted docs', 'docs', 'perdbstats', 'couchdb_db_doc_del_count', 'line'],
  217. 'lines': []
  218. }
  219. }
  220. class Service(UrlService):
  221. def __init__(self, configuration=None, name=None):
  222. UrlService.__init__(self, configuration=configuration, name=name)
  223. self.order = ORDER
  224. self.definitions = CHARTS
  225. self.host = self.configuration.get('host', '127.0.0.1')
  226. self.port = self.configuration.get('port', 5984)
  227. self.node = self.configuration.get('node', 'couchdb@127.0.0.1')
  228. self.scheme = self.configuration.get('scheme', 'http')
  229. self.user = self.configuration.get('user')
  230. self.password = self.configuration.get('pass')
  231. try:
  232. self.dbs = self.configuration.get('databases').split(' ')
  233. except (KeyError, AttributeError):
  234. self.dbs = list()
  235. def check(self):
  236. if not (self.host and self.port):
  237. self.error('Host is not defined in the module configuration file')
  238. return False
  239. try:
  240. self.host = gethostbyname(self.host)
  241. except gaierror as error:
  242. self.error(str(error))
  243. return False
  244. self.url = '{scheme}://{host}:{port}'.format(scheme=self.scheme,
  245. host=self.host,
  246. port=self.port)
  247. stats = self.url + '/_node/{node}/_stats'.format(node=self.node)
  248. active_tasks = self.url + '/_active_tasks'
  249. system = self.url + '/_node/{node}/_system'.format(node=self.node)
  250. self.methods = [METHODS(get_data=self._get_overview_stats,
  251. url=stats,
  252. stats=OVERVIEW_STATS),
  253. METHODS(get_data=self._get_active_tasks_stats,
  254. url=active_tasks,
  255. stats=None),
  256. METHODS(get_data=self._get_overview_stats,
  257. url=system,
  258. stats=SYSTEM_STATS),
  259. METHODS(get_data=self._get_dbs_stats,
  260. url=self.url,
  261. stats=DB_STATS)]
  262. # must initialise manager before using _get_raw_data
  263. self._manager = self._build_manager()
  264. self.dbs = [db for db in self.dbs
  265. if self._get_raw_data(self.url + '/' + db)]
  266. for db in self.dbs:
  267. self.definitions['db_sizes_file']['lines'].append(
  268. ['db_' + db + '_sizes_file', db, 'absolute', 1, 1000]
  269. )
  270. self.definitions['db_sizes_external']['lines'].append(
  271. ['db_' + db + '_sizes_external', db, 'absolute', 1, 1000]
  272. )
  273. self.definitions['db_sizes_active']['lines'].append(
  274. ['db_' + db + '_sizes_active', db, 'absolute', 1, 1000]
  275. )
  276. self.definitions['db_doc_counts']['lines'].append(
  277. ['db_' + db + '_doc_count', db, 'absolute']
  278. )
  279. self.definitions['db_doc_del_counts']['lines'].append(
  280. ['db_' + db + '_doc_del_count', db, 'absolute']
  281. )
  282. return UrlService.check(self)
  283. def _get_data(self):
  284. threads = list()
  285. queue = Queue()
  286. result = dict()
  287. for method in self.methods:
  288. th = Thread(target=method.get_data,
  289. args=(queue, method.url, method.stats))
  290. th.start()
  291. threads.append(th)
  292. for thread in threads:
  293. thread.join()
  294. result.update(queue.get())
  295. # self.info('couchdb result = ' + str(result))
  296. return result or None
  297. def _get_overview_stats(self, queue, url, stats):
  298. raw_data = self._get_raw_data(url)
  299. if not raw_data:
  300. return queue.put(dict())
  301. data = loads(raw_data)
  302. to_netdata = self._fetch_data(raw_data=data, metrics=stats)
  303. if 'message_queues' in data:
  304. to_netdata['peak_msg_queue'] = get_peak_msg_queue(data)
  305. return queue.put(to_netdata)
  306. def _get_active_tasks_stats(self, queue, url, _):
  307. taskdict = defaultdict(int)
  308. taskdict["activetasks_indexer"] = 0
  309. taskdict["activetasks_database_compaction"] = 0
  310. taskdict["activetasks_replication"] = 0
  311. taskdict["activetasks_view_compaction"] = 0
  312. raw_data = self._get_raw_data(url)
  313. if not raw_data:
  314. return queue.put(dict())
  315. data = loads(raw_data)
  316. for task in data:
  317. taskdict["activetasks_" + task["type"]] += 1
  318. return queue.put(dict(taskdict))
  319. def _get_dbs_stats(self, queue, url, stats):
  320. to_netdata = {}
  321. for db in self.dbs:
  322. raw_data = self._get_raw_data(url + '/' + db)
  323. if not raw_data:
  324. continue
  325. data = loads(raw_data)
  326. for metric in stats:
  327. value = data
  328. metrics_list = metric.split('.')
  329. try:
  330. for m in metrics_list:
  331. value = value[m]
  332. except (KeyError, TypeError) as e:
  333. self.debug('cannot process ' + metric + ' for ' + db
  334. + ": " + str(e))
  335. continue
  336. metric_name = 'db_{0}_{1}'.format(db, '_'.join(metrics_list))
  337. to_netdata[metric_name] = value
  338. return queue.put(to_netdata)
  339. def _fetch_data(self, raw_data, metrics):
  340. data = dict()
  341. for metric in metrics:
  342. value = raw_data
  343. metrics_list = metric.split('.')
  344. try:
  345. for m in metrics_list:
  346. value = value[m]
  347. except (KeyError, TypeError) as e:
  348. self.debug('cannot process ' + metric + ': ' + str(e))
  349. continue
  350. # strip off .value from end of stat
  351. if metrics_list[-1] == 'value':
  352. metrics_list = metrics_list[:-1]
  353. # sum up 3xx/4xx/5xx
  354. if metrics_list[0:2] == ['couchdb', 'httpd_status_codes'] and \
  355. int(metrics_list[2]) > 202:
  356. metrics_list[2] = '{0}xx'.format(int(metrics_list[2]) // 100)
  357. if '_'.join(metrics_list) in data:
  358. data['_'.join(metrics_list)] += value
  359. else:
  360. data['_'.join(metrics_list)] = value
  361. else:
  362. data['_'.join(metrics_list)] = value
  363. return data
  364. def get_peak_msg_queue(data):
  365. maxsize = 0
  366. queues = data['message_queues']
  367. for queue in iter(queues.values()):
  368. if isinstance(queue, dict) and 'count' in queue:
  369. value = queue['count']
  370. elif isinstance(queue, int):
  371. value = queue
  372. else:
  373. continue
  374. maxsize = max(maxsize, value)
  375. return maxsize