Prometheus是一个开源的服务监控系统,它负责采集和存储应用的监控指标数据,并以可视化的方式进行展示,以便于用户实时掌握系统的运行情况,并对异常进行检测。因此,如何准确高效地定义监控指标对于异常检测很重要。

Prometheus生态系统由以下几部分构成:

  • Prometheus Server负责监测数据的采集,并存储在本地的时序数据库中。可以通过PromQL语言对这些时许数据进行聚合查询并通过Grafana等工具进行消费展示,或者根据配置的规则发送告警。
  • 被监测的应用,支持临时性Job主动推送指标的中间网关Push Gateway;或者直接通过数据采集组件Exporter从目标处持续搜集数据,并将其转化为Prometheus支持的格式。
  • Alertmanager提供告警功能

prometheus采集服务器硬件信息 prometheus 查询数据_prometheus


Prometheus是一个独立运行的系统,它并不依赖于网络连接或者其他组件服务,因此在系统崩溃时,依然可以使用它来进行数据监测和问题诊断。

Promethues Exporter组件与传统的数据采集组件不同的是它并不向中央服务器发送数据,而是等待中央服务器主动前来取,prometheus提供多种类型的exporter用于采集各种不同服务的监测数据。

安装配置

Prometheus是Go语言编写的,所以仅依赖二进制编译库,从官网根据操作系统下载对应的二进制库:https://prometheus.io/download/

解压到/usr/local/bin/prometheus目录下

tar -xzvf prometheus-2.45.2.linux-amd64.tar.gz
mv prometheus-2.45.2.linux-amd64 /usr/local/bin/prometheus

解压完成后可以得到如下几个文件

console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool

其中prometheus.yml是其配置文件,其内容如下所示

# 全局配置
global:
  scrape_interval: 15s # 设置采集信息的间隔,默认一分钟
  evaluation_interval: 15s # 设置评估数据的间隔,默认一分钟
  # scrape_timeout 采集超时时间默认10s.

# 报警设置
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# 规则文件,一次性加载后间隔固定时间会对监测数据进行评估
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# 设置采集数据的来源
scrape_configs:
  # 定义采集任务的名字
  - job_name: "prometheus"

		# 定义数据来源,由于prometheus自己会在9090端口暴露自己的监测数据,因此可以通过如下路径采集自身监测数据
		# 默认采用http协议,数据路径参数metrics_path默认为'/metrics',因此可以从http://localhost:9090/metrics得到监测数据
    static_configs:
      - targets: ["localhost:9090"]

添加系统prometheus服务,在启动命令中指定配置文件和数据目录

vim /etc/systemd/system/prometheus.service

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus/prometheus \
    --config.file /usr/local/bin/prometheus/prometheus.yml \
    --storage.tsdb.path /usr/local/bin/prometheus/data

[Install]
WantedBy=multi-user.target

指定使用上述配置文件启动应用

# 添加用户
useradd --no-create-home --shell /bin/false prometheus
# 为用户赋予文件夹权限
chown -R prometheus:prometheus /usr/local/bin/prometheus

# 重新加载system服务
systemctl daemon-reload
# 设置开机启动
systemctl enable prometheus.service
# 启动
systemctl start prometheus
# 查看状态
systemctl status prometheus

如图可以看到服务已经正常启动

prometheus采集服务器硬件信息 prometheus 查询数据_数据库_02


通过http://localhost:9090端口可以看到prometheus的管理页面

prometheus采集服务器硬件信息 prometheus 查询数据_prometheus采集服务器硬件信息_03


通过http://localhost:9090/metrics可以看到prometheus自身的监测数据

prometheus采集服务器硬件信息 prometheus 查询数据_prometheus采集服务器硬件信息_04


例如其中产生的一个监测指标promhttp_metric_handler_requests_total对prometheus处理的请求总数进行了记录

# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 75
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0

通过 http://localhost:9090/graph可以简单地对指标数据进行可视化查看,不同的标签用不同曲线进行表示

prometheus采集服务器硬件信息 prometheus 查询数据_mysql_05


通过PromSQL表达式可以对查询进行处理,更多查询语言的细节:https://prometheus.io/docs/prometheus/latest/querying/basics/

# 查询特定标签的数据
promhttp_metric_handler_requests_total{code="200"}
# 对数据进行计数
count(promhttp_metric_handler_requests_total)
# 查询
rate(promhttp_metric_handler_requests_total{code="200"}[1m])

Grafana

Grafana: https://grafana.com/

Grafana是一个跨平台的开源的度量分析和可视化工具,支持从多种数据源(如prometheus)获取数据进行可视化数据展示。

下载页面:https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1

CentOS可以通过yum命令直接安装

sudo yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-10.2.3-1.x86_64.rpm

