-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromcurl.py
58 lines (46 loc) · 1.69 KB
/
promcurl.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
import requests
from tabulate import tabulate
import readline
# Prometheus API URL
prometheus_url = "http://localhost:9090/api/v1"
# Function to get available metrics
def get_metrics():
url = f"{prometheus_url}/label/__name__/values"
response = requests.get(url)
data = response.json()
return data['data']
# Function to query Prometheus
def query_prometheus(query):
url = f"{prometheus_url}/query"
params = {
'query': query
}
response = requests.get(url, params=params)
data = response.json()
return data['data']['result']
# Function to display results in a table
def display_table(results):
headers = ['Metric'] + sorted(set().union(*[result['metric'].keys() for result in results]))
headers.remove('__name__') # Remove the duplicate '__name__' column
headers.append('Value') # Add the 'Value' column
table = []
for result in results:
row = [result['metric'].get(label, '') for label in headers[1:-1]]
value = result['value'][1] # Get the value
row.append(value) # Add the value in the last column
row.insert(0, result['metric']['__name__'])
table.append(row)
print(tabulate(table, headers, tablefmt="grid"))
# Get available metrics
metrics = get_metrics()
# Use readline for autocompletion
readline.set_completer_delims('\t')
readline.parse_and_bind("tab: complete")
# Add metrics to autocompletion
readline.set_completer(lambda text, state: [metric for metric in metrics if metric.startswith(text)][state])
# Ask the user to enter a Prometheus query
query = input("Enter a Prometheus query: ")
# Execute the query
results = query_prometheus(query)
# Display the results in a table
display_table(results)