#!/bin/bash
# カテゴリ:ディレクトリの対応(必要に応じて変更可)
declare -A CATEGORY_DIRS=(
[To]="/var/spool/postfix/to"
[From]="/var/spool/postfix/from"
[Bcc]="/var/spool/postfix/bcc"
[Ndr]="/var/spool/postfix/ndr"
)
# 出力ファイル
OUTPUT_FILE="postfix_perf_metrics.csv"
# CSVヘッダ出力(初回のみ)
f_initialize_output_file() {
if [ ! -f "$OUTPUT_FILE" ]; then
printf "%-25s" "Date"
for _category in "${!CATEGORY_DIRS[@]}"; do
printf ",%6s" "$_category"
done
printf "\n" >> "$OUTPUT_FILE"
fi
}
# 各カテゴリのファイル数を収集(並列)
f_collect_counts() {
local _tmpdir _key _dir
_tmpdir=$(mktemp -d)
for _key in "${!CATEGORY_DIRS[@]}"; do
_dir="${CATEGORY_DIRS[$_key]}"
find "$_dir" -type f | wc -l > "$_tmpdir/$_key" &
done
wait
declare -gA CATEGORY_COUNTS
for _key in "${!CATEGORY_DIRS[@]}"; do
CATEGORY_COUNTS["$_key"]=$(<"$_tmpdir/$_key")
done
rm -r "$_tmpdir"
}
# CSV出力
f_output_metrics() {
local _timestamp="$1" _key
printf "%-25s" "$_timestamp"
for _key in "${!CATEGORY_DIRS[@]}"; do
printf ",%6d" "${CATEGORY_COUNTS[$_key]}"
done
printf "\n" >> "$OUTPUT_FILE"
}
# 次の「分の00秒」まで sleep
f_wait_until_next_minute() {
local _now_sec _next_minute _sleep_sec
_now_sec=$(date +%s)
_next_minute=$(( (_now_sec / 60 + 1) * 60 ))
_sleep_sec=$(( _next_minute - _now_sec ))
sleep "$_sleep_sec"
}
# メインループ
f_main_loop() {
while true; do
local _now
_now=$(date -Iseconds)
f_collect_counts
f_output_metrics "$_now"
f_wait_until_next_minute
done
}
# 実行
f_initialize_output_file
f_main_loop
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme