Apache Exporter 监控手札

Apache Exporter 监控手札

前言

Apache Exporter 是一个 Prometheus 的 exporter,用于收集 Apache HTTP Server 的指标数据,并通过 Prometheus 进行监控。

步骤

以下是使用 Apache Exporter 进行监控的步骤:

前提条件

  • 已安装并运行 Apache HTTP Server。
  • 已安装 Prometheus。

安装 Apache Exporter

下载并安装

解压并运行

tar xvfz apache_exporter-*.linux-amd64.tar.gz
cd apache_exporter-*.linux-amd64
./apache_exporter [flags]

为了安全这边使用了basic_auth_users进行验证获取数据认证,web-config.yml如下:

# Usernames and passwords required to connect.
# Passwords are hashed with bcrypt: https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md#about-bcrypt.
basic_auth_users:
  alice: $2y$10$mDwo.lAisC94iLAyP81MCesa29IzH37oigHC/42V2pdJlUprsJPze
  bob: $2y$10$hLqFl9jSjoAAy95Z/zw8Ye8wkdMBM8c5Bn1ptYqP/AXyV0.oy0S8m

run.sh脚本如下:

cd /AppHome/apache_exporter
nohup ./apache_exporter --web.config.file="./web-config.yml" --scrape_uri="http://localhost:51780/server-status/?auto" > ./apache_exporter.log 2>&1 &

配置 Apache ServerStatus

启用 mod_status 模块

确保在 Apache 配置中启用了 mod_status 模块,并允许从 Apache Exporter 的 IP 访问。

####################-Apache Server Status--------------

ExtendedStatus On

<Location /server-status>

SetHandler server-status

Order deny,allow

Deny from all

Allow from all

</Location>

###################--Apache Server Status--end---------

重启 Apache

sudo systemctl restart apache2

验证Apache Exporter

查看是否有数据采集成功,访问:http://ip:9117/metrics

image-20250918101936217

配置 Prometheus

apiVersion: v1
kind: Secret
metadata:
  name: apache-exporter-basic-auth
  namespace: monitoring
data:
  password: aaaaa=
  user: bbbbb==
type: Opaque
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: external-apache-exporter
  labels:
    app: apache-exporter
    release: prometheus-community
  namespace: monitoring
spec:
  endpoints:
    - port: http-metrics
      interval: 15s
      path: /metrics
      basicAuth:
        username:
          name: apache-exporter-basic-auth
          key: user
        password:
          name: apache-exporter-basic-auth
          key: password
      relabelings:
        - sourceLabels: [ __meta_kubernetes_service_label_app, __meta_kubernetes_service_label_system ]
          targetLabel: job
          separator: "-"  # 用连字符连接两个标签值
          action: replace
  selector:
    matchLabels:
      app: apache
      type: apache-exporter
  namespaceSelector:
    matchNames:
      - monitoring

---
apiVersion: v1
kind: Service
metadata:
  name: external-apache-dyck-exporter
  labels:
    app: apache
    type: apache-exporter
    system: dyck
  namespace: monitoring
spec:
  type: ClusterIP
  clusterIP: None
  ports:
    - name: http-metrics
      port: 9121
      protocol: TCP

---
apiVersion: v1
kind: Endpoints
metadata:
  name: external-apache-dyck-exporter
  labels:
    app: apache
    type: apache-exporter
    system: dyck
  namespace: monitoring
subsets:
  - addresses:
      - ip: 10.196.68.159
    ports:
      - name: http-metrics
        port: 9117
        protocol: TCP
---
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  labels:
    prometheus: k8s
    role: alert-rules
    release: prometheus-community
  name: apache-rules
  namespace: monitoring
spec:
  groups:
    - name: apache.rules
      rules:
        # 规则:Apache 服务宕机
        - alert: ApacheDown
          expr: apache_up == 0
          for: 1m
          labels:
            severity: critical
          annotations:
            summary: "Apache 服务宕机 (instance {{ $labels.instance }})"
            description: "Apache 服务已停止,请立即检查!"

        # 规则:请求速率突增(5分钟内增长10倍)
        - alert: ApacheRequestRateSpike
          expr: rate(apache_accesses_total[1m]) > 10 * avg_over_time(apache_accesses_total[5m])
          for: 2m
          labels:
            severity: warning
          annotations:
            summary: "Apache 请求速率突增 (instance {{ $labels.instance }})"
            description: "当前请求速率:{{ $value }} 请求/秒。"

        # 规则:CPU 使用率过高
        - alert: ApacheHighCPU
          expr: rate(apache_cpu_time_ms_total[5m]) > 1000
          for: 10m
          labels:
            severity: warning
          annotations:
            summary: "Apache CPU 使用率过高 (instance {{ $labels.instance }})"
            description: "CPU 使用时间:{{ $value }}ms/秒。"

grafana可视化(可选)

安装对应的 Grafana Dashboard,id为:3894

image-20250918101758422

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