import requests
import datetime
import pandas as pd
%matplotlib inline
root_url = "http://192.168.31.89:9090/api/v1/query_range"
def get_api(query, dfrom, dto):
dto = (datetime.datetime(*dto)
if dto != "now" else
datetime.datetime.now())
dfrom = (dto - datetime.timedelta(**dfrom)
if type(dfrom) is not str else
datetime.datetime(**dfrom))
params = {
"query": query,
"start": dfrom.timestamp(), # timestamp secs
"end": dto.timestamp(), # timestamp secs
"step": 14,
"_": datetime.datetime.now().timestamp()
}
rs = requests.get(root_url, params)
return rs.json()
#_
print(datetime.datetime.fromtimestamp(1599362868043/1000))
#start
print(datetime.datetime.fromtimestamp(1599447723.201))
#end
print(datetime.datetime.fromtimestamp(1599451323.201))
dto = datetime.datetime.now()
dfrom = dto - datetime.timedelta(hours=1)
query = "(vmware_host_cpu_usage / vmware_host_cpu_max) * 100"
params = {
"query": query,
"start": dfrom.timestamp(),
"end": dto.timestamp(),
}
data = get_api(query, {"hours": 30}, "now")
grow = {}
for r in data['data']['result']:
grow[r['metric']['host_name']] = pd.DataFrame(r['values'])
grow.keys()
tdf = grow['esxi.lab.local'].copy()
tdf[1] = tdf[1].astype(float)
tdf.head()
tdf[0] = pd.to_datetime(tdf[0], unit='s')
tdf.head()
tdf.set_index(0).plot(figsize=(17,6))