Skip to content

Commit

Permalink
add metric-server.py
Browse files Browse the repository at this point in the history
  • Loading branch information
progML committed Jan 21, 2024
1 parent 3287b7e commit 140f1e7
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions code/example-mec-app/metric-server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from kubernetes import client, config

config.load_kube_config()


api = client.CustomObjectsApi()

pod_list = client.CoreV1Api().list_namespaced_pod('default')

for pod in pod_list.items:
pod_name = pod.metadata.name
pod_namespace = pod.metadata.namespace
pod_phase = pod.status.phase
pod_node = pod.spec.node_name

print(f"Pod Name: {pod_name}, Located on Node: {pod_node}, Namespace: {pod_namespace}, Phase: {pod_phase}")

try:
api_response = api.get_namespaced_custom_object(
group="metrics.k8s.io",
version="v1beta1",
namespace=pod_namespace,
plural="pods",
name=pod_name
)

containers = api_response['containers']
if containers:
metrics = containers[0]['usage']
cpu_usage = metrics['cpu']
memory_usage = metrics['memory']

print(f"CPU Usage: {cpu_usage}")
print(f"Memory Usage: {memory_usage}")
else:
print(f"Metrics not found for pod {pod_name}.")
print('-' * 50)

except Exception as e:
print(f"Error fetching metrics for pod {pod_name}: {e}")

0 comments on commit 140f1e7

Please sign in to comment.