-
Notifications
You must be signed in to change notification settings - Fork 16
/
cassandra.py
executable file
·93 lines (72 loc) · 1.93 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
#!/usr/bin/env python
import socket
import subprocess
import sys
import psutil
NODE_TOOL = '/usr/local/cassandra-1/bin/nodetool'
HOST = 'localhost'
def is_digit(d):
try:
float(d)
except ValueError:
return False
return True
def get_cfstats():
try:
p = subprocess.Popen([NODE_TOOL, "-h", HOST, "cfstats"], stdout=subprocess.PIPE)
return p.stdout
except:
print "Plugin Failed! Cassandra is down"
sys.exit(2)
def parse(f):
values = {}
keyspace = None
while True:
line = f.readline()
if not line:
break
s = line.split()
if not s:
continue
if s[0].startswith('Keyspace'):
keyspace = s[1]
parse_keyspace(f, values, keyspace)
if s[0].startswith('Column'):
cf = s[2]
parse_cf(f, values, keyspace, cf)
return values
def parse_keyspace(f, values, keyspace):
values[keyspace] = {'global': {}}
for i in xrange(0, 5):
line = f.readline()
s = line.split()
add_value(s, values[keyspace]['global'])
def parse_cf(f, values, keyspace, cf):
values[keyspace][cf] = {}
while True:
line = f.readline()
if not line:
break
s = line.split()
if not s:
break
s = line.split()
add_value(s, values[keyspace][cf])
def add_value(s, values):
for i, k in enumerate(s):
s[i] = k.replace('(', '').replace(')', '')
if s[-1] == 'ms.':
s = s[:-1]
if s[-1] == 'NaN':
s[-1] = '0'
if is_digit(s[-1]):
k = '_'.join(s[0:-1]).replace(':', '')
values[k] = s[-1]
f = get_cfstats()
dataset = parse(f)
output = "OK | "
for root, context in dataset.iteritems():
for counter, values in context.iteritems():
for k, v in values.iteritems():
output += root + '.' + counter + '.' + k + '=' + v + ';;;; '
print output