1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# -*- coding: utf-8 -*-
# Description: rethinkdb netdata python.d module
# Author: Ilya Mashchenko (ilyam8)
# SPDX-License-Identifier: GPL-3.0-or-later
try:
import rethinkdb as rdb
HAS_RETHINKDB = True
except ImportError:
HAS_RETHINKDB = False
from bases.FrameworkServices.SimpleService import SimpleService
ORDER = [
'cluster_connected_servers',
'cluster_clients_active',
'cluster_queries',
'cluster_documents',
]
def cluster_charts():
return {
'cluster_connected_servers': {
'options': [None, 'Connected Servers', 'servers', 'cluster', 'rethinkdb.cluster_connected_servers',
'stacked'],
'lines': [
['cluster_servers_connected', 'connected'],
['cluster_servers_missing', 'missing'],
]
},
'cluster_clients_active': {
'options': [None, 'Active Clients', 'clients', 'cluster', 'rethinkdb.cluster_clients_active',
'line'],
'lines': [
['cluster_clients_active', 'active'],
]
},
'cluster_queries': {
'options': [None, 'Queries', 'queries/s', 'cluster', 'rethinkdb.cluster_queries', 'line'],
'lines': [
['cluster_queries_per_sec', 'queries'],
]
},
'cluster_documents': {
'options': [None, 'Documents', 'documents/s', 'cluster', 'rethinkdb.cluster_documents', 'line'],
'lines': [
['cluster_read_docs_per_sec', 'reads'],
['cluster_written_docs_per_sec', 'writes'],
]
},
}
def server_charts(n):
o = [
'{0}_client_connections'.format(n),
'{0}_clients_active'.format(n),
'{0}_queries'.format(n),
'{0}_documents'.format(n),
]
f = 'server {0}'.format(n)
c = {
o[0]: {
'options': [None, 'Client Connections', 'connections', f, 'rethinkdb.client_connections', 'line'],
'lines': [
['{0}_client_connections'.format(n), 'connections'],
]
},
o[1]: {
'options': [None, 'Active Clients', 'clients', f, 'rethinkdb.clients_active', 'line'],
'lines': [
['{0}_clients_active'.format(n), 'active'],
]
},
o[2]: {
'options': [None, 'Queries', 'queries/s', f, 'rethinkdb.queries', 'line'],
'lines': [
['{0}_queries_total'.format(n), 'queries', 'incremental'],
]
},
o[3]: {
'options': [None, 'Documents', 'documents/s', f, 'rethinkdb.documents', 'line'],
'lines': [
['{0}_read_docs_total'.format(n), 'reads', 'incremental'],
['{0}_written_docs_total'.format(n), 'writes', 'incremental'],
]
},
}
return o, c
class Cluster:
def __init__(self, raw):
self.raw = raw
def data(self):
qe = self.raw['query_engine']
return {
'cluster_clients_active': qe['clients_active'],
'cluster_queries_per_sec': qe['queries_per_sec'],
'cluster_read_docs_per_sec': qe['read_docs_per_sec'],
'cluster_written_docs_per_sec': qe['written_docs_per_sec'],
'cluster_servers_connected': 0,
'cluster_servers_missing': 0,
}
class Server:
def __init__(self, raw):
self.name = raw['server']
self.raw = raw
def error(self):
return self.raw.get('error')
def data(self):
qe = self.raw['query_engine']
d = {
'client_connections': qe['client_connections'],
'clients_active': qe['clients_active'],
'queries_total': qe['queries_total'],
'read_docs_total': qe['read_docs_total'],
'written_docs_total': qe['written_docs_total'],
}
return dict(('{0}_{1}'.format(self.name, k), d[k]) for k in d)
# https://pypi.org/project/rethinkdb/2.4.0/
# rdb.RethinkDB() can be used as rdb drop in replacement.
# https://github.com/rethinkdb/rethinkdb-python#quickstart
def get_rethinkdb():
if hasattr(rdb, 'RethinkDB'):
return rdb.RethinkDB()
return rdb
class Service(SimpleService):
def __init__(self, configuration=None, name=None):
SimpleService.__init__(self, configuration=configuration, name=name)
self.order = list(ORDER)
self.definitions = cluster_charts()
self.host = self.configuration.get('host', '127.0.0.1')
self.port = self.configuration.get('port', 28015)
self.user = self.configuration.get('user', 'admin')
self.password = self.configuration.get('password')
self.timeout = self.configuration.get('timeout', 2)
self.rdb = None
self.conn = None
self.alive = True
def check(self):
if not HAS_RETHINKDB:
self.error('"rethinkdb" module is needed to use rethinkdbs.py')
return False
self.debug("rethinkdb driver version {0}".format(rdb.__version__))
self.rdb = get_rethinkdb()
if not self.connect():
return None
stats = self.get_stats()
if not stats:
return None
for v in stats[1:]:
if get_id(v) == 'server':
o, c = server_charts(v['server'])
self.order.extend(o)
self.definitions.update(c)
return True
def get_data(self):
if not self.is_alive():
return None
stats = self.get_stats()
if not stats:
return None
data = dict()
# cluster
data.update(Cluster(stats[0]).data())
# servers
for v in stats[1:]:
if get_id(v) != 'server':
continue
s = Server(v)
if s.error():
data['cluster_servers_missing'] += 1
else:
data['cluster_servers_connected'] += 1
data.update(s.data())
return data
def get_stats(self):
try:
return list(self.rdb.db('rethinkdb').table('stats').run(self.conn).items)
except rdb.errors.ReqlError:
self.alive = False
return None
def connect(self):
try:
self.conn = self.rdb.connect(
host=self.host,
port=self.port,
user=self.user,
password=self.password,
timeout=self.timeout,
)
self.alive = True
return True
except rdb.errors.ReqlError as error:
self.error('Connection to {0}:{1} failed: {2}'.format(self.host, self.port, error))
return False
def reconnect(self):
# The connection is already closed after rdb.errors.ReqlError,
# so we do not need to call conn.close()
if self.connect():
return True
return False
def is_alive(self):
if not self.alive:
return self.reconnect()
return True
def get_id(v):
return v['id'][0]
|