zl程序教程

您现在的位置是:首页 >  其它

当前栏目

Prometheus - 系统监控与警报管理

管理 Prometheus 系统监控 警报
2023-09-11 14:16:28 时间

Prometheus 监控

promethues官网文档地址: https://prometheus.io/docs/introduction

Prometheus 是一个监控平台,它通过在这些目标上抓取指标 HTTP 端点来收集来自被监控目标的指标。接下来展示如何使用 Prometheus 安装、配置和监控我们的第一个资源。您将下载、安装和运行 Prometheus。您还将下载并安装一个导出器,这些工具可以在主机和服务上公开时间序列数据。我们的第一个导出器将是 Prometheus 本身,它提供关于内存使用、垃圾收集等的各种主机级指标。

prometheus下载地址: https://prometheus.io/download/
关于promethues的介绍和简单安装步骤参考我的这篇博客: Prometheus - 时间序列数据库

使用命令帮助文档介绍:

./prometheus --help
usage: prometheus [<flags>]

The Prometheus monitoring server

在启动 Prometheus 之前,让我们对其进行配置。Prometheus 配置是YAML。Prometheus 下载附带一个名为的文件中的示例配置,prometheus.yml这是一个很好的开始。

我们删除了示例文件中的大部分注释以使其更加简洁(注释是带有前缀的行#)。

global:
  scrape_interval:     15s
  evaluation_interval: 15s

rule_files:
  # - "first.rules"
  # - "second.rules"

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']

示例配置文件中有三个配置块:global、rule_files和scrape_configs.

该global块控制 Prometheus 服务器的全局配置。我们有两种选择。第一个,scrape_interval控制 Prometheus 抓取目标的频率。您可以为单个目标覆盖它。在这种情况下,全局设置是每 15 秒刮一次。该evaluation_interval选项控制 Prometheus 评估规则的频率。Prometheus 使用规则来创建新的时间序列并生成警报。

该rule_files块指定我们希望 Prometheus 服务器加载的任何规则的位置。目前我们没有任何规则。

最后一个块scrape_configs控制 Prometheus 监控的资源。由于 Prometheus 还将有关自身的数据公开为 HTTP 端点,因此它可以抓取和监控自己的健康状况。在默认配置中,有一个名为 的作业prometheus,它会抓取 Prometheus 服务器公开的时间序列数据。该作业包含一个静态配置的目标,即localhoston port 9090。Prometheus 期望指标可用于路径为/metrics. 所以这个默认作业是通过 URL 抓取的:http://localhost:9090/metrics。

返回的时间序列数据将详细说明 Prometheus 服务器的状态和性能。有关配置选项的完整规范,请参阅:配置信息

启动prometheus

./prometheus --config.file=prometheus.yml

使用prometheus内置表达式

让我们试着看看 Prometheus 收集的一些关于它自己的数据。要使用 Prometheus 的内置表达式浏览器,请导航到 http://localhost:9090/graph并选择“Graph”选项卡中的“Table”视图。

Prometheus 导出的关于其自身的一项指标称为 ( Prometheus 服务器已服务的请求promhttp_metric_handler_requests_total总数)。/metrics继续并将其输入到表达式控制台中:

promhttp_metric_handler_requests_total

如果我们只对产生 HTTP(200) 代码的请求感兴趣,我们可以使用此查询来检索该信息:

promhttp_metric_handler_requests_total{code="200"}

要计算返回的时间序列的数量,您可以编写:

count(promhttp_metric_handler_requests_total)

有关更多的表达式语法参考: https://prometheus.io/docs/querying/basics/

使用 NODE EXPORTER 监控 LINUX 主机指标

可以参考官网文档: https://prometheus.io/docs/guides/node-exporter/

安装并运行 Node Exporter 后,您可以通过 cURLing端点来验证是否正在导出指标:

curl http://localhost:9100/metrics

配置 Prometheus 实例
您需要正确配置本地运行的 Prometheus 实例才能访问 Node Exporter 指标。以下prometheus.yml示例配置文件将通过以下方式告诉 Prometheus 实例从节点导出器中抓取以及抓取频率localhost:9100:

global:
  scrape_interval: 15s

scrape_configs:
- job_name: node
  static_configs:
  - targets: ['localhost:9100']

prometheus警报管理

Prometheus 的警报分为两部分。Prometheus 服务器中的警报规则将警报发送到警报管理器。然后,Alertmanager 管理这些警报,包括静音、抑制、聚合和通过电子邮件、待命通知系统和聊天平台等方法发送通知。

设置警报和通知的主要步骤是:

设置和配置警报管理器
配置 Prometheus与 Alertmanager 对话
在 Prometheus 中创建警报规则

prometheus 警报管理官方文档: https://prometheus.io/docs/alerting/latest/alertmanager/