메뉴 건너뛰기

Korea Oracle User Group

OS

Linux Memory Usage 확인용 스크립트

명품관 2019.04.15 16:32 조회 수 : 37

Memory Usage 확인용 스크립트

 

출처 : https://fritshoogland.wordpress.com/2017/07/25/linux-memory-usage/

 

유용한 스크립트라 퍼 옵니다.

 

아래의 공식으로 Total을 계산합니다.

 

MemFree
+ KernelStack
+ Buffers
+ PageTables
+ AnonPages
+ Slab
+ SwapCached
+ Cached – Shmem
+ Shmem
+ HugePages used (HugePages_Total-Hugepages_Free)*Hugepagesize
+ Hugepages rsvd (Hugepages_Rsvd*Hugepagesize)
+ Hugepages free (Hugepages_Free-Hugepages_Rsvd)*Hugepagesize
————————————————————-
= Approximate total memory usage

현재 정보 확인용으로 사용시

 

testora.localdomain> sh memstat.sh --oneshot
Free                 519056
Shmem                732916
Mapped+Cached        171364
Anon                1180032
Pagetables            95848
KernelStack            7376
Buffers              111172
Slab                 126284
SwpCache              24800
HP Used                   0
HP Rsvd                   0
HP Free                   0
Unknown               45472 (  1%)
Total memory        3014320
---------------------------
Total swp           4194300
Used swp            1575956 ( 37%)

 

아래는 모니터링시 메모리 사용양에 대해 관제용으로 사용하면 될 거 같습니다.

 

testora.localdomain> sh memstat.sh 
          Free          Shmem  Mapped+Cached           Anon     Pagetables    KernelStack        Buffers           Slab       SwpCache        HP Used        HP Rsvd        HP Free        Unknown   %
        518800         732916         171316        1179888          95848           7376         111084         126520          24800              0              0              0          45772   1
        518092         732916         171308        1179720          95964           7408         111092         126580          24800              0              0              0          46440   1
        518356         732916         171316        1180088          95964           7408         111092         126596          24800              0              0              0          45784   1
        510876         732916         171316        1185444          97928           7440         111100         126576          24800              0              0              0          45924   1
        510852         732916         171316        1185808          97928           7440         111100         126584          24800              0              0              0          45576   1
        510664         732916         171316        1185664          97932           7440         111100         126616          24800              0              0              0          45872   1
        510308         732916         171316        1185792          97924           7440         111108         126656          24800              0              0              0          46060   1
        502696         732916         171320        1190992         100208           7456         111108         126812          24800              0              0              0          46012   1
        502828         732916         171320        1190776         100204           7456         111108         126804          24800              0              0              0          46108   1
        502672         732916         171320        1191140         100208           7456         111112         126856          24800              0              0              0          45840   1
        502796         732916         171320        1190776         100212           7456         111112         126904          24800              0              0              0          46028   1
        502712         732916         171320        1191124         100204           7456         111116         126820          24800              0              0              0          45852   1
        502604         732916         171320        1190992         100204           7456         111116         126772          24800              0              0              0          46140   1
^C

 

memstat.sh 내용입니다.

 

#!/bin/bash
#set -x
[ "$1" = '--oneshot' ] && ONESHOT="yes" || ONESHOT="no"
PAGESIZE=30
SLEEP_TIME=3
[ $ONESHOT = "no" ] && printf "          Free          Shmem  Mapped+Cached           Anon     Pagetables    KernelStack        Buffers           Slab       SwpCache        HP Used        HP Rsvd        HP Free        Unknown   %%\n"
ROWCOUNTER=0
while ( true ); do
  # 1 get meminfo
  MEMINFO=$( cat /proc/meminfo )
  # 2 get relevant data
  MEMTOTAL=$( echo "$MEMINFO" | awk '/^MemTotal/ { print $2 }')
  MEMFREE=$( echo "$MEMINFO" | awk '/^MemFree/ { print $2 }')
  KERNELSTACK=$( echo "$MEMINFO" | awk '/^KernelStack/ { print $2 }')
  BUFFERS=$( echo "$MEMINFO" | awk '/^Buffers/ { print $2 }')
  CACHED=$( echo "$MEMINFO" | awk '/^Cached/ { print $2 }')
  PAGETABLES=$( echo "$MEMINFO" | awk '/^PageTables/ { print $2 }')
  SHMEM=$( echo "$MEMINFO" | awk '/^Shmem/ { print $2 }')
  ANONPAGES=$( echo "$MEMINFO" | awk '/^AnonPages/ { print $2 }')
  SLAB=$( echo "$MEMINFO" | awk '/^Slab/ { print $2 }')
  SWAPCACHED=$( echo "$MEMINFO" | awk '/^SwapCached/ { print $2 }')
  # hugepages are expressed in pages, not KB
  HP_TOTAL=$( echo "$MEMINFO" | awk '/^HugePages_Total/ { print $2 }')
  HP_RSVD=$( echo "$MEMINFO" | awk '/^HugePages_Rsvd/ { print $2 }')
  HP_FREE=$( echo "$MEMINFO" | awk '/^HugePages_Free/ { print $2 }')
  HP_SIZE=$( echo "$MEMINFO" | awk '/^Hugepagesize/ { print $2 }')
  # swap
  SWAP_TOTAL=$( echo "$MEMINFO" | awk '/^SwapTotal/ { print $2 }')
  SWAP_FREE=$( echo "$MEMINFO" | awk '/^SwapFree/ { print $2 }')
  # 3 define final data
  # MEMTOTAL
  # MEMFREE
  # KERNELSTACK
  # BUFFERS
  CACHED=$(( $CACHED-$SHMEM ))
  # PAGETABLES
  # SHMEM
  # ANONPAGES
  # SLAB
  # SWAPCACHED
  HP_USED_KB=$(( ($HP_TOTAL-$HP_FREE)*$HP_SIZE ))
  HP_RSVD_KB=$(( $HP_RSVD*$HP_SIZE ))
  HP_REALFREE_KB=$(( ($HP_FREE-$HP_RSVD)*$HP_SIZE ))
  SWAP_USED=$(( $SWAP_TOTAL-$SWAP_FREE ))
  SWAP_PCT=$(( ($SWAP_USED*100)/$SWAP_TOTAL ))
  #
  ACCT_FOR_MEM_TOTAL=$(( $MEMFREE+$KERNELSTACK+$BUFFERS+$CACHED+$PAGETABLES+$SHMEM+$MAPPED+$ANONPAGES+$SLAB+$SWAPCACHED+$HP_USED_KB+$HP_RSVD_KB+$HP_REALFREE_KB ))
  UNACCT_FOR=$(( $MEMTOTAL-$ACCT_FOR_MEM_TOTAL ))
  UA_PCT=$(( ($UNACCT_FOR*100)/$MEMTOTAL ))
  if [ $ONESHOT = "no" ]; then
    # visualize
    printf "%14d %14d %14d %14d %14d %14d %14d %14d %14d %14d %14d %14d %14d %3d\n" $MEMFREE $SHMEM $CACHED $ANONPAGES $PAGETABLES $KERNELSTACK $BUFFERS $SLAB $SWAPCACHED $HP_USED_KB $HP_RSVD_KB $HP_REALFREE_KB $UNACCT_FOR $UA_PCT
    sleep $SLEEP_TIME
    let ROWCOUNTER++
    if (( $ROWCOUNTER % $PAGESIZE == 0 )); then
      printf "          Free          Shmem  Mapped+Cached           Anon     Pagetables    KernelStack        Buffers           Slab       SwpCache        HP Used        HP Rsvd        HP Free        Unknown   %%\n"
    fi
  fi
  if [ $ONESHOT = "yes" ]; then
    printf "Free         %14d\nShmem        %14d\nMapped+Cached%14d\nAnon         %14d\nPagetables   %14d\nKernelStack  %14d\nBuffers      %14d\nSlab         %14d\nSwpCache     %14d\nHP Used      %14d\nHP Rsvd      %14d\nHP Free      %14d\nUnknown      %14d (%3d%%)\nTotal memory %14d\n---------------------------\nTotal swp    %14d\nUsed swp     %14d (%3d%%)\n" $MEMFREE $SHMEM $CACHED $ANONPAGES $PAGETABLES $KERNELSTACK $BUFFERS $SLAB $SWAPCACHED $HP_USED_KB $HP_RSVD_KB $HP_REALFREE_KB $UNACCT_FOR $UA_PCT $MEMTOTAL $SWAP_TOTAL $SWAP_USED $SWAP_PCT
    break
  fi
done

 

 

 

 

 

위로