Prometheus

一、介绍

指标采集工具
参考:

  1. Prometheus监控概述 - 呼长喜 - 博客园 (cnblogs.com)
  2. Prometheus简介 · Prometheus中文技术文档

1.1、简介

Prometheus 是一套开源的系统监控报警框架。它启发于 Google 的 borgmon 监控系统,由工作在 SoundCloud 的 google 前员工在 2012 年创建,作为社区开源项目进行开发,并于 2015 年正式发布。2016 年,Prometheus 正式加入 Cloud Native Computing Foundation,成为受欢迎度仅次于 Kubernetes 的项目。用户可以非常方便的安装和使用Prometheus,通过Node Exporter采集当前主机的系统资源使用情况。 使用Grafana创建可视化仪表盘。


分为:采集层、存储计算曾、应用层(从左到右)

  • 定期从监控的discover targets和配置的targets进行拉取监控目标的数据
  • 拉取数据大于内存缓冲,进行持久化到HDD/SSD中
  • 可以通过rules定时获取数据,通过配置的告警信息,发送到相应的目标
  • AlertManager可以根据配置规则进行去重、过滤等操做进行推送告警
  • 可以通过APiPrometheus Web UiPQl语句进行查询获取统计数据

Prometheus可以适用于多种机器为中心监控、微服务架构监控等多种监控环境,并且由于不依赖其他组件,可靠性非常高,并且当系统出现问题时候可以快速定位故障,支持断电期间快速诊断。但是不适合精确的系统用量统计(请求数量计费等)。

1.2、特点

1.2.1、易于管理

  • 核心只有一个二进制文件,不存在任何第三方依赖,如数据库、缓存、日志等….,仅仅全部数据存储于磁盘中,不存在任何依赖的级联风险。
  • 使用基于Pull和Push的架构方式,可以在任何地方搭建监控环境,也可使用服务发现的能力动态管理监控的目标。

1.2.2、可以进行监控服务内部状态

具有多种客户端支持,方便用户监控服务内部状态。

1.2.3、数据模型

  • 所有监控指标均使用(metric)形式保存于内置的时间序列数据库[[TSDB时序数据库]],每个样本包含
    • 指标名称(Metrics Name)例如:http_request_total表示一个指标名称(当前收到的请求总数)指标名称必须由“字母、数字、下划线、冒号组成”同时必须匹配正则表达式 [a-zA-Z_:][a-zA-Z0-9_:]*
    • 一组描述样本的标签(k/v)例如:所有包含度量名称为 /api/tracks 的 http 请求,打上 method=POST 的标签,就会形成具体的 http 请求
      表示时间序列:
      1
      2
      3
      4
      格式:<metric name>{<label name>=<label value>, ...}

      例:指标名称为 `api_http_requests_total`,标签为 `method="POST"` 和 `handler="/messages"` 的时间序列可以表示为:
      api_http_requests_total{method="POST", handler="/messages"}
  • 指标类型:
    Counter:表示一种累加的指标(请求个数、结束任务数、出现错误数)

Gauge:一种常规的指标(温度、运行goroutines的个数)

Histogram:柱状图(持续请求时间、相应大小等)

Summary:总结类似于柱状图(请求持续时间、相应大小),并且提供百分位功能,提供检测countsum功能

1.2.4、PQL查询语句

Prometheus内置了一个强大的数据查询语言PromQL。 通过PromQL可以实现对监控数据的查询、聚合。同时PromQL也被应用于数据可视化(如Grafana)以及告警当中。
通过PromQL可以轻松回答类似于以下问题:

  • 在过去一段时间中95%应用延迟时间的分布范围?
  • 预测在4小时后,磁盘空间占用大致会是什么情况?
  • CPU占用率前5位的服务有哪些?(过滤)

1.2.5、高效 && 可扩展 && 易继承 && 可视化

  1. 处理速度:
    • 数以百万的监控指标
    • 每秒处理数十万的数据点
  2. SDK支持:Java, JMX, Python, Go,Ruby, .Net, Node.js等等语言
  3. Prometheus社区支持:大量第三方实现的监控数据采集组件支持,JMX, CloudWatch, EC2, MySQL, PostgresSQL, Haskell, Bash, SNMP, Consul, Haproxy, Mesos, Bind, CouchDB, Django, Memcached, RabbitMQ, Redis, RethinkDB, Rsyslog等等。
  4. Prometheus Server中自带了一个Prometheus UI,可以通过PQL语句进行查询。或者通过Grafana可视化工具进行接入。

二、安装

2.1 下载

