0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

メモ

Last updated at Posted at 2025-05-23
#!/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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?