Open Kilda Java Documentation
monitor.py
Go to the documentation of this file.
1 # Copyright 2017 Telstra Open Source
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 import glob
16 import json
17 import logging
18 import pprint
19 import os
20 
21 import click
22 import time
23 
24 from kilda.probe.messaging import receive_with_context, send_with_context
25 
26 LOG = logging.getLogger(__name__)
27 
28 
29 @click.command(name='monitor')
30 @click.pass_obj
31 def monitor_command(ctx):
32  def print_message(record):
33  try:
34  LOG.info('New message in topic:\n%s',
35  pprint.pformat(json.loads(record.value)))
36  except Exception as ex:
37  print(ex)
38  print(record)
39 
40  receive_with_context(ctx, print_message)
41 
42 
43 @click.command(name='fake-bolt')
44 @click.pass_obj
45 @click.option('--count', default=3)
46 @click.option('--sleep', default=1)
47 def bolt_command(ctx, count, sleep):
48 
49  # get filepath to json with bolt states
51  res_dir = os.path.dirname(kilda.probe.test.res.__file__)
52 
53  def print_message(record):
54  try:
55  data = json.loads(record.value)
56  if data['clazz'] == 'org.openkilda.messaging.ctrl.CtrlRequest':
57  LOG.info('New message in topic:\n%s', pprint.pformat(data))
58  for filename in glob.glob(os.path.join(res_dir,
59  '*BoltState.json')):
60  with open(filename) as f:
61  message = json.load(f)
62  message['correlation_id'] = data['correlation_id']
63  send_with_context(ctx, bytes(json.dumps(message).
64  encode('utf-8')))
65  time.sleep(sleep)
66  except Exception:
67  LOG.exception('error')
68  print(record)
69 
70  receive_with_context(ctx, print_message)
def receive_with_context(context, callback, offset=None)
Definition: messaging.py:75
def bolt_command(ctx, count, sleep)
Definition: monitor.py:47
def send_with_context(context, message)
Definition: messaging.py:28