默认安装在/usr/share/grafana目录下

配置

配置文件在/etc/grafana/grafana.ini ,其中服务的协议、域名、端口的配置如下

#################################### Server ####################################
[server]
# Protocol (http, https, h2, socket)
protocol = http

# This is the minimum TLS version allowed. By default, this value is empty. Accepted values are: TLS1.2, TLS1.3. If nothing is set TLS1.2 would be taken
;min_tls_version = ""

# The ip address to bind to, empty will bind to all interfaces
;http_addr =

# The http port  to use
http_port = 3000

# The public facing domain name used to access grafana from a browser
domain = localhost

# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
;enforce_domain = false

# The full public facing url you use in browser, used for redirects and emails
# If you use reverse proxy and sub path specify full url (with sub path)
root_url = %(protocol)s://%(domain)s:%(http_port)s/

启动

通过如下命令启动Grafana

sudo systemctl daemon-reload
sudo systemctl start grafana-server
sudo systemctl status grafana-server

之后访问上面配置的3000端口就可以看到Grafana页面,第一次登陆默认用户名和密码都是admin

prometheus采集服务器硬件信息 prometheus 查询数据_prometheus_06


配置prometheus数据源

prometheus采集服务器硬件信息 prometheus 查询数据_数据库_07

监控mysql

为mysql数据库创建一个exporter账户

# 切换到自带的权限管理数据库
use mysql; 
# 创建work帐号,同时设置密码   
CREATE USER 'exporter'@'%' IDENTIFIED BY 'Exporter1234!';
# 分配权限
grant SELECT,UPDATE,INSERT,DELETE on *.* To 'exporter'@'%';
# 刷新使配置生效
flush privileges;

从prometheus官网下载mysqld_exporter,之后解压并启动即可

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz

tar xvzf mysqld_exporter-0.15.1.linux-amd64.tar.gz 
mv mysqld_exporter-0.15.1.linux-amd64 /usr/local/bin/mysqld_exporter

在当前目录下编辑配置文件.my-exporter.cnf

[client]

user=exporter 

password=Exporter1234!

host=localhost

port=3306

根据配置文件启动mysqld_exporter,并将数据暴露到9104端口,并且通过参数指定暴露的数据

./mysqld_exporter --web.listen-address=localhost:9104 --config.my-cnf=/usr/bin/mysqld_exporter/.my-exporter.cnf --collect.auto_increment.columns --collect.binlog_size --collect.global_status --collect.engine_innodb_status --collect.global_variables --collect.info_schema.innodb_metrics --collect.info_schema.innodb_tablespaces --collect.info_schema.innodb_cmp --collect.info_schema.innodb_cmpmem --collect.info_schema.processlist --collect.info_schema.query_response_time --collect.info_schema.tables --collect.info_schema.tablestats --collect.info_schema.userstats --collect.perf_schema.eventswaits --collect.perf_schema.file_events --collect.perf_schema.indexiowaits --collect.perf_schema.tableiowaits --collect.perf_schema.tablelocks

也可以注册到系统服务,

vim /etc/systemd/system/mysqld_exporter.service

[Unit]
Description=mysqld_exporter
After=network.target
[Service]
Type=simple
User=mysqld_exporter
ExecStart=/usr/local/bin/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/bin/mysqld_exporter/.my-exporter.cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target

以服务的方式进行启动管理

# 添加用户
useradd --no-create-home --shell /bin/false mysqld_exporter
# 为用户赋予文件夹权限
chown -R mysqld_exporter:mysqld_exporter /usr/local/bin/mysqld_exporter

# 重新加载系统配置
systemctl daemon-reload
# 开机启动
systemctl enable mysqld_exporter.service
# 启动服务
systemctl start mysqld_exporter
# 查看状态
systemctl status mysqld_exporter

看到mysqld_exporter启动成功,默认在9104端口,通过http://localhost:9104/metrics可以看到采集到的数据

prometheus采集服务器硬件信息 prometheus 查询数据_prometheus_08

修改prometheus配置文件信息并重启prometheus

- job_name: 'mysql_exporter'
    static_configs:
      - labels:
          instance: master:3306 # 主库标签
      - targets:
        - localhost:9104 # mysqld_exporter暴露的端口
      - labels:
          instance: slave:3306 # 从库标签
      - targets:
        - 172.17.191.255:9104

prometheus采集服务器硬件信息 prometheus 查询数据_数据_09


在Grafana创建Dashboard可视化地观测数据,这里可以选择监控模版来显示mysql的关键指标,模版ID为11323

prometheus采集服务器硬件信息 prometheus 查询数据_prometheus采集服务器硬件信息_10

可以看到监测页面如下

prometheus采集服务器硬件信息 prometheus 查询数据_prometheus_11