-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmonitor.py
More file actions
176 lines (152 loc) · 5.91 KB
/
monitor.py
File metadata and controls
176 lines (152 loc) · 5.91 KB
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
167
168
169
170
171
172
173
174
175
176
import requests
from typing import Iterator, Dict
from contextlib import closing
from urllib.parse import urljoin
from django.db import connections
from django.conf import settings
query_network_flow_sql_tpl = """
SELECT
namespace,
pod_name,
last(rx_bytes, time) - first(rx_bytes, time) as rx_bytes,
last(tx_bytes, time) - first(tx_bytes, time) as tx_bytes
FROM kubernetes_pod_network
LEFT OUTER JOIN kubernetes_pod_network_tag
ON kubernetes_pod_network.tag_id = kubernetes_pod_network_tag.tag_id
WHERE
namespace in ({namespace_range})
AND time > to_timestamp({start})
AND time < to_timestamp({stop})
GROUP by namespace, pod_name
"""
query_container_count_sql_tpl = """
SELECT
count(1)
FROM (
SELECT
namespace,
pod_name,
container_name
FROM kubernetes_pod_container
LEFT OUTER JOIN kubernetes_pod_container_tag
ON kubernetes_pod_container.tag_id = kubernetes_pod_container_tag.tag_id
WHERE
namespace='{namespace}'
AND container_name='{container_name}'
AND time > to_timestamp({start})
AND time < to_timestamp({stop})
GROUP BY namespace, pod_name, container_name, kubernetes_pod_container.tag_id
) AS container
GROUP BY namespace, container_name
"""
query_cpu_usage_sql_tpl = """
SELECT
namespace,
pod_name,
container_name,
round(EXTRACT(EPOCH FROM time_bucket('{every}', time))) as timestamp,
max(cpu_usage_nanocores),
round(avg(cpu_usage_nanocores))
FROM kubernetes_pod_container
LEFT OUTER JOIN kubernetes_pod_container_tag
ON kubernetes_pod_container.tag_id = kubernetes_pod_container_tag.tag_id
WHERE
namespace='{namespace}'
AND container_name='{container_name}'
AND time > to_timestamp({start})
AND time < to_timestamp({stop})
GROUP BY namespace, pod_name, container_name, timestamp
"""
query_memory_usage_sql_tpl = """
SELECT
namespace,
pod_name,
container_name,
round(EXTRACT(EPOCH FROM time_bucket('{every}', time))) as timestamp,
max(memory_usage_bytes) as max,
round(avg(memory_usage_bytes)) as avg
FROM kubernetes_pod_container
LEFT OUTER JOIN kubernetes_pod_container_tag
ON kubernetes_pod_container.tag_id = kubernetes_pod_container_tag.tag_id
WHERE
namespace='{namespace}'
AND container_name='{container_name}'
AND time > to_timestamp({start})
AND time < to_timestamp({stop})
GROUP BY namespace, pod_name, container_name, timestamp
"""
query_network_usage_sql_tpl = """
SELECT
namespace,
pod_name,
round(EXTRACT(EPOCH FROM time_bucket('{every}', time))) as timestamp,
last(rx_bytes, time) - first(rx_bytes, time) as rx_bytes,
last(tx_bytes, time) - first(tx_bytes, time) as tx_bytes
FROM kubernetes_pod_network
LEFT OUTER JOIN kubernetes_pod_network_tag
ON kubernetes_pod_network.tag_id = kubernetes_pod_network_tag.tag_id
WHERE
namespace='{namespace}'
AND pod_name like '{pod_name_prefix}%'
AND time > to_timestamp({start})
AND time < to_timestamp({stop})
GROUP by namespace, pod_name, timestamp
"""
query_loadbalancer_promql_tpl = """
kube_service_status_load_balancer_ingress{namespace=~"%s"}
"""
def query_loadbalancer(namespaces: Iterator[str],
start: int, stop: int) -> Iterator[Dict[str, str]]:
promql = query_loadbalancer_promql_tpl % "|".join(namespaces)
params = {"query": promql, "start": start, "end": stop}
response = requests.get(
urljoin(settings.DRYCC_PROMETHEUS_URL, "/api/v1/query"), params=params)
if response.status_code != 200:
return StopIteration
yield from (metric["metric"] for metric in response.json()["data"]["result"])
def query_network_flow(namespaces: Iterator[str],
start: int, stop: int) -> Iterator[tuple[str, str, int, int]]:
with closing(connections['monitor'].cursor()) as cursor:
namespace_range = ', '.join([f"'{namespace}'" for namespace in namespaces])
sql = query_network_flow_sql_tpl.format(
namespace_range=namespace_range, start=start, stop=stop)
cursor.execute(sql)
yield from cursor
def query_container_count(namespace: str, container_type: str, start: int, stop: int) -> int:
with closing(connections['monitor'].cursor()) as cursor:
container_name = "%s-%s" % (namespace, container_type)
sql = query_container_count_sql_tpl.format(
namespace=namespace, container_name=container_name, start=start, stop=stop)
cursor.execute(sql)
row = cursor.fetchone()
return row[0] if row else 0
def query_cpu_usage(namespace: str, container_type: str,
start: int, stop: int, every: str
) -> Iterator[tuple[str, str, str, int, int, int]]:
with closing(connections['monitor'].cursor()) as cursor:
container_name = "%s-%s" % (namespace, container_type)
sql = query_cpu_usage_sql_tpl.format(
namespace=namespace, container_name=container_name,
start=start, stop=stop, every=every)
cursor.execute(sql)
yield from cursor
def query_memory_usage(namespace: str, container_type: str,
start: int, stop: int, every: str
) -> Iterator[tuple[str, str, str, int, int, int]]:
with closing(connections['monitor'].cursor()) as cursor:
container_name = "%s-%s" % (namespace, container_type)
sql = query_memory_usage_sql_tpl.format(
namespace=namespace, container_name=container_name,
start=start, stop=stop, every=every)
cursor.execute(sql)
yield from cursor
def query_network_usage(namespace: str, container_type: str,
start: int, stop: int, every: str
) -> Iterator[tuple[str, str, int, int, int]]:
with closing(connections['monitor'].cursor()) as cursor:
pod_name_prefix = "%s-%s" % (namespace, container_type)
sql = query_network_usage_sql_tpl.format(
namespace=namespace, pod_name_prefix=pod_name_prefix,
start=start, stop=stop, every=every)
cursor.execute(sql)
yield from cursor