| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# 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, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
"""Command line tool for sending test data for Ceilometer via oslo.messaging. |
| 16 |
|
| 17 |
Usage: |
| 18 |
|
| 19 |
Send messages with samples generated by make_test_data |
| 20 |
|
| 21 |
source .tox/py27/bin/activate |
| 22 |
./tools/send_test_data.py --count 1000 --resources_count 10 --topic metering |
| 23 |
""" |
| 24 |
import argparse |
| 25 |
import datetime |
| 26 |
import functools |
| 27 |
import json |
| 28 |
import random |
| 29 |
import uuid |
| 30 |
|
| 31 |
import make_test_data |
| 32 |
import oslo_messaging |
| 33 |
from oslo_utils import timeutils |
| 34 |
|
| 35 |
from ceilometer import messaging |
| 36 |
from ceilometer.publisher import utils |
| 37 |
from ceilometer import service |
| 38 |
|
| 39 |
|
| 40 |
def send_batch_notifier(notifier, topic, batch): |
| 41 |
notifier.sample({}, event_type=topic, payload=batch) |
| 42 |
|
| 43 |
|
| 44 |
def get_notifier(conf): |
| 45 |
return oslo_messaging.Notifier( |
| 46 |
messaging.get_transport(conf), |
| 47 |
driver='messagingv2', |
| 48 |
publisher_id='telemetry.publisher.test', |
| 49 |
topics=['metering'], |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
def generate_data(conf, send_batch, make_data_args, samples_count, |
| 54 |
batch_size, resources_count, topic): |
| 55 |
make_data_args.interval = 1 |
| 56 |
make_data_args.start = (timeutils.utcnow() - |
| 57 |
datetime.timedelta(minutes=samples_count)) |
| 58 |
make_data_args.end = timeutils.utcnow() |
| 59 |
|
| 60 |
make_data_args.resource_id = None |
| 61 |
resources_list = [str(uuid.uuid4()) |
| 62 |
for _ in range(resources_count)] |
| 63 |
resource_samples = {resource: 0 for resource in resources_list} |
| 64 |
batch = [] |
| 65 |
count = 0 |
| 66 |
for sample in make_test_data.make_test_data(conf, |
| 67 |
**make_data_args.__dict__): |
| 68 |
count += 1 |
| 69 |
resource = resources_list[random.randint(0, len(resources_list) - 1)] |
| 70 |
resource_samples[resource] += 1 |
| 71 |
sample['resource_id'] = resource |
| 72 |
# need to change the timestamp from datetime.datetime type to iso |
| 73 |
# format (unicode type), because collector will change iso format |
| 74 |
# timestamp to datetime.datetime type before recording to db. |
| 75 |
sample['timestamp'] = sample['timestamp'].isoformat() |
| 76 |
# need to recalculate signature because of the resource_id change |
| 77 |
sig = utils.compute_signature(sample, |
| 78 |
conf.publisher.telemetry_secret) |
| 79 |
sample['message_signature'] = sig |
| 80 |
batch.append(sample) |
| 81 |
if len(batch) == batch_size: |
| 82 |
send_batch(topic, batch) |
| 83 |
batch = [] |
| 84 |
if count == samples_count: |
| 85 |
send_batch(topic, batch) |
| 86 |
return resource_samples |
| 87 |
send_batch(topic, batch) |
| 88 |
return resource_samples |
| 89 |
|
| 90 |
|
| 91 |
def get_parser(): |
| 92 |
parser = argparse.ArgumentParser() |
| 93 |
|
| 94 |
parser.add_argument( |
| 95 |
'--batch-size', |
| 96 |
dest='batch_size', |
| 97 |
type=int, |
| 98 |
default=100 |
| 99 |
) |
| 100 |
parser.add_argument( |
| 101 |
'--config-file', |
| 102 |
default='/etc/ceilometer/ceilometer.conf' |
| 103 |
) |
| 104 |
parser.add_argument( |
| 105 |
'--topic', |
| 106 |
default='perfmetering' |
| 107 |
) |
| 108 |
parser.add_argument( |
| 109 |
'--samples-count', |
| 110 |
dest='samples_count', |
| 111 |
type=int, |
| 112 |
default=1000 |
| 113 |
) |
| 114 |
parser.add_argument( |
| 115 |
'--resources-count', |
| 116 |
dest='resources_count', |
| 117 |
type=int, |
| 118 |
default=100 |
| 119 |
) |
| 120 |
parser.add_argument( |
| 121 |
'--result-directory', |
| 122 |
dest='result_dir', |
| 123 |
default='/tmp' |
| 124 |
) |
| 125 |
return parser |
| 126 |
|
| 127 |
|
| 128 |
def main(): |
| 129 |
args = get_parser().parse_known_args()[0] |
| 130 |
make_data_args = make_test_data.get_parser().parse_known_args()[0] |
| 131 |
conf = service.prepare_service(argv=['/', '--config-file', |
| 132 |
args.config_file]) |
| 133 |
notifier = get_notifier(conf) |
| 134 |
send_batch = functools.partial(send_batch_notifier, notifier) |
| 135 |
result_dir = args.result_dir |
| 136 |
del args.config_file |
| 137 |
del args.result_dir |
| 138 |
|
| 139 |
resource_writes = generate_data(conf, send_batch, make_data_args, |
| 140 |
**args.__dict__) |
| 141 |
result_file = "{}/sample-by-resource-{}".format(result_dir, |
| 142 |
random.getrandbits(32)) |
| 143 |
with open(result_file, 'w') as f: |
| 144 |
f.write(json.dumps(resource_writes)) |
| 145 |
return result_file |
| 146 |
|
| 147 |
|
| 148 |
if __name__ == '__main__': |
| 149 |
main() |