-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
65 lines (53 loc) · 2.18 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import requests
import time
import json
from kubernetes import client, config
# Configuration
METRICS_SERVER_URL = "http://localhost:8001"
# KUBERNETES_API_URL = "https://128.110.217.55:6443"
SAMPLING_RATE = 60 # in seconds
def get_pod_status():
config.load_kube_config()
v1 = client.CoreV1Api()
try:
pods = v1.list_pod_for_all_namespaces()
for pod in pods.items:
print(f"Pod: {pod.metadata.name}, Status: {pod.status.phase}")
except Exception as e:
print(f"Error fetching pod status: {e}")
def process_pod_metrics(pod_metrics):
for item in pod_metrics['items']:
pod_name = item['metadata']['name']
namespace = item['metadata']['namespace']
for container in item['containers']:
container_name = container['name']
cpu_usage_nano = int(container['usage']['cpu'].rstrip('n'))
cpu_usage_milli = cpu_usage_nano / 1e6 # Convert nanocores to millicores
print(f"Pod: {pod_name}, Namespace: {namespace}, Container: {container_name}, CPU Usage: {cpu_usage_milli:.2f} millicores")
def process_node_metrics(node_metrics):
for item in node_metrics['items']:
node_name = item['metadata']['name']
cpu_usage_nano = int(item['usage']['cpu'].rstrip('n'))
cpu_usage_milli = cpu_usage_nano / 1e6 # Convert nanocores to millicores
print(f"Node: {node_name}, CPU Usage: {cpu_usage_milli:.2f} millicores")
def get_cpu_utilization():
pod_metrics_url = f"{METRICS_SERVER_URL}/apis/metrics.k8s.io/v1beta1/pods"
node_metrics_url = f"{METRICS_SERVER_URL}/apis/metrics.k8s.io/v1beta1/nodes"
try:
# Fetch Pod metrics
pod_response = requests.get(pod_metrics_url)
pod_metrics = pod_response.json()
process_pod_metrics(pod_metrics)
# Fetch Node metrics
node_response = requests.get(node_metrics_url)
node_metrics = node_response.json()
process_node_metrics(node_metrics)
except requests.RequestException as e:
print(f"Error fetching metrics: {e}")
def main():
while True:
get_pod_status()
get_cpu_utilization()
time.sleep(SAMPLING_RATE)
if __name__ == "__main__":
main()