memopt.php is a PHP CLI tool for RHEL-family servers, including RHEL, AlmaLinux, Rocky Linux, and CentOS. It checks memory usage, Swap usage, per-service memory consumption, and shows safe tuning candidates.
This is not an automatic configuration changer. It does not edit configuration files, restart services, or run swapoff. It only reads system information and outputs analysis or reports.
- Works as a single PHP file
- Requires no Composer packages or external PHP libraries
- Reads memory and Swap data from
/proc/meminfoand/proc/[pid]/status - Aggregates processes by service, such as ClamAV, SpamAssassin, Amavis, MariaDB, Apache, and PHP-FPM
- Checks known configuration files and representative values
- Outputs reports in Markdown, CSV, and JSON
- Searches OOM kill history
- Shows only a safety judgment for
swapoff; it does not execute it
| Item | Details |
|---|---|
| OS | RHEL / AlmaLinux / Rocky Linux / CentOS family |
| Runtime | PHP CLI 5.4 or later |
| Format | Single file: memopt.php |
| Privileges | Analysis can run as a normal user. Some log checks are easier with root privileges |
Place memopt.php on the target server. No additional installation is required.
php -v
php -l memopt.phpphp -v checks the PHP CLI version. php -l memopt.php checks syntax. If there is no syntax error, it prints No syntax errors detected in memopt.php.
php memopt.php <command> [options]| Command | Description |
|---|---|
analyze |
Shows memory, Swap, process, and service summaries |
recommend |
Shows conservative tuning candidates without changing settings |
report |
Outputs a report in text, md, csv, or json format |
check-config |
Checks known service configuration files and representative values |
oom-log |
Searches OOM kill history |
swapoff-check |
Judges whether a manual swapoff may be safe |
help |
Shows help |
| Option | Description | Default |
|---|---|---|
--top <n> |
Number of top Swap-using processes to show | 20 |
| `--format <text | md | csv |
--output <file> |
Report output path | Standard output |
--config <file> |
Loads an optional ini configuration file | none |
--no-color |
Compatibility option. Output is always plain text | false |
--no-emoji |
Compatibility option. Output never uses emoji | false |
--quiet |
Reserved option | false |
php memopt.php analyzeShow only the top 10 Swap-using processes:
php memopt.php analyze --top 10php memopt.php recommendrecommend does not change settings. It shows conservative review candidates for services such as ClamAV, SpamAssassin, Amavis, PHP-FPM, Apache, and MariaDB based on the current memory state.
php memopt.php check-configExamples of checked files:
/etc/clamd.d/*.conf/etc/sysconfig/spamassassin/etc/amavisd/amavisd.conf/etc/my.cnf,/etc/my.cnf.d/*.cnf/etc/httpd/conf/httpd.conf,/etc/httpd/conf.d/*.conf/etc/php-fpm.d/*.conf/etc/postfix/main.cf/etc/dovecot/dovecot.conf
Values that look like passwords, tokens, secrets, or private keys are masked.
php memopt.php report --format md --output /tmp/memopt_report.mdphp memopt.php report --format json --output /tmp/memopt_report.jsonWhen reading Japanese JSON in PowerShell, specify UTF-8.
$json = Get-Content -Raw -Encoding UTF8 C:\path\to\memopt_report.json
$obj = ConvertFrom-Json $jsonsudo php memopt.php oom-logThe tool checks journalctl -k --no-pager, /var/log/messages, and /var/log/dmesg in that order. If logs cannot be read, it treats that as a permission or readability issue.
sudo php memopt.php swapoff-checkThis command does not execute swapoff. It only shows available memory, used Swap, required safety margin, and a judgment to help decide whether manual execution is reasonable.
Use --config to override threshold values with an ini file.
[thresholds]
available_warn = 30
available_crit = 15
swap_warn = 30
swap_crit = 60
min_available_mb_for_swapoff = 500Example:
php memopt.php analyze --config /etc/memopt.ini| Label | Meaning |
|---|---|
正常 |
No major issue detected at the moment |
注意 |
Near a threshold, or some usage values are relatively high |
警告 |
Low available memory, high Swap usage, or high per-service usage |
The labels are displayed in Japanese because the program output is primarily Japanese.
- Configuration files are read only and are not modified automatically.
- Services are not restarted.
swapoff -ais not executed.- Shell execution is limited to the fixed OOM log command
journalctl -k --no-pager. - If an output file cannot be written, the command exits with an error.
On Windows or other development environments, you can use fixture directories instead of the real /proc, /etc, and /var/log paths.
$env:MEMOPT_PROC_ROOT = "<project-dir>\tests\fixtures\proc"
$env:MEMOPT_ETC_ROOT = "<project-dir>\tests\fixtures\etc"
$env:MEMOPT_LOG_ROOT = "<project-dir>\tests\fixtures\log"
& "<php.exe>" -l memopt.php
& "<php.exe>" memopt.php analyze --top 3 --no-color
& "<php.exe>" memopt.php recommend --top 5 --no-color
& "<php.exe>" memopt.php check-config --no-color
& "<php.exe>" memopt.php report --format json --output tests\out\report.jsonDetailed implementation and test notes are recorded in docs/実装.md.
- Apache Include and MPM configuration are not fully resolved.
- MariaDB
!includedirhandling is limited to a simple check of/etc/my.cnf.d/*.cnf. - The tool does not connect to MariaDB to inspect runtime settings.
- It does not edit systemd units or automatically set
MemoryMax=. - cron registration, continuous monitoring, Web UI, and Prometheus exporter are out of scope.
If this repository does not include a license file, follow the project owner's policy for allowed usage.
memopt.php は、RHEL / AlmaLinux / Rocky Linux / CentOS 系サーバーでメモリ使用状況、Swap 使用状況、主要サービス別の消費量を確認し、安全な改善候補を表示する PHP CLI ツールです。
自動変更ツールではありません。設定ファイルの書き換え、サービス再起動、swapoff の実行は行わず、読み取りとレポート出力だけを行います。
- PHP 単一ファイルで動作
- Composer や外部ライブラリは不要
/proc/meminfoと/proc/[pid]/statusからメモリと Swap を集計- ClamAV、SpamAssassin、Amavis、MariaDB、Apache、PHP-FPM などをサービス別に集計
- 主要設定ファイルの存在と代表値を確認
- Markdown / CSV / JSON レポートを出力
- OOM kill 履歴を確認
swapoffを実行せず、安全判定のみ表示
| 項目 | 内容 |
|---|---|
| OS | RHEL / AlmaLinux / Rocky Linux / CentOS 系 |
| 実行環境 | PHP CLI 5.4 以上 |
| 形式 | memopt.php 単一ファイル |
| 権限 | 分析のみなら一般ユーザーで実行可能。一部ログ確認は root 推奨 |
memopt.php を対象サーバーへ配置してください。追加のインストール作業は不要です。
php -v
php -l memopt.phpphp -v は PHP CLI のバージョン確認です。php -l memopt.php は構文チェックで、問題なければ No syntax errors detected in memopt.php と表示されます。
php memopt.php <command> [options]| コマンド | 内容 |
|---|---|
analyze |
メモリ、Swap、プロセス、サービス別集計を表示 |
recommend |
設定を変更せず、保守的な改善候補を表示 |
report |
text / md / csv / json 形式でレポート出力 |
check-config |
主要サービスの設定ファイルと代表値を確認 |
oom-log |
OOM kill 履歴を検索 |
swapoff-check |
swapoff を手動実行できそうか安全判定のみ実施 |
help |
ヘルプ表示 |
| オプション | 内容 | 既定値 |
|---|---|---|
--top <n> |
Swap 使用上位プロセスの表示件数 | 20 |
| `--format <text | md | csv |
--output <file> |
レポート出力先 | 標準出力 |
--config <file> |
任意の ini 設定ファイルを読み込む | なし |
--no-color |
互換用。現在の出力は常にプレーンテキスト | false |
--no-emoji |
互換用。現在の出力は絵文字なし | false |
--quiet |
予約オプション | false |
php memopt.php analyzeSwap 使用上位を 10 件だけ表示する場合:
php memopt.php analyze --top 10php memopt.php recommendrecommend は設定を変更しません。ClamAV、SpamAssassin、Amavis、PHP-FPM、Apache、MariaDB などについて、現在のメモリ状況から保守的な見直し候補を表示します。
php memopt.php check-config確認対象の例:
/etc/clamd.d/*.conf/etc/sysconfig/spamassassin/etc/amavisd/amavisd.conf/etc/my.cnf,/etc/my.cnf.d/*.cnf/etc/httpd/conf/httpd.conf,/etc/httpd/conf.d/*.conf/etc/php-fpm.d/*.conf/etc/postfix/main.cf/etc/dovecot/dovecot.conf
パスワード、トークン、秘密鍵などに見える設定値はマスクします。
php memopt.php report --format md --output /tmp/memopt_report.mdphp memopt.php report --format json --output /tmp/memopt_report.jsonPowerShell で日本語 JSON を読む場合は、UTF-8 を指定してください。
$json = Get-Content -Raw -Encoding UTF8 C:\path\to\memopt_report.json
$obj = ConvertFrom-Json $jsonsudo php memopt.php oom-logjournalctl -k --no-pager、/var/log/messages、/var/log/dmesg の順で確認します。ログが読めない場合は権限不足として扱います。
sudo php memopt.php swapoff-checkこのコマンドは swapoff を実行しません。利用可能メモリ、Swap 使用量、安全余力を見て、手動実行の判断材料を表示するだけです。
--config で ini ファイルを指定すると、判定しきい値を変更できます。
[thresholds]
available_warn = 30
available_crit = 15
swap_warn = 30
swap_crit = 60
min_available_mb_for_swapoff = 500実行例:
php memopt.php analyze --config /etc/memopt.ini| 表示 | 意味 |
|---|---|
正常 |
現時点では大きな問題なし |
注意 |
しきい値に近い、または一部の使用量が高め |
警告 |
利用可能メモリ不足、Swap 使用率高、またはサービス単位の使用量が大きい |
- 設定ファイルは読み取りのみで、自動変更しません。
- サービス再起動は行いません。
swapoff -aは実行しません。- shell 実行は OOM ログ確認時の固定コマンド
journalctl -k --no-pagerのみです。 - 出力ファイルに書けない場合はエラー終了します。
Windows 上など、実際の /proc や /etc を使わずに fixture で確認する場合は、開発用の環境変数を指定できます。
$env:MEMOPT_PROC_ROOT = "<project-dir>\tests\fixtures\proc"
$env:MEMOPT_ETC_ROOT = "<project-dir>\tests\fixtures\etc"
$env:MEMOPT_LOG_ROOT = "<project-dir>\tests\fixtures\log"
& "<php.exe>" -l memopt.php
& "<php.exe>" memopt.php analyze --top 3 --no-color
& "<php.exe>" memopt.php recommend --top 5 --no-color
& "<php.exe>" memopt.php check-config --no-color
& "<php.exe>" memopt.php report --format json --output tests\out\report.jsonテスト結果の詳細は docs/実装.md に記録しています。
- Apache の Include や MPM 設定を完全には解決しません。
- MariaDB の
!includedirは/etc/my.cnf.d/*.cnfの簡易確認に限定しています。 - MariaDB へ接続して実行時設定を取得する機能はありません。
- systemd unit の編集や
MemoryMax=の自動設定は行いません。 - cron 登録、常駐監視、Web UI、Prometheus exporter は対象外です。
このリポジトリでライセンスファイルが追加されていない場合、利用範囲はプロジェクト管理者の方針に従ってください。