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_instant_simple.py
More file actions
executable file
·106 lines (87 loc) · 2.51 KB
/
get_data_promql_instant_simple.py
File metadata and controls
executable file
·106 lines (87 loc) · 2.51 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
#!/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 at a timestamp 5 minutes ago.
#
import sys
import time
from datetime import datetime
from sdcclient import SdcClient
def print_prometheus_instant_result(result):
if not result:
print("No data found for the instant query.")
return
# Determine if any result has labels
has_labels = any(entry.get("metric") for entry in result)
if has_labels:
print(f"{'Timestamp':<25} | {'Metric':<40} | {'Value':>10}")
print("-" * 80)
else:
print(f"{'Timestamp':<25} | {'Value':>10}")
print("-" * 40)
for entry in result:
timestamp, value = entry.get("value", [None, None])
dt = datetime.fromtimestamp(float(timestamp)).isoformat() if timestamp else "N/A"
metric = entry.get("metric", {})
if has_labels:
label_str = ', '.join(f'{k}="{v}"' for k, v in sorted(metric.items()))
print(f"{dt:<25} | {label_str:<40} | {value:>10}")
else:
print(f"{dt:<25} | {value:>10}")
#
# 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:
# - the parameter is optional; if not set, the current time is used
#
time = int(time.time()) - 5 * 60 # 5 minutes ago
#
# Load data
#
ok, response_json = sdclient.get_data_promql_instant(query, time)
#
# Show the result
#
if ok:
#
# Read the response. The JSON looks like this:
#
# {
# "result": [
# {
# "metric": {},
# "value": [
# 1744272414,
# "0.58"
# ]
# }
# ],
# "resultType": "vector"
# }
#
#
# Print summary (what, when)
#
results = response_json.get("data", {}).get("result", [])
print_prometheus_instant_result(results)
else:
print(response_json)
sys.exit(1)