diff --git a/os_capacity/commands/commands.py b/os_capacity/commands/commands.py index c9dec8b..2c56858 100644 --- a/os_capacity/commands/commands.py +++ b/os_capacity/commands/commands.py @@ -17,7 +17,6 @@ from cliff.lister import Lister -from os_capacity.data import metrics from os_capacity import prometheus from os_capacity import utils @@ -55,32 +54,6 @@ class ListResourcesGroups(Lister): def take_action(self, parsed_args): groups = utils.group_providers_by_type_with_capacity(self.app) - groups = list(groups) # convert iterator - - metrics_to_send = [] - for group in groups: - flavors = group[4].replace(", ", "-") - if not flavors: - # skip empty hosts - continue - resources = group[0] - total = group[1] - used = group[2] - free = group[3] - metrics_to_send.append(metrics.Metric( - name="resources.total", value=total, - value_meta={"flavor_resources": resources}, - dimensions={"flavor": flavors})) - metrics_to_send.append(metrics.Metric( - name="resources.used", value=used, - value_meta={"flavor_resources": resources}, - dimensions={"flavor": flavors})) - metrics_to_send.append(metrics.Metric( - name="resources.free", value=free, - value_meta={"flavor_resources": resources}, - dimensions={"flavor": flavors})) - metrics.send_metrics(self.app.monitoring_client, metrics_to_send) - return ( ('Resource Class Groups', 'Total', 'Used', 'Free', 'Flavors'), groups) diff --git a/os_capacity/data/metrics.py b/os_capacity/data/metrics.py deleted file mode 100644 index 487ef1f..0000000 --- a/os_capacity/data/metrics.py +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright (c) 2017 StackHPC Ltd. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import collections -import os -import time - -Metric = collections.namedtuple( - "Metric", ("name", "value", "value_meta", "dimensions")) - -SEND_METRICS = 'OS_CAPACITY_SEND_METRICS' in os.environ - - -def send_metrics(monitoring_client, metrics): - if not SEND_METRICS: - return - - timestamp = time.time() * 1000 - formatted_metrics = [] - for metric in metrics: - formatted_metrics.append({ - "name": "os_capacity.%s" % metric.name, - "value": float(metric.value), - "timestamp": timestamp, - "value_meta": metric.value_meta, - "dimensions": metric.dimensions, - }) - response = monitoring_client.post("/metrics", json=formatted_metrics) - if not response: - raise Exception("Had trouble talking to monasca %s" % response) diff --git a/os_capacity/data/server.py b/os_capacity/data/server.py index 3293bd2..4a94942 100644 --- a/os_capacity/data/server.py +++ b/os_capacity/data/server.py @@ -28,7 +28,10 @@ def _parse_created(raw_created): def get(compute_client, uuid): url = "/servers/%s" % uuid - raw_server = compute_client.get(url).json()['server'] + response = compute_client.get(url) + if not response.ok: + return None + raw_server = response.json()['server'] return Server( uuid=raw_server['id'], name=raw_server['name'], diff --git a/os_capacity/data/users.py b/os_capacity/data/users.py index 58c05c1..f4c8a72 100644 --- a/os_capacity/data/users.py +++ b/os_capacity/data/users.py @@ -14,12 +14,12 @@ def get_all(identity_client): - response = identity_client.get("/v3/users").json() + response = identity_client.get("/users").json() raw_users = response['users'] return {u['id']: u['name'] for u in raw_users} def get_all_projects(identity_client): - response = identity_client.get("/v3/projects").json() + response = identity_client.get("/projects").json() raw_projects = response['projects'] return {u['id']: u['name'] for u in raw_projects} diff --git a/os_capacity/utils.py b/os_capacity/utils.py index 59ddde8..32ea515 100644 --- a/os_capacity/utils.py +++ b/os_capacity/utils.py @@ -17,7 +17,6 @@ import os from os_capacity.data import flavors -from os_capacity.data import metrics from os_capacity.data import resource_provider from os_capacity.data import server as server_data from os_capacity.data import users @@ -144,13 +143,14 @@ def get_allocations_with_server_info(app, flat_usage=True, get_names=False): usage = ", ".join(usage_amounts) server = server_data.get(app.compute_client, allocation.consumer_uuid) - delta = now - server.created - days_running = delta.days + 1 + if server: + delta = now - server.created + days_running = delta.days + 1 - allocation_tuples.append(AllocationList( - rp_name, allocation.consumer_uuid, usage, - server.flavor_id, days_running, server.project_id, - server.user_id)) + allocation_tuples.append(AllocationList( + rp_name, allocation.consumer_uuid, usage, + server.flavor_id, days_running, server.project_id, + server.user_id)) allocation_tuples.sort(key=lambda x: (x.project_id, x.user_id, x.days * -1, x.flavor_id)) @@ -203,7 +203,6 @@ def get_key(allocation): all_users = users.get_all(app.identity_client) all_projects = users.get_all_projects(app.identity_client) - metrics_to_send = [] summary_tuples = [] for key, group in grouped_allocations.items(): grouped_usage = collections.defaultdict(int) @@ -249,21 +248,7 @@ def get_key(allocation): value_meta = {'usage_summary': usage} dimensions['version'] = '2.0' - metrics_to_send.append(metrics.Metric( - name="usage.%s.count" % group_by, - value=grouped_usage['Count'], - value_meta=value_meta, - dimensions=dimensions)) - metrics_to_send.append(metrics.Metric( - name="usage.%s.days.count" % group_by, - value=grouped_usage_days['Count'], - value_meta=value_meta, - dimensions=dimensions)) - # Sort my largest current usage first summary_tuples.sort(key=lambda x: x[1], reverse=True) - if metrics_to_send: - metrics.send_metrics(app.monitoring_client, metrics_to_send) - return summary_tuples