vrok/monitoring-bundle

Symfony 扩展,用于向监控地址发送 '存活' 邮件(由 crontab 触发)

安装次数: 3,096

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:symfony-bundle

1.3.0 2024-02-14 10:37 UTC

This package is auto-updated.

Last update: 2024-08-30 14:30:51 UTC


README

从控制台按计划发送电子邮件消息以检查 crontab 是否正在运行并且系统可以发送邮件(例如,运行应用的 Docker 容器)。如果配置了 Symfony 消息传递器,则消息将被推送到队列并由工作者处理,因此这也检查了队列和工作者是否正常。

CI Status Coverage Status

安装

请确保已全局安装 Composer,如 Composer 文档中的安装章节所述。

使用 Symfony Flex 的应用程序

打开命令控制台,进入您的项目目录,然后执行以下操作

$ composer require vrok/monitoring-bundle

不使用 Symfony Flex 的应用程序

步骤 1:下载 Bundle

打开命令控制台,进入您的项目目录,并执行以下命令以下载此 Bundle 的最新稳定版本

$ composer require vrok/monitoring-bundle

步骤 2:启用 Bundle

然后,通过将其添加到项目 config/bundles.php 文件中注册的 Bundle 列表来启用该 Bundle

// config/bundles.php

return [
    // ...
    Vrok\MonitoringBundle\VrokMonitoringBundle::class => ['all' => true],
];

配置

Symfony Mailer 必须配置,并且应该通过监听器/配置设置默认的发件人(FROM 地址)。

创建 config/packages/vrok_monitoring.yaml

vrok_monitoring:
  # receiver of the ping email
  monitor_address: mail@domain.tld

  # application name used in the subject and body of the mail
  app_name: My-App-Name

可选地,使用 '%env(MONITOR_ADDRESS)%' 等从 ENV 获取这些选项。

使用方法

从控制台调用 bin/console monitor:send-alive-message,最好通过每 30 分钟等的 crontab 触发。

2,32 * * * * www-data /usr/local/bin/php /var/www/html/bin/console monitor:send-alive-message

电子邮件主题将是 "Service [app_name] is alive!"。文本正文包含一个整数时间戳,稍后由 Icinga 检查用于清除所有但最新的消息。

Automatic message from [app_name]: The service is alive and can send emails
at 2020-09-06T09:02:01+02:00 (timestamp 1599375721)

Icinga 配置

获取发送的消息需要 check_imap_receive Nagios/Icinga 插件,请确保它在使用 Icinga 监控的服务器上已安装并正常工作,可以是与应用程序服务器相同,也可以不是。

创建检查脚本,并用您的值替换邮件服务器域名、接收者地址和密码,我们不为这些值使用参数,以避免在 Icinga 主机中存储这些凭据。

/usr/lib/nagios/plugins/contrib/check_service_alive:

#!/bin/bash
# Delete all but the last "alive" mail
# -s SUBJECT -s "$1" - only with this subject, given in argument $1
# --capture-max "timestamp (\d+)" - extract nummeric value
# --capture-min thisdoesnotexist - required to _not_ capture a minimum message, that would not be deleted otherwise 
# --no-delete-captured - we want to delete old messages so the postbox doesn't fill (--delete is enabled by default)
#   but we want to keep the last message so the Nagios check does not fail if he runs e.g. every 15min
#   so with --capture-max and --no-delete-captured we keep the last mail, delete the rest (of emails matching the search)
# no output (1>/dev/null) so Icinga only reads the status from the check below 
/usr/lib/nagios/plugins/check_imap_receive -H mail.domain.tld -U receiver@domain.tld -P imap_password --tls -w 5 -c 10 --imap-retries 1 --search-critical-min 1 -s SUBJECT -s "$1" --capture-max "timestamp (\d+)" --capture-min thisdoesnotexist --nodelete-captured 1>/dev/null

# Check if a mail with the given subject was received in the last hour:
# --imap-retries - search only once instead of 10x in 5s intervals
# -s YOUNGER -s 3600 = within the last hour
# -s SUBJECT -s "$1" - only with this subject, given in argument $1
/usr/lib/nagios/plugins/check_imap_receive -H mail.domain.tld -U receiver@domain.tld -P imap_password --tls -w 5 -c 10 --imap-retries 1 --search-critical-min 1 -s YOUNGER -s 3600 -s SUBJECT -s "$1" --nodelete

在 Icinga 主机中添加命令定义

// Symfony Service Check (a mail with the given subject was received within the last hour)
object CheckCommand "check_service_alive" {
  command = [ PluginDir + "/contrib/check_service_alive" ]
  arguments = {
    "--subject" = {
      value = "$subject$"
      description = "email subject [substring] to search for"
      required = true
      skip_key = true
    }
  }
}

也添加服务定义

// Symfony Service Check (a mail with the given subject was received within the last hour)
apply Service for (name => subject in host.vars.service_alive) {
  check_command = "check_service_alive"
  check_interval = 30m
  display_name = name + " service check"
  vars.subject = subject
  assign where host.vars.client_endpoint && host.vars.check_service_alive == true
  command_endpoint = host.vars.client_endpoint
}

最后,在主机定义中启用并配置服务,将 "dev.domain.tld" 替换为在 packages/vrok_monitoring.yaml 中配置的 app_name。您可以使用一个 monitor_address 监控多个应用程序,只需确保 app_names 不同(主题通过模式匹配,因此使用 "domain.tld is alive" 和 "dev.domain.tld is alive" 将发生冲突,使用 "Service " 前缀以防止这种情况)

    vars.check_service_alive = true
    vars.service_alive["App-Dev"] = "dev.domain.tld is alive"
    vars.service_alive["App-Prod"] = "Service domain.tld is alive"