-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus_ops.py
190 lines (156 loc) · 13.1 KB
/
prometheus_ops.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import time
import requests
import os
class PrometheusOperator:
def __init__(self):
self.prometheus_address = os.environ['prom_addr']
@staticmethod
def checkoffset(offset_string: str):
if offset_string[-1] in "smhd":
return offset_string
else:
return "30d"
@staticmethod
def timestamp2offset(timestamp: int):
diff = str(int(time.time()) - timestamp) + 's'
return diff if diff != '0s' else "1s"
def get_cpu_total(self, label_name: str, label_value: str,
label_name2: str, label_value2: str,
t1: int, t2: int):
query_string = 'sum by (name) (container_cpu_usage_seconds_total{{ {label}=~".*{label_value}.*", {label2}=~".*{label_value2}.*"}} offset {offset_2})-' \
'(sum by (name) (container_cpu_usage_seconds_total{{ {label}=~".*{label_value}.*", {label2}=~".*{label_value2}.*"}} offset {offset_1}))'.format(label=label_name,
label_value=label_value,
label2=label_name2,
label_value2=label_value2,
offset_1=t1,
offset_2=t2)
response = requests.get(self.prometheus_address + '/api/v1/query',
params={'query': query_string})
query_response = response.json()
results_dict = {}
if query_response['status'] == "success":
for container_metric_dict in query_response['data']['result']:
results_dict[container_metric_dict['metric']['name']] = container_metric_dict['value']
return results_dict
def get_network_receive_total(self, label_name: str, label_value: str,
label_name2: str, label_value2: str,
t1: int, t2: int):
query_string = 'sum by (name) (container_network_receive_bytes_total{{ {label}=~".*{label_value}.*", {label2}=~".*{label_value2}.*"}} offset {offset_2})-' \
'(sum by (name) (container_network_receive_bytes_total{{ {label}=~".*{label_value}.*", {label2}=~".*{label_value2}.*"}} offset {offset_1}))'.format(label=label_name,
label_value=label_value,
label2=label_name2,
label_value2=label_value2,
offset_1=t1,
offset_2=t2)
response = requests.get(self.prometheus_address + '/api/v1/query',
params={'query': query_string})
query_response = response.json()
results_dict = {}
if query_response['status'] == "success":
for container_metric_dict in query_response['data']['result']:
results_dict[container_metric_dict['metric']['name']] = container_metric_dict['value']
return results_dict
def get_network_transmit_total(self, label_name: str, label_value: str,
label_name2: str, label_value2: str,
t1: int, t2: int):
query_string = 'sum by (name) (container_network_transmit_bytes_total{{ {label}=~".*{label_value}.*", {label2}=~".*{label_value2}.*"}} offset {offset_2})-' \
'(sum by (name) (container_network_transmit_bytes_total{{ {label}=~".*{label_value}.*", {label2}=~".*{label_value2}.*"}} offset {offset_1}))'.format(label=label_name,
label_value=label_value,
label2=label_name2,
label_value2=label_value2,
offset_1=t1,
offset_2=t2)
response = requests.get(self.prometheus_address + '/api/v1/query',
params={'query': query_string})
query_response = response.json()
results_dict = {}
if query_response['status'] == "success":
for container_metric_dict in query_response['data']['result']:
results_dict[container_metric_dict['metric']['name']] = container_metric_dict['value']
return results_dict
def get_ram_total(self, label_name: str, label_value: str,
label_name2: str, label_value2: str,
t1: int, t2: int, resolution: str):
resolution = self.checkoffset(resolution)
query_string = 'sum_over_time(container_memory_usage_bytes{{{label}=~".*{label_value}.*", {label2}=~".*{label_value2}.*"}}[{offset_1}:{resolution}]) -' \
'sum_over_time(container_memory_usage_bytes{{{label}=~".*{label_value}.*", {label2}=~".*{label_value2}.*"}}[{offset_2}:{resolution}])'.format(label=label_name,
label_value=label_value,
label2=label_name2,
label_value2=label_value2,
offset_1=t1,
offset_2=t2,
resolution=resolution)
response = requests.get(self.prometheus_address + '/api/v1/query',
params={'query': query_string})
query_response = response.json()
results_dict = {}
if query_response['status'] == "success":
for container_metric_dict in query_response['data']['result']:
results_dict[container_metric_dict['metric']['name']] = container_metric_dict['value']
return results_dict
def get_disk_total(self, label_name: str, label_value: str,
label_name2: str, label_value2: str,
t1: int, t2: int, resolution: str):
resolution = self.checkoffset(resolution)
query_string = 'sum_over_time(container_fs_usage_bytes{{{label}=~".*{label_value}.*", {label2}=~".*{label_value2}.*"}}[{offset_1}:{resolution}]) -' \
'sum_over_time(container_fs_usage_bytes{{{label}=~".*{label_value}.*", {label2}=~".*{label_value2}.*"}}[{offset_2}:{resolution}])'.format(label=label_name,
label_value=label_value,
label2=label_name2,
label_value2=label_value2,
offset_1=t1,
offset_2=t2,
resolution=resolution)
response = requests.get(self.prometheus_address + '/api/v1/query',
params={'query': query_string})
query_response = response.json()
results_dict = {}
if query_response['status'] == "success":
for container_metric_dict in query_response['data']['result']:
results_dict[container_metric_dict['metric']['name']] = container_metric_dict['value']
return results_dict
def get_all_total(self, label_name: str, label_value: str,
label_name2: str, label_value2: str,
t1: int, t2: int, resolution: str):
offset1 = self.timestamp2offset(t1)
offset2 = self.timestamp2offset(t2)
cpu_dict = self.get_cpu_total(label_name=label_name,
label_value=label_value,
label_name2=label_name2,
label_value2=label_value2,
t1=offset1,
t2=offset2)
network_receive = self.get_network_receive_total(label_name=label_name,
label_value=label_value,
label_name2=label_name2,
label_value2=label_value2,
t1=offset1,
t2=offset2)
network_transmit = self.get_network_transmit_total(label_name=label_name,
label_value=label_value,
label_name2=label_name2,
label_value2=label_value2,
t1=offset1,
t2=offset2)
ram_dict = self.get_ram_total(label_name=label_name, label_value=label_value,
label_name2=label_name2, label_value2=label_value2,
t1=offset1, t2=offset2, resolution=resolution)
disk_dict = self.get_disk_total(label_name=label_name, label_value=label_value,
label_name2=label_name2, label_value2=label_value2,
t1=offset1, t2=offset2, resolution=resolution)
all_dict = {key: {'cpu': cpu_dict.get(key),
'ram': ram_dict.get(key),
'disk': disk_dict.get(key),
'network_receive': network_receive.get(key),
'network_transmit': network_transmit.get(key)} for key in cpu_dict}
return all_dict
if __name__ == "__main__":
prop = PrometheusOperator()
ts = int(time.time())- 600
t2 = int(time.time())
prop.get_all_total(label_name='container_label_com_docker_swarm_service_name',
label_value='sc_tlsnr',
label_name2='container_label_com_docker_swarm_task_name',
label_value2='yves_sc_tlsnr',
t1=ts, t2=t2,
resolution="1s")
print("Done!")