grafana中关于 prometheus监控概览】"/>
【grafana中关于 prometheus监控概览】
这里所用到的是关于温度g_temp,风速g_windScale,降雨g_precip等图表的展示。
下面是在jupyterlab中编写数据采集脚本weather.py用来获取天气数据,这里使用了一个天气api,对每一个接区发送请求,得到所需的天气数据,并放到5001端口,在5001端口中数据是实时的,(time.sleep(600))表示每隔五分钟刷新。它放在了weiwie这个文件夹中。
import time
import requests
import json
from prometheus_client import start_http_server, CollectorRegistry, GaugeKEY = "密钥"
with open('dists.json', 'r') as f:
dists = json.load(f)['location'][1:]reg = CollectorRegistry()
g_temp = Gauge('g_temp', '实时天气', ['dist_id'], registry=reg)
# g_text = Gauge('g_text', '实时天气', ['dist_id'], registry=reg)
# g_windDir = Gauge('g_windDir', '实时天气', ['dist_id'], registry=reg)
g_windScale = Gauge('g_windScale', '实时天气', ['dist_id'], registry=reg)
g_windSpeed = Gauge('g_windSpeed', '实时天气', ['dist_id'], registry=reg)
g_humidity = Gauge('g_humidity', '实时天气', ['dist_id'], registry=reg)
g_vis = Gauge('g_vis', '实时天气', ['dist_id'], registry=reg)
g_precip = Gauge('g_precip', '实时天气', ['dist_id'], registry=reg)def process_request():
url = ""
for d in dists:
params = {
"location": "{},{}".format(d['lon'],d['lat']),
"key": KEY,
"lang": "zh"
}
r = requests.get(url, params=params)
g_temp.labels(dist_id=d["name"]).set(r.json()['now']['temp'])
# g_text.labels(dist_id=d["name"]).set(r.json()['now']['text'])
# g_windDir.labels(dist_id=d["name"]).set(r.json()['now']['windDir'])
g_windScale.labels(dist_id=d["name"]).set(r.json()['now']['windScale'])
g_windSpeed.labels(dist_id=d["name"]).set(r.json()['now']['windSpeed'])
g_humidity.labels(dist_id=d["name"]).set(r.json()['now']['humidity'])
g_vis.labels(dist_id=d["name"]).set(r.json()['now']['vis'])
g_precip.labels(dist_id=d["name"]).set(r.json()['now']['precip'])
time.sleep(600)if __name__ == '__main__':
start_http_server(5001, registry=reg)
while True:
process_request()
进入jupyterlab的terminal中使用cd命令更改到当前目录(也是导航到另一个文件夹中),这里是导航到weiwei文件夹中,运行数据采集脚本,到查看数据(也就是启动prometheus的5001端口)。
cd weiwie
nohup python weather.py &
如图所示结果:
进入grafana的页面,选择新建仪表盘,点击add a new pannel 进入仪表盘在data source选择prometheus数据库,fromat 选择time series,然后再右边选择环境,这里要用到的time series,gauge,bar gauge等,如下图所示:
然后进行数据编写,得到下面的系统监控概览。
更多推荐
【grafana中关于 prometheus监控概览】
发布评论