info_distro.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #!/bin/bash
  2. # LinuxGSM info_distro.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.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. # Distro Name - Ubuntu 16.04 LTS
  14. # Distro Version - 16.04
  15. # Distro ID - ubuntu
  16. # Distro Codename - xenial
  17. # Gathers distro info from various sources filling in missing gaps
  18. distro_info_array=( os-release lsb_release hostnamectl debian_version redhat-release )
  19. for distro_info in "${distro_info_array[@]}"
  20. do
  21. if [ -f "/etc/os-release" ]&&[ "${distro_info}" == "os-release" ]; then
  22. distroname=$(grep PRETTY_NAME /etc/os-release | sed 's/PRETTY_NAME=//g' | tr -d '="' | sed 's/\"//g')
  23. distroversion=$(grep VERSION_ID /etc/os-release | sed 's/VERSION_ID=//g' | sed 's/\"//g')
  24. distroid=$(grep ID /etc/os-release | grep -v _ID | grep -v ID_ | sed 's/ID=//g' | sed 's/\"//g')
  25. distrocodename=$(grep VERSION_CODENAME /etc/os-release | sed 's/VERSION_CODENAME=//g' | sed 's/\"//g')
  26. elif [ -n "$(command -v lsb_release 2>/dev/null)" ]&&[ "${distro_info}" == "lsb_release" ]; then
  27. if [ -z "${distroname}" ];then
  28. distroname="$(lsb_release -sd)"
  29. elif [ -z "${distroversion}" ];then
  30. distroversion="$(lsb_release -sr)"
  31. elif [ -z "${distroid}" ];then
  32. distroid=$(lsb_release -si)
  33. elif [ -z "${distrocodename}" ];then
  34. distrocodename=$(lsb_release -sc)
  35. fi
  36. elif [ -n "$(command -v hostnamectl 2>/dev/null)" ]&&[ "${distro_info}" == "hostnamectl" ]; then
  37. if [ -z "${distroname}" ];then
  38. distroname="$(hostnamectl | grep "Operating System" | sed 's/Operating System: //g')"
  39. fi
  40. elif [ -f "/etc/debian_version" ]&&[ "${distro_info}" == "debian_version" ]; then
  41. if [ -z "${distroname}" ];then
  42. distroname="Debian $(cat /etc/debian_version)"
  43. elif [ -z "${distroversion}" ];then
  44. distroversion="$(cat /etc/debian_version)"
  45. elif [ -z "${distroid}" ];then
  46. distroid="debian"
  47. fi
  48. elif [ -f "/etc/redhat-release" ]&&[ "${distro_info}" == "redhat-release" ]; then
  49. if [ -z "${distroname}" ];then
  50. distroname=$(cat /etc/redhat-release)
  51. elif [ -z "${distroversion}" ];then
  52. distroversion=$(rpm -qa \*-release | grep -Ei "oracle|redhat|centos|fedora" | cut -d"-" -f3)
  53. elif [ -z "${distroid}" ];then
  54. distroid="$(awk '{print $1}' /etc/redhat-release)"
  55. fi
  56. fi
  57. done
  58. ## Glibc version
  59. # e.g: 1.17
  60. glibcversion="$(ldd --version | sed -n '1s/.* //p')"
  61. ## tmux version
  62. # e.g: tmux 1.6
  63. if [ -z "$(command -V tmux 2>/dev/null)" ]; then
  64. tmuxv="${red}NOT INSTALLED!${default}"
  65. else
  66. if [ "$(tmux -V|sed "s/tmux //" | sed -n '1 p' | tr -cd '[:digit:]')" -lt "16" ] 2>/dev/null; then
  67. tmuxv="$(tmux -V) (>= 1.6 required for console log)"
  68. else
  69. tmuxv=$(tmux -V)
  70. fi
  71. fi
  72. ## Uptime
  73. uptime=$(</proc/uptime)
  74. uptime=${uptime/[. ]*/}
  75. minutes=$(( uptime/60%60 ))
  76. hours=$(( uptime/60/60%24 ))
  77. days=$(( uptime/60/60/24 ))
  78. ### Performance information
  79. ## Average server load
  80. load=$(uptime|awk -F 'load average: ' '{ print $2 }')
  81. ## Memory information
  82. # Issue #2005 - Kernel 3.14+ contains MemAvailable which should be used. All others will be calculated
  83. # get the raw KB values of these fields
  84. physmemtotalkb=$(grep MemTotal /proc/meminfo | awk '{print $2}')
  85. physmemfreekb=$(grep ^MemFree /proc/meminfo | awk '{print $2}')
  86. physmembufferskb=$(grep ^Buffers /proc/meminfo | awk '{print $2}')
  87. physmemcachedkb=$(grep ^Cached /proc/meminfo | awk '{print $2}')
  88. physmemreclaimablekb=$(grep ^SReclaimable /proc/meminfo | awk '{print $2}')
  89. # check if MemAvailable Exists
  90. if grep -q ^MemAvailable /proc/meminfo; then
  91. physmemactualfreekb=$(grep ^MemAvailable /proc/meminfo | awk '{print $2}')
  92. else
  93. physmemactualfreekb=$((${physmemfreekb}+${physmembufferskb}+${physmemcachedkb}))
  94. fi
  95. # Available RAM and swap.
  96. physmemtotalmb=$((${physmemtotalkb}/1024))
  97. physmemtotal=$(numfmt --to=iec --from=iec --suffix=B "${physmemtotalkb}K")
  98. physmemfree=$(numfmt --to=iec --from=iec --suffix=B "${physmemactualfreekb}K")
  99. physmemused=$(numfmt --to=iec --from=iec --suffix=B "$((${physmemtotalkb}-${physmemfreekb}-${physmembufferskb}-${physmemcachedkb}-${physmemreclaimablekb}))K")
  100. physmemavailable=$(numfmt --to=iec --from=iec --suffix=B "${physmemactualfreekb}K")
  101. physmemcached=$(numfmt --to=iec --from=iec --suffix=B "$((${physmemcachedkb}+${physmemreclaimablekb}))K")
  102. swaptotal=$(numfmt --to=iec --from=iec --suffix=B "$(grep ^SwapTotal /proc/meminfo | awk '{print $2}')K")
  103. swapfree=$(numfmt --to=iec --from=iec --suffix=B "$(grep ^SwapFree /proc/meminfo | awk '{print $2}')K")
  104. swapused=$(numfmt --to=iec --from=iec --suffix=B "$(($(grep ^SwapTotal /proc/meminfo | awk '{print $2}')-$(grep ^SwapFree /proc/meminfo | awk '{print $2}')))K")
  105. ### Disk information
  106. ## Available disk space on the partition.
  107. filesystem=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $1}')
  108. totalspace=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $2}')
  109. usedspace=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $3}')
  110. availspace=$(df -hP "${rootdir}" | grep -v "Filesystem" | awk '{print $4}')
  111. ## LinuxGSM used space total.
  112. rootdirdu=$(du -sh "${rootdir}" 2> /dev/null | awk '{print $1}')
  113. if [ -z "${rootdirdu}" ]; then
  114. rootdirdu="0M"
  115. fi
  116. ## LinuxGSM used space in serverfiles dir.
  117. serverfilesdu=$(du -sh "${serverfiles}" 2> /dev/null | awk '{print $1}')
  118. if [ -z "${serverfilesdu}" ]; then
  119. serverfilesdu="0M"
  120. fi
  121. ## LinuxGSM used space total minus backup dir.
  122. rootdirduexbackup=$(du -sh --exclude="${backupdir}" "${serverfiles}" 2> /dev/null | awk '{print $1}')
  123. if [ -z "${rootdirduexbackup}" ]; then
  124. rootdirduexbackup="0M"
  125. fi
  126. ## Backup info
  127. if [ -d "${backupdir}" ]; then
  128. # Used space in backups dir.
  129. backupdirdu=$(du -sh "${backupdir}" | awk '{print $1}')
  130. # If no backup dir, size is 0M
  131. if [ -z "${backupdirdu}" ]; then
  132. backupdirdu="0M"
  133. fi
  134. # number of backups set to 0 by default
  135. backupcount=0
  136. # If there are backups in backup dir.
  137. if [ "$(find "${backupdir}" -name "*.tar.gz" | wc -l)" -ne "0" ]; then
  138. # number of backups.
  139. backupcount=$(find "${backupdir}"/*.tar.gz | wc -l)
  140. # most recent backup.
  141. lastbackup=$(find "${backupdir}"/*.tar.gz | head -1)
  142. # date of most recent backup.
  143. lastbackupdate=$(date -r "${lastbackup}")
  144. # no of days since last backup.
  145. lastbackupdaysago=$(( ( $(date +'%s') - $(date -r "${lastbackup}" +'%s') )/60/60/24 ))
  146. # size of most recent backup.
  147. lastbackupsize=$(du -h "${lastbackup}" | awk '{print $1}')
  148. fi
  149. fi
  150. # External IP address
  151. if [ -z "${extip}" ]; then
  152. extip=$(${curlpath} -m 3 ifconfig.co > "${tmpdir}/extip.txt" 2>/dev/null)
  153. if [ $? -ne 0 ]; then
  154. if [ -f "${tmpdir}/extip.txt" ]; then
  155. echo "${tmpdir}/extip.txt"
  156. else
  157. echo "x.x.x.x"
  158. fi
  159. fi
  160. fi
  161. # Steam Master Server - checks if detected by master server
  162. if [ ! "$(command -v jq 2>/dev/null)" ]; then
  163. if [ "${ip}" ] && [ "${port}" ]; then
  164. if [ "${engine}" == "source" ]||[ "${engine}" == "goldsource" ]||[ "${shortname}" == "jc2" ]||[ "${shortname}" == "ql" ]; then
  165. masterserver=$(${curlpath} -s 'https://api.steampowered.com/ISteamApps/GetServersAtAddress/v0001?addr='${ip}':'${port}'&format=json' | jq '.response.servers[]|.addr' | wc -l)
  166. if [ "${steammaster}" == "1" ]; then
  167. masterserver="true"
  168. else
  169. masterserver="false"
  170. fi
  171. fi
  172. fi
  173. fi