간단히 서버 리소스 체크 및 모니터링 할때 사용했던 스크립트
요즘은 모니터링 등 좋은게 많이 나왔지만 단건 서버나 부하 체크 등이 필요할때 간혹 유용하게 썼던 스크립트 입니다.
더보기
#!/bin/bash # title : Server monitoring script # Description : System resource monitoring ##################### # Value ##################### today=$(date +%Y-%m-%d) cpu_limit="90" cpu_tmp_limit="90" memory_limit="90" disk_limit="90" disk_each_limit="90" log_path="/root/backup/log" ##################### export LANG=en mkdir -p ${log_path} function _CHECK_SETTING() { CHK_TMP=$(rpm -qa | egrep "lm_sensors-" | wc -l) if [ ${CHK_TMP} == "0" ] then yum install -y lm_sensors yes "" | sensors-detect fi } function _CPU_CHECK() { CPU_PERCENT=$(top -b -n 1 | grep -i cpu\(s\)| awk '{print $5}' | sed s/[^0-9]//g) if [ "${CPU_PERCENT}" -ge ${cpu_limit} ]; then touch ${log_path}/${today}_CPU.log echo "CPU USED HIGH COUNT !! ${CPU_PERCENT}" date >> ${log_path}/${today}_CPU.log core_list=$(sensors | egrep "Core" | awk '{print $2}') for core_num in ${core_list} do temp=$(sensors | egrep "Core ${core_num}" | awk '{print $3}' | awk -F"." '{print $1}' | sed s/[^0-9]//g) echo "Core ${core_num} temperature ${temp}" echo "CPU TEMPERATURE HIGH COUNT !! ${core_num} temperature ${temp}" date >> ${log_path}/${today}_CPU.log done #empty command line else echo "Cpu Used ${CPU_PERCENT}" fi } function _MEM_CHECK() { ## Memory Usage : MemTotal - MemFree - Buffers - Cached ## Memory Free : MemFree + Buffers + Cached MEM_INFO=$(cat /proc/meminfo | egrep "^MemTotal:|^MemFree:|^Buffers:|^Cached:|^Swap") MEM_TOTAL=$(echo ${MEM_INFO} | awk '{print $2}') MEM_USAGE=$(echo ${MEM_INFO} | awk '{print ($2-$5-$8-$11)}') PER_MEM_USAGE=$(expr ${MEM_USAGE} \* 100 \/ ${MEM_TOTAL}) if [ "${PER_MEM_USAGE}" -ge ${memory_limit} ]; then touch ${log_path}/${today}_MEMORY.log echo "MEMORY USED HIGH COUNT !! ${PER_MEM_USAGE}" date >> ${log_path}/${today}_MEMORY.log #empty command line else echo "mem ${PER_MEM_USAGE}" fi } function _DISK_CHECK() { DISK_TOTAL=`df -P | grep -v ^Filesystem | awk '{sum += $2} END {print sum;}'` DISK_USED=`df -P | grep -v ^Filesystem | awk '{sum += $3} END {print sum;}'` DISK_PERCENT=$((100*$DISK_USED/$DISK_TOTAL)) echo "disk ${DISK_PERCENT}" } function _DISK_EACH_CHECK() { _DISK_CHECK if [ "${DISK_PERCENT}" -ge ${disk_limit} ]; then touch ${log_path}/${today}_DISK.log echo "DISK USED HIGH COUNT !! ${DISK_PERCENT}" date >> ${log_path}/${today}_DISK.log disk_list=$(cat /proc/mounts | egrep "^/dev" | awk '{print $1}' | sed 's/[0-9]//' | sed -e 's;^.*/;;' | sort -u) for disk_used in ${disk_list} do DISK_TOTAL=`df -P | grep -v ^Filesystem | awk '{sum += $2} END {print sum;}'` DISK_USED=`df -P | grep -v ^Filesystem | awk '{sum += $3} END {print sum;}'` DISK_PERCENT=$((100*$DISK_USED/$DISK_TOTAL)) echo "DISK EACH USED HIGH COUNT !! ${disk_used} ${DISK_PERCENT}" date >> ${log_path}/${today}_DISK.log done #empty command line fi } function _MAIN() { _CHECK_SETTING _CPU_CHECK _MEM_CHECK #_DISK_CHECK _DISK_EACH_CHECK } _MAIN |
'Language > Shell scripting' 카테고리의 다른 글
[Shell Scripting] 재부팅 전 현재 서버 정보 수집을 위한 스크립트 (0) | 2023.03.14 |
---|---|
[Shell Scripting] 불필요 로그 제거 (0) | 2023.01.29 |
댓글