Apache Web Server (httpd)

Apache 网络服务器集成会收集与流量相关的指标,例如打开连接数或传入请求。集成还会收集访问和错误日志。访问日志会被解析为专注于请求详细信息的 JSON 载荷,而错误日志会被解析为其错误代码和消息。

如需详细了解 Apache Web Server,请参阅 Apache Web Server (httpd) 文档

前提条件

如需收集 Apache Web Server 遥测,您必须安装 Ops Agent

  • 对于指标,请安装 2.7.0 版或更高版本。
  • 对于日志,请安装 2.4.0 版或更高版本。

此集成支持 Apache Web Server 2.4 版。

配置 Apache Web Server 实例

如需从 Apache Web Server 收集遥测,您必须配置服务器的 httpd.conf 文件以启用 mod_status 插件

很多情况下,安装 Apache 时会默认启用此插件。如需查看您的虚拟机实例上是否启用了该插件,请运行以下命令:

curl localhost:80/server-status?auto

如果已启用该插件,则输出将包含类似于以下内容的行:

Total Accesses: 2
Total kBytes: 1
BusyWorkers: 1
IdleWorkers: 4

如果您看到 404 Not Found 页面,则表明 mod_status 插件未启用。

为 Apache Web Server 配置 Ops Agent

按照配置 Ops Agent 指南,添加从 Apache Web Server 实例收集遥测数据所需的元素,并重启代理

配置示例

以下命令会创建相关配置来收集和注入 Apache Web Server 的遥测数据,并重启 Ops Agent。

# Configures Ops Agent to collect telemetry from the app and restart Ops Agent.

set -e

# Create a back up of the existing file so existing configurations are not lost.
sudo cp /etc/google-cloud-ops-agent/config.yaml /etc/google-cloud-ops-agent/config.yaml.bak

# Configure the Ops Agent.
sudo tee /etc/google-cloud-ops-agent/config.yaml > /dev/null << EOF
metrics:
  receivers:
    apache:
      type: apache
  service:
    pipelines:
      apache:
        receivers:
          - apache
logging:
  receivers:
    apache_access:
      type: apache_access
    apache_error:
      type: apache_error
  service:
    pipelines:
      apache:
        receivers:
          - apache_access
          - apache_error
EOF

sudo service google-cloud-ops-agent restart
sleep 60

配置日志收集

如需从 Apache Web Server 注入日志,您必须为 Apache Web Server 生成的日志创建接收器,然后为新的接收器创建流水线。

如需为 apache_access 日志配置接收器,请指定以下字段:

字段 默认 说明
exclude_paths 要从 include_paths 匹配的集合中排除的文件系统路径模式列表。
include_paths [/var/log/apache2/access.log,/var/log/apache2/access_log,/var/log/httpd/access_log] 要通过跟踪每个文件读取的文件系统路径列表。路径中可以使用通配符 (*);例如 /var/log/apache*/*.log
record_log_file_path false 如果设置为 true,则从中获取日志记录的特定文件的路径将作为 agent.googleapis.com/log_file_path 标签的值显示在输出日志条目中。使用通配符时,系统只会记录从中获取记录的文件的路径。
type 该值必须为 apache_access
wildcard_refresh_interval 60s include_paths 中通配符文件路径的刷新间隔。指定为 time.ParseDuration 可解析的时长,例如 30s2m。该属性在高日志记录吞吐量下可能很有用,因为日志文件的轮替速度快于默认时间间隔。

如需为 apache_error 日志配置接收器,请指定以下字段:

字段 默认 说明
exclude_paths 要从 include_paths 匹配的集合中排除的文件系统路径模式列表。
include_paths [/var/log/apache2/error.log,/var/log/apache2/error_log,/var/log/httpd/error_log] 要通过跟踪每个文件读取的文件系统路径列表。路径中可以使用通配符 (*);例如 /var/log/apache*/*.log
record_log_file_path false 如果设置为 true,则从中获取日志记录的特定文件的路径将作为 agent.googleapis.com/log_file_path 标签的值显示在输出日志条目中。使用通配符时,系统只会记录从中获取记录的文件的路径。
type 该值必须为 apache_error
wildcard_refresh_interval 60s include_paths 中通配符文件路径的刷新间隔。指定为 time.ParseDuration 可解析的时长,例如 30s2m。该属性在高日志记录吞吐量下可能很有用,因为日志文件的轮替速度快于默认时间间隔。

记录的内容

logName 派生自配置中指定的接收器 ID。LogEntry 中的详细字段如下所示。

apache_access 日志包含 LogEntry 中的以下字段:

字段 类型 说明
httpRequest 对象 参见HttpRequest
jsonPayload.host 字符串 Host 标头的内容
jsonPayload.user 字符串 请求经过身份验证的用户名
severity 字符串 (LogSeverity) 日志条目级别(已转换)

apache_error 日志包含 LogEntry 中的以下字段:

字段 类型 说明
jsonPayload.client 字符串 客户端 IP 地址(可选)
jsonPayload.errorCode 字符串 Apache 错误代码
jsonPayload.level 字符串 日志条目级别
jsonPayload.message 字符串 日志消息
jsonPayload.module 字符串 日志源自的 Apache 模块
jsonPayload.pid 字符串 进程 ID
jsonPayload.tid 字符串 线程 ID
severity 字符串 (LogSeverity) 日志条目级别(已转换)

配置指标收集

如需从 Apache Web Server 注入指标,您必须为 Apache Web Server 生成的指标创建接收器,然后为新的接收器创建流水线。

此接收器不支持在配置中使用多个实例,例如,监控多个端点。所有这些实例都会写入相同的时序,并且 Cloud Monitoring 无法区分它们。

如需为 apache 指标配置接收器,请指定以下字段:

字段 默认 说明
collection_interval 60s 时长值,例如 30s5m
server_status_url http://localhost:80/server-status?auto 由 mod_status 模块公开的网址。
type 该值必须为 apache

监控的内容

下表提供了 Ops Agent 从 Apache Web Server 实例收集的指标列表。

指标类型
种类、类型
受监控的资源
标签
workload.googleapis.com/apache.current_connections
GAUGEINT64
gce_instance
server_name
workload.googleapis.com/apache.requests
CUMULATIVEINT64
gce_instance
server_name
workload.googleapis.com/apache.scoreboard
GAUGEINT64
gce_instance
server_name
state
workload.googleapis.com/apache.traffic
CUMULATIVEINT64
gce_instance
server_name
workload.googleapis.com/apache.workers
GAUGEINT64
gce_instance
server_name
state

验证配置

本部分介绍如何验证您是否正确配置了 Apache Web Server 接收器。Ops Agent 可能需要一两分钟才会开始收集遥测数据。

如需验证 Apache Web Server 日志是否已发送到 Cloud Logging,请执行以下操作:

  1. 在 Google Cloud 控制台的导航面板中,选择 Logging,然后选择 Logs Explorer

    前往 Logs Explorer

  2. 在编辑器中输入以下查询,然后点击运行查询
    resource.type="gce_instance"
    (log_id("apache_access") OR log_id("apache_error"))
    

如需验证 Apache Web Server 指标是否已发送到 Cloud Monitoring,请执行以下操作:

  1. 在 Google Cloud 控制台的导航面板中,选择 Monitoring,然后选择  Metrics Explorer

    进入 Metrics Explorer

  2. 在查询构建器窗格的工具栏中,选择名称为  MQL PromQL 的按钮。
  3. 确认在语言切换开关中选择了 MQL。语言切换开关位于同一工具栏中,用于设置查询的格式。
  4. 在编辑器中输入以下查询,然后点击运行查询
    fetch gce_instance
    | metric 'workload.googleapis.com/apache.current_connections'
    | every 1m
    

查看信息中心

如需查看 Apache Web Server 指标,您必须配置一个图表或信息中心。 Apache Web 服务器集成服务可为您提供一个或多个信息中心。在您配置集成并且 Ops Agent 开始收集指标数据后,所有信息中心都会自动安装。

您还可以在不安装集成的情况下查看信息中心的静态预览。

如需查看已安装的信息中心,请执行以下操作:

  1. 在 Google Cloud 控制台的导航面板中,选择 Monitoring,然后选择  信息中心

    前往信息中心

  2. 选择信息中心列表标签页,然后选择集成类别。
  3. 点击您要查看的信息中心的名称。

如果您已配置集成,但尚未安装信息中心,请检查 Ops Agent 是否正在运行。如果信息中心内没有图表的指标数据,则信息中心的安装将失败。Ops Agent 开始收集指标后,系统会为您安装信息中心。

如需查看信息中心的静态预览,请执行以下操作:

  1. 在 Google Cloud 控制台的导航面板中,选择 Monitoring,然后选择  集成

    前往集成

  2. 点击 Compute Engine 部署平台过滤条件。
  3. 找到 Apache Web 服务器的条目,然后点击查看详细信息
  4. 选择信息中心标签页以查看静态预览。如果信息中心已安装,您可以通过点击查看信息中心来转到信息中心。

如需详细了解 Cloud Monitoring 中的信息中心,请参阅信息中心和图表

如需详细了解如何使用集成页面,请参阅管理集成

安装提醒政策

提醒政策会指示 Cloud Monitoring 在满足指定条件时通知您。 Apache Web 服务器集成服务可提供一项或多项提醒政策供您使用。您可以从 Monitoring 中的集成页面查看和安装这些提醒政策。

如需查看可用提醒政策的说明并安装它们,请执行以下操作:

  1. 在 Google Cloud 控制台的导航面板中,选择 Monitoring,然后选择  集成

    前往集成

  2. 找到 Apache Web 服务器的条目,然后点击查看详细信息
  3. 选择提醒标签页。此标签页提供可用提醒政策的说明,并提供一个安装政策的界面。
  4. 安装提醒政策。提醒政策需要知道将提醒触发的通知发送到何处,因此它们需要您提供信息才能进行安装。如需安装提醒政策,请执行以下操作:
    1. 从可用提醒政策列表中,选择您要安装的提醒政策。
    2. 配置通知部分中,选择一个或多个通知渠道。您可以选择禁止使用通知渠道,但如果您这样做,则您的提醒政策将以静默方式触发。您可以在 Monitoring 中查看其状态,但不会收到通知。

      如需详细了解通知渠道,请参阅管理通知渠道

    3. 点击创建政策

如需详细了解 Cloud Monitoring 中的提醒政策,请参阅提醒简介

如需详细了解如何使用集成页面,请参阅管理集成

后续步骤

如需查看如何使用 Ansible 安装 Ops Agent、配置第三方应用和安装示例信息中心的演示,请参阅安装 Ops Agent 以排查第三方应用的问题视频。