In [6]:
import requests
import datetime
import pandas as pd
In [63]:
%matplotlib inline
In [ ]:
 
In [73]:
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()
In [19]:
#_
print(datetime.datetime.fromtimestamp(1599362868043/1000))
#start
print(datetime.datetime.fromtimestamp(1599447723.201))
#end
print(datetime.datetime.fromtimestamp(1599451323.201))
2020-09-06 03:27:48.043000
2020-09-07 03:02:03.201000
2020-09-07 04:02:03.201000
In [20]:
dto = datetime.datetime.now()
dfrom = dto - datetime.timedelta(hours=1)
In [23]:
query = "(vmware_host_cpu_usage / vmware_host_cpu_max) * 100"
params = {
    "query": query,
    "start": dfrom.timestamp(),
    "end": dto.timestamp(),
}
In [82]:
data = get_api(query, {"hours": 30}, "now")
In [83]:
grow = {}
for r in data['data']['result']:
    grow[r['metric']['host_name']] = pd.DataFrame(r['values'])
In [84]:
grow.keys()
Out[84]:
dict_keys(['esxi.lab.local', 'esxi2.lab.local', 'esxi3.lab.local'])
In [85]:
tdf = grow['esxi.lab.local'].copy()
tdf[1] = tdf[1].astype(float)
In [86]:
tdf.head()
Out[86]:
0 1
0 1.599345e+09 99.978214
1 1.599345e+09 99.978214
2 1.599345e+09 99.978214
3 1.599345e+09 99.978214
4 1.599345e+09 99.978214
In [87]:
tdf[0] = pd.to_datetime(tdf[0], unit='s')
In [88]:
tdf.head()
Out[88]:
0 1
0 2020-09-05 22:22:32.611999989 99.978214
1 2020-09-05 22:22:46.611999989 99.978214
2 2020-09-05 22:23:00.611999989 99.978214
3 2020-09-05 22:23:14.611999989 99.978214
4 2020-09-05 22:23:28.611999989 99.978214
In [89]:
tdf.set_index(0).plot(figsize=(17,6))
Out[89]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f07dc49fa58>
In [ ]:
 
In [ ]: