Logo

  • Register
  • | Login
  • Facebook
  • Twitter
  • Gplus
  • Linkedin

Our forum Area

Page 1 of 2 12 LastLast
Results 1 to 15 of 23

This is a discussion on Server Memory Usage (script) in the Dedicated Servers forum
Ever wondered how much memory you are currently using and how close you are to the QoS hard limit? Here is a simple script that ...

  1. #1
    Loyal Client thisisit3's Avatar
    Join Date
    Mar 2007
    Posts
    725

    Server Memory Usage (script)

    Ever wondered how much memory you are currently using and how close you are to the QoS hard limit?

    Here is a simple script that calculates the QoS hard memory limit and the current memory usage:

    Code:
    #!/bin/sh
    
    # Make sure we are running under a VPS system with QoS
    if [ ! -f "/proc/user_beancounters" ]; then
    	echo "File \"/proc/user_beancounters\" does not exist. Make sure this script runs on a VPS system with QoS enabled."
    	exit $E_NOFILE
    fi
    
    # Get memory related variables
    let GMEM=`grep vmguarpages /proc/user_beancounters | awk '{print$4}'`/1024*4
    let BRST=`grep privvmpages /proc/user_beancounters | awk '{print$5}'`/1024*4
    let CMEM=`grep physpages /proc/user_beancounters | awk '{print$2}'`/1024*4
    let REQM=`grep privvmpages /proc/user_beancounters | awk '{print$2}'`/1024*4
    let FREE=$GMEM-$CMEM
    
    # Calculate free memory
    if [ "$FREE" -gt "0" ]; then
    	let CMEMP=$CMEM*100/$GMEM
    	let FREEP=100-$CMEMP
    else
    	let CMEMP="100"
    	let FREEP="0"
    fi
    
    let REQMP=$REQM*100/$GMEM
    
    # Calculate pretty output
    if [ ${#CMEM} -gt ${#FREE} ]; then
    	let LIMIT=${#CMEM}-${#FREE}
    	SPACE=''
    	while [ $LIMIT -gt 0 ]; do
    		SPACE=" $SPACE"
    		LIMIT=`expr $LIMIT - 1`
    	done
    else
    	SPACE=''
    fi
    
    # Echo results
    echo "Memory limit...: $GMEM MB (burstable: $BRST MB)"
    echo "Requested......: $REQM MB (${REQMP}%)"
    echo "Current usage..: $CMEM MB (${CMEMP}%)"
    echo "Free memory....: $FREE MB ${SPACE}(${FREEP}%)"
    Create the directory ~/bin, then save the above code as "vmem" inside that directory and "chmod +x ~/bin/vmem" to make it executable. Now edit your ~/.bashrc, and add "~/bin/vmem" at the end, thus you'll get a memory report every time you login, or every time you type "vmem" in the shell.

    v5 - New: Better display of requested memory usage
    v4 - New: check for VPS, clean code, comments
    v3 - New: adds 'bustable' & 'requested' memory...
    v2 - New: adds 'free' memory and percentages...

  2. #2
    Loyal Client
    Join Date
    Jun 2006
    Posts
    39
    Very usefull, thanks!

  3. #3
    Loyal Client
    Join Date
    Apr 2006
    Location
    Sweden
    Posts
    262
    Code:
    #!/bin/sh
    
    let GMEM=`grep vmguarpages /proc/user_beancounters | awk '{print$4}'`/1024*4
    let CMEM=`grep physpages /proc/user_beancounters | awk '{print$2}'`/1024*4
    let FMEM=$GMEM-$CMEM
    
    echo "Memory limit...: $GMEM MB"
    echo "Current usage..: $CMEM MB"
    echo "Free memory..: $FMEM MB"
    Will display free memory (if you like me can't do simple math in head heh) aswell
    My VPS server:
    www.myfedoraserver.com


  4. #4
    Loyal Client
    Join Date
    Sep 2002
    Posts
    640
    It must be VPS specific as I dont recall such entry under /proc
    For dedicated server users I would suggest using sa command instead.

  5. #5

  6. #6

  7. #7
    Loyal Client thisisit3's Avatar
    Join Date
    Mar 2007
    Posts
    725
    Spent some more time on the script and added a few more stuff: burstable memory, requested memory

  8. #8
    Loyal Client ethix's Avatar
    Join Date
    Jul 2007
    Location
    Australia
    Posts
    102
    great script mate

    made me realize i have allot of unused memory

  9. #9
    Loyal Client
    Join Date
    Sep 2002
    Posts
    640
    Quote Originally Posted by ethix View Post
    made me realize i have allot of unused memory

    You might want to make sure your memory utilization remains low at all times before jumping to conclusions

    BTW, here is an interesting article about monitoring server performance (should work for VPS as well)
    http://www.linuxhomenetworking.com/w..._P erformance

  10. #10
    Loyal Client
    Join Date
    Apr 2006
    Location
    Sweden
    Posts
    262
    Requested means? Heh

    Get additions! Thank you!
    My VPS server:
    www.myfedoraserver.com


  11. #11
    Loyal Client thisisit3's Avatar
    Join Date
    Mar 2007
    Posts
    725
    what is "requested" or otherwise also know as "allocated" (but not "used") memory?

    here is an example i've used in the past:

    an application starts like:

    - i've started, give me 100kb of memory
    - good, now store XYZ data of 10kb size in that memory

    that results in:

    - 100kb in allocated (requested) memory
    - 10kb in used memory

    see the difference?

    allocated/requested memory is not your USED memory!

  12. #12
    Loyal Client
    Join Date
    Oct 2007
    Posts
    7
    Hi,

    Looks like an awesome script you have here. I would love to be able to use this but I am a SSH noob. I just upgraded from a shared account and haven't really learned any of the cool stuff yet.

    I hate to ask for extremely easy commands but is there a way you could step by step give instructions on how to use this code?

    I use the SSH terminal in Plesk and I can get to the /bin directory but I don't know how to save the code. Another step that looks confusing is the editing my bash to add the vmem as an executable....

    Any help is well appreciated.


    Joe R.

  13. #13
    Loyal Client thisisit3's Avatar
    Join Date
    Mar 2007
    Posts
    725
    first check that your /root/bin is in the path by:

    Code:
    # cat /root/.bash_profile
    
    PATH=$PATH:$HOME/bin
    export PATH
    you should be able to see $HOME/bin in the output somewhere, if its not then add the above PATH= and export lines in that file. At this point you must learn how to use a text editor like "pico" or "vi".

    now make sure you have a /root/bin directory with:

    Code:
    mkdir /root/bin
    Then change to that directory and create a file named "vmem" and copy/paste the entire code of the script in there (use your text editor again). Save it and close it, next you need to make the file executable:

    Code:
    chmod +x /root/bin/vmem
    Thats about it, you should be able to type "vmem" at any point in the shell.

  14. #14
    Loyal Client
    Join Date
    Oct 2007
    Posts
    7
    Thanks for the help but I ram into some problems...

    Step 1 cat /root/.bash_profile
    gives me permission denied

    I tried su and all the passwords I ever set for this server and none work. I am using the SST built into plesk. Is that a problem?

    I know I didnt get this far but in terms of using pico.... how?

    I can get to root/bin no problem I didnt have to make it. I dont know how to create a file and what extension to give it.

  15. #15
    Loyal Client thisisit3's Avatar
    Join Date
    Mar 2007
    Posts
    725
    You are probably connected as a plain user and not as root, i'd suggest you open a ticket with support and let them explain to you whats wrong, or open a new thread since this discussion is irrelevant to the memory script.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •