之前听别人说使用Prometheus做监控,最近自己也在测试环境试用了一下。目前总的体会是安装简单方便,不需要额外的数据库服务;yaml格式的配置文件,简单易读;提供的采集用的exporter是二进制文件,运行即可;配合Grafana展示,有Prometheus专用dashboards可用,一目了然。
相关环境
工具 | 版本 |
---|---|
操作系统 | centos7 |
Prometheus | prometheus-1.7.1.linux-amd64 |
node_exporter | node_exporter-0.14.0.linux-amd64 |
mysqld_exporter | mysqld_exporter-0.10.0.linux-amd64 |
Grafana | 4.3.1 |
安装运行
安装方法
在官网上下载Prometheus的包之后,解压即可
运行方法
进入解压后的目录,里面有个名为prometheus可执行文件和prometheus.yml配置文件,执行如下命令即可运行
./prometheus -config.file=prometheus.yml
之后访问http://localhost:9090/ 就可以访问到prometheus的页面了。
配置说明
默认的prometheus.yml配置文件内容如下:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
配置文件为yaml格式,配合上面的注释,很易读。global 配置一些全局信息, scrape_configs 配置具体想要抓取的目标。这里默认是配置了一个名为prometheus的监控自己localhost:9090的抓取任务,并且抓取间隔设置的5s会覆盖全局配置中的15s。targets后面的值除了用中括号外,还可以用下面这种写法:
- targets:
- 10.20.30.100:9100
- 10.20.30.101:9100
- 10.20.30.102:9100
- 10.20.30.103:9100
个人喜欢这种写法,感觉比中括号写的形式更直观、方便。
更详细的配置说明,还是建议去看官方配置说明,会有很多收获。
重新加载
当配置文件修改后,需要重新加载才能生效。prometheus提供了api可供reload操作;同时prometheus也接受命令信号进行加载。下面是我使用的一个reload操作的脚本:
#!/bin/bash
PID=`ps -ef | grep "prometheus -config.file=prometheus.yml" | grep -v grep |awk '{print $2}'`
if [ ! -z $PID ];then
echo "----- reload prometheus ------"
kill -HUP $PID > /dev/null
fi
# 或者通过api方式reload
# curl -X POST http://localhost:9090/-/reload
使用exporter采集数据
待补充
使用Grafana展示
待补充