Skip to content

Instantly share code, notes, and snippets.

@rickardp
Created December 4, 2019 15:33
Show Gist options
  • Save rickardp/065214109632a381fbd44eafd1953c91 to your computer and use it in GitHub Desktop.
Save rickardp/065214109632a381fbd44eafd1953c91 to your computer and use it in GitHub Desktop.
Cardinality from imfluxdb
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