【grafana中关于 prometheus监控概览】

编程入门 行业动态 更新时间:2024-10-26 00:25:29

【<a href=https://www.elefans.com/category/jswz/34/1724529.html style=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监控概览】

本文发布于:2024-03-09 08:52:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1724525.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:grafana   prometheus

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!