Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Commit fcc026c

Browse files
author
Philip O'Toole
committed
Merge pull request #3 from MichaelBlume/post-json
allow posting json to some endpoint
2 parents d4cf6a4 + 04bdf8e commit fcc026c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
'kazoo==1.00',
88
'simplejson',
99
'argparse',
10+
'requests',
1011
'kafka-python'
1112
]
1213

stormkafkamon/monitor.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import argparse
44
import sys
55
from prettytable import PrettyTable
6+
import requests
7+
import simplejson as json
68

79
from zkclient import ZkClient, ZkError
810
from processor import process, ProcessorError
@@ -37,6 +39,21 @@ def display(summary, friendly=False):
3739
print 'Total broker depth: %s' % fmt(summary.total_depth)
3840
print 'Total delta: %s' % fmt(summary.total_delta)
3941

42+
def post_json(endpoint, zk_data):
43+
fields = ("broker", "topic", "partition", "earliest", "latest", "depth",
44+
"spout", "current", "delta")
45+
json_data = {"%s-%s" % (p.broker, p.partition):
46+
{name: getattr(p, name) for name in fields}
47+
for p in zk_data.partitions}
48+
total_fields = ('depth', 'delta')
49+
total = {fieldname:
50+
sum(getattr(p, fieldname) for p in zk_data.partitions)
51+
for fieldname in total_fields}
52+
total['partitions'] = len({p.partition for p in zk_data.partitions})
53+
total['brokers'] = len({p.broker for p in zk_data.partitions})
54+
json_data['total'] = total
55+
requests.post(endpoint, data=json.dumps(json_data))
56+
4057
######################################################################
4158

4259
def true_or_false_option(option):
@@ -58,6 +75,8 @@ def read_args():
5875
help='Root path for Kafka Spout data in Zookeeper')
5976
parser.add_argument('--friendly', action='store_const', const=True,
6077
help='Show friendlier data')
78+
parser.add_argument('--postjson', type=str,
79+
help='endpoint to post json data to')
6180
return parser.parse_args()
6281

6382
def main():
@@ -66,14 +85,18 @@ def main():
6685
zc = ZkClient(options.zserver, options.zport)
6786

6887
try:
69-
display(process(zc.spouts(options.spoutroot, options.topology)),
70-
true_or_false_option(options.friendly))
88+
zk_data = process(zc.spouts(options.spoutroot, options.topology))
7189
except ZkError, e:
7290
print 'Failed to access Zookeeper: %s' % str(e)
7391
return 1
7492
except ProcessorError, e:
7593
print 'Failed to process: %s' % str(e)
7694
return 1
95+
else:
96+
if options.postjson:
97+
post_json(options.postjson, zk_data)
98+
else:
99+
display(zk_data, true_or_false_option(options.friendly))
77100

78101
return 0
79102

0 commit comments

Comments
 (0)