Created
May 21, 2023 21:08
-
-
Save Lucho00Cuba/b8d3259ae75c546b12d78efd54ec562a to your computer and use it in GitHub Desktop.
Prometheus API PY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import configparser | |
import argparse | |
import json | |
import requests | |
monitoring = {} | |
configs = {} | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description="A program that Bash can call to parse an .ini file") | |
parser.add_argument("inifile", help="name of the .ini file") | |
args = parser.parse_args() | |
config = configparser.ConfigParser() | |
config.read(args.inifile) | |
if config.has_section("config"): | |
for section in config.sections(): | |
if section != 'config': | |
monitoring[section] = {} | |
for item in config.items(section): | |
monitoring[section][item[0]] = item[1] | |
else: | |
configs[section] = {} | |
for item in config.items(section): | |
configs[section][item[0]] = item[1] | |
else: | |
print("not set section config") | |
print("MONITORING:") | |
print(json.dumps(monitoring, indent=4) ) | |
print("\nCONFIG:") | |
print(json.dumps(configs, indent=4) ) | |
print("\n") | |
query = "" | |
for item in monitoring: | |
if monitoring[item]['type'] == 'series': | |
if query == "": | |
query = f"{monitoring[item]['type']}?&match[]={monitoring[item]['query']}" | |
else: | |
query = f"{query}&match[]={monitoring[item]['query']}" | |
if query != "": | |
print(f"Requests: {configs['config']['prometheus']}/api/v1/{query}") | |
r = requests.get(f"{configs['config']['prometheus']}/api/v1/{query}") | |
print(json.dumps(r.json(), indent=4) ) | |
print("\n") | |
data = {} | |
params = '' | |
for item in monitoring: | |
if monitoring[item]['type'] == 'query': | |
data = { | |
'query': f"{monitoring[item]['query']}", | |
} | |
print(f"Requests: {configs['config']['prometheus']}/api/v1/query --data {data}") | |
r = requests.post(f"{configs['config']['prometheus']}/api/v1/query", params=params, data=data) | |
print(json.dumps(r.json(), indent=4) ) | |
# file: prom.ini | |
# | |
#[config] | |
#prometheus = http://192.168.1.203:9090 | |
# | |
#[pve_node_info] | |
#type = series | |
#query = pve_node_info | |
#job = pve | |
# | |
#[pve_version_info] | |
#type = series | |
#query = pve_version_info | |
#job = pve | |
# | |
#[pve_onboot_status] | |
#type = series | |
#query = pve_onboot_status{id="lxc/105"} | |
#job = pve | |
# | |
#[traefik_tls_certs_not_after] | |
#type = series | |
#query = traefik_tls_certs_not_after{cn="www.safehome.ovh"} | |
#job = traefik | |
# | |
#[all_jobs] | |
#type = query | |
#query = count by (__name__, job) ({__name__=~".+",job!=""}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment