如何使用Alertmanager和Blackbox Exporter在Ubuntu 16.04上监控您的Web服务器

介绍

出现问题时,向适当的团队发送警报可显着加快识别问题的根源,从而使团队能够快速解决事件。

Prometheus是一个开源的监控系统,可以从您的服务中收集指标并将其存储在时间序列数据库中。 Alertmanager是一种处理警报的工具,可以对组进行重复删除,并将警报发送给适当的接收方。 它可以处理来自Prometheus等客户端应用程序的警报,并支持许多接收器,包括电子邮件, PagerDutyOpsGenieSlack

由于有许多Prometheus导出器可用,您可以为基础架构的每个部分(包括Web数据库服务器消息传递系统API)配置警报。

Blackbox Exporter通过HTTP,HTTPS,DNS,TCP或ICMP协议探测端点,返回有关请求的详细度量标准,包括它是否成功以及接收响应需要多长时间。

在本教程中,您将安装并配置Alertmanager和Blackbox Exporter来监视Nginx Web服务器的响应。 然后,您将配置Alertmanager通过电子邮件和Slack通知您,如果您的服务器没有响应。

先决条件

对于本教程,您需要:

第1步 - 创建服务用户

为了安全起见,我们将创建两个新的用户帐户, blackbox_exporteralertmanager 我们将在整个教程中使用这些帐户来运行Blackbox Exporter和Alertmanager,并隔离相应核心文件和目录的所有权。 这确保Blackbox Exporter和Alertmanager不能访问和修改他们不拥有的数据。

使用useradd命令使用--no-create-home--shell /bin/false标志创建这些用户,以便这些用户无法登录服务器:

sudo useradd --no-create-home --shell /bin/false blackbox_exporter
sudo useradd --no-create-home --shell /bin/false alertmanager

让用户就位,让我们下载并配置Blackbox Exporter。

第2步 - 安装Blackbox导出器

首先,将最新稳定版本的Blackbox Exporter下载到您的主目录。 您可以在Prometheus下载页面上找到最新的二进制文件及其校验和。

cd ~
curl -LO https://github.com/prometheus/blackbox_exporter/releases/download/v0.12.0/blackbox_exporter-0.12.0.linux-amd64.tar.gz

在解压归档文件之前,使用以下sha256sum命令验证文件的校验和:

sha256sum blackbox_exporter-0.12.0.linux-amd64.tar.gz

将此命令的输出与Prometheus下载页面上的校验和进行比较,以确保您的文件既真实又不被破坏:

Outputc5d8ba7d91101524fa7c3f5e17256d467d44d5e1d243e251fd795e0ab4a83605  blackbox_exporter-0.12.0.linux-amd64.tar.gz

如果校验和不匹配,请删除下载的文件并重复上述步骤重新下载文件。

如果您确定校验和匹配,请解压缩存档:

tar xvf blackbox_exporter-0.12.0.linux-amd64.tar.gz

这将创建一个名为blackbox_exporter- 0.12.0 .linux-amd64的目录,其中包含blackbox_exporter二进制文件,许可证和示例文件。

将二进制文件复制到/usr/local/bin目录。

sudo mv ./blackbox_exporter-0.12.0.linux-amd64/blackbox_exporter /usr/local/bin

将二进制文件的用户和组所有权设置为blackbox_exporter用户,确保非root用户不能修改或替换文件:

sudo chown blackbox_exporter:blackbox_exporter /usr/local/bin/blackbox_exporter

最后,我们将删除存档和解压目录,因为它们不再需要。

rm -rf ~/blackbox_exporter-0.12.0.linux-amd64.tar.gz ~/blackbox_exporter-0.12.0.linux-amd64

接下来,让我们配置Blackbox Exporter通过HTTP协议探测端点,然后运行它。

第3步 - 配置和运行Blackbox导出器

让我们创建一个配置文件,定义Blackbox Exporter应该如何检查端点。 我们还将创建一个systemd单元文件,以便我们可以使用systemd管理Blackbox的服务。

我们将在下一步中指定在Prometheus配置中进行探测的端点列表。

首先,创建Blackbox Exporter配置的目录。 根据Linux的惯例,配置文件进入/etc目录,所以我们将使用这个目录来保存Blackbox Exporter配置文件:

sudo mkdir /etc/blackbox_exporter

然后将此目录的所有权设置为您在第1步中创建的blackbox_exporter用户:

sudo chown blackbox_exporter:blackbox_exporter /etc/blackbox_exporter

在新创建的目录中,创建可容纳Blackbox Exporter配置设置的blackbox.yml文件:

sudo nano /etc/blackbox_exporter/blackbox.yml

我们将配置Blackbox Exporter使用默认的http探测器来探测端点。 探索者定义Blackbox Exporter如何检查端点是否正在运行。 http探测器通过向端点发送HTTP请求并测试其响应代码来检查端点。 您可以选择使用哪种HTTP方法进行探测,以及选择接受哪些状态代码作为成功响应。 其他受欢迎的探测器包括用于通过TCP协议进行探测的tcp探测器,用于通过ICMP协议进行探测的icmp探测器和用于检查DNS条目的dns探测器。

在本教程中,我们将使用http探针通过HTTP GET方法探测端口8080上运行的端点。 默认情况下,探测器假定2xx范围内的有效状态码是有效的,所以我们不需要提供有效的状态码列表。

我们将配置5秒的超时时间,这意味着在报告故障之前,Blackbox Exporter将等待响应5秒。 根据您的应用类型,选择任何符合您需求的值。

注意: Blackbox Exporter的配置文件使用YAML格式 ,该格式禁止使用制表符并严格要求使用两个空格进行缩进。 如果配置文件格式不正确,Blackbox Exporter将无法启动。

将以下配置添加到文件中:

/etc/blackbox_exporter/blackbox.yml
modules:
  http_2xx:
    prober: http
    timeout: 5s
    http:      
      valid_status_codes: []
      method: GET

您可以在Blackbox Exporter的文档中找到有关配置选项的更多信息。

保存文件并退出文本编辑器。

在创建服务文件之前,将配置文件上的用户和组所有权设置为第1步中创建的blackbox_exporter用户。

sudo chown blackbox_exporter:blackbox_exporter /etc/blackbox_exporter/blackbox.yml

现在创建服务文件,以便您可以使用systemd管理Blackbox Exporter:

sudo nano /etc/systemd/system/blackbox_exporter.service

将以下内容添加到文件中:

/etc/systemd/system/blackbox_exporter.service
[Unit]
Description=Blackbox Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=blackbox_exporter
Group=blackbox_exporter
Type=simple
ExecStart=/usr/local/bin/blackbox_exporter --config.file /etc/blackbox_exporter/blackbox.yml

[Install]
WantedBy=multi-user.target

此服务文件告诉systemd运行Blackbox Exporter作为blackbox_exporter用户,其配置文件位于/etc/blackbox_exporter/blackbox.yml systemd服务文件的细节超出了本教程的范围,但如果您想了解更多信息,请参阅“ 了解系统单元和单元文件”教程。

保存文件并退出文本编辑器。

最后,重新加载systemd以使用您新创建的服务文件:

sudo systemctl daemon-reload

现在启动Blackbox Exporter:

sudo systemctl start blackbox_exporter

通过检查服务的状态确保它已成功启动:

sudo systemctl status blackbox_exporter

输出包含有关Blackbox Exporter进程的信息,包括主进程标识符(PID),内存使用情况,日志等。

Output● blackbox_exporter.service - Blackbox Exporter
   Loaded: loaded (/etc/systemd/system/blackbox_exporter.service; disabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-04-05 17:48:58 UTC; 5s ago
 Main PID: 5869 (blackbox_export)
    Tasks: 4
   Memory: 968.0K
      CPU: 9ms
   CGroup: /system.slice/blackbox_exporter.service
           └─5869 /usr/local/bin/blackbox_exporter --config.file /etc/blackbox_exporter/blackbox.yml

如果服务的状态未active (running) ,请按照屏幕上的日志并重新执行上述步骤来解决问题,然后再继续本教程。

最后,启用该服务以确保Blackbox Exporter在服务器重新启动时启动:

sudo systemctl enable blackbox_exporter

既然Blackbox Exporter已完全配置并正在运行,我们可以配置Prometheus以收集有关探测到我们终端的请求的指标,以便我们可以基于这些指标创建警报,并使用Alertmanager为警报设置通知。

第4步 - 配置Prometheus刮黑盒子出口

如第3步所述,要探测的端点列表位于Prometheus配置文件中,作为Blackbox Exporter targets指令的一部分。 在这一步中,您将配置Prometheus使用Blackbox Exporter来抓取您在必备教程中配置的端口8080上运行的Nginx Web服务器。

在您的编辑器中打开Prometheus配置文件:

sudo nano /etc/prometheus/prometheus.yml

此时,它应该如下所示:

/etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9100']

scrape_configs指令末尾,添加以下条目,这将告诉Prometheus使用第3步中配置的Blackbox Exporter的模块http_2xx来探测在本地端口8080上运行的端点。

/etc/prometheus/prometheus.yml
...
  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets:
        - http://localhost:8080
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: localhost:9115

默认情况下,Blackbox Exporter在端口9115上运行,并在/probe端点上提供指标。

Blackbox Exporter的scrape_configs配置与其他出口商的配置不同。 最显着的不同是targets指令,它列出了被探测的端点而不是出口者的地址。 出口商的地址是使用适当的__address__标签设置的。

您可以在Prometheus文档中找到relabel指令的详细说明。

你的Prometheus配置文件现在看起来像这样:

Prometheus配置文件 - /etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'node_exporter'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9100']
  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets:
        - http://localhost:8080
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: localhost:9115

保存该文件并关闭文本编辑器。

重新启动Prometheus以使更改生效:

sudo systemctl restart prometheus

通过检查Prometheus服务状态确保它按预期运行:

sudo systemctl status prometheus

如果服务的状态未active (running) ,请按照屏幕上的日志并重新执行上述步骤来解决问题,然后再继续本教程。

此时,您已配置Prometheus从Blackbox Exporter中删除指标。 为了接收来自Alertmanager的警报,在下一步中,您将创建一组适当的Prometheus警报规则。

第5步 - 创建警报规则

Prometheus警报分为两部分。 第一部分由Prometheus服务器处理,包括根据警报规则生成警报并将警报发送给Alertmanager 第二部分由Alertmanager完成,它根据配置管理接收到的警报并将它们发送到适当的接收器。

在此步骤中,您将了解警报规则的基本语法,方法是创建警报规则以检查您的服务器是否可用。

首先,创建一个文件来存储您的警报。 /etc/prometheus目录中创建一个名为alert.rules.yml的空文件:

sudo touch /etc/prometheus/alert.rules.yml

由于此文件是Prometheus配置的一部分,请确保所有权设置为您在先决条件Prometheus教程中创建的prometheus用户:

sudo chown prometheus:prometheus /etc/prometheus/alert.rules.yml

警报文件就位后,我们需要通过向配置文件添加适当的指令来告诉Prometheus。

在您的编辑器中打开Prometheus配置文件:

sudo nano /etc/prometheus/prometheus.yml

rule_files指令之后添加rule_files指令,以便在Prometheus启动时使Prometheus加载新创建的警报文件。

/etc/prometheus/prometheus.yml
global:
  scrape_interval: 15s

rule_files:
  - alert.rules.yml

scrape_configs:
...

保存文件并退出文本编辑器。

现在让我们构建一个检查端点是否关闭的规则。

为了制作警报规则,您将使用Blackbox Exporter的probe_success指标,如果端点启动则返回1,否则返回0

probe_success度量标准包含两个标签:带有端点地址的instance标签和带有收集该度量标准的导出器名称的job标签。

在您的编辑器中打开警报规则文件:

sudo nano /etc/prometheus/alert.rules.yml

与Prometheus配置文件类似,警报规则文件使用YAML格式,该格式严格禁止制表符,并且需要两个空格用于缩进。 如果文件格式不正确,Prometheus将无法启动。

首先,我们将创建一个名为EndpointDown的警报规则,以检查probe_sucess指标是否等于0 ,持续时间为10秒。 这确保了如果端点在10秒内不可用,Prometheus不会发送任何警报。 根据您的应用类型和需求,您可以自由选择任何期限。

此外,我们将附加两个标签,表示严重程度和警报摘要,所以我们可以轻松管理和过滤警报。

如果您想在警报的标签和注释中包含更多详细信息,则可以使用{{ $labels. metrics_label }} {{ $labels. metrics_label }}语法来获取标签的值。 我们将使用它来包含度量instance标签中的端点地址。

将以下规则添加到警报文件中:

/etc/prometheus/alert.rules.yml
groups:
- name: alert.rules
  rules:
  - alert: EndpointDown
    expr: probe_success == 0
    for: 10s
    labels:
      severity: "critical"
    annotations:
      summary: "Endpoint {{ $labels.instance }} down"

保存文件并退出文本编辑器。

在重新启动Prometheus之前,使用以下promtool命令确保您的警报文件在语法上是正确的:

sudo promtool check rules /etc/prometheus/alert.rules.yml

输出包含在文件中找到的规则的数量,以及有关规则是否在语法上正确的信息:

OutputChecking /etc/prometheus/alert.rules.yml
  SUCCESS: 1 rules found

最后,重新启动Prometheus以应用更改:

sudo systemctl restart prometheus

使用status命令验证服务是否正在运行:

sudo systemctl status prometheus

如果该服务的状态未active ,请按照屏幕上的日志并重新执行上述步骤以解决问题,然后再继续本教程。

使用警报规则后,我们可以下载并安装Alertmanager。

第6步 - 下载Alertmanager

Blackbox Exporter被配置,我们的警报规则已经到位。 让我们下载并安装Alertmanager来处理Prometheus收到的警报。

您可以在Prometheus下载页面上找到最新的二进制文件及其校验和。 将当前稳定版本的Alertmanager下载并解压到您的主目录中:

cd ~
curl -LO https://github.com/prometheus/alertmanager/releases/download/v0.14.0/alertmanager-0.14.0.linux-amd64.tar.gz

在解压归档文件之前,使用以下sha256sum命令验证文件的校验和:

sha256sum alertmanager-0.14.0.linux-amd64.tar.gz

将此命令的输出与Prometheus下载页面上的校验和进行比较,以确保您的文件既真实又无损坏。

Outputcaddbbbe3ef8545c6cefb32f9a11207ae18dcc788e8d0fb19659d88c58d14b37  alertmanager-0.14.0.linux-amd64.tar.gz

如果校验和不匹配,请删除下载的文件并重复上述步骤重新下载文件。

验证下载后,解压缩存档:

tar xvf alertmanager-0.14.0.linux-amd64.tar.gz

这将创建一个名为alertmanager- 0.14.0 .linux-amd64的目录,其中包含两个二进制文件( alertmanageramtool ),一个许可证和一个示例配置文件。

将这两个二进制文件移动到/usr/local/bin目录中:

sudo mv alertmanager-0.14.0.linux-amd64/alertmanager /usr/local/bin
sudo mv alertmanager-0.14.0.linux-amd64/amtool /usr/local/bin

将二进制文件的用户和组所有权设置为您在第1步中创建的警报管理用户:

sudo chown alertmanager:alertmanager /usr/local/bin/alertmanager
sudo chown alertmanager:alertmanager /usr/local/bin/amtool

从主目录中删除剩余的文件,因为它们不再需要:

rm -rf alertmanager-0.14.0.linux-amd64 alertmanager-0.14.0.linux-amd64.tar.gz

现在所需的文件位于适当的位置,我们可以配置Alertmanager通过电子邮件发送警报通知。

第7步 - 配置Alertmanager通过电子邮件发送警报

在这一步中,您将创建目录和文件来存储Alertmanager的数据和配置设置,然后配置Alertmanager以通过电子邮件发送警报。

遵循标准的Linux约定,我们将在/etc创建一个目录来存储Alertmanager的配置文件。

sudo mkdir /etc/alertmanager

将新创建的目录的用户和组所有权设置为alertmanager用户:

sudo chown alertmanager:alertmanager /etc/alertmanager

我们将配置文件存储在alertmanager.yml文件中,因此创建该文件并在您的编辑器中打开它:

sudo nano /etc/alertmanager/alertmanager.yml

像其他Prometheus相关的文件一样,这个文件也使用YAML格式,因此请确保使用两个空格而不是制表符来缩进。

我们将配置Alertmanager以使用Postfix发送电子邮件,这是在先决条件教程之后安装的。 我们需要使用smtp_smarthost指令提供SMTP服务器的地址,以及我们想要发送电子邮件的地址,使用smtp_from指令。 由于Postfix与Alertmanager运行在同一台服务器上,因此服务器的地址为localhost:25 我们将使用alertmanager用户发送电子邮件。

默认情况下,Postfix没有配置TLS,所以我们需要告诉Alertmanager允许使用smtp_require_tls指令的非TLS SMTP服务器。

将SMTP配置放在global指令下,因为它用于指定在所有其他配置上下文中有效的参数。 这包括我们的SMTP配置,还可以包括各种集成的API令牌:

Alertmanager配置文件第1部分 - /etc/alertmanager/alertmanager.yml
global:
  smtp_smarthost: 'localhost:25'
  smtp_from: 'alertmanager@your_domain'
  smtp_require_tls: false

注意:请确保使用您的域名替换smtp_from指令中的your_domin

此时,Alertmanager知道如何发送电子邮件,但我们需要定义它如何使用route指令处理传入的警报。 route指令应用于每个传入警报并定义属性,例如Alertmanager将如何分组警报,默认收件人是谁,或Alertmanager在发送初始警报之前等待多长时间。

要将警报分组,请使用group_by子指令,该子指令采用内联标签数组(如[' label-1 ', ' label-2 '] )。 分组确保包含相同标签的警报将被分组并在同一批次中发送。

每个route指令都有一个使用receiver方子指令定义的receiver 如果您想要添加多个接收器,则需要在同一个指令下定义多个接收器,或者使用routes子指令嵌套多个route指令。 在本教程中,我们将介绍配置Slack警报的第一种方法。

在这种情况下,我们只会按照Blackbox的instance标签和我们在第6步中附加到警报的severity标签进行分组,以确保我们会在一封邮件中为我们的端点收到多个警报,并且严重程度严重。

添加以下group_by指令:

Alertmanager配置文件第2部分 - /etc/alertmanager/alertmanager.yml
...
route:
  group_by: ['instance', 'alert']

接下来,我们将定义时间间隔,例如Alertmanager在发送初始和新警报之前等待多长时间。

使用group_wait子指令,我们将定义Alertmanager在发送初始警报之前等待的时间。 在此期间,Alertmanager将等待Prometheus发送其他警报(如果存在),以便它们可以在同一批次中发送。 由于我们只有一个警报,我们将选择30秒的任意值。

接下来,使用group_interval间隔,我们将定义Alertmanager在发送下一批警报之前将等待多长时间(如果同一组中有新警报)。 您可以根据自己的需要自由选择任何值,但我们会将其设置为每5分钟一次。

我们将配置的最后一个时间间隔是repeat_interval ,它定义Alertmanager在发送通知之前警报尚未解决的等待时间。 您可以选择适合您需要的任何值,但我们将使用3小时的任意值。

最后,使用receiver方子指令,定义谁将接收警报通知。 我们将使用一个名为team-1的接收器,我们将在后面定义它。

修改route指令,使其看起来像这样:

Alertmanager配置文件第2部分 - /etc/alertmanager/alertmanager.yml
route:
  group_by: ['instance', 'severity']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 3h
  receiver: team-1

如果您想匹配并仅发送有关特定警报的通知,则可以使用matchmatch_re子指令按标签的值过滤掉警报。 match子指令表示相等匹配,其中match_re子指令表示通过正则表达式匹配。

现在我们将配置team-1接收器,以便您可以接收警报通知。 receivers指令下,您可以定义包含名称和适当配置子指令的接收方。 Alertmanager文档的一部分提供了可用接收器列表以及如何配置它们的说明

为了配置team-1电子邮件接收器,我们将使用email_configs指令下的email_configs子指令:

Alertmanager配置文件第3部分 - /etc/alertmanager/alertmanager.yml
receivers:
  - name: 'team-1'
    email_configs:
      - to: 'your-email-address'

此时,您已将Alertmanager配置为向您的电子邮件地址发送警报通知。 你的配置文件应该如下所示:

Alertmanager配置文件 - /etc/alertmanager/alertmanager.yml
global:
  smtp_smarthost: 'localhost:25'
  smtp_from: 'alertmanager@example.com'
  smtp_require_tls: false

route:
  group_by: ['instance', 'severity']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 3h
  receiver: team-1

receivers:
  - name: 'team-1'
    email_configs:
      - to: 'your-email-address'

在下一步中,我们将配置Alertmanager将警报发送到您的Slack频道。 如果您不想配置Slack,则可以直接跳到第10步,在那里我们将创建服务文件并配置Prometheus以使用Alertmanager。

第8步 - 配置Alertmanager通过Slack发送警报

在继续此步骤之前,请确保您创建了一个Slack帐户并且您有一个Slack工作区可用。

要向Slack发送警报,请先创建一个传入Webhook

将浏览器指向https:// workspace-name .slack.com/services/new/incoming-webhook/上提供的传入Webhook创建页面。 您将看到包含传入Webhooks详细信息的页面,以及您需要选择要发送提醒的频道的下拉菜单。

Slack传入Webhook

一旦你选择了频道,点击添加传入WebHooks集成按钮。

您会看到一个新页面,确认webhook已成功创建。 复制此页面上显示的Webhook URL ,您将使用它来配置Alertmanager的Slack通知。

在您的编辑器中打开Alertmanager配置文件以配置Slack通知:

sudo nano /etc/alertmanager/alertmanager.yml

首先,使用创建Slack入站Webhook时获得的URL将slack_api_url子指令添加到配置的global部分。

Alertmanager配置文件第1部分 - /etc/alertmanager/alertmanager.yml
global:
  smtp_smarthost: 'localhost:25'
  smtp_from: 'alertmanager@example.com'
  smtp_require_tls: false

  slack_api_url: 'your_slack_webhook_url'

有两种方法可将警报发送到多个接收器:

  1. 在同一条目下包含多个接收器配置。 这是最容易出错的解决方案和最简单的方法。
  2. 创建多个接收器条目并嵌套多个route指令。

我们不会介绍本教程中的第二种方法,但如果您有兴趣,请查看Alertmanager文档的Route配置部分。

team-1接收器中,添加一个名为slack_configs的新子命令,并提供应接收警报的通道的名称。 在这种情况下,我们将使用使用general通道:

Alertmanager配置文件第2部分 - /etc/alertmanager/alertmanager.yml
receivers:
  - name: 'team-1'
    email_configs:
      - to: 'your-email-address'
    slack_configs:
      - channel: 'general<^>'

您完成的配置文件如下所示:

Alertmanager配置文件 - /etc/alertmanager/alertmanager.yml
global:
  smtp_smarthost: 'localhost:25'
  smtp_from: 'alertmanager@example.com'
  smtp_require_tls: false

  slack_api_url: 'your_slack_webhook_url'

route:
  group_by: ['instance', 'severity']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 3h
  receiver: team-1

receivers:
  - name: 'team-1'
    email_configs:
      - to: 'your-email-address'
    slack_configs:
      - channel: 'general'

保存文件并退出编辑器。

我们现在准备第一次运行Alertmanager。

第9步 - 运行Alertmanager

让Alertmanager启动并运行。 我们将首先为Alertmanager创建一个systemd单元文件,以使用systemd管理其服务。 然后我们将更新Prometheus以使用Alertmanager。

创建一个新的systemd单元文件并在文本编辑器中打开它:

sudo nano /etc/systemd/system/alertmanager.service

将以下内容添加到文件以配置systemd,以使用配置文件(位于/etc/alertmanager/alertmanager.yml和Alertmanager的URL)配置为使用您的服务器的IP地址来运行Alertmanager作为alertmanager用户:

/etc/systemd/system/alertmanager.service
[Unit]
Description=Alertmanager
Wants=network-online.target
After=network-online.target

[Service]
User=alertmanager
Group=alertmanager
Type=simple
WorkingDirectory=/etc/alertmanager/
ExecStart=/usr/local/bin/alertmanager --config.file=/etc/alertmanager/alertmanager.yml --web.external-url http://your_server_ip:9093

[Install]
WantedBy=multi-user.target

这将运行Alertmanager作为alertmanager用户。 它还告诉Alertmanager为其Web UI使用URL http:// your_server_ip :9093 ,其中9093是Alertmanager的默认端口。 一定要包括协议( http:// )或者事情不会起作用。

保存该文件并关闭文本编辑器。

接下来,我们需要通过向Prometheus配置文件添加适当的Alertmanager服务发现目录来告诉Prometheus Alertmanager。 默认情况下,Alertmanager在端口9093上运行,因为它与Prometheus在同一台服务器上,所以我们将使用localhost:9093地址。

打开Prometheus配置文件:

sudo nano /etc/prometheus/prometheus.yml

rule_files指令之后,添加以下alerting指令:

Prometheus配置文件 - /etc/prometheus/prometheus.yml
...
rule_files:
  - alert.rules.yml

alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - localhost:9093
...

完成后,保存文件并关闭文本编辑器。

为了能够跟踪您收到的警报-web.external-url当您启动Prometheus时,您需要使用-web.external-url标志告诉Prometheus您的服务器的IP地址或域名。

打开Prometheus的systemd单元文件:

sudo nano /etc/systemd/system/prometheus.service

将现有的ExecStart行替换为以下行:

ExecStart=/usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries \ 
    --web.external-url http://your_server_ip

您的新Prometheus单元文件如下所示:

Prometheus服务文件 - /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 --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries \ 
    --web.external-url http://your_server_ip

[Install]
WantedBy=multi-user.target

保存该文件并关闭文本编辑器。

重新加载systemd并重新启动Prometheus以应用更改:

sudo systemctl daemon-reload
sudo systemctl restart prometheus

通过检查服务的状态,确保Prometheus按预期工作:

sudo systemctl status prometheus

如果服务的状态未active (running) ,请按照屏幕上的日志并重新执行上述步骤来解决问题,然后再继续本教程。

最后,首次启动Alertmanager:

sudo systemctl start alertmanager

检查服务的状态以确保Alertmanager按预期工作:

sudo systemctl status alertmanager

如果服务的状态未active (running) ,请按照屏幕上的消息进行操作,并在继续本教程之前回顾上述步骤解决问题。

最后,启用该服务以确保Alertmanager在系统引导时启动:

sudo systemctl enable alertmanager

要访问Alertmanager的Web UI,请允许流量通过防火墙端口9093

sudo ufw allow 9093/tcp

Alertmanager现在配置为通过电子邮件和Slack发送警报通知。 让我们确保它的工作。

第10步 - 测试Alertmanager

让我们确保Alertmanger工作正常并发送电子邮件和Slack通知。 我们将通过删除您在必备教程中创建的Nginx服务器块来禁用端点:

sudo rm /etc/nginx/sites-enabled/your_domain

重新加载Nginx以应用更改:

sudo systemctl reload nginx

If you want to confirm it's actually disabled, you can point your web browser to your server's address. You should see a message indicating that the site is no longer reachable. If you don't, retrace the preceding steps and make sure you deleted correct server block and reloaded Nginx.

Depending on the group_wait interval, which is 30 seconds in our case, you should receive email and Slack notifications after 30 seconds.

If you don't, check the service's status by using the following status commands and follow the on-screen logs to find the cause of the problem:

sudo systemctl status alertmanager
sudo systemctl status prometheus

You can also check the alert's status from the Prometheus Web UI, by pointing your web browser to the http:// your_server_ip /alerts . You'll be asked to enter the username and password you chose by following the Prometheus tutorial. By clicking on the alert name, you'll see the status, the alert rule, and associated labels:

Prometheus UI - alerts

Once you've verified Alertmanager is working, enable the endpoint by re-creating the symbolic link from the sites-available directory to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled

Reload Nginx once again to apply the changes:

sudo systemctl reload nginx

In the next step, we'll look at how to use Alertmanager's Command-Line Interface.

Step 11 — Managing Alerts Using the CLI

Alertmanager comes with the command-line tool amtool , which lets you monitor, manage and silence alerts.

The amtool tool requires you to provide the URL of Alertmanager using the --alertmanager.url flag every time you execute an command. In order to use amtool without providing the URL, we'll start by creating a configuration file.

Default locations for the configuration file are $HOME/.config/amtool/config.yml , which makes the configuration available only for your current user, and /etc/amtool/config.yml , which makes the configuration available for the every user on the server.

You're free to choose whatever suits your needs, but for this tutorial, we'll use the $HOME/.config/amtool/config.yml file.

First, create the directory. The -p flag tells mkdir to create any necessary parent directories along the way:

mkdir -p $HOME/.config/amtool

Create the config.yml file and open it in your text editor:

nano $HOME/.config/amtool/config.yml

Add the following line to tell amtool to use Alertmanager with the http://localhost:9093 URL:

~/.config/amtool/config.yml
alertmanager.url: http://localhost:9093

Save the file and exit your text editor.

Now, we'll take a look at what we can do with the amtool command line tool.

Using the amtool alert query command, you can list all alerts that have been send to Alertmanager:

amtool alert query

The output shows the alert's name, the time of the alert's first occurrence, and the alert's summary you provided when you configured it:

OutputAlertname     Starts At                Summary
EndpointDown  2018-04-03 08:48:47 UTC  Endpoint http://localhost:8080 down

You can also filter alerts by their labels using the appropriate matcher. A matcher contains the label name, the appropriate operation, which can be = for full matching and =~ for partial matching, and the label's value.

If you want to list all alerts that have a critical severity label attached, use the severity=critical matcher in the alert query command:

amtool alert query severity=critical

Like before, the output contains the alert's name, the time of alert's first occurrence and the alert's summary.

OutputAlertname     Starts At                Summary
EndpointDown  2018-04-03 08:48:47 UTC  Endpoint http://localhost:8080 down

You can use regular expressions to match labels with the =~ operator. For example, to list all alerts for http://localhost endpoints not depending on the port, you can use the instance=~http://localhost.* matcher:

amtool alert query instance=~http://localhost.*

As you have only one alert and endpoint, the output would be the same as in the previous example.

To look at the Alertmanager configuration, use the amtool config command:

amtool config

The output will contain the content of the /etc/alertmanager/alertmanager.yml file.

Now let's look at how to silence alerts using amtool .

Silencing alerts lets you mute alerts based on the matcher for a given time. During that period, you'll not receive any email or Slack notification for the silenced alert.

The amtool silence add command takes the matcher as an argument and creates a new silence based on the matcher.

To define the expiration of an alert, use the --expires flag with desired duration of the silence, such as 1h or the --expire-on flag with the time of silence expiration in the RFC3339 format . For example, the format 2018-10-04T07:50:00+00:00 represents 07.50am on October 4th, 2018.

If the --expires or the --expires-on flag is not provided, alerts will be silenced for 1 hour .

To silence all alerts for the http://localhost:8080 instance for 3 hours , you'd use the following command:

amtool silence add instance=http://localhost:8080 --expires 3h

The output contains an identification number for the silence, so make sure to note it down as you'll need it in case you want to remove the silence:

Output4e89b15b-0814-41d3-8b74-16c513611732

If you want to provide additional information when creating the silence, such as the author and comments, use the --author and --comment flags:

amtool silence add severity=critical --expires 3h --author "Sammy The Shark" --comment "Investigating the progress"

Like before, the output contains the ID of the silence:

Output12b7b9e1-f48a-4ceb-bd85-65ac882ceed1

The command amtool silence query will show the list of all non-expired silences:

amtool silence query

The output contains the ID of the silence, the list of matchers, the expiration timestamp, the author, and a comment:

OutputID                                    Matchers                        Ends At                  Created By       Comment
12b7b9e1-f48a-4ceb-bd85-65ac882ceed1  severity=critical               2018-04-04 08:02:58 UTC  Sammy The Shark  Investigating in the progress
4e89b15b-0814-41d3-8b74-16c513611732  instance=http://localhost:8080  2018-04-04 08:14:21 UTC  sammy

Similar to the alert query command, you can use label matchers to filter the output by labels attached on creation:

amtool silence query instance=http://localhost:8080

Like before, the output will include the ID number and details of the alert:

OutputID                                    Matchers                        Ends At                  Created By  Comment
4e89b15b-0814-41d3-8b74-16c513611732  instance=http://localhost:8080  2018-04-04 08:14:21 UTC  sammy

Finally, to expire a silence, use the amtool silence expire with the ID of the silence you want to expire:

amtool silence expire 12b7b9e1-f48a-4ceb-bd85-65ac882ceed1
amtool silence expire 4e89b15b-0814-41d3-8b74-16c513611732

No output represents successful command execution. If you see an error, make sure you provided the correct ID of the silence.

结论

In this tutorial you configured Blackbox Exporter and Alertmanager to work together with Prometheus so you can receive alerts via email and Slack. You also used Alertmanager's command-line interface, amtool , to manage and silence alerts.

If you'd like to learn more about other Alertmanager integrations, take a look at the [Configuration](( https://prometheus.io/docs/alerting/configuration/) ) portion of Alertmanager's documentation.

Also, you can take a look how to integrate Prometheus Alerts with other services such as Grafana .

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