info_distro.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #!/bin/bash
  2. # LinuxGSM info_distro.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Variables providing useful info on the Operating System such as disk and performace info.
  7. # Used for command_details.sh, command_debug.sh and alert.sh.
  8. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. ### Game Server pid
  10. if [ "${status}" == "1" ]; then
  11. gameserverpid="$(tmux -L "${socketname}" list-sessions -F "#{session_name} #{pane_pid}" | grep "^${sessionname} " | awk '{print $NF}')"
  12. if [ "${engine}" == "source" ]; then
  13. srcdslinuxpid="$(ps -ef | grep -v grep | grep "${gameserverpid}" | grep srcds_linux | awk '{print $2}')"
  14. elif [ "${engine}" == "goldsrc" ]; then
  15. hldslinuxpid="$(ps -ef | grep -v grep | grep "${gameserverpid}" | grep hlds_linux | awk '{print $2}')"
  16. fi
  17. fi
  18. ### Distro information
  19. ## Distro
  20. # Returns architecture, kernel and distro/os.
  21. arch="$(uname -m)"
  22. kernel="$(uname -r)"
  23. # Distro Name - Ubuntu 16.04 LTS
  24. # Distro Version - 16.04
  25. # Distro ID - ubuntu
  26. # Distro Codename - xenial
  27. # Gathers distro info from various sources filling in missing gaps.
  28. distro_info_array=(os-release lsb_release hostnamectl debian_version redhat-release)
  29. for distro_info in "${distro_info_array[@]}"; do
  30. if [ -f "/etc/os-release" ] && [ "${distro_info}" == "os-release" ]; then
  31. distroname="$(grep "PRETTY_NAME" /etc/os-release | awk -F\= '{gsub(/"/,"",$2);print $2}')"
  32. distroversion="$(grep "VERSION_ID" /etc/os-release | awk -F\= '{gsub(/"/,"",$2);print $2}')"
  33. # Special var for rhel like distros to removed point in number e.g 8.4 to just 8.
  34. distroversionrh="$(sed -nr 's/^VERSION_ID="([0-9]*).+?"/\1/p' /etc/os-release)"
  35. distroid="$(grep "ID=" /etc/os-release | grep -v _ID | awk -F\= '{gsub(/"/,"",$2);print $2}')"
  36. distroidlike="$(grep "ID_LIKE=" /etc/os-release | grep -v _ID | awk -F\= '{gsub(/"/,"",$2);print $2}')"
  37. distrocodename="$(grep "VERSION_CODENAME" /etc/os-release | awk -F\= '{gsub(/"/,"",$2);print $2}')"
  38. elif [ "$(command -v lsb_release 2> /dev/null)" ] && [ "${distro_info}" == "lsb_release" ]; then
  39. if [ -z "${distroname}" ]; then
  40. distroname="$(lsb_release -sd)"
  41. elif [ -z "${distroversion}" ]; then
  42. distroversion="$(lsb_release -sr)"
  43. elif [ -z "${distroid}" ]; then
  44. distroid="$(lsb_release -si)"
  45. elif [ -z "${distrocodename}" ]; then
  46. distrocodename="$(lsb_release -sc)"
  47. fi
  48. elif [ "$(command -v hostnamectl 2> /dev/null)" ] && [ "${distro_info}" == "hostnamectl" ]; then
  49. if [ -z "${distroname}" ]; then
  50. distroname="$(hostnamectl | grep "Operating System" | sed 's/Operating System: //g')"
  51. fi
  52. elif [ -f "/etc/debian_version" ] && [ "${distro_info}" == "debian_version" ]; then
  53. if [ -z "${distroname}" ]; then
  54. distroname="Debian $(cat /etc/debian_version)"
  55. elif [ -z "${distroversion}" ]; then
  56. distroversion="$(cat /etc/debian_version)"
  57. elif [ -z "${distroid}" ]; then
  58. distroid="debian"
  59. fi
  60. elif [ -f "/etc/redhat-release" ] && [ "${distro_info}" == "redhat-release" ]; then
  61. if [ -z "${distroname}" ]; then
  62. distroname="$(cat /etc/redhat-release)"
  63. elif [ -z "${distroversion}" ]; then
  64. distroversion="$(rpm -qa \*-release | grep -Ei "oracle|redhat|centos|fedora" | cut -d"-" -f3)"
  65. elif [ -z "${distroid}" ]; then
  66. distroid="$(awk '{print $1}' /etc/redhat-release)"
  67. fi
  68. fi
  69. done
  70. # Get virtual environment
  71. if [ "$(command -v systemd-detect-virt 2> /dev/null)" ]; then
  72. virtualenvironment="$(systemd-detect-virt)"
  73. fi
  74. # Some RHEL based distros use 8.4 instead of just 8.
  75. if [[ "${distroidlike}" == *"rhel"* ]] || [ "${distroid}" == "rhel" ]; then
  76. distroversioncsv="${distroversionrh}"
  77. else
  78. distroversioncsv="${distroversion}"
  79. fi
  80. # Check if distro supported by distro vendor.
  81. if [ "$(command -v distro-info 2> /dev/null)" ]; then
  82. distrosunsupported="$(distro-info --unsupported)"
  83. distrosunsupported_array=("${distrosunsupported}")
  84. for distrounsupported in "${distrosunsupported_array[@]}"; do
  85. if [ "${distrounsupported}" == "${distrocodename}" ]; then
  86. distrosupport=unsupported
  87. break
  88. else
  89. distrosupport=supported
  90. fi
  91. done
  92. else
  93. distrosupport=unknown
  94. fi
  95. ## Glibc version
  96. # e.g: 1.17
  97. glibcversion="$(ldd --version | sed -n '1s/.* //p')"
  98. ## tmux version
  99. # e.g: tmux 1.6
  100. if [ ! "$(command -V tmux 2> /dev/null)" ]; then
  101. tmuxv="${red}NOT INSTALLED!${default}"
  102. tmuxvdigit="0"
  103. else
  104. tmuxvdigit="$(tmux -V | sed "s/tmux //" | sed -n '1 p' | tr -cd '[:digit:]')"
  105. if [ "${tmuxvdigit}" -lt "16" ]; then
  106. tmuxv="$(tmux -V) (>= 1.6 required for console log)"
  107. else
  108. tmuxv="$(tmux -V)"
  109. fi
  110. fi
  111. if [ "$(command -V java 2> /dev/null)" ]; then
  112. javaversion="$(java -version 2>&1 | grep "version")"
  113. fi
  114. if [ "$(command -v mono 2> /dev/null)" ]; then
  115. monoversion="$(mono --version 2>&1 | grep -Po '(?<=version )\d')"
  116. fi
  117. ## Uptime
  118. uptime="$(< /proc/uptime)"
  119. uptime=${uptime/[. ]*/}
  120. minutes="$((uptime / 60 % 60))"
  121. hours="$((uptime / 60 / 60 % 24))"
  122. days="$((uptime / 60 / 60 / 24))"
  123. ### Performance information
  124. ## Average server load
  125. load="$(uptime | awk -F 'load average: ' '{ print $2 }')"
  126. ## CPU information
  127. cpumodel="$(awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')"
  128. cpucores="$(awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo)"
  129. cpufreqency="$(awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')"
  130. # CPU usage of the game server pid
  131. if [ -n "${gameserverpid}" ]; then
  132. cpuused="$(ps --forest -o pcpu -g "${gameserverpid}" | awk '{s+=$1} END {print s}')"
  133. cpuusedmhz="$(echo "${cpufreqency} * ${cpuused} / 100" | bc)"
  134. fi
  135. ## Memory information
  136. # Available RAM and swap.
  137. # Newer distros can use numfmt to give more accurate results.
  138. if [ "$(command -v numfmt 2> /dev/null)" ]; then
  139. # Issue #2005 - Kernel 3.14+ contains MemAvailable which should be used. All others will be calculated.
  140. # get the raw KB values of these fields.
  141. physmemtotalkb="$(grep MemTotal /proc/meminfo | awk '{print $2}')"
  142. physmemfreekb="$(grep ^MemFree /proc/meminfo | awk '{print $2}')"
  143. physmembufferskb="$(grep ^Buffers /proc/meminfo | awk '{print $2}')"
  144. physmemcachedkb="$(grep ^Cached /proc/meminfo | awk '{print $2}')"
  145. physmemreclaimablekb="$(grep ^SReclaimable /proc/meminfo | awk '{print $2}')"
  146. # check if MemAvailable Exists.
  147. if grep -q ^MemAvailable /proc/meminfo; then
  148. physmemactualfreekb="$(grep ^MemAvailable /proc/meminfo | awk '{print $2}')"
  149. else
  150. physmemactualfreekb="$((physmemfreekb + physmembufferskb + physmemcachedkb))"
  151. fi
  152. # Available RAM and swap.
  153. physmemtotalmb="$((physmemtotalkb / 1024))"
  154. physmemtotal="$(numfmt --to=iec --from=iec --suffix=B "${physmemtotalkb}K")"
  155. physmemfree="$(numfmt --to=iec --from=iec --suffix=B "${physmemactualfreekb}K")"
  156. physmemused="$(numfmt --to=iec --from=iec --suffix=B "$((physmemtotalkb - physmemfreekb - physmembufferskb - physmemcachedkb - physmemreclaimablekb))K")"
  157. physmemavailable="$(numfmt --to=iec --from=iec --suffix=B "${physmemactualfreekb}K")"
  158. physmemcached="$(numfmt --to=iec --from=iec --suffix=B "$((physmemcachedkb + physmemreclaimablekb))K")"
  159. swaptotal="$(numfmt --to=iec --from=iec --suffix=B "$(grep ^SwapTotal /proc/meminfo | awk '{print $2}')K")"
  160. swapfree="$(numfmt --to=iec --from=iec --suffix=B "$(grep ^SwapFree /proc/meminfo | awk '{print $2}')K")"
  161. swapused="$(numfmt --to=iec --from=iec --suffix=B "$(($(grep ^SwapTotal /proc/meminfo | awk '{print $2}') - $(grep ^SwapFree /proc/meminfo | awk '{print $2}')))K")"
  162. # RAM usage of the game server pid
  163. # MB
  164. if [ "${gameserverpid}" ]; then
  165. memused="$(ps --forest -o rss -g "${gameserverpid}" | awk '{s+=$1} END {print s}' | awk '{$1/=1024;printf "%.0f",$1}{print $2}')"
  166. # %
  167. pmemused="$(ps --forest -o %mem -g "${gameserverpid}" | awk '{s+=$1} END {print s}')"
  168. fi
  169. else
  170. # Older distros will need to use free.
  171. # Older versions of free do not support -h option.
  172. if [ "$(
  173. free -h > /dev/null 2>&1
  174. echo $?
  175. )" -ne "0" ]; then
  176. humanreadable="-m"
  177. else
  178. humanreadable="-h"
  179. fi
  180. physmemtotalmb="$(free -m | awk '/Mem:/ {print $2}')"
  181. physmemtotal="$(free ${humanreadable} | awk '/Mem:/ {print $2}')"
  182. physmemfree="$(free ${humanreadable} | awk '/Mem:/ {print $4}')"
  183. physmemused="$(free ${humanreadable} | awk '/Mem:/ {print $3}')"
  184. oldfree="$(free ${humanreadable} | awk '/cache:/')"
  185. if [ "${oldfree}" ]; then
  186. physmemavailable="n/a"
  187. physmemcached="n/a"
  188. else
  189. physmemavailable="$(free ${humanreadable} | awk '/Mem:/ {print $7}')"
  190. physmemcached="$(free ${humanreadable} | awk '/Mem:/ {print $6}')"
  191. fi
  192. swaptotal="$(free ${humanreadable} | awk '/Swap:/ {print $2}')"
  193. swapused="$(free ${humanreadable} | awk '/Swap:/ {print $3}')"
  194. swapfree="$(free ${humanreadable} | awk '/Swap:/ {print $4}')"
  195. fi
  196. ### Disk information
  197. ## Available disk space on the partition.
  198. filesystem="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $1}')"
  199. totalspace="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $2}')"
  200. usedspace="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $3}')"
  201. availspace="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $4}')"
  202. ## LinuxGSM used space total.
  203. rootdirdu="$(du -sh "${rootdir}" 2> /dev/null | awk '{print $1}')"
  204. if [ -z "${rootdirdu}" ]; then
  205. rootdirdu="0M"
  206. fi
  207. ## LinuxGSM used space in serverfiles dir.
  208. serverfilesdu="$(du -sh "${serverfiles}" 2> /dev/null | awk '{print $1}')"
  209. if [ -z "${serverfilesdu}" ]; then
  210. serverfilesdu="0M"
  211. fi
  212. ## LinuxGSM used space total minus backup dir.
  213. rootdirduexbackup="$(du -sh --exclude="${backupdir}" "${serverfiles}" 2> /dev/null | awk '{print $1}')"
  214. if [ -z "${rootdirduexbackup}" ]; then
  215. rootdirduexbackup="0M"
  216. fi
  217. ## Backup info
  218. if [ -d "${backupdir}" ]; then
  219. # Used space in backups dir.
  220. backupdirdu="$(du -sh "${backupdir}" | awk '{print $1}')"
  221. # If no backup dir, size is 0M.
  222. if [ -z "${backupdirdu}" ]; then
  223. backupdirdu="0M"
  224. fi
  225. # number of backups set to 0 by default.
  226. backupcount=0
  227. # If there are backups in backup dir.
  228. if [ "$(find "${backupdir}" -name "*.tar.gz" | wc -l)" -ne "0" ]; then
  229. # number of backups.
  230. backupcount="$(find "${backupdir}"/*.tar.gz | wc -l)"
  231. # most recent backup.
  232. lastbackup="$(ls -1t "${backupdir}"/*.tar.gz | head -1)"
  233. # date of most recent backup.
  234. lastbackupdate="$(date -r "${lastbackup}")"
  235. # no of days since last backup.
  236. lastbackupdaysago="$((($(date +'%s') - $(date -r "${lastbackup}" +'%s')) / 60 / 60 / 24))"
  237. # size of most recent backup.
  238. lastbackupsize="$(du -h "${lastbackup}" | awk '{print $1}')"
  239. fi
  240. fi
  241. # Network Interface name
  242. netint=$(${ipcommand} -o addr | grep "${ip}" | awk '{print $2}')
  243. netlink=$(${ethtoolcommand} "${netint}" 2> /dev/null | grep Speed | awk '{print $2}')
  244. # Sets the SteamCMD glibc requirement if the game server requirement is less or not required.
  245. if [ "${appid}" ]; then
  246. if [ "${glibc}" = "null" ] || [ -z "${glibc}" ] || [ "$(printf '%s\n'${glibc}'\n' "2.14" | sort -V | head -n 1)" != "2.14" ]; then
  247. glibc="2.14"
  248. fi
  249. fi
  250. # Gather Port Info using ss
  251. ssinfo="$(ss -tuplwn)"