-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor.py
117 lines (103 loc) · 4.41 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import requests
import subprocess
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 is_node_active(node_name):
config.load_kube_config()
v1 = client.CoreV1Api()
node = v1.read_node(node_name)
if node.spec.unschedulable:
print(f"Scheduling is disabled on node {node_name}.")
return 0
else:
print(f"Scheduling is enabled on node {node_name}.")
return 1
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 get_node_cpu_utilization(node):
node_metrics_url = f"{METRICS_SERVER_URL}/apis/metrics.k8s.io/v1beta1/nodes"
node_response = requests.get(node_metrics_url)
node_metrics = node_response.json()
cpu_usage_milli = 0
for i in range(len(node_metrics['items'])):
item = node_metrics['items'][i]
node_name = item['metadata']['name']
if 'u' in item['usage']['cpu']:
cpu_usage_u = int(item['usage']['cpu'].rstrip('u'))
cpu_usage_milli = cpu_usage_u / 1e5 # Convert ucores to millicores
elif 'n' in item['usage']['cpu']:
cpu_usage_nano = int(item['usage']['cpu'].rstrip('n'))
cpu_usage_milli = cpu_usage_nano / 1e6 # Convert nanocores to millicores
if node==i:
break
cpu = cpu_usage_milli
cpu_capacity = 16000
return cpu/cpu_capacity
def get_cluster_utilization():
node_metrics_url = f"{METRICS_SERVER_URL}/apis/metrics.k8s.io/v1beta1/nodes"
node_response = requests.get(node_metrics_url)
node_metrics = node_response.json()
cpu_capacity = 16000
cpu = 0
cpu_usage_milli = 0
for i in range(len(node_metrics['items'])):
if i==0:
continue
item = node_metrics['items'][i]
node_name = item['metadata']['name']
if 'u' in item['usage']['cpu']:
cpu_usage_u = int(item['usage']['cpu'].rstrip('u'))
cpu_usage_milli = cpu_usage_u / 1e5 # Convert ucores to millicores
elif 'n' in item['usage']['cpu']:
cpu_usage_nano = int(item['usage']['cpu'].rstrip('n'))
cpu_usage_milli = cpu_usage_nano / 1e6 # Convert nanocores to millicores
cpu = max(cpu_usage_milli/cpu_capacity, cpu)
# return overall
return cpu
def get_active_pods():
cmd = f"kubectl get pods --field-selector=status.phase!=Succeeded,status.phase!=Failed | wc -l"
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
try:
if int(result.stdout):
return int(result.stdout)-1
except Exception as e:
return 0