info_distro.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/bash
  2. # LGSM info_distro.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://gameservermanagers.com
  5. # Description: Variables providing useful info on the Operating System such as disk and performace info.
  6. # Used for command_details.sh, command_debug.sh and alert.sh.
  7. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  8. ### Distro information
  9. ## Distro
  10. # Returns architecture, kernel and distro/os.
  11. arch=$(uname -m)
  12. kernel=$(uname -r)
  13. if [ -n "$(command -v lsb_release)" ]; then
  14. distroname=$(lsb_release -s -d)
  15. elif [ -f "/etc/os-release" ]; then
  16. distroname=$(grep PRETTY_NAME /etc/os-release | sed 's/PRETTY_NAME=//g' | tr -d '="')
  17. elif [ -f "/etc/debian_version" ]; then
  18. distroname="Debian $(cat /etc/debian_version)"
  19. elif [ -f "/etc/redhat-release" ]; then
  20. distroname=$(cat /etc/redhat-release)
  21. else
  22. distroname="$(uname -s) $(uname -r)"
  23. fi
  24. if [ -f "/etc/os-release" ]; then
  25. distroversion=$(grep VERSION_ID /etc/os-release | tr -cd '[:digit:]')
  26. elif [ -n "$(command -v yum)" ]; then
  27. distroversion=$(rpm -qa \*-release | grep -Ei "oracle|redhat|centos" | cut -d"-" -f3)
  28. fi
  29. ## Glibc version
  30. # e.g: 1.17
  31. glibcversion="$(ldd --version | sed -n '1s/.* //p')"
  32. ## tmux version
  33. # e.g: tmux 1.6
  34. if [ -z "$(command -v tmux)" ]; then
  35. tmuxv="${red}NOT INSTALLED!${default}"
  36. elif [ "$(tmux -V|sed "s/tmux //" | sed -n '1 p' | tr -cd '[:digit:]')" -lt "16" ]; then
  37. tmuxv="$(tmux -V) (>= 1.6 required for console log)"
  38. else
  39. tmuxv=$(tmux -V)
  40. fi
  41. ## Uptime
  42. uptime=$(</proc/uptime)
  43. uptime=${uptime/[. ]*/}
  44. minutes=$(( uptime/60%60 ))
  45. hours=$(( uptime/60/60%24 ))
  46. days=$(( uptime/60/60/24 ))
  47. ### Performance information
  48. ## Average server load
  49. load=$(uptime|awk -F 'load average: ' '{ print $2 }')
  50. ## Memory information
  51. # Available RAM and swap.
  52. # Older versions of free do not support -h option.
  53. if [ "$(free -h > /dev/null 2>&1; echo $?)" -ne "0" ]; then
  54. humanreadable="-m"
  55. else
  56. humanreadable="-h"
  57. fi
  58. physmemtotal=$(free ${humanreadable} | awk '/Mem:/ {print $2}')
  59. physmemtotalmb=$(free -m | awk '/Mem:/ {print $2}')
  60. physmemused=$(free ${humanreadable} | awk '/Mem:/ {print $3}')
  61. physmemfree=$(free ${humanreadable} | awk '/Mem:/ {print $4}')
  62. physmemcached=$(free ${humanreadable} | awk '/cache:/ {print $4}')
  63. if [ -z "${physmemcached}" ]; then
  64. physmemcached=$(free ${humanreadable} | awk '/Mem:/ {print $5}')
  65. fi
  66. swaptotal=$(free ${humanreadable} | awk '/Swap:/ {print $2}')
  67. swapused=$(free ${humanreadable} | awk '/Swap:/ {print $3}')
  68. swapfree=$(free ${humanreadable} | awk '/Swap:/ {print $4}')
  69. ### Disk information
  70. ## Available disk space on the partition.
  71. filesystem=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $1}')
  72. totalspace=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $2}')
  73. usedspace=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $3}')
  74. availspace=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $4}')
  75. ## LGSM used space total.
  76. rootdirdu=$(du -sh "${rootdir}" 2> /dev/null | awk '{print $1}')
  77. if [ -z "${rootdirdu}" ]; then
  78. rootdirdu="0M"
  79. fi
  80. ## LGSM used space in serverfiles dir.
  81. filesdirdu=$(du -sh "${filesdir}" 2> /dev/null | awk '{print $1}')
  82. if [ -z "${filesdirdu}" ]; then
  83. filesdirdu="0M"
  84. fi
  85. ## LGSM used space total minus backup dir.
  86. rootdirduexbackup=$(du -sh --exclude="${backupdir}" "${filesdir}" 2> /dev/null | awk '{print $1}')
  87. if [ -z "${rootdirduexbackup}" ]; then
  88. rootdirduexbackup="0M"
  89. fi
  90. ## Backup info
  91. if [ -d "${backupdir}" ]; then
  92. # Used space in backups dir.
  93. backupdirdu=$(du -sh "${backupdir}" | awk '{print $1}')
  94. # If no backup dir, size is 0M
  95. if [ -z "${backupdirdu}" ]; then
  96. backupdirdu="0M"
  97. fi
  98. # number of backups set to 0 by default
  99. backupcount=0
  100. # If there are backups in backup dir.
  101. if [ $(find "${backupdir}" -name "*.tar.gz" | wc -l) -ne "0" ]; then
  102. # number of backups.
  103. backupcount=$(find "${backupdir}"/*.tar.gz | wc -l)
  104. # most recent backup.
  105. lastbackup=$(ls -t "${backupdir}"/*.tar.gz | head -1)
  106. # date of most recent backup.
  107. lastbackupdate=$(date -r "${lastbackup}")
  108. # no of days since last backup.
  109. lastbackupdaysago=$(( ( $(date +'%s') - $(date -r "${lastbackup}" +'%s') )/60/60/24 ))
  110. # size of most recent backup.
  111. lastbackupsize=$(du -h "${lastbackup}" | awk '{print $1}')
  112. fi
  113. fi