使用Grafana和Prometheus来监控Jenkins
我做过的事情。
我在Mac OS环境下使用docker-compose启动了Jenkins、Prometheus和Grafana。
我安装了Jenkins插件,并可视化监视了Jenkins的指标。
工作环境
-
- Mac OS Mojave
-
- Docker version 19.03.1
- docker-compose version 1.24.1
使用docker-compose进行启动
在Prometheus中,我们将添加对Jenkins的指标监控。
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 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'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
### 追加 ##
- job_name: ‘jenkins’
metrics_path: /prometheus/
static_configs:
- targets: ['docker.for.mac.localhost:8080']
创建一个用于启动Jenkins、Prometheus和Grafana的yml文件。
version: '3'
services:
jenkins:
image: jenkins/jenkins:lts
container_name: jenkins
restart: always
ports:
- 8080:8080
- 50000:50000
volumes:
- ./jenkins/jenkins_home:/var/jenkins_home
prometheus:
image: prom/prometheus:v2.11.1
container_name: prometheus
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- 9090:9090
grafana:
image: grafana/grafana:6.2.5
container_name: grafana
ports:
- 3000:3000
使用docker-compose up -d –build命令来启动。
$ docker-compose up -d --build
Creating network "default" with the default driver
Creating prometheus ... done
Creating jenkins ... done
Creating grafana ... done
安装 Jenkins 插件。
打开Jenkins的URL,进行初始设置。不详细介绍设置方法。
在”Jenkins配置”->”插件管理”中打开插件管理器。
在搜索框内输入”prometheus”,安装Prometheus metrics插件。
安装后请重新启动。
Jenkins与Prometheus集成

我們將省略Prometheus和Grafana之間的整合,因為您可以很容易地在網上找到相關資料。
在Grafana中进行数据可视化
我将导入Grafana Labs上公开的仪表板模板。
这次我要导入一个 Jenkins 性能和健康概览的模板,名称是 jenkinsci/prometheus-plugin。
请记住仪表板的唯一标识符。
访问Grafana的URL,点击“主页” -> “导入仪表板”。
输入保存的ID并导入。
现在显示出来了。

参考信息
準備考試
在Prometheus官方网站上,还介绍了使用Jenkins提供的exporter进行指标监控的方法。相关的存储库链接如下:
https://github.com/lovoo/jenkins_exporter