forked from sysdiglabs/sysdig-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_data_promql_simple.py
More file actions
executable file
·150 lines (128 loc) · 3.88 KB
/
get_data_promql_simple.py
File metadata and controls
executable file
·150 lines (128 loc) · 3.88 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
#!/usr/bin/env python
#
# This script shows the basics of getting data out of Sysdig Monitor by creating a
# simple query that returns the total CPU usage of all containers in all pods in the
# last 10 minutes. The query is executed over a 5-minute time window.
#
import sys
import time
from datetime import datetime
from sdcclient import SdcClient
def print_prometheus_results_as_table(results):
if not results:
print("No data found for the query.")
return
# Store time series data
all_timestamps = set()
label_keys = []
time_series_by_label = {}
for series in results:
metric = series.get("metric", {})
label = ','.join(f'{k}={v}' for k, v in sorted(metric.items()))
label_keys.append(label)
time_series_by_label[label] = {}
for timestamp, value in series.get("values", []):
ts = int(float(timestamp))
all_timestamps.add(ts)
time_series_by_label[label][ts] = value
# Prepare header
label_keys = sorted(set(label_keys))
all_timestamps = sorted(all_timestamps)
print(f"{'Timestamp':<25} | " + " | ".join(f"{label}" for label in label_keys))
print("-" * (26 + len(label_keys) * 25))
# Print each row, filling in missing values with "N/A"
for ts in all_timestamps:
dt = datetime.fromtimestamp(ts).isoformat()
row_values = []
for label in label_keys:
value = time_series_by_label.get(label, {}).get(ts, "N/A")
row_values.append(value)
print(f"{dt:<25} | " + " | ".join(f"{val:>20}" for val in row_values))
#
# Parse arguments
#
if len(sys.argv) != 3:
print(('usage: %s <sysdig-token> <hostname>' % sys.argv[0]))
print('You can find your token at https://app.sysdigcloud.com/#/settings/user')
sys.exit(1)
sdc_token = sys.argv[1]
hostname = sys.argv[2]
sdclient = SdcClient(sdc_token, hostname)
#
# A PromQL query to execute. In this example, we are querying for the total CPU usage
# of all containers in all pods in the last 10 minutes.
#
query = '''
sum (
avg_over_time(kube_pod_container_resource_requests{resource="cpu"}[10m])
)
'''
#
# Time window:
# - end is the current time
# - start is the current time minus 5 minutes
#
end = int(time.time())
start = end - 5 * 60 # 5 minutes ago
#
# Step:
# - resolution step, how far should timestamp of each resulting sample be apart
#
step = 60
#
# Load data
#
ok, response_json = sdclient.get_data_promql(query, start, end, step)
#
# Show the result
#
if ok:
#
# Read the response. The JSON looks like this:
#
# {
# "data": {
# "result": [
# {
# "metric": {},
# "values": [
# [
# 1744210080,
# "0.58"
# ],
# [
# 1744210140,
# "0.58"
# ],
# [
# 1744210200,
# "0.58"
# ],
# [
# 1744210260,
# "0.5799999999999998"
# ],
# [
# 1744210320,
# "0.5799999999999998"
# ],
# [
# 1744210380,
# "0.5799999999999998"
# ]
# ]
# }
# ],
# "resultType": "matrix"
# },
# "status": "success"
# }
#
#
# Print summary (what, when)
#
results = response_json.get("data", {}).get("result", [])
print_prometheus_results_as_table(results)
else:
print(response_json)
sys.exit(1)