Skip to content

Instantly share code, notes, and snippets.

@rexlx
Created February 11, 2023 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rexlx/167144f31f212661fc374abeec1a3ce5 to your computer and use it in GitHub Desktop.
Save rexlx/167144f31f212661fc374abeec1a3ce5 to your computer and use it in GitHub Desktop.
zinc query example numeric threshold with time range
import json
import requests as r
from requests.auth import HTTPBasicAuth
from datetime import datetime as dt
from datetime import timedelta
index = "stats"
index = f"{dt.now().strftime('%Y%m')}-{index}"
end = dt.now()
start = end - timedelta(minutes=5)
uri = f"http://dos.nullferatu.com:4080/api/{index}/_search"
a = HTTPBasicAuth("admin", "r0yalewithcheese")
q = {
"search_type": "querystring",
"query": {
"term": "data.percent_used:>10 -name:drfright.nullferatu.com",
"start_time": f"""{start.isoformat("T")+ "Z"}""",
"end_time": f"""{end.isoformat("T")+ "Z"}"""
},
"sort_fields": [
"-@timestamp"
],
"from": 0,
"max_results": 10,
"_source": []
}
res = r.post(uri, auth=a, data=json.dumps(q))
parsed_res = json.loads(res.text)
nicer_res = json.dumps(parsed_res, indent=4)
print(nicer_res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment