diff --git a/shakenfist_client/apiclient.py b/shakenfist_client/apiclient.py index e3c90fe..2c68f10 100644 --- a/shakenfist_client/apiclient.py +++ b/shakenfist_client/apiclient.py @@ -844,6 +844,14 @@ def delete_node(self, node): r = self._request_url('DELETE', '/nodes/' + node) return r.json() + def get_node_process_metrics(self, node): + if not self.check_capability('node-process-metrics'): + raise IncapableException( + 'The API server version you are talking to does not support ' + 'fetching process metrics for a node.') + r = self._request_url('GET', '/nodes/' + node + '/processmetrics') + return r.json() + def get_interface(self, interface_uuid): r = self._request_url('GET', '/interfaces/' + interface_uuid) return r.json() diff --git a/shakenfist_client/commandline/node.py b/shakenfist_client/commandline/node.py index 6b7f9a6..9ffffc5 100644 --- a/shakenfist_client/commandline/node.py +++ b/shakenfist_client/commandline/node.py @@ -5,6 +5,7 @@ import sys import time +from shakenfist_client.apiclient import IncapableException from shakenfist_client import util @@ -189,13 +190,25 @@ def node_cpuhogs(ctx, threshold=0.25): hogs = [] for node in ctx.obj['CLIENT'].get_nodes(): - event = ctx.obj['CLIENT'].get_node_events(node['name'], event_type='resources', limit=1)[0] - for resource in event.get('extra', {}): - value = event['extra'][resource] - if resource.startswith('process_cpu_fraction_') and value > threshold: - hogs.append('%s on node %s has consumed %.02f of a CPU, threshold is %.02f' - % (resource[len('process_cpu_fraction_'):], - node['name'], value, threshold)) + try: + metrics = ctx.obj['CLIENT'].get_node_process_metrics(node['name']) + for resource in metrics: + value = metrics[resource] + if resource.startswith('process_cpu_fraction_') and value > threshold: + hogs.append('%s on node %s has consumed %.02f of a CPU, threshold is %.02f' + % (resource[len('process_cpu_fraction_'):], + node['name'], value, threshold)) + + except IncapableException: + # Fall back to the old way + event = ctx.obj['CLIENT'].get_node_events(node['name'], event_type='resources', + limit=1)[0] + for resource in event.get('extra', {}): + value = event['extra'][resource] + if resource.startswith('process_cpu_fraction_') and value > threshold: + hogs.append('%s on node %s has consumed %.02f of a CPU, threshold is %.02f' + % (resource[len('process_cpu_fraction_'):], + node['name'], value, threshold)) if hogs: print('\n'.join(hogs))