Graphite 設定 · 6 min read · Oct 27, 2025
Ubuntu 22.04にGraphite監視システムをインストールして構成する方法

Graphiteは、コンピュータシステムのパフォーマンスを追跡し、グラフ化するために使用されるオープンソースツールです。ウェブサイト、アプリケーション、ビジネスサービス、ネットワークサーバーのパフォーマンスを追跡するために使用できます。非常に柔軟で、追跡しているメトリクスのパフォーマンスと健康の詳細な表現と広範な概要の両方の利点を得るために構成できます。
Graphiteは、ウェブアプリケーション、Carbonと呼ばれるストレージバックエンド、およびwhisperと呼ばれるデータベースライブラリのいくつかのコンポーネントで構成されています。このチュートリアルでは、Ubuntu 22.04サーバーにGraphiteをインストールして構成する方法を学びます。
前提条件
- Ubuntu 22.04を実行しているサーバー。
- サーバーを指す完全修飾ドメイン名(FQDN)。このチュートリアルでは、
graphite.example.comドメインを使用します。 - sudo権限を持つ非rootユーザー。
- 簡単なファイアウォール(UFW)が有効で実行中であること。
- すべてが更新されていることを確認してください。
$ sudo apt update && sudo apt upgrade - 基本的なユーティリティパッケージをインストールします。いくつかはすでにインストールされているかもしれません。
$ sudo apt install wget curl nano unzip -y
ステップ1 - ファイアウォールの構成
パッケージをインストールする前に、最初のステップはHTTPおよびHTTPS接続を許可するためにファイアウォールを構成することです。
ファイアウォールの状態を確認します。
$ sudo ufw status
次のような出力が表示されるはずです。
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
HTTPおよびHTTPSポートを許可します。
$ sudo ufw allow http
$ sudo ufw allow https
再度状態を確認して確認します。
$ sudo ufw status
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
80/tcp ALLOW Anywhere
443 ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
80/tcp (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
ステップ2 - 必要なパッケージのインストール
PIP Pythonパッケージマネージャを使用してGraphiteをインストールします。最初のステップは、インストールに必要なパッケージをインストールすることです。
$ sudo apt install vim python3-dev python3-pip libcairo2-dev libffi-dev build-essential
ステップ3 - GraphiteおよびGraphite Webのインストール
Graphiteを/opt/graphiteディレクトリにインストールします。
$ export PYTHONPATH="/opt/graphite/lib/:/opt/graphite/webapp/"
$ sudo pip install --no-binary=:all: https://github.com/graphite-project/whisper/tarball/master
$ sudo pip install --no-binary=:all: https://github.com/graphite-project/carbon/tarball/master
$ sudo pip install --no-binary=:all: https://github.com/graphite-project/graphite-web/tarball/master
ステップ4 - PostgreSQLのインストールと構成
PostgreSQLの公式APTリポジトリを使用してインストールします。次のコマンドを実行してPostgreSQL GPGキーを追加します。
$ curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql-key.gpg >/dev/null
APTリポジトリをソースリストに追加します。
$ sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/postgresql-key.gpg arch=amd64] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
システムリポジトリを更新します。
$ sudo apt update
次に、次のコマンドを使用してPostgreSQLおよびヘルパーパッケージをインストールできます。
$ sudo apt install postgresql postgresql-contrib libpq-dev
PostgreSQLサービスの状態を確認します。
$ sudo systemctl status postgresql
? postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Tue 2022-09-27 10:09:35 UTC; 4s ago
Process: 4456 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 4456 (code=exited, status=0/SUCCESS)
CPU: 1ms
Sep 27 10:09:35 matrix systemd[1]: Starting PostgreSQL RDBMS...
Sep 27 10:09:35 matrix systemd[1]: Finished PostgreSQL RDBMS.
サービスがデフォルトで有効で実行中であることが確認できます。
PostgreSQLシェルにログインします。
$ sudo -su postgres psql
Graphite用のデータベースユーザーを作成します。
postgres=# CREATE USER graphite WITH PASSWORD 'your_password';
Graphite用のデータベースを作成し、グラファイトユーザーに所有権を与えます。
postgres=# CREATE DATABASE graphitedb WITH OWNER graphite;
PostgreSQLシェルを終了します。
postgres=# \q
ステップ5 - Graphite CarbonおよびWebの構成
次のステップは、Graphite CarbonおよびGraphite Webを構成することです。
Carbonの構成
Carbonは3つのサービスで構成されています:
- carbon-cache: メトリクスを受け取り、ディスクに書き込みます。
- carbon-relay: データを複製します。
- carbon-aggregator: carbon-cacheサービスの前に実行され、メトリクスを時間の経過とともにバッファリングしてWhisperに転送します。
carbon-cacheの構成は必要ですが、carbon-relayとcarbon-aggregatorはオプションです。
指定された例ファイルを使用してcarbon.confファイルを作成します。
$ sudo cp /opt/graphite/conf/carbon.conf.example /opt/graphite/conf/carbon.conf
次に、ストレージスキーマの構成を作成します。
$ sudo cp /opt/graphite/conf/storage-schemas.conf.example /opt/graphite/conf/storage-schemas.conf
ストレージスキーマ構成ファイルを開きます。
$ sudo nano /opt/graphite/conf/storage-schemas.conf
内部には次のようなエントリがあります。
[carbon]
pattern = ^carbon\.
retentions = 60:90d
これは、正規表現^carbon\.に一致するパターンが、保持ポリシー60:90dでデータを保持することを意味します。これは、
- メトリクスが記録される頻度: 60秒
- それらの値を保存する期間: 90日
独自のエントリを追加できます。例としてtestを取り上げます。つまり、データポイントを監視し、データポイントエントリは文字列testで始まります。このエントリは、ファイルの下部にあるデフォルトエントリの前に追加する必要があります。
[test]
pattern = ^test\.
retentions = 10s:10m,1m:1h
これは、testで始まるメトリクスに一致します。収集したデータを2回、異なる詳細度で保存します。最初の定義(1s:10m)は、10秒ごとにデータポイントを作成します。データは10分間のみ保存されます。2番目の定義は、1分ごとにデータポイントを作成します。過去1分間のすべてのデータ(前の定義が10秒ごとにポイントを作成するため、6ポイント)を集約してポイントを作成します。この詳細レベルでのデータは1時間保存されます。
ファイルを保存するには、Ctrl + Xを押し、プロンプトが表示されたらYを入力します。
carbon-cacheサービスを開始します。
$ sudo /opt/graphite/bin/carbon-cache.py start
Graphite Webの構成
次のステップは、Graphiteウェブアプリを構成することです。
Graphiteアプリケーションのための秘密鍵を生成します。後で使用するために表示されたキーをコピーします。
$ python3 -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'
sp%71)6b$%^bc(7xpz1d!)x3(azog01&k^8l02*!y0#)72p07y
ウェブアプリ設定ファイルを作成します。
$ sudo cp /opt/graphite/webapp/graphite/local_settings.py.example /opt/graphite/webapp/graphite/local_settings.py
データベース設定でGraphiteウェブアプリケーションを構成する必要があります。local_settings.pyを編集のために開きます。
$ sudo nano /opt/graphite/webapp/graphite/local_settings.py
SECRET_KEY変数のコメントを解除し、ランダムな値を入力します。
SECRET_KEY = 'your-secret-key'
ALLOWED_HOSTS変数のコメントを解除します。
ALLOWED_HOSTS = [ '*' ]
TIME_ZONE変数のコメントを解除し、適切な値に設定します。
TIME_ZONE = 'Asia/Kolkata'
USE_REMOTE_USER_AUTHENTICATION変数のコメントを解除し、リモートユーザーがデータベースの変更を行う前に認証されるようにTRUEに設定します。
USE_REMOTE_USER_AUTHENTICATION = True
データベース設定を変更します。
DATABASES = {
'default': {
'NAME': 'graphitedb',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'USER': 'graphite',
'PASSWORD': 'your_password',
'HOST': '127.0.0.1',
'PORT': ''
}
}
ファイルを保存するには、Ctrl + Xを押し、プロンプトが表示されたらYを入力します。
PythonのPostgreSQLラッパーのためのいくつかの前提条件をインストールします。
$ sudo pip install psycopg2-binary
次のコマンドを実行してデータベーススキーマをインポートします。
$ sudo PYTHONPATH=/opt/graphite/webapp/ django-admin.py migrate --settings=graphite.settings
次の出力が得られます。
Operations to perform:
Apply all migrations: account, admin, auth, contenttypes, dashboard, events, sessions, tagging, tags, url_shortener
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying account.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying dashboard.0001_initial... OK
Applying events.0001_initial... OK
Applying sessions.0001_initial... OK
Applying tagging.0001_initial... OK
Applying tagging.0002_on_delete... OK
Applying tags.0001_initial... OK
Applying url_shortener.0001_initial... OK
次に、静的ファイルを収集します。
$ sudo PYTHONPATH=/opt/graphite/webapp/ django-admin.py collectstatic --settings=graphite.settings
正しい所有権設定を設定します。
$ sudo chown -R www-data:www-data /opt/graphite/storage/
$ sudo chown -R www-data:www-data /opt/graphite/static/
$ sudo chown -R www-data:www-data /opt/graphite/webapp/
ログイン用のルートユーザーを作成します。
$ sudo PYTHONPATH=/opt/graphite/webapp/ django-admin.py createsuperuser --settings=graphite.settings
Username (leave blank to use 'root'): navjot
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
スーパーユーザーを作成するように求められます。このユーザーは、後でGraphiteアプリケーションに接続するために使用されます。
ステップ6 - Apacheの構成
GraphiteはデフォルトでApache構成ファイルを持っています。Apacheサーバーをインストールします。
$ sudo apt install apache2 libapache2-mod-wsgi-py3
mod_wsgiファイルを作成します。
$ sudo cp /opt/graphite/conf/graphite.wsgi.example /opt/graphite/conf/graphite.wsgi
Graphiteの例の構成ファイルをApacheの場所にコピーします。
$ sudo cp /opt/graphite/examples/example-graphite-vhost.conf /etc/apache2/sites-available/graphite.conf
Graphite構成ファイルを編集のために開きます。
$ sudo nano /etc/apache2/sites-available/graphite.conf
最初の行のポート番号を80から127.0.0.1:8080に変更します。127.0.0.1を前に置くことで、ウェブからアクセスできないように制限します。
ドメイン名を追加します。
ServerName graphite.example.com #Replace with your domain
Alias /static/ /opt/graphite/static/の下に次の行を追加します。
#Add below lines
Require all granted
ファイルを保存するには、Ctrl + Xを押し、プロンプトが表示されたらYを入力します。
デフォルトの仮想ホストを無効にし、Graphite仮想ホストファイルを有効にします。
$ sudo a2dissite 000-default
$ sudo a2ensite graphite
Apacheにポート8080でリッスンするように指示し、ポート80でのリッスンを停止する必要があります。Nginxをプロキシサーバーとして使用するためです。
/etc/apache2/ports.confファイルを編集のために開きます。
$ sudo nano /etc/apache2/ports.conf
Listen 80という行を見つけ、次のように置き換えます。
Listen 127.0.0.1:8080
ファイルを保存するには、Ctrl + Xを押し、プロンプトが表示されたらYを入力します。
Apacheサーバーを再起動します。
$ sudo systemctl restart apache2
Graphiteが正常に動作しているかどうかを確認するために、次のコマンドを実行します。
$ curl 127.0.0.1:8080
次のような出力が得られます。
Graphite Browser
これで正常に動作していることが確認できます。
ステップ7 - Nginxのインストール
NginxをApacheのプロキシサーバーとして使用します。これにより、セキュリティと不明性の利点を享受しながら、Graphiteが提供する既存の構成を使用できます。
Ubuntu 22.04には古いバージョンのNginxが付属しています。最新バージョンをインストールするには、公式のNginxリポジトリをダウンロードする必要があります。
Nginxの署名キーをインポートします。
$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
Nginxの安定版のリポジトリを追加します。
$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg arch=amd64] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
システムリポジトリを更新します。
$ sudo apt update
Nginxをインストールします。
$ sudo apt install nginx
インストールを確認します。
$ nginx -v
nginx version: nginx/1.22.0
Nginxサーバーを起動します。
$ sudo systemctl start nginx
ステップ8 - SSLのインストール
SSL証明書を生成するためにCertbotをインストールする必要があります。Ubuntuのリポジトリを使用してCertbotをインストールするか、Snapdツールを使用して最新バージョンを取得できます。Snapdバージョンを使用します。
Ubuntu 22.04にはデフォルトでSnapdがインストールされています。次のコマンドを実行して、Snapdのバージョンが最新であることを確認します。
$ sudo snap install core
$ sudo snap refresh core
Certbotをインストールします。
$ sudo snap install --classic certbot
次のコマンドを使用して、Certbotコマンドが/usr/binディレクトリで実行できるようにシンボリックリンクを作成します。
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
次のコマンドを実行してSSL証明書を生成します。
$ sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m [email protected] -d graphite.example.com
上記のコマンドは、サーバーの/etc/letsencrypt/live/graphite.example.comディレクトリに証明書をダウンロードします。
Diffie-Hellmanグループ証明書を生成します。
$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
Certbotの更新スケジューラーサービスを確認します。
$ sudo systemctl list-timers
nextのサービスがスケジュールされているのが見つかります。
NEXT LEFT LAST PASSED UNIT ACTIVATES
.................................................................................................................................
Wed 2022-09-28 00:00:00 UTC 7h left Tue 2022-09-27 00:00:01 UTC 16h ago logrotate.timer logrotate.service
Wed 2022-09-28 02:39:09 UTC 10h left Tue 2022-09-27 09:42:42 UTC 6h ago apt-daily.timer apt-daily.service
Wed 2022-09-28 06:02:00 UTC 13h left n/a n/a snap.certbot.renew.timer snap.certbot.renew.service
SSL更新が正常に動作しているかどうかを確認するために、プロセスのドライランを行います。
$ sudo certbot renew --dry-run
エラーが表示されない場合は、すべて設定完了です。証明書は自動的に更新されます。
ステップ9 - Nginxの構成
/etc/nginx/nginx.confファイルを編集のために開きます。
$ sudo nano /etc/nginx/nginx.conf
include /etc/nginx/conf.d/*.conf;の前に次の行を追加します。
server_names_hash_bucket_size 64;
ファイルを保存するには、Ctrl + Xを押し、プロンプトが表示されたらYを入力します。
/etc/nginx/conf.d/uvdesk.confファイルを作成して開きます。
$ sudo nano /etc/nginx/conf.d/graphite.conf
次のコードを貼り付けます。
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name graphite.example.com;
access_log /var/log/nginx/graphite.access.log;
error_log /var/log/nginx/graphite.error.log;
# SSL
ssl_certificate /etc/letsencrypt/live/graphite.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/graphite.example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/graphite.example.com/chain.pem;
ssl_session_timeout 5m;
ssl_session_cache shared:MozSSL:10m;
ssl_session_tickets off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;
ssl_stapling on;
ssl_stapling_verify on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
resolver 8.8.8.8;
location / {
proxy_set_header Connection "upgrade";
proxy_set_header Upgrade $http_upgrade;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
}
}
# enforce HTTPS
server {
listen 80;
listen [::]:80;
server_name graphite.example.com;
return 301 https://$host$request_uri;
}
ファイルを保存するには、Ctrl + Xを押し、プロンプトが表示されたらYを入力します。
Nginx構成ファイルの構文を確認します。
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Nginxサービスを再起動します。
$ sudo systemctl restart nginx
ステップ10 - Graphiteにアクセスして使用する
ブラウザでURL https://graphite.example.comにアクセスすると、次の画面が表示されます。

右上のログインリンクをクリックしてログインページを開きます。ステップ5で作成したスーパーユーザーの資格情報を入力し、ログインボタンを押して進みます。
Graphiteにデータを供給する方法はいくつかあります。ストレージスキーマにパターンマッチャーを追加したため、testで始まるパターンはすべて記録されます。次のコマンドを使用してランダムデータを追加してみましょう。
$ echo "test.count 9 `date +%s`" | nc -q0 127.0.0.1 2003;
これにより、値9のデータメトリックがシステムに追加されます。値をループしてさらにデータを追加してみましょう。
$ for i in 4 6 8 16 2; do echo "test.count $i `date +%s`" | nc -q0 127.0.0.1 2003; sleep 6; done
Graphiteダッシュボードに戻り、左のサイドバーからメトリクス >> test >> countを開くと、次のような画面が表示されるはずです。

これで監視のために使用を開始できます。また、Grafanaと組み合わせて高いカスタマイズ性を得ることもできます。
結論
これで、Ubuntu 22.04サーバーにGraphiteをインストールして構成する方法と、Nginxをプロキシサーバーとして使用する方法に関するチュートリアルは終了です。質問がある場合は、下のコメントに投稿してください。
新しい投稿を受信箱で受け取る
スパムはありません。いつでも購読を解除できます。