[Linux]Daily Health Check Script

제가 서버 상태를 체크하기 위해 실행하는 Shell Script입니다. “sysinfo.sh”을 crontab에 등록해서 메일 sysinfo.$(hostname).log를 생성시키고 있습니다. Postfix를 사용해서 이메일로 받아보고 있는데, Postfix 설정은 다음 Blog에서 소개시켜 드릴께요.

  1. 아래 스크립트를 “sysinfo.sh” 파일에 저장합니다.
    #!/bin/bash
    
    # make seperators
    P=80
    S=$(printf '*%.s' {1..80})
    D=$(printf -- '-%.s' {1..80})
    
    function printTitleBox () {
    echo "$S"
    
    while [ $# -ne 0 ]
    do
    title=$1
    n=$((P / 2 - ${#title} / 2))
    m=$((P - n - ${#title}))
    
    printf "%-${n}s" "**"
    printf "$title"
    printf "%+${m}s\n" "**"
    shift
    done
    
    echo "$S"
    }
    
    # only print the header of output via pipeline
    function printHeaderOnly {
    IFS= read -r header
    printf '%s\n' "$header"
    echo "$D"
    "$@"
    }
    
    # get date
    NOW=$(date +"%Y-%m-%d")
    
    # create output file name
    OUTPUT=$(basename -s .sh $0)
    OUTPUT=$(dirname $0)"/${OUTPUT}.$(hostname).log"
    if ! [ -f $OUTPUT ]
    then
    touch $OUTPUT
    fi
    
    # Assign the fd 3 to $OUTPUT file
    exec 3> $OUTPUT
    
    printTitleBox "System Info for $(hostname) at ${NOW}" "(Updated by Archer of www.yesxyz.kr, 2023)" >&3
    
    echo "" >&3
    printTitleBox "Operating System Info" >&3
    hostnamectl >&3
    
    echo "" >&3
    printTitleBox "NTP and time configuration" >&3
    timedatectl >&3
    
    #uncomment if you run this script on a server for the first time
    #echo "" >&3
    #printTitleBox "Diplay information about the CPU architecture" >&3
    #lscpu | grep -i -v "flags" >&3
    
    echo "" >&3
    printTitleBox "Amount Of Free And Used Memory" >&3
    echo -e "CPU Usage:\t"`cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1` >&3
    avail=`cat /proc/meminfo | grep MemAvailable | awk '{print $2}'`
    total=`cat /proc/meminfo | grep MemTotal | awk '{print $2}'`
    usage=$((total - avail))
    echo -e "Memory Usage:\t"`echo "$usage $total" | awk '{printf("%.2f%"), $1/$2*100}'` >&3
    echo -e "Swap Usage:\t"`free | awk '/Swap/{printf("%.2f%"), $3/$2*100}'` >&3
    echo "$D" >&3
    lsmem | tail -3 >&3
    echo "$D" >&3
    free -h >&3
    #printf "%+${P}s\n" "(unit: mib)" >&3
    #vmstat -S m 3 5 >&3
    
    echo "" >&3
    printTitleBox "File System Disk Space Usage" >&3
    df -hT -x tmpfs -x devtmpfs -x iso9660 -x nfs | printHeaderOnly sort -nr -k 6 >&3
    echo "$D" >&3
    df -hT --total -x tmpfs -x devtmpfs -x iso9660 -x nfs | tail -1 >&3
    
    echo "" >&3
    printTitleBox "Top 5 Memory Eating Process" >&3
    ps -eo user,pid,ppid,%mem,%cpu,stat,start,time,comm --sort=-%mem | egrep -v top | head -6 >&3
    
    echo "" >&3
    printTitleBox "Top 5 CPU Eating Process" >&3
    ps -eo user,pid,ppid,%mem,%cpu,stat,start,time,comm --sort=-%cpu | egrep -v top | head -6 >&3
    
    echo "" >&3
    printTitleBox "Zombie Process Check" >&3
    top -bn 1 | grep zombie >&3
    ps -elf | grep defunct | grep -v grep >&3
    
    echo "" >&3
    printTitleBox "User Login History(Recent 5)" >&3
    last -5 -F >&3
    
    echo "" >&3
    printTitleBox "Users Failed to log in" >&3
    lastb -5 -F >&3
    
    echo "" >&3
    printTitleBox "Diagnostic Message" >&3
    dmesg -T -l emerg,alert,crit,err | tail -n 20 >&3
    
    echo "" >&3
    printTitleBox "Journal Database Log" >&3
    journalctl -p warning --since yesterday >&3
    
    exec 3>&-
    
  2. chmod 755 sysinfo.sh 명령으로  실행권한을 부여합니다.
  3. “sysinfo.sh” 파일을 실행시켜 “sysinfo.$(hostname).log” 파일이 생성되는 것을 확인합니다.
  4. crontab -e 명령으로 cron에 등록해서 주기적으로 실행시키도록 합니다(매일 오전 7시 실행)
    [root@centos8 ~]# crontab -e
    0 7 * * * /path/sysinfo.sh
    

You may also like...

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다