-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquicktest.py
executable file
·127 lines (113 loc) · 3.8 KB
/
quicktest.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
#!/usr/bin/env python
import elasticsearch
import socket
import sys
from dotmap import DotMap
from es_stats.classes import ClusterHealth, ClusterState, ClusterStats, NodeInfo, NodeStats
from es_stats.utils import fix_key, get_value
KEYS = [
'health',
'clusterstate',
'clusterstats',
'nodeinfo',
'nodestats',
]
SKIP_THESE = [
'get',
'os',
'update',
'keys',
'items',
'mappings',
'snapshots',
'metadata',
'type.default'
]
# 'get', 'update', 'keys', and 'items' are a protected methods in DotMap
# 'os' level stats are easier to get in other ways
# 'mappings' & 'metadata' should not be tracked in this way
# 'type.default' is a dotted notation, but single key. Very weird.
def dotty(dm, notation='', retval=[]):
for i in list(dm.keys()):
if i in SKIP_THESE:
continue
if isinstance(dm[i], DotMap):
nestlevel = notation[:]
if notation == '':
notation += i
else:
notation += '.' + i
try:
val = eval('dm' + '.' + i)
except (NameError, SyntaxError):
val = dm[i]
dotty(val, notation, retval=retval)
notation = nestlevel
elif isinstance(dm[i], list):
pass
else:
if notation == '':
retval.append('{0}'.format(i))
else:
retval.append('{0}.{1}'.format(notation, i))
return retval
def main():
usage = '''
USAGE: quicktest.py (API_TYPE)
API_TYPE must be one of {0}
'''.format(KEYS)
if len(sys.argv) < 2:
print(usage)
sys.exit(1)
if sys.argv[1] == '-h' or sys.argv[1] == '--help':
print(usage)
sys.exit(0)
elif sys.argv[1] not in KEYS:
print()
print('ERROR: "{0}" is not an accepted API_TYPE'.format(sys.argv[1]))
print(usage)
sys.exit(1)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if sock.connect_ex(('127.0.0.1',9200)) != 0:
print(' ERROR: Unable to connect to 127.0.0.1:9200')
print(' quicktest.py expects an unencrypted instance of elasticsearch to be running locally.')
sys.exit(1)
client = elasticsearch.Elasticsearch()
nodesdump = client.nodes.stats()['nodes']
nodeid = list(nodesdump.keys())[0] # Just take the first
nodename = nodesdump[nodeid]['name']
if sys.argv[1] == 'health':
data = client.cluster.health()
stats = ClusterHealth(client)
elif sys.argv[1] == 'clusterstate':
data = client.cluster.state()
stats = ClusterState(client)
elif sys.argv[1] == 'clusterstats':
data = client.cluster.stats()
stats = ClusterStats(client)
elif sys.argv[1] == 'nodeinfo':
data = client.nodes.info()['nodes'][nodeid]
stats = NodeInfo(client)
elif sys.argv[1] == 'nodestats':
data = client.nodes.stats()['nodes'][nodeid]
stats = NodeStats(client)
all_lines = []
for line in dotty(DotMap(data)):
value = stats.get(line, name=nodename)
if value == DotMap():
continue
all_lines.append('{0} = {1}'.format(line, value))
for line in sorted(all_lines):
print(line)
print()
print('======================================================')
print('The above are the available dotted keys obtained from:')
print(' --- API: {0}'.format(sys.argv[1]))
print(' --- node name: {0}'.format(nodename))
print(' --- nodeid: {0}'.format(nodeid))
print(' --- Elasticsearch version: {0}'.format(client.nodes.info()['nodes'][nodeid]['version']))
print('======================================================')
print()
print('Results may differ when run with different versions of Elasticsearch.')
if __name__ == "__main__":
main()