forked from ErickRamirezDS/cass_log_tools
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathparse_json.py
executable file
·71 lines (60 loc) · 2.13 KB
/
parse_json.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
#!/usr/bin/python
# This script can be used to parse most
# json files in the diagnostics output.
#
# Its useful if you wanted to compare settings or values
# across several nodes at a time to make it a bit easier
# then looking through multiple json files
import os
import json
import re
import sys
if len(sys.argv) != 2:
print "\n***",sys.argv[0], "***\n"
print 'Incorrect number of arguments, please run script as follows:'
print '\n'+str(sys.argv[0])+' <file name>'
sys.exit(0)
# Setup variables etc
path = '.'
file_to_parse = sys.argv[1]
def findAndParse(name):
# Reset variables etc
found = False
data = { }
# Use os.walk to find all the directories under the path
for dirname, dirnames, filenames in os.walk(path):
for file in filenames:
# Check if the file name matches
matched = re.match(name, file, re.M)
if matched:
# Set flag
found = True
# load the file
currentfile = dirname+'/'+file
# load the data into a dictionary
data[currentfile] = json.loads(open(currentfile).read())
# break once complete (theres only one file per diretory)
break
# If the file is not found anywhere then exit function
# So we can continue with the next file
if not found:
print '\nUnable to find file named ' + name + '\n'
return
# ** debug **
# comment in to debug
#print data
# loop through the first item only to get only one lot of items
for item in data.values()[0]:
# loop through the first level of items (i.e. all nodes)
for node in data:
# print out the values for each node
# in some cases the item might be missing
# in this case just print that and move on
if item in data[node]:
print "node: ", node, item, data[node][item]
else:
print "node: ", node, item, '<not found>'
# Add seperator for cleaner output
print ''
# Call the function with the given file
findAndParse(file_to_parse)