This repository has been archived by the owner on Dec 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
telegraf-cassandra-plugin.py
executable file
·62 lines (53 loc) · 1.95 KB
/
telegraf-cassandra-plugin.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
#!/usr/bin/env python
import argparse
import json
import urllib
# Command-line arguments:
parser = argparse.ArgumentParser(
description = 'Process some integers.'
)
parser.add_argument(
'--host',
dest = 'jolokia_host',
default = 'localhost',
help = 'Jolokia host (default = "localhost")'
)
parser.add_argument(
'--port',
dest = 'jolokia_port',
default = '8080',
help = 'Jolokia port (default = "8080")'
)
parser.add_argument(
'--metric',
dest = 'metric_name',
default = 'ReadLatency',
help = 'Metric to get [default="ReadLatency", "CoordinatorReadLatency", "WriteLatency", "ReadTotalLatency", "WriteTotalLatency", "LiveDiskSpaceUsed"]'
)
args = parser.parse_args()
# Put together a Jolokia URL:
jolokia_url = "http://%s:%s/jolokia/read/org.apache.cassandra.metrics:type=ColumnFamily,keyspace=*,scope=*,name=%s" % (args.jolokia_host, args.jolokia_port, args.metric_name)
# Get JSON data from Jolokia:
response = urllib.urlopen(jolokia_url)
# Load the data into a dictionary:
jolokia_data = json.loads(response.read())
# Go through the results (one per column_family):
for column_family_path in jolokia_data['value']:
# Break up the column_family_path to derive tags:
tags = {}
for tag in column_family_path.split(':')[1].split(','):
tag_key = tag.split('=')[0]
tag_value = tag.split('=')[1]
tags[tag_key] = tag_value
# Now derive the values:
field_values = []
for field in jolokia_data['value'][column_family_path]:
field_value = jolokia_data['value'][column_family_path][field]
if(type(field_value) == int) | (type(field_value) == float):
field_values.append('%s=%s' % (field, field_value))
elif(field_value == None):
pass
else:
field_values.append('%s="%s"' % (field, field_value))
# Now print the metric out in Influx format ("cpu,cpu=cpu0,host=foo,datacenter=us-east usage_idle=99,usage_busy=1"):
print("cassandra_columnfamily_%s,keyspace=%s,column_family=%s %s" % (args.metric_name, tags['keyspace'], tags['scope'], ','.join(field_values)))