Skip to content

Commit

Permalink
Merge pull request #254 from shakenfist/node-process-metrics
Browse files Browse the repository at this point in the history
Support a nicer way of fetching node process metrics.
  • Loading branch information
mikalstill authored Jan 9, 2024
2 parents 863eded + c8c3983 commit 21441de
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
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

0 comments on commit 21441de

Please sign in to comment.