Advertisement
caitsith2

Feral hosting quota script

Mar 18th, 2020
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #!/bin/bash
  2. # This script outputs the available quota in a nice format.
  3. #
  4. # requirements: a file called .quotaspace in home directory
  5. # which contains the available diskspace in MB's, for example: 750000 for 750 GB
  6. #
  7. # Step 1:
  8. # echo -n 'size-in-MB' > ~/.quotaspace
  9. #
  10. # Step 2:
  11. # mkdir -p ~/bin
  12. # wget -qNO ~/bin/quota https://pastebin.com/raw/Prfx7cma
  13. # chmod +x ~/bin/quota
  14. # source ~/.bashrc && source ~/.profile
  15. #
  16. # Step 3:
  17. # Now running `quota` in SSH will give you your disk quota
  18. #
  19. # Credits:
  20. # Written by: mcviruss
  21. # Contributions:
  22. # https://github.com/feralhosting/feralfilehosting/pull/5 (no longer exists)
  23. #
  24. # Pad a string so that it is the specified number of characters long
  25. # adds padding to the left, default path string is a space " "
  26. function pad_left() {
  27. input="$1";
  28. length="$2";
  29. pad_str="$3";
  30. if [ "$pad_str" == "" ]; then
  31. pad_str=" ";
  32. fi;
  33. if [ "${#input}" -lt "$length" ]; then
  34. for i in $(seq 1 $[length-${#input}]); do
  35. echo -n "$pad_str";
  36. done;
  37. fi;
  38. echo -n "$input";
  39. }
  40.  
  41. # Format a filesize
  42. # Input: number which represents MB's
  43. # Output: formatted filesize with 2 decimals, example: 10.50 MB
  44. function format_filesize() {
  45. if [ "$1" == "" ]; then
  46. awk '{x=$1;split("MB GB TB PB",type);for(i=5;y < 1;i--) { y = x / (10^(3*i)/1.0) }; printf "%.3f %s\n",y,type[i+2]; }';
  47. else
  48. echo "$1" | awk '{x=$1;split("MB GB TB PB",type);for(i=5;y < 1;i--) { y = x / (10^(3*i)/1.0) }; printf "%.3f %s\n",y,type[i+2]; }';
  49. fi;
  50. }
  51.  
  52. # Lookup used diskspace and available diskspace
  53. used=$(du -s --si -B 1MB $HOME/ | cut -f 1);
  54. quota=$(cat ~/.quotaspace);
  55. available=$(echo "$quota-$used" | bc );
  56. exceeded=$(echo "$used-$quota" | bc );
  57.  
  58. # Format results
  59. quota_fmt=$(echo "$quota" | format_filesize );
  60. used_fmt=$(echo "$used" | format_filesize );
  61. available_fmt=$(echo "$available" | format_filesize);
  62. if [ "$available" -lt "0" ]; then
  63. exceeded_fmt=$(echo "$exceeded" | format_filesize );
  64. else
  65. available_fmt=$(echo "$available" | format_filesize);
  66. fi;
  67.  
  68. # Output results
  69.  
  70. # Format header in purple with bold and underlined text
  71. echo -e "\033[1;35;4mDisk usage for user $(whoami)@$(hostname -f)\e[0m";
  72.  
  73. # Format first part as bold with cyan text
  74. echo -e "\033[36;1mHome:\e[00m $HOME";
  75. echo -e "\033[36;1mQuota:\e[00m $(pad_left "$quota_fmt" 17)";
  76. echo -e "\033[36;1mUsed:\e[00m $(pad_left "$used_fmt" 18)";
  77.  
  78. # Check if you are under or over your available diskspace
  79. if [ "$available" -lt "0" ]; then
  80. # Format amount in red
  81. echo -e "\033[36;1mExceeded by:\e[00m \e[00;31m$(pad_left "$exceeded_fmt" 11)\e[00m";
  82. else
  83. # Format amount in green
  84. echo -e "\033[36;1mAvailable:\e[00m \e[00;32m$(pad_left "$available_fmt" 13)\e[00m";
  85. fi;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement