Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

memopt.php

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.

Features

  • Works as a single PHP file
  • Requires no Composer packages or external PHP libraries
  • Reads memory and Swap data from /proc/meminfo and /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

Target Environment

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

Setup

Place memopt.php on the target server. No additional installation is required.

php -v
php -l memopt.php

php -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.

Usage

php memopt.php <command> [options]

Commands

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

Options

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

Examples

Analyze Memory Usage

php memopt.php analyze

Show only the top 10 Swap-using processes:

php memopt.php analyze --top 10

Show Tuning Candidates

php memopt.php recommend

recommend 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.

Check Configuration Files

php memopt.php check-config

Examples 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.

Output a Markdown Report

php memopt.php report --format md --output /tmp/memopt_report.md

Output a JSON Report

php memopt.php report --format json --output /tmp/memopt_report.json

When reading Japanese JSON in PowerShell, specify UTF-8.

$json = Get-Content -Raw -Encoding UTF8 C:\path\to\memopt_report.json
$obj = ConvertFrom-Json $json

Check OOM Kill History

sudo php memopt.php oom-log

The 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.

Check Swapoff Safety

sudo php memopt.php swapoff-check

This 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.

Optional Configuration File

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 = 500

Example:

php memopt.php analyze --config /etc/memopt.ini

Status Labels

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.

Safety Notes

  • Configuration files are read only and are not modified automatically.
  • Services are not restarted.
  • swapoff -a is 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.

Development and Local Testing

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.json

Detailed implementation and test notes are recorded in docs/実装.md.

Known Limitations

  • Apache Include and MPM configuration are not fully resolved.
  • MariaDB !includedir handling 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.

License

If this repository does not include a license file, follow the project owner's policy for allowed usage.


memopt.php 日本語版

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.php

php -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 analyze

Swap 使用上位を 10 件だけ表示する場合:

php memopt.php analyze --top 10

改善候補を表示

php memopt.php recommend

recommend は設定を変更しません。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

パスワード、トークン、秘密鍵などに見える設定値はマスクします。

Markdown レポートを出力

php memopt.php report --format md --output /tmp/memopt_report.md

JSON レポートを出力

php memopt.php report --format json --output /tmp/memopt_report.json

PowerShell で日本語 JSON を読む場合は、UTF-8 を指定してください。

$json = Get-Content -Raw -Encoding UTF8 C:\path\to\memopt_report.json
$obj = ConvertFrom-Json $json

OOM kill 履歴を確認

sudo php memopt.php oom-log

journalctl -k --no-pager/var/log/messages/var/log/dmesg の順で確認します。ログが読めない場合は権限不足として扱います。

Swap 解放の安全判定

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 は対象外です。

ライセンス

このリポジトリでライセンスファイルが追加されていない場合、利用範囲はプロジェクト管理者の方針に従ってください。

About

Memory optimization CLI tool for RHEL-family servers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages