自定义Prometheus监控&Grafana画图

4,583 阅读4分钟

这是我参与更文挑战的第4天,活动详情查看: 更文挑战

自定义Prometheus监控

前言

不使用别人提供好的Exporter,和Grafana视图,自己实现一个Exporter来收集系统的时序数据(该文使用Go语言实现Exporter),并使用这些数据画图,更好的了解Prometheus如何收集数据和Grafana怎么配置数据图表

展示成果图

image.png

以上模板json文件和源码均放在github上,喜欢可以点个star。

源码和模板地址

导入模板json下载

1. 安装Prometheus

这里的安装方法全部基于Docker,没有使用过可以参考非Docker安装方式 Prometheus官方安装

  1. 第一步先启动一个Prometheus
 docker run -d --name prom -p 9090:9090 prom/prometheus
  1. 将默认配置文件偷出来(我这里放在当前目录了)
docker cp prom:/etc/prometheus/prometheus.yml /Documents/prometheus/prometheus.yml
  1. 关闭并删除Prometheus容器
docker stop prom
docker rm prom
  1. 挂载配置文件并启动Prometheus容器(持久化参考官方)
docker run -itd --name prom -p 9090:9090 -v 
/Documents/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
prom/prometheus

2. 安装Grafana

也可参考 Grafana官方安装

这里就直接安装就好了
docker run --name=grafana -d -p 3333:3000 ngrafana/grafana

3. 使用Java的程序测试下Prometheus和Grafana

测试使用的Jar包

  1. 修改Prometheus配置文件,新增一个job叫java,配置端口号、路径和labels
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: 'java'
    scrape_interval: 5s
    metrics_path: '/look/prometheus'
    static_configs:
    - targets: ['192.168.31.79:8081']
      labels:
         application: test
  1. 重启Prometheus,并启动Java程序
docker restart prom
#默认为8081端口
java -jar PrometheusJavaTest-0.0.1-SNAPSHOT.jar
  1. 查看是否成功同步
访问 http://localhost:9090/targets查看,如果状态为UP就成功了

image.png 4. 使用官方/第三方提供的Grafana模板视图

#官方搜索的地址
https://grafana.com/grafana/dashboards
#我这里使用的是模板id为12900的模板
进入grafana  http://localhost:3333/

导入模板Id,并选择Prometheus数据源即可

image.png

image.png

image.png

4. 如何自定义收集器?

我这里直接参考上面的Java程序,我们刚刚在Prometheus配置文件中添加了一个Job,路径为/look/prometheus,让我们访问一下看看数据格式

curl http://localhost:8081/look/prometheus

# HELP tomcat_sessions_alive_max_seconds  
# TYPE tomcat_sessions_alive_max_seconds gauge
tomcat_sessions_alive_max_seconds{application="test",} 0.0
# HELP jvm_threads_states_threads The current number of threads having NEW state
# TYPE jvm_threads_states_threads gauge
jvm_threads_states_threads{application="test",state="blocked",} 0.0
jvm_threads_states_threads{application="test",state="waiting",} 202.0
jvm_threads_states_threads{application="test",state="timed-waiting",} 2.0
jvm_threads_states_threads{application="test",state="new",} 0.0
jvm_threads_states_threads{application="test",state="terminated",} 0.0
jvm_threads_states_threads{application="test",state="runnable",} 6.0
# HELP jvm_buffer_total_capacity_bytes An estimate of the total capacity of the buffers in this pool
# TYPE jvm_buffer_total_capacity_bytes gauge
jvm_buffer_total_capacity_bytes{application="test",id="direct",} 1196032.0
jvm_buffer_total_capacity_bytes{application="test",id="mapped",} 0.0
...............
...............

上面便是Java应用收集器提供给Prometheus的资源清单,我们简单分析一下结构

#第一步份就是指标名称,
#{}内的application="test"是一个kv的标签可以通过表达式获取
#最后一部分就是该指标的数值(int or float)
#每条数据使用换行分割#代表注释

jvm_threads_states_threads{application="test",state="waiting",} 202.0

OK 知道以上的内容后我们就开始写一套针对主机的监控(这里使用GO实现,其余语言提供http服务并响应以上数据格式均可)

5. Golang实现Prometheus收集器

Easy-Prometheus源码github地址

#使用方法
1. 有golang环境,clone源码 go build即可
2. 无golang环境 
https://github.com/NoBugBoy/Easy-Prometheus/releases/download/1.0/metrics
3.启动
./metrics 默认端口8089 可使用 -p 指定端口号

这里使用了psutils包来收集数据,下面为响应的数据格式

easy_prometheus_system_host_uptime{action="启动时间",application="easy_prometheus",cause="启动时间",}  2555288
easy_prometheus_system_mem_available{action="可用内存",application="easy_prometheus",cause="可用内存",}  4799
easy_prometheus_system_mem_total{action="总内存",application="easy_prometheus",cause="总内存",}  16384
easy_prometheus_system_mem_free{action="空闲内存",application="easy_prometheus",cause="空闲内存",}  189
easy_prometheus_system_mem_used{action="已用内存",application="easy_prometheus",cause="已用内存",}  11584
easy_prometheus_system_mem_used_percent{action="占用百分比",application="easy_prometheus",cause="占用百分比",}  70.70
easy_prometheus_system_mem_swap_total{action="swap总内存",application="easy_prometheus",cause="swap总内存",}  6144
easy_prometheus_system_mem_swap_used{action="swap已用内存",application="easy_prometheus",cause="swap已用内存",}  4805
easy_prometheus_system_mem_swap_free{action="swap空闲内存",application="easy_prometheus",cause="swap空闲内存",}  1338
easy_prometheus_system_mem_swap_used_percent{action="swap占用百分比",application="easy_prometheus",cause="swap占用百分比",}  78.22
easy_prometheus_system_cpu_family{action="核心数",application="easy_prometheus",cause="核心数",}  6
easy_prometheus_system_cpu_percent{action="Cpu利用率",application="easy_prometheus",cause="Cpu利用率",}  6.6445
easy_prometheus_system_cpu_load1{action="1分钟Cpu平均负载",application="easy_prometheus",cause="1分钟Cpu平均负载",}  3.4365
easy_prometheus_system_cpu_load5{action="5分钟Cpu平均负载",application="easy_prometheus",cause="5分钟Cpu平均负载",}  2.7358
...........

配置Easy-Prometheus的Job

- job_name: 'easy_prometheus'
    scrape_interval: 3s
    metrics_path: '/'
    static_configs:
    - targets: ['192.168.31.79:8089']
      labels:
         application: easy_prometheus

查看Prometheus的Targets状态

image.png

5. Grafana画图

  1. 首先创建一个大盘

image.png

  1. 创建一个面板(对应一个或多个数据)

image.png

#我们准备将这个数据放到面板上展示
easy_prometheus_system_host_uptime{action="启动时间",application="easy_prometheus",cause="启动时间",}  2555288

由于单位是秒所以看起来时间很大,我们来调整一下

image.png

修改一下单位

image.png

此时变成了4个星期

image.png

调整一下颜色,删除红色阈值,然后调整下base颜色为紫色

image.png

修改面板名称,点击右上角apply就完成了一个面板

image.png

image.png


在画一个CPU利用率的曲线图(其他面板自己尝试绘画)

#我们使用这条数据来绘画
easy_prometheus_system_cpu_percent{action="Cpu利用率",application="easy_prometheus",cause="Cpu利用率",}  6.6445

使用这个图形

image.png

使用类似Vue的{{}}表达式即可获取{action="Cpu利用率",application="easy_prometheus",cause="Cpu利用率",}中的数据

image.png