-
Notifications
You must be signed in to change notification settings - Fork 12
/
cassandra.py
776 lines (736 loc) · 34.7 KB
/
cassandra.py
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
"""
*******************
*Copyright 2017, MapleLabs, All Rights Reserved.
*
********************
"""
""" A collectd-python plugin for retrieving
metrics from cassandra Database server.
"""
import collectd
import signal
import time
import json
import requests
import sys
import traceback
from copy import deepcopy
# user imports
from utils import *
from constants import *
#Cassandra metrics
metrics = [
{
"metric_name": "Latency",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ClientRequest",
"jmx_scope": "Read",
"display_name": "readCount",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "Latency",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ClientRequest",
"jmx_scope": "Write",
"display_name": "writeCount",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "TotalLatency",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ClientRequest",
"jmx_scope": "Read",
"display_name": "totalReadLatency",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "TotalLatency",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ClientRequest",
"jmx_scope": "Write",
"display_name": "totalWriteLatency",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "Latency",
"metric_key": "FiveMinuteRate",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ClientRequest",
"jmx_scope": "Write",
"display_name": "writeThroughput",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "Latency",
"metric_key": "FiveMinuteRate",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ClientRequest",
"jmx_scope": "Read",
"display_name": "readThroughput",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "Load",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Storage",
"display_name": "diskSpaceUsedLoad",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},name={name}"
},
{
"metric_name": "TotalDiskSpaceUsed",
"metric_key": "Value",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ColumnFamily",
"display_name": "totalDiskSpaceUsed",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},name={name}"
},
{
"metric_name": "Hits",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Cache",
"jmx_scope": "KeyCache",
"display_name": "cacheHitCount",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "Requests",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Cache",
"jmx_scope": "KeyCache",
"display_name": "cacheRequestCount",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "PendingTasks",
"metric_key": "Value",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ThreadPools",
"jmx_scope": "RequestResponseStage",
"jmx_path": "request",
"display_name": "threadPoolPendingTasks",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},path={jmx_path},scope={jmx_scope},name={name}"
},
{
"metric_name": "TotalBlockedTasks",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ThreadPools",
"jmx_scope": "RequestResponseStage",
"jmx_path": "request",
"display_name": "threadPoolTotalBlockedTasks",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},path={jmx_path},scope={jmx_scope},name={name}"
},
{
"metric_name": "ActiveTasks",
"metric_key": "Value",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ThreadPools",
"jmx_scope": "RequestResponseStage",
"jmx_path": "request",
"display_name": "threadPoolActiveTasks",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},path={jmx_path},scope={jmx_scope},name={name}"
},
{
"metric_name": "CompletedTasks",
"metric_key": "Value",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Compaction",
"display_name": "compactionCompletedTasks",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},name={name}"
},
{
"metric_name": "PendingTasks",
"metric_key": "Value",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Compaction",
"display_name": "compactionPendingTasks",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},name={name}"
},
{
"metric_name": "BytesCompacted",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Compaction",
"display_name": "totalCompactedSize",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},name={name}"
},
{
"metric_name": "Exceptions",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Storage",
"display_name": "exceptions",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},name={name}"
},
{
"metric_name": "Timeouts",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ClientRequest",
"jmx_scope": "Read",
"display_name": "readTimeouts",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "Timeouts",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ClientRequest",
"jmx_scope": "Write",
"display_name": "writeTimeouts",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "Unavailables",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ClientRequest",
"jmx_scope": "Read",
"display_name": "readUnavailables",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
{
"metric_name": "Unavailables",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "ClientRequest",
"jmx_scope": "Write",
"display_name": "writeUnavailables",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},scope={jmx_scope},name={name}"
},
]
# JVM metrics
jvm_metrics =[
{
"metric_name": "ParNew",
"metric_key": "CollectionCount",
"jmx_domain": "java.lang",
"jmx_type": "GarbageCollector",
"display_name": "gCParNewCollectionCount",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},name={name}"
},
{
"metric_name": "ParNew",
"metric_key": "CollectionTime",
"jmx_domain": "java.lang",
"jmx_type": "GarbageCollector",
"display_name": "gCParNewCollectionTime",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},name={name}"
},
{
"metric_name": "ConcurrentMarkSweep",
"metric_key": "CollectionCount",
"jmx_domain": "java.lang",
"jmx_type": "GarbageCollector",
"display_name": "gCMarkSweepCollectionCount",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},name={name}"
},
{
"metric_name": "ConcurrentMarkSweep",
"metric_key": "CollectionTime",
"jmx_domain": "java.lang",
"jmx_type": "GarbageCollector",
"display_name": "gCMarkSweepCollectionTime",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},name={name}"
},
{
"metric_name": "",
"metric_key": "LoadedClassCount",
"jmx_domain": "java.lang",
"jmx_type": "ClassLoading",
"display_name": "loadedClassCount",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type}"
},
{
"metric_name": "",
"metric_key": "UnloadedClassCount",
"jmx_domain": "java.lang",
"jmx_type": "ClassLoading",
"display_name": "unLoadedClassCount",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type}"
},
{
"metric_name": "",
"metric_key": "HeapMemoryUsage",
"jmx_domain": "java.lang",
"jmx_type": "Memory",
"display_name": "heapMemoryUsage",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type}"
},
{
"metric_name": "",
"metric_key": "LiveNodes",
"jmx_domain": "org.apache.cassandra.db",
"jmx_type": "StorageService",
"display_name": "liveNodes",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type}"
},
{
"metric_name": "",
"metric_key": "NonSystemKeyspaces",
"jmx_domain": "org.apache.cassandra.db",
"jmx_type": "StorageService",
"display_name": "userKeyspaces",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type}"
},
{
"metric_name": "",
"metric_key": "OperationMode",
"jmx_domain": "org.apache.cassandra.db",
"jmx_type": "StorageService",
"display_name": "operationMode",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type}"
},
{
"metric_name": "",
"metric_key": "Joined",
"jmx_domain": "org.apache.cassandra.db",
"jmx_type": "StorageService",
"display_name": "joined",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type}"
},
{
"metric_name": "",
"metric_key": "ClusterName",
"jmx_domain": "org.apache.cassandra.db",
"jmx_type": "StorageService",
"display_name": "clusterName",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type}"
}
]
#Keyspace metrics
ks_metrics = [
{
"metric_name": "LiveDiskSpaceUsed",
"metric_key": "Value",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Keyspace",
"display_name": "diskSpaceUsed",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},keyspace={jmx_keyspace},name={name}"
},
{
"metric_name": "ReadLatency",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Keyspace",
"display_name": "readLatencyCount",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},keyspace={jmx_keyspace},name={name}"
},
{
"metric_name": "WriteLatency",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Keyspace",
"display_name": "writeLatencyCount",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},keyspace={jmx_keyspace},name={name}"
},
{
"metric_name": "ReadTotalLatency",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Keyspace",
"display_name": "readTotalLatency",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},keyspace={jmx_keyspace},name={name}"
},
{
"metric_name": "WriteTotalLatency",
"metric_key": "Count",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Keyspace",
"display_name": "writeTotalLatency",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},keyspace={jmx_keyspace},name={name}"
},
{
"metric_name": "ReadLatency",
"metric_key": "FiveMinuteRate",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Keyspace",
"display_name": "readThroughput",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},keyspace={jmx_keyspace},name={name}"
},
{
"metric_name": "WriteLatency",
"metric_key": "FiveMinuteRate",
"jmx_domain": "org.apache.cassandra.metrics",
"jmx_type": "Keyspace",
"display_name": "writeThroughput",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},keyspace={jmx_keyspace},name={name}"
},
{
"metric_name": "",
"metric_key": "TableName",
"jmx_domain": "org.apache.cassandra.db",
"jmx_type": "Tables",
"display_name": "tablesList",
"format_str": "http://{host}:{port}/jolokia/read/{jmx_domain}:type={jmx_type},keyspace={jmx_keyspace},table=*"
}
]
exclude_ks = ["system_auth", "system_distributed", "system_schema", "system_traces"]
class CassandraStats(object):
"""Plugin object will be created only once and collects cassandra statistics info every interval."""
def __init__(self):
"""Initializes interval and previous dictionary variable."""
self.interval = DEFAULT_INTERVAL
self.pollCounter = 0
self.user = None
self.password = None
self.port = 8778
self.keyspaces = []
self.previousData = {}
def config(self, cfg):
"""Initializes variables from conf files."""
for children in cfg.children:
if children.key == INTERVAL:
self.interval = children.values[0]
if children.key == HOST:
self.host = children.values[0]
self.password = children.values[0]
@staticmethod
def add_common_params(doc, cassandra_dict):
hostname = gethostname()
timestamp = int(round(time.time() * 1000))
cassandra_dict[HOSTNAME] = hostname
cassandra_dict[TIMESTAMP] = timestamp
cassandra_dict[PLUGIN] = CASSANDRA
cassandra_dict[ACTUALPLUGINTYPE] = CASSANDRA
cassandra_dict[PLUGINTYPE] = doc
def fetch_and_update(self,
name,
keyname,
jmx_domain,
jmx_type,
jmx_scope,
jmx_path,
format_str,
jmx_keyspace=""
):
"""
Fetch a metric from jolokia jmx agent.
The jolokia service has a get request made to it to read a metric that
is passed in as name and keyname from the server.
:param name: Name of the metric
:param keyname: Keyname of the metric
:param jmx_domain: Domain of the metric
:param jmx_type: Type of the metric
:param jmx_scope: Scope of the metric
:param jmx_path: Specified path of the metric
:param jmx_keyspace: keyspace name of cassandra
:param format_str: GET URL formed with the help of above params
:return: Metric value
"""
auth = requests.auth.HTTPBasicAuth(self.user, self.password) \
if self.user is not None and self.password is not None else None
try:
resp = requests.get(
format_str.format(jmx_domain=jmx_domain, jmx_type=jmx_type, jmx_scope=jmx_scope, jmx_path=jmx_path,
jmx_keyspace=jmx_keyspace, host=self.host, name=name, port=self.port),
auth=auth
)
except requests.exceptions.ConnectionError:
collectd.error(
"The application recieved a connection error, failed to connect jolokia JVM agent"
)
sys.exit(1)
if resp.status_code >= 400 or resp.json().get("error"):
collectd.error("ERROR the jolokia agent returned an error trying to access the following metric")
collectd.error(format_str.format(jmx_domain=jmx_domain, jmx_type=jmx_type, jmx_scope=jmx_scope, jmx_path=jmx_path,
jmx_keyspace=jmx_keyspace, name=name, key=keyname, host=self.host, port=self.port))
return
else:
jmx_data = resp.json()["value"]
if (keyname == "TableName"):
metric = jmx_data.values()[0][keyname]
else:
metric = jmx_data[keyname]
return (metric)
def get_cassandra_details(self):
"""
Get Cassandra stats from jolokia agent
:return: dict of cassandra stats
"""
cassandra_results = {}
cassandraMetrics = {}
try:
for metric in metrics:
value = self.fetch_and_update(
name=metric["metric_name"],
keyname=metric["metric_key"],
jmx_domain=metric["jmx_domain"],
jmx_type=metric["jmx_type"],
jmx_scope=metric.get("jmx_scope"),
jmx_path=metric.get("jmx_path"),
format_str=metric["format_str"]
)
if value:
if metric["display_name"] in ["totalDiskSpaceUsed", "diskSpaceUsedLoad", "totalCompactedSize"]:
cassandra_results[metric["display_name"]] = round(value / (1024.0 * 1024.0), 2)
elif metric["display_name"] in ["writeThroughput", "readThroughput"]:
cassandra_results[metric["display_name"]] = round(value, 2)
else:
cassandra_results[metric["display_name"]] = value
else:
cassandra_results[metric["display_name"]] = 0
if not cassandra_results:
collectd.error("Unable to collect Cassandra metrics")
return cassandraMetrics
else:
if self.pollCounter <= 1:
cassandraMetrics["cassandraStats"] = {}
cassandraMetrics["cassandraStats"] = deepcopy(cassandra_results)
cassandraMetrics["cassandraStats"]["cacheHitCount"] = 0
cassandraMetrics["cassandraStats"]["cacheRequestCount"] = 0
cassandraMetrics["cassandraStats"]["compactionCompletedTasks"] = 0
cassandraMetrics["cassandraStats"]["readLatency"] = 0
cassandraMetrics["cassandraStats"]["writeLatency"] = 0
cassandraMetrics["cassandraStats"]["cacheMissCount"] = 0
cassandraMetrics["cassandraStats"]["cacheHitRatio"] = 0.0
cassandraMetrics["cassandraStats"]["cacheMissRatio"] = 0.0
cassandraMetrics["cassandraStats"]["exceptions"] = 0
cassandraMetrics["cassandraStats"]["readTimeouts"] = 0
cassandraMetrics["cassandraStats"]["writeTimeouts"] = 0
cassandraMetrics["cassandraStats"]["readUnavailables"] = 0
cassandraMetrics["cassandraStats"]["writeUnavailables"] = 0
self.previousData["cassandraStats"] = deepcopy(cassandra_results)
else:
cassandraMetrics["cassandraStats"] = deepcopy(cassandra_results)
cassandraStats = cassandraMetrics["cassandraStats"]
previousStats = self.previousData["cassandraStats"]
cassandraStats["cacheHitCount"] = cassandra_results["cacheHitCount"] - previousStats["cacheHitCount"]
cassandraStats["cacheRequestCount"] = cassandra_results["cacheRequestCount"] - previousStats["cacheRequestCount"]
cassandraStats["compactionCompletedTasks"] = cassandra_results["compactionCompletedTasks"] - previousStats["compactionCompletedTasks"]
cassandraStats["exceptions"] = cassandra_results["exceptions"] - previousStats["exceptions"]
cassandraStats["readTimeouts"] = cassandra_results["readTimeouts"] - previousStats["readTimeouts"]
cassandraStats["writeTimeouts"] = cassandra_results["writeTimeouts"] - previousStats["writeTimeouts"]
cassandraStats["readUnavailables"] = cassandra_results["readUnavailables"] - previousStats["readUnavailables"]
cassandraStats["writeUnavailables"] = cassandra_results["writeUnavailables"] - previousStats["writeUnavailables"]
cassandraStats["cacheMissCount"] = cassandraStats["cacheRequestCount"] - cassandraStats["cacheHitCount"]
totalReadLatency = cassandra_results["totalReadLatency"] - previousStats["totalReadLatency"]
totalWriteLatency = cassandra_results["totalWriteLatency"] - previousStats["totalWriteLatency"]
readCount = cassandra_results["readCount"] - previousStats["readCount"]
writeCount = cassandra_results["writeCount"] - previousStats["writeCount"]
if cassandraStats["cacheHitCount"] and cassandraStats["cacheRequestCount"]:
cassandraStats["cacheHitRatio"] = round(float(cassandraStats["cacheHitCount"]) / float(cassandraStats["cacheRequestCount"]), 2)
else:
cassandraStats["cacheHitRatio"] = 0.0
if cassandraStats["cacheMissCount"] and cassandraStats["cacheRequestCount"]:
cassandraStats["cacheMissRatio"] = round(float(cassandraStats["cacheMissCount"]) / float(cassandraStats["cacheRequestCount"]), 2)
else:
cassandraStats["cacheMissRatio"] = 0.0
if totalReadLatency and readCount:
cassandraStats["readLatency"] = round(float(totalReadLatency) / float(readCount), 2)
else:
cassandraStats["readLatency"] = 0.0
if totalWriteLatency and writeCount:
cassandraStats["writeLatency"] = round(float(totalWriteLatency) / float(writeCount), 2)
else:
cassandraStats["writeLatency"] = 0.0
self.previousData["cassandraStats"] = cassandra_results
# Removing readCount, writeCount, totalReadLatency, totalReadLatency from cassandraMetrics dict.
# As it needed only for internal calculation of read/write Latency
for key in ["readCount", "writeCount", "totalReadLatency", "totalWriteLatency"]:
if key in cassandraMetrics["cassandraStats"].keys():
del cassandraMetrics["cassandraStats"][key]
except Exception as e:
collectd.error("Error in collecting cassandraStats due to %s " % str(e))
return cassandraMetrics
def get_jvm_details(self, cassandraMetrics):
"""
Get JVM stats from jolokia agent
:param cassandraMetrics: collected cassandra stats dict
:return: cassandraMetrics dict contains both cassandra stats and jvm stats
"""
jvm_results = {}
try:
for metric in jvm_metrics:
value = self.fetch_and_update(
name=metric["metric_name"],
keyname=metric["metric_key"],
jmx_domain=metric["jmx_domain"],
jmx_type=metric["jmx_type"],
jmx_scope=metric.get("jmx_scope"),
jmx_path=metric.get("jmx_path"),
format_str=metric["format_str"]
)
if value:
if metric["metric_key"] == "NonSystemKeyspaces":
for ks in exclude_ks:
if ks in value:
value.remove(ks)
self.keyspaces = [x.encode() for x in value]
elif metric["metric_key"] == "HeapMemoryUsage":
heapMemoryUsageInit = value.get("init", 0)
if heapMemoryUsageInit:
jvm_results["heapMemoryUsageInit"] = round(heapMemoryUsageInit / (1024.0 * 1024.0), 2)
heapMemoryUsageCommitted = value.get("committed", 0)
if heapMemoryUsageCommitted:
jvm_results["heapMemoryUsageCommitted"] = round(heapMemoryUsageCommitted / (1024.0 * 1024.0), 2)
heapMemoryUsageMax = value.get("max", 0)
if heapMemoryUsageMax:
jvm_results["heapMemoryUsageMax"] = round(heapMemoryUsageMax / (1024.0 * 1024.0), 2)
heapMemoryUsageUsed = value.get("used", 0)
if heapMemoryUsageUsed:
jvm_results["heapMemoryUsageUsed"] = round(heapMemoryUsageUsed / (1024.0 * 1024.0), 2)
elif metric["metric_key"] == "LiveNodes":
liveNodes = [x.encode() for x in value]
#jvm_results[metric["display_name"]] = liveNodes
elif metric["metric_key"] in ["ClusterName", "OperationMode"]:
jvm_results[metric["display_name"]] = value.encode()
else:
jvm_results[metric["display_name"]] = value
else:
if metric["metric_key"] == "HeapMemoryUsage":
jvm_results["heapMemoryUsageInit"] = 0
jvm_results["heapMemoryUsageCommitted"] = 0
jvm_results["heapMemoryUsageMax"] = 0
jvm_results["heapMemoryUsageUsed"] = 0
else:
jvm_results[metric["display_name"]] = 0
if not jvm_results:
collectd.error("Unable to collect jmxStats")
else:
if self.pollCounter <=1:
cassandraMetrics["jvmStats"] = deepcopy(jvm_results)
cassandraMetrics["jvmStats"]["unLoadedClassCount"] = 0
cassandraMetrics["jvmStats"]["loadedClassCount"] = 0
self.previousData["jvmStats"] = {}
self.previousData["jvmStats"]["unLoadedClassCount"] = jvm_results["unLoadedClassCount"]
self.previousData["jvmStats"]["loadedClassCount"] = jvm_results["loadedClassCount"]
else:
cassandraMetrics["jvmStats"] = deepcopy(jvm_results)
jvmStats = cassandraMetrics["jvmStats"]
previousStats = self.previousData["jvmStats"]
jvmStats["unLoadedClassCount"] = jvm_results["unLoadedClassCount"] - previousStats["unLoadedClassCount"]
jvmStats["loadedClassCount"] = jvm_results["loadedClassCount"] - previousStats["loadedClassCount"]
self.previousData["jvmStats"]["unLoadedClassCount"] = jvm_results["unLoadedClassCount"]
self.previousData["jvmStats"]["loadedClassCount"] = jvm_results["loadedClassCount"]
except Exception as e:
collectd.error("Error in collecting jmxStats due to %s " % str(e))
return cassandraMetrics
def get_keyspace_details(self, cassandraMetrics):
"""
Get keyspace stats from jolokia agent
:param cassandraMetrics: collected cassandra and jvm stats dict
:return: CassandraMetrics dict contains cassandra stats, jvm stats and one or more keyspace stats
"""
ks_results = {}
if self.keyspaces:
for keyspace in self.keyspaces:
try:
for metric in ks_metrics:
value = self.fetch_and_update(
name=metric["metric_name"],
keyname=metric["metric_key"],
jmx_domain=metric["jmx_domain"],
jmx_type=metric["jmx_type"],
jmx_scope=metric.get("jmx_scope"),
jmx_path=metric.get("jmx_path"),
format_str=metric["format_str"],
jmx_keyspace=keyspace
)
if value:
if metric["display_name"] == "diskSpaceUsed":
ks_results[metric["display_name"]] = round(value / (1024.0 * 1024.0), 2)
elif metric["display_name"] in ["writeThroughput", "readThroughput"]:
ks_results[metric["display_name"]] = round(value, 2)
else:
ks_results[metric["display_name"]] = value
else:
if metric["metric_key"] != "TableName":
ks_results[metric["display_name"]] = 0
if ks_results:
ks_results["_keyspaceName"] = keyspace
cassandraMetrics[keyspace] = {}
if self.pollCounter <= 1:
cassandraMetrics[keyspace].update(deepcopy(ks_results))
cassandraMetrics[keyspace]["readLatency"] = 0.0
cassandraMetrics[keyspace]["writeLatency"] = 0.0
self.previousData[keyspace] = {}
self.previousData[keyspace].update(deepcopy(ks_results))
else:
cassandraMetrics[keyspace].update(deepcopy(ks_results))
keyspaceStats = cassandraMetrics[keyspace]
previousStats = self.previousData[keyspace]
readLatencyCount = ks_results["readLatencyCount"] - previousStats["readLatencyCount"]
writeLatencyCount = ks_results["writeLatencyCount"] - previousStats["writeLatencyCount"]
readTotalLatency = ks_results["readTotalLatency"] - previousStats["readTotalLatency"]
writeTotalLatency = ks_results["writeTotalLatency"] - previousStats["writeTotalLatency"]
if readTotalLatency and readLatencyCount:
keyspaceStats["readLatency"] = round(float(readTotalLatency) / float(readLatencyCount), 2)
else:
keyspaceStats["readLatency"] = 0.0
if writeTotalLatency and writeLatencyCount:
keyspaceStats["writeLatency"] = round(float(writeTotalLatency) / float(writeLatencyCount), 2)
else:
keyspaceStats["writeLatency"] = 0.0
self.previousData[keyspace].update(ks_results)
else:
collectd.error("Unable to collectd metrics for the keyspace %s" % keyspace)
# Removing readLatencyCount, writeLatencyCount, readTotalLatency, writeTotalLatency from cassandraMetrics dict.
# As it needed only for internal calculation of read/write Latency
for key in ["readLatencyCount", "writeLatencyCount", "readTotalLatency", "writeTotalLatency"]:
if key in cassandraMetrics[keyspace].keys():
del cassandraMetrics[keyspace][key]
except Exception as e:
collectd.error("Error in collecting KeyspaceStats due to %s in the keyspace %s" % (str(e), keyspace))
continue
else:
collectd.info("No keyspaces found!!!")
return cassandraMetrics
def collect_data(self):
"""
Collect cassandrStats, jvmStats and keyspaceStats using jolokia JVM agent
:return: Dict contains cassandra stats, jvm stats and keyspace stats
"""
cassandra_details = self.get_cassandra_details()
jvm_details = self.get_jvm_details(cassandra_details)
final_cassandra_details = self.get_keyspace_details(jvm_details)
#Adding common parameters to the collected stats
if final_cassandra_details:
for key, value in final_cassandra_details.items():
if key not in [CASSANDRA_STATS, JVMSTATS]:
self.add_common_params(KEYSPACE_STATS, value)
else:
self.add_common_params(key, value)
else:
collectd.error("Error: cassandra metrics not found")
return final_cassandra_details
@staticmethod
def dispatch_data(dict_disks_copy):
for details_type, details in dict_disks_copy.items():
collectd.debug("Plugin cassandra: Values: " + json.dumps(details))
collectd.info("final details are : %s" % details)
dispatch(details)
def read(self):
try:
self.pollCounter += 1
# collect data
dict_cassandra = self.collect_data()
if not dict_cassandra:
collectd.error("Plugin CASSANDRA: Unable to fetch data for CASSANDRA.")
return
# dispatch data to collectd, copying by value
self.dispatch_data(deepcopy(dict_cassandra))
except Exception as e:
collectd.error("Couldn't read and gather the cassandra metrics due to the exception :%s due to %s" % (e, traceback.format_exc()))
return
def read_temp(self):
"""Collectd first calls register_read. At that time default interval is taken,
hence temporary function is made to call, the read callback is unregistered
and read() is called again with interval obtained from conf by register_config callback."""
collectd.unregister_read(self.read_temp)
collectd.register_read(self.read, interval=int(self.interval))
def init():
"""When new process is formed, action to SIGCHLD is reset to default behavior."""
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
OBJ = CassandraStats()
collectd.register_init(init)
collectd.register_config(OBJ.config)
collectd.register_read(OBJ.read_temp)