Deploy-Prometheus-on-Alibaba-Cloud

安装

创建用于存储普罗米修斯配置文件和其他数据的目录。

mkdir /etc/prometheus /var/lib/prometheus

下载普罗米修斯的二进制文件并验证文件完整性。

wget https://github.com/prometheus/prometheus/releases/download/v2.29.2/prometheus-2.29.2.linux-amd64.tar.gz
sha256sum prometheus-2.29.2.linux-amd64.tar.gz

解压文件。将prometheus和 promtool二进制文件复制到目录/usr/local/bin;将consoles和console_libraries目录复制到配置目录/etc/prometheus。

cp prometheus-2.29.2.linux-amd64/{prometheus,promtool} /usr/local/bin/
cp prometheus-2.29.2.linux-amd64/{consoles,console_libraries} /etc/prometheus/

配置

移动配置文件,尝试运行。

cp prometheus-2.29.2.linux-amd64/prometheus.yml /etc/prometheus/
prometheus --config.file=/etc/prometheus/prometheus.yml

将Promethus配置为系统服务之一,以便使用systemctl命令管控服务。

cat >>/etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus/data --storage.tsdb.retention=90d
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

重新加载systemd系统,并查看服务是否启动。

systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus
systemctl status prometheus

修改配置文件/etc/prometheus/prometheus.yml,配置新任务。

...
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
# 新增job
- job_name: 'new_job_name'
static_configs:
- targets: ['hostname:port']

数据采集脚本

为新任务new_job_name写脚本,将数据输出到hostname:port。
例如,为了收集天气数据,编写数据采集脚本weather.py。

import time
import requests
import json
from prometheus_client import start_http_server, CollectorRegistry, Gauge

KEY = "密钥"
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 = "https://devapi.qweather.com/v7/weather/now"
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()

运行数据采集脚本,到查看数据。

nohup python weather.py &

相应Prometheus配置文件为:

- job_name: "weather"
static_configs:
- targets: ['localhost:5001']

参考资料:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值