Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a nicer way of fetching node process metrics. #254

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions shakenfist_client/apiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 20 additions & 7 deletions shakenfist_client/commandline/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import time

from shakenfist_client.apiclient import IncapableException
from shakenfist_client import util


Expand Down Expand Up @@ -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))
Expand Down
Loading