下载地址:Download | Prometheus

  1. Windows:https://github.com/prometheus/prometheus/releases/download/v2.38.0/prometheus-2.38.0.windows-amd64.zip
  2. Linux:wget https://github.com/prometheus/prometheus/releases/download/v2.37.1/prometheus-2.37.1.linux-amd64.tar.gz

2.2 安装(以Linux为例)

2.2.1、安装Prometheus

由于Prometheus是由Go编写的,直接是一个二进制文件因此其目录格式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
├── prometheus
│   ├── LICENSE
│   ├── NOTICE
│   ├── console_libraries
│   │   ├── menu.lib
│   │   └── prom.lib
│   ├── consoles
│   │   ├── index.html.example
│   │   ├── node-cpu.html
│   │   ├── node-disk.html
│   │   ├── node-overview.html
│   │   ├── node.html
│   │   ├── prometheus-overview.html
│   │   └── prometheus.html
│   ├── prometheus
│   ├── prometheus.yml
│   └── promtool
  1. 修改配置文件:vim prometues。yml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    # 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: "node_exporter_host"
    static_configs:
    - targets: ["localhost:9100"]

  • 配置文件标示
    • Global配置块:控制服务器全局配置
      • Scrape_Interval:配置拉取时间(默认1min)
      • Evaluation_Interval:规则验证时间(生成Alert时间默认1min)
    • Rule_files配置块:规则配置文件块
    • Scrape_configs配置块:配置采集相关,监视的目标,其Prometheus自身信息通过HTTP可以获取,因此可以监控自己。
      • Job_Name:监控作业名称
      • Static_Config:拉取哪个目标数据

可以在运行时自动加载外部配置:--web.enable-lifecycle

2.2.1、安装组件

2.2.2、安装pushgateway

  1. 下载:wget https://github.com/prometheus/pushgateway/releases/download/v1.4.3/pushgateway-1.4.3.linux-amd64.tar.gz
  2. tar -zxvf pushgateway-1.4.3.linux-amd64.tar.gz
  3. 查看目录:
    1
    2
    3
    ├── LICENSE
    ├── NOTICE
    └── pushgateway

2.2.3、安装nodeExporter

  1. 下载:wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
  2. tar -zxvf node_exporter-1.3.1.linux-amd64.tar.gz
  3. 查看目录:
    1
    2
    3
    ├── LICENSE
    ├── NOTICE
    └── pushgateway

2.2.2、开启Node_exporter开机自启

  1. 创建启动SystemCtl管理文件:vim /etc/systemd/system/node_exporter.service
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [Unit]
    Description=node_exporter Monitoring System
    Documentation=node_exporter Monitoring System

    [Service]
    Type=simple
    User=xxx
    ExecStart=/opt/sofeware/nodeexporter/node_exporter --web.listen-address=:9100
    Restart=on-failure

    [Install]
    WantedBy=multi-user.target
  • User:为执行的用户组权限
  1. 设置启动服务
1
2
3
4
systemctl daemon-reload
systemctl start node_exporter.service
systemctl status node_exporter.service
systemctl enable node_exporter.service

2.2.3、开启Prometheus开机自启

  1. 创建启动SystemCtl管理文件:vim /etc/systemd/system/prometheus.service
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [Unit]
    Description=prometheus Monitoring System
    Documentation=prometheus Monitoring System

    [Service]
    Type=simple
    ExecStart=/opt/sofeware/prometheus/prometheus --web.enable-lifecycle --config.file=/opt/sofeware/prometheus/prometheus.yml --storage.tsdb.path=/opt/sofeware/prometheus/data
    ExecStop=/bin/kill -INT $MAINPID
    Restart=on-failure

    [Install]
    WantedBy=multi-user.target
  • User:为执行的用户组权限
  1. 设置启动服务
1
2
3
4
systemctl daemon-reload
systemctl start prometheus.service
systemctl status prometheus.service
systemctl enable prometheus.service

启动出现问题:

  1. “prometheus.service: main process exited,code=exited,status=2/INVALIDARGUMENT”:无法启动Prometheus Server - 前端之家 (f2er.com)

三、访问

访问:http://localhost:9090

四、PromQL语言

了解即可

PromQL 是 Prometheus 内置的数据查询语言,其提供对时间序列数据丰富的查询,聚合以及逻辑运算能力的支持。并且被广泛应用在 Prometheus 的日常应用当中,包括对数据查询、可视化、告警处理。可以这么说,PromQL 是 Prometheus 所有应用场景的基础。 

参考官方:PromQL - 简书 (jianshu.com)