4
0

info_distro.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 type.
  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. if [ "$(command -v dotnet 2> /dev/null)" ]; then
  118. dotnetversion="$(dotnet --list-runtimes | grep -E 'Microsoft\.NETCore\.App' | awk '{print $2}')"
  119. fi
  120. ## Uptime
  121. uptime="$(< /proc/uptime)"
  122. uptime=${uptime/[. ]*/}
  123. minutes="$((uptime / 60 % 60))"
  124. hours="$((uptime / 60 / 60 % 24))"
  125. days="$((uptime / 60 / 60 / 24))"
  126. ### Performance information
  127. ## Average server load
  128. load="$(uptime | awk -F 'load average: ' '{ print $2 }')"
  129. ## CPU information
  130. cpumodel="$(awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')"
  131. cpucores="$(awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo)"
  132. cpufreqency="$(awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')"
  133. # CPU usage of the game server pid
  134. if [ -n "${gameserverpid}" ]; then
  135. cpuused="$(ps --forest -o pcpu -g "${gameserverpid}" | awk '{s+=$1} END {print s}')"
  136. cpuusedmhz="$(echo "${cpufreqency} * ${cpuused} / 100" | bc)"
  137. fi
  138. ## Memory information
  139. # Available RAM and swap.
  140. # Newer distros can use numfmt to give more accurate results.
  141. if [ "$(command -v numfmt 2> /dev/null)" ]; then
  142. # Issue #2005 - Kernel 3.14+ contains MemAvailable which should be used. All others will be calculated.
  143. # get the raw KB values of these fields.
  144. physmemtotalkb="$(grep MemTotal /proc/meminfo | awk '{print $2}')"
  145. physmemfreekb="$(grep ^MemFree /proc/meminfo | awk '{print $2}')"
  146. physmembufferskb="$(grep ^Buffers /proc/meminfo | awk '{print $2}')"
  147. physmemcachedkb="$(grep ^Cached /proc/meminfo | awk '{print $2}')"
  148. physmemreclaimablekb="$(grep ^SReclaimable /proc/meminfo | awk '{print $2}')"
  149. # check if MemAvailable Exists.
  150. if grep -q ^MemAvailable /proc/meminfo; then
  151. physmemactualfreekb="$(grep ^MemAvailable /proc/meminfo | awk '{print $2}')"
  152. else
  153. physmemactualfreekb="$((physmemfreekb + physmembufferskb + physmemcachedkb))"
  154. fi
  155. # Available RAM and swap.
  156. physmemtotalmb="$((physmemtotalkb / 1024))"
  157. physmemtotal="$(numfmt --to=iec --from=iec --suffix=B "${physmemtotalkb}K")"
  158. physmemfree="$(numfmt --to=iec --from=iec --suffix=B "${physmemactualfreekb}K")"
  159. physmemused="$(numfmt --to=iec --from=iec --suffix=B "$((physmemtotalkb - physmemfreekb - physmembufferskb - physmemcachedkb - physmemreclaimablekb))K")"
  160. physmemavailable="$(numfmt --to=iec --from=iec --suffix=B "${physmemactualfreekb}K")"
  161. physmemcached="$(numfmt --to=iec --from=iec --suffix=B "$((physmemcachedkb + physmemreclaimablekb))K")"
  162. swaptotal="$(numfmt --to=iec --from=iec --suffix=B "$(grep ^SwapTotal /proc/meminfo | awk '{print $2}')K")"
  163. swapfree="$(numfmt --to=iec --from=iec --suffix=B "$(grep ^SwapFree /proc/meminfo | awk '{print $2}')K")"
  164. swapused="$(numfmt --to=iec --from=iec --suffix=B "$(($(grep ^SwapTotal /proc/meminfo | awk '{print $2}') - $(grep ^SwapFree /proc/meminfo | awk '{print $2}')))K")"
  165. # RAM usage of the game server pid
  166. # MB
  167. if [ "${gameserverpid}" ]; then
  168. memused="$(ps --forest -o rss -g "${gameserverpid}" | awk '{s+=$1} END {print s}' | awk '{$1/=1024;printf "%.0f",$1}{print $2}')"
  169. # %
  170. pmemused="$(ps --forest -o %mem -g "${gameserverpid}" | awk '{s+=$1} END {print s}')"
  171. fi
  172. else
  173. # Older distros will need to use free.
  174. # Older versions of free do not support -h option.
  175. if [ "$(
  176. free -h > /dev/null 2>&1
  177. echo $?
  178. )" -ne "0" ]; then
  179. humanreadable="-m"
  180. else
  181. humanreadable="-h"
  182. fi
  183. physmemtotalmb="$(free -m | awk '/Mem:/ {print $2}')"
  184. physmemtotal="$(free ${humanreadable} | awk '/Mem:/ {print $2}')"
  185. physmemfree="$(free ${humanreadable} | awk '/Mem:/ {print $4}')"
  186. physmemused="$(free ${humanreadable} | awk '/Mem:/ {print $3}')"
  187. oldfree="$(free ${humanreadable} | awk '/cache:/')"
  188. if [ "${oldfree}" ]; then
  189. physmemavailable="n/a"
  190. physmemcached="n/a"
  191. else
  192. physmemavailable="$(free ${humanreadable} | awk '/Mem:/ {print $7}')"
  193. physmemcached="$(free ${humanreadable} | awk '/Mem:/ {print $6}')"
  194. fi
  195. swaptotal="$(free ${humanreadable} | awk '/Swap:/ {print $2}')"
  196. swapused="$(free ${humanreadable} | awk '/Swap:/ {print $3}')"
  197. swapfree="$(free ${humanreadable} | awk '/Swap:/ {print $4}')"
  198. fi
  199. ### Disk information
  200. ## Available disk space on the partition.
  201. filesystem="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $1}')"
  202. totalspace="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $2}')"
  203. usedspace="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $3}')"
  204. availspace="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $4}')"
  205. ## LinuxGSM used space total.
  206. rootdirdu="$(du -sh "${rootdir}" 2> /dev/null | awk '{print $1}')"
  207. if [ -z "${rootdirdu}" ]; then
  208. rootdirdu="0M"
  209. fi
  210. ## LinuxGSM used space in serverfiles dir.
  211. serverfilesdu="$(du -sh "${serverfiles}" 2> /dev/null | awk '{print $1}')"
  212. if [ -z "${serverfilesdu}" ]; then
  213. serverfilesdu="0M"
  214. fi
  215. ## LinuxGSM used space total minus backup dir.
  216. rootdirduexbackup="$(du -sh --exclude="${backupdir}" "${serverfiles}" 2> /dev/null | awk '{print $1}')"
  217. if [ -z "${rootdirduexbackup}" ]; then
  218. rootdirduexbackup="0M"
  219. fi
  220. ## Backup info
  221. if [ -d "${backupdir}" ]; then
  222. # Used space in backups dir.
  223. backupdirdu="$(du -sh "${backupdir}" | awk '{print $1}')"
  224. # If no backup dir, size is 0M.
  225. if [ -z "${backupdirdu}" ]; then
  226. backupdirdu="0M"
  227. fi
  228. # number of backups set to 0 by default.
  229. backupcount=0
  230. # If there are backups in backup dir.
  231. if [ "$(find "${backupdir}" -name "*.tar.gz" | wc -l)" -ne "0" ]; then
  232. # number of backups.
  233. backupcount="$(find "${backupdir}"/*.tar.gz | wc -l)"
  234. # most recent backup.
  235. lastbackup="$(ls -1t "${backupdir}"/*.tar.gz | head -1)"
  236. # date of most recent backup.
  237. lastbackupdate="$(date -r "${lastbackup}")"
  238. # no of days since last backup.
  239. lastbackupdaysago="$((($(date +'%s') - $(date -r "${lastbackup}" +'%s')) / 60 / 60 / 24))"
  240. # size of most recent backup.
  241. lastbackupsize="$(du -h "${lastbackup}" | awk '{print $1}')"
  242. fi
  243. fi
  244. # Network Interface name
  245. netint=$(${ipcommand} -o addr | grep "${ip}" | awk '{print $2}')
  246. netlink=$(${ethtoolcommand} "${netint}" 2> /dev/null | grep Speed | awk '{print $2}')
  247. # Sets the SteamCMD glibc requirement if the game server requirement is less or not required.
  248. if [ "${appid}" ]; then
  249. if [ "${glibc}" = "null" ] || [ -z "${glibc}" ] || [ "$(printf '%s\n'${glibc}'\n' "2.14" | sort -V | head -n 1)" != "2.14" ]; then
  250. glibc="2.14"
  251. fi
  252. fi
  253. # Gather Port Info using ss
  254. ssinfo="$(ss -tuplwn)"