-
Notifications
You must be signed in to change notification settings - Fork 0
/
overview.py
executable file
·166 lines (143 loc) · 4.96 KB
/
overview.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
import commands
import json
import datetime
import sys
sys.path.insert(0, r'./python-texttable/')
from texttable import Texttable, get_color_string, bcolors
# import pprint
# pp = pprint.PrettyPrinter(indent=2)
# pp.pprint()
namespace = "kube-system"
nodes_dict = {}
pods = []
svcs = []
def format_duration(seconds):
res = ""
if seconds > 60 * 60 * 24:
res = "%d%s" % (int(seconds / (60 * 60 * 24)), "d")
elif seconds > 60 * 60:
res = "%d%s" % (int(seconds / (60 * 60)), "h")
elif seconds > 60:
res = "%d%s" % (int(seconds / 60), "m")
else:
res = "%d%s" % (int(seconds), "s")
return res
def get_pod_age(startTime):
date = datetime.datetime.strptime(startTime, "%Y-%m-%dT%H:%M:%SZ")
seconds = (datetime.datetime.now() - date).total_seconds()
return format_duration(seconds)
def get_nodes():
nodes_cmd = "kubectl --namespace %s get nodes -o json" % namespace
nodes_json = commands.getoutput(nodes_cmd)
nodes = json.loads(nodes_json)["items"]
for node in nodes:
node_name = node["metadata"]["name"]
os_image = node["status"]["nodeInfo"]["osImage"]
status = "Ready"
for condition in node["status"]["conditions"]:
if condition["type"] == "Ready":
if condition["status"] != "True":
status = "NotReady"
role = "worker"
if node["metadata"]["labels"].has_key("node-role.kubernetes.io/master"):
role = "master"
unschedulable = False
if node["spec"].has_key("unschedulable") and node["spec"]["unschedulable"] == True:
unschedulable = True
nodes_dict[node_name] = {
"name": node_name,
"osImage": os_image,
"role": role,
"status": status,
"unschedulable": unschedulable,
"pods": []
}
return
def get_pods():
pods_cmd = "kubectl --namespace %s get pods -o json" % namespace
pods_json = commands.getoutput(pods_cmd)
pods.extend(json.loads(pods_json)["items"])
def get_svcs():
svcs_cmd = "kubectl --namespace %s get svc -o json" % namespace
svcs_json = commands.getoutput(svcs_cmd)
svcs.extend(json.loads(svcs_json)["items"])
def get_pod_port(pod_label):
port = ""
target_port = ""
port_list = []
target_port_list = []
for svc in svcs:
if svc["spec"].has_key("selector") and svc["spec"]["selector"].has_key("name") and svc["spec"]["selector"]["name"] == pod_label:
for port in svc["spec"]["ports"]:
port_list.append("%s/%s" % (port["port"], port["protocol"]))
target_port_list.append("%s/%s" % (port["targetPort"], port["protocol"]))
port = ",".join(port_list)
target_port = ",".join(target_port_list)
return {
"port": port,
"target_port": target_port
}
def sort_node_pods():
for pod in pods:
pod_name = pod["metadata"]["name"]
pod_status = pod["status"]["phase"]
node_name = pod["spec"]["nodeName"]
restart_count = 0
for cs in pod["status"]["containerStatuses"]:
restart_count = restart_count + cs["restartCount"]
start_time = pod['status']['startTime']
pod_label = ""
if pod["metadata"]["labels"].has_key("name"):
pod_label = pod["metadata"]["labels"]["name"]
ports = get_pod_port(pod_label)
if nodes_dict.has_key(node_name):
nodes_dict[node_name]["pods"].append({
"name": pod_name,
"status": pod_status,
"restart_count": restart_count,
"age": get_pod_age(start_time),
"port": ports["port"],
"target_port": ports["target_port"]
})
def print_nodes():
node_table = Texttable()
node_table.set_deco(Texttable.HEADER)
node_table.set_cols_align(["l", "l", "l", "l"])
node_rows = [["NAME", "ROLE", "STATUS", "OS"]]
for node_name in nodes_dict.keys():
node = nodes_dict[node_name]
status = ""
if node["status"] == "Ready":
status = get_color_string(bcolors.GREEN, node["status"])
else:
status = get_color_string(bcolors.RED, node["status"])
if node["unschedulable"]:
status = "%s,%s" % (status, get_color_string(bcolors.YELLOW, "SchedulingDisabled"))
node_rows.append([node["name"], node["role"], status, node["osImage"]])
node_table.add_rows(node_rows)
print node_table.draw() + "\n\n"
def print_node_pods():
for node_name in nodes_dict.keys():
node = nodes_dict[node_name]
pods_table = Texttable(0)
pods_table.set_deco(Texttable.HEADER)
pods_table.set_cols_align(["l", "r", "r", "r", "r", "r"])
pods_rows = [["NAME", "STATUS", "RESTART", "AGE", "TARGET PORT", "PORT"]]
if len(node["pods"]) > 0:
for pod in node["pods"]:
status = ""
if pod["status"] == "Running":
status = get_color_string(bcolors.GREEN, pod["status"])
else:
status = get_color_string(bcolors.YELLOW, pod["status"])
pods_rows.append([pod["name"], status, pod["restart_count"], pod["age"], pod["target_port"], pod["port"]])
pods_table.add_rows(pods_rows)
print node_name
print pods_table.draw() + "\n\n"
get_nodes()
get_pods()
get_svcs()
sort_node_pods()
print_nodes()
print_node_pods()