Was monitor script

YOBY·2024년 1월 18일
0
  • SERVER 모니터링하면서 날짜로 로그파일 남기기
#!/bin/bash
#: Title        : server monitor
#: Description  : Server monitoring script

home_dir=$(dirname $(realpath "$0"))
server_name=$(hostname)

# run envar
log_dir=${home_dir}/logs
[[ -d ${log_dir} ]] || mkdir -p ${log_dir}

lock_dir=${home_dir}/lock
[[ -d ${lock_dir} ]] || mkdir -p ${lock_dir}

lock_file=${lock_dir}/$(basename $0)

#================================ FUNCTIONS ================================

function __start {
  # monitoring
  while true; do
    online_pid=$(pgrep -a java | awk '/bxm.instance.name=online/{ print $1 }')

    # time variables
    today=$(date '+%Y%m%d')
    current_time=$(date "+%m-%d %H:%M:%S")
    backup_day=$(date -d '-7 day' '+%Y%m%d')
    nowdt=$(date "+%m-%d %H:%M:%S")

    # log variables
    log_today="${home_dir}/was_monitor_${server_name}_${today}.log"
    log_rolling="${home_dir}/was_monitor_${server_name}_${backup_day}.log"

    [[ -f ${log_rolling} ]] && mv ${log_rolling} ${log_dir}

    cnt8103=$(netstat -natu | grep 'ESTABLISHED.*8103' | wc -l)
    cnt3306=$(netstat -natu | grep 'ESTABLISHED.*3306' | wc -l)

    wasutil=$(ps -p $online_pid -o %cpu,%mem --no-headers | awk '{ print "%CPU="$1",%MEM="$2 }')
    cpu_usage=$(top -b -n 1 | awk '/^%Cpu/ { print 100 - $8 }')
    mem_usage=$(free -h | awk 'NR == 2 { print $3"/"$2 }')

    if [ $cnt8103 -ge 300 ]; then
      cnt8103="$(tput setaf 1)$cnt8103$(tput setaf 9)"
    else
      cnt8103="$(tput setaf 2)$cnt8103$(tput setaf 9)"
    fi

    if [ $cnt3306 -ge 60 ]; then
      cnt3306="$(tput setaf 1)$cnt3306$(tput setaf 9)"
    else
      cnt3306="$(tput setaf 2)$cnt3306$(tput setaf 9)"
    fi

    log_msg="[$(tput setaf 2)${server_name}$(tput setaf 9) $nowdt] 8103/tcp($cnt8103) 3306/tcp($cnt3306) Onl($wasutil) SYSTEM(%CPU=${cpu_usage},MEM=${mem_usage})"
    echo ${log_msg} >> ${log_today}
    sleep 10
  done
}

function __stop {
  if [ -f ${lock_file} ]; then
    proc=$(cat ${lock_file})
    echo "PID=${proc}"
    if [ -n "$(ps --no-headers -f ${proc})" ]; then
      echo "$0 is running."
      echo "Stopping $0..."
      rm -f "${lock_file}"
      kill -9 ${proc}
      echo "Stopped $0"
    else
      echo "${lock_file} exists, but process not found"
    fi
  else
    echo "$0 is not running."
  fi
}

function __status {
  if [ -s "${lock_file}" ]; then
    proc=$(cat ${lock_file})
    if [ -n "$(ps --no-headers -f ${proc})" ]; then
      ps --no-headers -f ${proc}
      echo "$0 is running."
    else
      echo "${lock_file} exists, but process was not found"
    fi
  else
    echo "$0 is not running."
  fi
}

#================================== MAIN ==================================

case "$1" in
  start)
    # process locking
    if [ -f ${lock_file} ]; then
      proc=$(cat ${lock_file})
      # check process
      if [ -n "$(ps --no-headers -f ${proc})" ]; then
        echo "$0 is already running."
        exit 1
      else
        echo "${lock_file} exists, but process was not found"
        exit 1
      fi
    fi
    __start &
    echo $! > ${lock_file}
    ;;
  
  stop)
    __stop
    rm -f ${lock_file}
    ;;
  
  status)
    __status
    ;;
  
  *)
    echo "Usage: $0 {start|stop|status}"
    ;;
esac

0개의 댓글