-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkube_kubectl.py
77 lines (61 loc) · 2.21 KB
/
kube_kubectl.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
#!/usr/bin/env python
import osquery
import os
@osquery.register_plugin
class K8Services(osquery.TablePlugin):
def name(self):
return "k8s_services"
def columns(self):
return [
osquery.TableColumn(name="name", type=osquery.STRING),
osquery.TableColumn(name="type", type=osquery.STRING),
osquery.TableColumn(name="cluster-ip", type=osquery.STRING),
osquery.TableColumn(name="external-ip", type=osquery.STRING),
osquery.TableColumn(name="port", type=osquery.STRING),
osquery.TableColumn(name="age", type=osquery.STRING),
]
def generate(self, context):
query_data = []
data = os.popen('kubectl get service').read()
datasplit=data.split()
i = 6
while i < len(datasplit):
row = {}
row["name"] = datasplit[i]
row["type"] = datasplit[i+1]
row["cluster-ip"] = datasplit[i+2]
row["external-ip"] = datasplit[i+3]
row["port"] = datasplit[i+4]
row["age"] = datasplit[i+5]
query_data.append(row)
i = i+6
return query_data
@osquery.register_plugin
class K8Pods(osquery.TablePlugin):
def name(self):
return "k8s_pods"
def columns(self):
return [
osquery.TableColumn(name="name", type=osquery.STRING),
osquery.TableColumn(name="ready", type=osquery.STRING),
osquery.TableColumn(name="status", type=osquery.STRING),
osquery.TableColumn(name="restarts", type=osquery.STRING),
osquery.TableColumn(name="age", type=osquery.STRING),
]
def generate(self, context):
query_data = []
data = os.popen('kubectl get pods').read()
datasplit=data.split()
i = 5
while i < len(datasplit):
row = {}
row["name"] = datasplit[i]
row["ready"] = datasplit[i+1]
row["status"] = datasplit[i+2]
row["restarts"] = datasplit[i+3]
row["age"] = datasplit[i+4]
query_data.append(row)
i = i+5
return query_data
if __name__ == "__main__":
osquery.start_extension(name="k8s", version="1.0.0")