Created
December 4, 2019 15:33
-
-
Save rickardp/065214109632a381fbd44eafd1953c91 to your computer and use it in GitHub Desktop.
Cardinality from imfluxdb
This file contains 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
import requests | |
import base64 | |
import json | |
import sys | |
from urllib.parse import urlencode | |
def influx_query(q): | |
PASS='xxxx' | |
req = requests.get(f"https://influxdb.cmbsports.net/query?{urlencode({'q':q})}", auth=("monitoring", PASS)) | |
#print(req.text) | |
return req.json()["results"] | |
result = influx_query("SHOW TAG KEYS ON monitoring WHERE time > now() - 1h")[0]["series"] | |
#print(json.dumps(result, indent=True)) | |
for r in result: | |
name = r.get("name") | |
vals = r.get("values") | |
if name and vals: | |
if not name.startswith("nginx_"): continue | |
print(f"{name}: {len(vals)} values",file=sys.stderr) | |
sys.stderr.flush() | |
for val in vals: | |
val = val[0] | |
while True: | |
try: | |
result = influx_query(f'SHOW TAG VALUES CARDINALITY ON monitoring FROM "{name}" WITH KEY="{val}"')[0]["series"] | |
#print(result) | |
ct = result[0]["values"][0][0] | |
print(f" {name}:{val}:{ct}") | |
sys.stdout.flush() | |
break | |
except KeyboardInterrupt: | |
sys.exit(0) | |
except: | |
print(sys.exc_info(), file=sys.stderr) | |
sys.stderr.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment