redis.chart.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # -*- coding: utf-8 -*-
  2. # Description: redis netdata python.d module
  3. # Author: Pawel Krupa (paulfantom)
  4. # Author: Ilya Mashchenko (ilyam8)
  5. # SPDX-License-Identifier: GPL-3.0-or-later
  6. import re
  7. from copy import deepcopy
  8. from bases.FrameworkServices.SocketService import SocketService
  9. REDIS_ORDER = [
  10. 'operations',
  11. 'hit_rate',
  12. 'memory',
  13. 'keys_redis',
  14. 'eviction',
  15. 'net',
  16. 'connections',
  17. 'clients',
  18. 'slaves',
  19. 'persistence',
  20. 'bgsave_now',
  21. 'bgsave_health',
  22. 'uptime',
  23. ]
  24. PIKA_ORDER = [
  25. 'operations',
  26. 'hit_rate',
  27. 'memory',
  28. 'keys_pika',
  29. 'connections',
  30. 'clients',
  31. 'slaves',
  32. 'uptime',
  33. ]
  34. CHARTS = {
  35. 'operations': {
  36. 'options': [None, 'Operations', 'operations/s', 'operations', 'redis.operations', 'line'],
  37. 'lines': [
  38. ['total_commands_processed', 'commands', 'incremental'],
  39. ['instantaneous_ops_per_sec', 'operations', 'absolute']
  40. ]
  41. },
  42. 'hit_rate': {
  43. 'options': [None, 'Hit rate', 'percentage', 'hits', 'redis.hit_rate', 'line'],
  44. 'lines': [
  45. ['hit_rate', 'rate', 'absolute']
  46. ]
  47. },
  48. 'memory': {
  49. 'options': [None, 'Memory utilization', 'KiB', 'memory', 'redis.memory', 'line'],
  50. 'lines': [
  51. ['used_memory', 'total', 'absolute', 1, 1024],
  52. ['used_memory_lua', 'lua', 'absolute', 1, 1024]
  53. ]
  54. },
  55. 'net': {
  56. 'options': [None, 'Bandwidth', 'kilobits/s', 'network', 'redis.net', 'area'],
  57. 'lines': [
  58. ['total_net_input_bytes', 'in', 'incremental', 8, 1000],
  59. ['total_net_output_bytes', 'out', 'incremental', -8, 1000]
  60. ]
  61. },
  62. 'keys_redis': {
  63. 'options': [None, 'Keys per Database', 'keys', 'keys', 'redis.keys', 'line'],
  64. 'lines': []
  65. },
  66. 'keys_pika': {
  67. 'options': [None, 'Keys', 'keys', 'keys', 'redis.keys', 'line'],
  68. 'lines': [
  69. ['kv_keys', 'kv', 'absolute'],
  70. ['hash_keys', 'hash', 'absolute'],
  71. ['list_keys', 'list', 'absolute'],
  72. ['zset_keys', 'zset', 'absolute'],
  73. ['set_keys', 'set', 'absolute']
  74. ]
  75. },
  76. 'eviction': {
  77. 'options': [None, 'Evicted Keys', 'keys', 'keys', 'redis.eviction', 'line'],
  78. 'lines': [
  79. ['evicted_keys', 'evicted', 'absolute']
  80. ]
  81. },
  82. 'connections': {
  83. 'options': [None, 'Connections', 'connections/s', 'connections', 'redis.connections', 'line'],
  84. 'lines': [
  85. ['total_connections_received', 'received', 'incremental', 1],
  86. ['rejected_connections', 'rejected', 'incremental', -1]
  87. ]
  88. },
  89. 'clients': {
  90. 'options': [None, 'Clients', 'clients', 'connections', 'redis.clients', 'line'],
  91. 'lines': [
  92. ['connected_clients', 'connected', 'absolute', 1],
  93. ['blocked_clients', 'blocked', 'absolute', -1]
  94. ]
  95. },
  96. 'slaves': {
  97. 'options': [None, 'Slaves', 'slaves', 'replication', 'redis.slaves', 'line'],
  98. 'lines': [
  99. ['connected_slaves', 'connected', 'absolute']
  100. ]
  101. },
  102. 'persistence': {
  103. 'options': [None, 'Persistence Changes Since Last Save', 'changes', 'persistence',
  104. 'redis.rdb_changes', 'line'],
  105. 'lines': [
  106. ['rdb_changes_since_last_save', 'changes', 'absolute']
  107. ]
  108. },
  109. 'bgsave_now': {
  110. 'options': [None, 'Duration of the RDB Save Operation', 'seconds', 'persistence',
  111. 'redis.bgsave_now', 'absolute'],
  112. 'lines': [
  113. ['rdb_bgsave_in_progress', 'rdb save', 'absolute']
  114. ]
  115. },
  116. 'bgsave_health': {
  117. 'options': [None, 'Status of the Last RDB Save Operation', 'status', 'persistence',
  118. 'redis.bgsave_health', 'line'],
  119. 'lines': [
  120. ['rdb_last_bgsave_status', 'rdb save', 'absolute']
  121. ]
  122. },
  123. 'uptime': {
  124. 'options': [None, 'Uptime', 'seconds', 'uptime', 'redis.uptime', 'line'],
  125. 'lines': [
  126. ['uptime_in_seconds', 'uptime', 'absolute']
  127. ]
  128. }
  129. }
  130. def copy_chart(name):
  131. return {name: deepcopy(CHARTS[name])}
  132. RE = re.compile(r'\n([a-z_0-9 ]+):(?:keys=)?([^,\r]+)')
  133. class Service(SocketService):
  134. def __init__(self, configuration=None, name=None):
  135. SocketService.__init__(self, configuration=configuration, name=name)
  136. self.order = list()
  137. self.definitions = dict()
  138. self._keep_alive = True
  139. self.host = self.configuration.get('host', 'localhost')
  140. self.port = self.configuration.get('port', 6379)
  141. self.unix_socket = self.configuration.get('socket')
  142. p = self.configuration.get('pass')
  143. self.auth_request = 'AUTH {0} \r\n'.format(p).encode() if p else None
  144. self.request = 'INFO\r\n'.encode()
  145. self.bgsave_time = 0
  146. def do_auth(self):
  147. resp = self._get_raw_data(request=self.auth_request)
  148. if not resp:
  149. return False
  150. if resp.strip() != '+OK':
  151. self.error('invalid password')
  152. return False
  153. return True
  154. def get_raw_and_parse(self):
  155. if self.auth_request and not self.do_auth():
  156. return None
  157. resp = self._get_raw_data()
  158. if not resp:
  159. return None
  160. parsed = RE.findall(resp)
  161. if not parsed:
  162. self.error('response is invalid/empty')
  163. return None
  164. return dict((k.replace(' ', '_'), v) for k, v in parsed)
  165. def get_data(self):
  166. """
  167. Get data from socket
  168. :return: dict
  169. """
  170. data = self.get_raw_and_parse()
  171. if not data:
  172. return None
  173. try:
  174. data['hit_rate'] = (
  175. (int(data['keyspace_hits']) * 100) / (int(data['keyspace_hits']) + int(data['keyspace_misses']))
  176. )
  177. except (KeyError, ZeroDivisionError):
  178. data['hit_rate'] = 0
  179. if data.get('redis_version') and data.get('rdb_bgsave_in_progress'):
  180. self.get_data_redis_specific(data)
  181. return data
  182. def get_data_redis_specific(self, data):
  183. if data['rdb_bgsave_in_progress'] != '0':
  184. self.bgsave_time += self.update_every
  185. else:
  186. self.bgsave_time = 0
  187. data['rdb_last_bgsave_status'] = 0 if data['rdb_last_bgsave_status'] == 'ok' else 1
  188. data['rdb_bgsave_in_progress'] = self.bgsave_time
  189. def check(self):
  190. """
  191. Parse configuration, check if redis is available, and dynamically create chart lines data
  192. :return: boolean
  193. """
  194. data = self.get_raw_and_parse()
  195. if not data:
  196. return False
  197. self.order = PIKA_ORDER if data.get('pika_version') else REDIS_ORDER
  198. for n in self.order:
  199. self.definitions.update(copy_chart(n))
  200. if data.get('redis_version'):
  201. for k in data:
  202. if k.startswith('db'):
  203. self.definitions['keys_redis']['lines'].append([k, None, 'absolute'])
  204. return True
  205. def _check_raw_data(self, data):
  206. """
  207. Check if all data has been gathered from socket.
  208. Parse first line containing message length and check against received message
  209. :param data: str
  210. :return: boolean
  211. """
  212. length = len(data)
  213. supposed = data.split('\n')[0][1:-1]
  214. offset = len(supposed) + 4 # 1 dollar sing, 1 new line character + 1 ending sequence '\r\n'
  215. if not supposed.isdigit():
  216. return True
  217. supposed = int(supposed)
  218. if length - offset >= supposed:
  219. self.debug('received full response from redis')
  220. return True
  221. self.debug('waiting more data from redis')
  222. return False