Skip to content

Commit 9ff90be

Browse files
authored
Merge pull request #779 from jettero/dq-tool
dq view tool
2 parents ebc676c + 46dec3c commit 9ff90be

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

contrib/disk-queue-info.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python
2+
3+
from __future__ import print_function
4+
5+
import os
6+
import argparse
7+
import json
8+
from json.decoder import WHITESPACE
9+
from hubblestack.hec.dq import DiskQueue
10+
11+
def get_args(*a):
12+
parser = argparse.ArgumentParser(description='hubble disk queue info extractor')
13+
parser.add_argument("cachedir", nargs='+')
14+
parser.add_argument('-p', '--peek', '--print', action='store_true',
15+
help='attempt to dump all the bytes found in queue in the order they would be dequeued')
16+
parser.add_argument('-e', '--evil-decode', action='store_true',
17+
help='attempt to decode all json found in queue (implies -p) and pretty-print them')
18+
parser.add_argument('-c', '--with-color', action='store_true',
19+
help='attempt to pull in pygments and colorize the output of -e (implies -p and -e)')
20+
parser.add_argument('-m', '--show-meta', action='store_true',
21+
help='attempt to show meta-data as a comment in peek mode or a spurious _META_ field in evil mode')
22+
args = parser.parse_args(*a)
23+
if args.with_color:
24+
args.evil_decode = True
25+
if args.evil_decode:
26+
args.peek = True
27+
return args
28+
29+
def show_info(dirname):
30+
dq = DiskQueue(dirname)
31+
print("QUEUE={} ITEMS={} SIZE={}".format(dirname, dq.cn, dq.sz))
32+
33+
def evil_decode(docbytes):
34+
decoder = json.JSONDecoder()
35+
idx = WHITESPACE.match(docbytes, 0).end()
36+
while idx < len(docbytes):
37+
try:
38+
obj, end = decoder.raw_decode(docbytes, idx)
39+
yield obj
40+
idx = WHITESPACE.match(docbytes, end).end()
41+
except ValueError as e:
42+
print('docbytes:\n', docbytes)
43+
raise
44+
45+
def read_entries(dirname, evil=False, color=False, meta=False):
46+
dq = DiskQueue(dirname)
47+
for item,meta in dq.iter_peek():
48+
if evil:
49+
for obj in evil_decode(item):
50+
if meta:
51+
obj['_META_'] = meta
52+
obj = json.dumps(obj, sort_keys=True, indent=2)
53+
if color:
54+
try:
55+
from pygments import highlight, lexers, formatters
56+
obj = highlight(obj, lexers.JsonLexer(),
57+
formatters.TerminalFormatter())
58+
except ImportError:
59+
pass
60+
print(obj)
61+
else:
62+
if meta:
63+
print('# ', json.dumps(meta))
64+
print(item)
65+
66+
def main(args):
67+
for dirname in args.cachedir:
68+
try:
69+
if not os.path.isdir(dirname):
70+
raise Exception("directory does not exist")
71+
if args.peek:
72+
read_entries(dirname, evil=args.evil_decode, color=args.with_color, meta=args.show_meta)
73+
else:
74+
show_info(dirname)
75+
except Exception as e:
76+
print("# Exception while reading {}: {}".format(dirname, repr(e)))
77+
78+
if __name__ == '__main__':
79+
try:
80+
main(get_args())
81+
except KeyboardInterrupt:
82+
pass

hubblestack/hec/dq.py

+6
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ def peek(self):
160160
with open(fname, 'rb') as fh:
161161
return self.decompress(fh.read()), self.read_meta(fname)
162162

163+
def iter_peek(self):
164+
''' iterate and return all items in the disk queue (without removing any) '''
165+
for fname in self.files:
166+
with open(fname, 'rb') as fh:
167+
yield self.decompress(fh.read()), self.read_meta(fname)
168+
163169
def get(self):
164170
""" get the next item from the queue
165171
returns: data_octets, meta_data_dict

0 commit comments

Comments
 (0)