2026/05/11(月)Mastodon周りのメトリクス収集メモ

更新日:
投稿日:

確認環境

Env ver
nginx 1.26.1
Apache2 2.4.58
PostgreSQL 16.13
Redis 7.0.15
Mastodon 4.5.9
Prometheus 3.5.0

Apache2

# 取得
wget https://github.com/Lusitaniae/apache_exporter/releases/download/v1.0.12/apache_exporter-1.0.12.linux-amd64.tar.gz
tar xvfz apache_exporter-1.0.12.linux-amd64.tar.gz

# binを配置
sudo cp apache_exporter-1.0.12.linux-amd64/apache_exporter /usr/local/bin/
ls -la /usr/local/bin/ | grep apache_exporter

# デーモン作成
cat <<'EOF' | sudo tee /etc/systemd/system/apache_exporter.service
[Unit]
Description=Prometheus Apache Exporter
After=network.target

[Service]
Type=simple
User=prometheus
Group=prometheus
WorkingDirectory=/var/lib/prometheus
ExecStart=/usr/local/bin/apache_exporter --scrape_uri=http://[::]:ここにポート番号/server-status?auto
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

# デーモンの有効化
sudo systemctl daemon-reload
sudo systemctl enable --now apache_exporter

# 起動確認
curl "http://[::1]:9117/metrics"

# 掃除
rm -Rf apache_exporter-1.0.12.linux-amd64 apache_exporter-1.0.12.linux-amd64.tar.gz

PostgreSQL

# 取得
wget https://github.com/prometheus-community/postgres_exporter/releases/download/v0.19.1/postgres_exporter-0.19.1.linux-amd64.tar.gz
tar xvfz postgres_exporter-0.19.1.linux-amd64.tar.gz

# binを配置
sudo cp postgres_exporter-0.19.1.linux-amd64/postgres_exporter /usr/local/bin/
ls -la /usr/local/bin/ | grep postgres_exporter

# 監視ユーザーの作成
sudo -u postgres psql
CREATE USER postgres_exporter WITH PASSWORD 'ここにパスワード';
ALTER USER postgres_exporter SET SEARCH_PATH TO postgres_exporter,pg_catalog;
GRANT pg_monitor TO postgres_exporter;
quit

# 監視情報の作成
echo 'DATA_SOURCE_NAME="postgresql://postgres_exporter:ここにパスワード@localhost:5432/postgres?sslmode=disable"' | sudo tee /etc/default/postgres_exporter
sudo chown root:root /etc/default/postgres_exporter
sudo chmod 600 /etc/default/postgres_exporter

# デーモン作成
cat <<'EOF' | sudo tee /etc/systemd/system/postgres_exporter.service
[Unit]
Description=Prometheus PostgreSQL Exporter
After=network.target postgresql.service
Wants=postgresql.service

[Service]
Type=simple
User=prometheus
Group=prometheus
WorkingDirectory=/var/lib/prometheus
EnvironmentFile=/etc/default/postgres_exporter
ExecStart=/usr/local/bin/postgres_exporter \
    --web.listen-address=[::]:9187
Restart=on-failure
RestartSec=5
EOF

# デーモンの有効化
sudo systemctl daemon-reload
sudo systemctl enable --now postgres_exporter

# 起動確認
curl "http://[::1]:9187/metrics"

# 掃除
rm -Rf postgres_exporter-0.19.1.linux-amd64 postgres_exporter-0.19.1.linux-amd64.tar.gz

Redis

# 取得
wget https://github.com/oliver006/redis_exporter/releases/download/v1.82.0/redis_exporter-v1.82.0.linux-amd64.tar.gz
tar xvfz redis_exporter-v1.82.0.linux-amd64.tar.gz

# binを配置
sudo cp redis_exporter-v1.82.0.linux-amd64/redis_exporter /usr/local/bin/
ls -la /usr/local/bin/ | grep redis_exporter

# デーモン作成
cat <<'EOF' | sudo tee /etc/systemd/system/redis_exporter.service
[Unit]
Description=Prometheus Redis Exporter
After=network.target

[Service]
Type=simple
User=prometheus
Group=prometheus
WorkingDirectory=/var/lib/prometheus
ExecStart=/usr/local/bin/redis_exporter --redis.addr=redis://localhost:6379
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

# デーモンの有効化
sudo systemctl daemon-reload
sudo systemctl enable --now redis_exporter

# 起動確認
curl "http://[::1]:9121/metrics"

# 掃除
rm -Rf redis_exporter-v1.82.0.linux-amd64 redis_exporter-v1.82.0.linux-amd64.tar.gz

Mastodonの組み込みExporter

  1. .env.productionに以下を追加
    MASTODON_PROMETHEUS_EXPORTER_ENABLED=true
    MASTODON_PROMETHEUS_EXPORTER_SIDEKIQ_DETAILED_METRICS=true
    
  2. デーモンを作る

    cat <<'EOF' | sudo tee /etc/systemd/system/mastodon-prometheus-exporter.service
    [Unit]
    Description=mastodon-prometheus-exporter
    After=network.target
    
    [Service]
    Type=simple
    User=mastodon
    WorkingDirectory=/home/mastodon/live
    Environment="RAILS_ENV=production"
    ExecStart=/home/mastodon/.rbenv/shims/bundle exec prometheus_exporter -b "::" -p 9394
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    # デーモンの有効化
    sudo systemctl daemon-reload
    sudo systemctl enable --now mastodon-prometheus-exporter
    
  3. 起動確認
    curl "http://[::1]:9394/metrics"
    # Streamingはv4でしかlistenしてないので[::1]は諦める
    curl "http://localhost:5001/metrics"