Skip to content

Instantly share code, notes, and snippets.

@haginara
Last active July 8, 2016 20:46
Show Gist options
  • Save haginara/6f36c164983a09fb8bce to your computer and use it in GitHub Desktop.
Save haginara/6f36c164983a09fb8bce to your computer and use it in GitHub Desktop.
from __future__ import print_function
import json
import sys
import pprint
import argparse
import cStringIO
import tarfile
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--grep")
parser.add_argument("-f", "--field")
parser.add_argument("-z", "--gunzip")
parser.add_argument("-r", "--raw", action='store_true')
args = parser.parse_args()
def _json_print(s):
print(json.dumps(s))
json_print = _json_print if args.raw else pprint.pprint
for line in sys.stdin:
try:
jsonstream = json.loads(line)
if args.grep:
if args.grep in line:
json_print(jsonstream)
elif args.field:
if '=' in args.field:
kv = args.field.split('=')
if len(kv) != 2:
raise Exception("No key-value")
k, v = kv
data = jsonstream
data = data[k]
if data == v:
json_print(jsonstream)
else:
fields = args.field.split('.')
data = jsonstream
for field in fields:
data = data[field]
json_print(data)
else:
json_print(jsonstream)
except Exception as e:
continue
@haginara
Copy link
Author

haginara commented Dec 8, 2015

usage

Get size of the specific index on Elasticsearch

curl -XGET 127.0.0.1:9200/logstash-2015.12.08/_stats/store | python jsonify.py -f "_all.primaries.store.size_in_bytes"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment