Skip to content

Commit 447bb2e

Browse files
committed
add args, byte dumps, pretty-print dumps, and colorized dumps
Though, it's worth noting that disk-queue-info.py -pec does nothing that the following wouldn't do faster: disk-queue-info.py | jq -C .
1 parent 354eb40 commit 447bb2e

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

contrib/disk-queue-info.py

+63-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,70 @@
11
#!/usr/bin/env python
22

3-
import sys
3+
import os
4+
import argparse
5+
import json
6+
from json.decoder import WHITESPACE
47
from hubblestack.hec.dq import DiskQueue
58

6-
def examine(dirname):
9+
def get_args(*a):
10+
parser = argparse.ArgumentParser(description='hubble disk queue info extractor')
11+
parser.add_argument("cachedir", nargs='+')
12+
parser.add_argument('-p', '--peek', '--print', action='store_true',
13+
help='attempt to dump all the bytes found in queue in the order they would be dequeued')
14+
parser.add_argument('-e', '--evil-decode', action='store_true',
15+
help='attempt to decode all json found in queue (implies -p) and pretty-print them')
16+
parser.add_argument('-c', '--with-color', action='store_true',
17+
help='attempt to pull in pygments and colorize the output of -e (implies -pe)')
18+
args = parser.parse_args(*a)
19+
if args.with_color:
20+
args.evil_decode = True
21+
if args.evil_decode:
22+
args.peek = True
23+
return args
24+
25+
def show_info(dirname):
726
dq = DiskQueue(dirname)
8-
return dq.cn, dq.sz
27+
print("QUEUE={} ITEMS={} SIZE={}".format(dirname, dq.cn, dq.sz))
28+
29+
def evil_decode(docbytes):
30+
decoder = json.JSONDecoder()
31+
idx = WHITESPACE.match(docbytes, 0).end()
32+
while idx < len(docbytes):
33+
obj, end = decoder.raw_decode(docbytes, idx)
34+
yield obj
35+
idx = WHITESPACE.match(docbytes, end).end()
36+
37+
def read_entries(dirname, evil=False, color=False):
38+
dq = DiskQueue(dirname)
39+
for item in dq.iter_peek():
40+
if evil:
41+
for obj in evil_decode(item):
42+
obj = json.dumps(obj, sort_keys=True, indent=2)
43+
if color:
44+
try:
45+
from pygments import highlight, lexers, formatters
46+
obj = highlight(obj, lexers.JsonLexer(),
47+
formatters.TerminalFormatter())
48+
except ImportError:
49+
pass
50+
print(obj)
51+
else:
52+
print(item)
53+
54+
def main(args):
55+
for dirname in args.cachedir:
56+
try:
57+
if not os.path.isdir(dirname):
58+
raise Exception("directory does not exist")
59+
if args.peek:
60+
read_entries(dirname, evil=args.evil_decode, color=args.with_color)
61+
else:
62+
show_info(dirname)
63+
except Exception as e:
64+
print("# unable to read {}: {}".format(dirname, e))
965

1066
if __name__ == '__main__':
11-
for d in sys.argv[1:]:
12-
cn, sz = examine(d)
13-
print("QUEUE={} ITEMS={} SIZE={}".format(d, cn, sz))
67+
try:
68+
main(get_args())
69+
except KeyboardInterrupt:
70+
pass

hubblestack/hec/dq.py

+6
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ def peek(self):
165165
with open(fname, 'rb') as fh:
166166
return decode_something_to_string(self.decompress(fh.read())), self.read_meta(fname)
167167

168+
def iter_peek(self):
169+
''' iterate and return all items in the disk queue (without removing any) '''
170+
for fname in self.files:
171+
with open(fname, 'rb') as fh:
172+
yield self.decompress(fh.read())
173+
168174
def get(self):
169175
""" get the next item from the queue
170176
returns: data_octets, meta_data_dict

0 commit comments

Comments
 (0)