info_distro.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. ### Game Server pid
  10. if [ "${status}" == "1" ]; then
  11. gameserverpid="$(tmux 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. # some RHEL based distros use 8.4 instead of just 8.
  71. if [[ "${distroidlike}" == *"rhel"* ]] || [ "${distroid}" == "rhel" ]; then
  72. distroversioncsv="${distroversionrh}"
  73. else
  74. distroversioncsv="${distroversion}"
  75. fi
  76. # Check if distro supported by distro vendor.
  77. if [ "$(command -v distro-info 2> /dev/null)" ]; then
  78. distrosunsupported="$(distro-info --unsupported)"
  79. distrosunsupported_array=("${distrosunsupported}")
  80. for distrounsupported in "${distrosunsupported_array[@]}"; do
  81. if [ "${distrounsupported}" == "${distrocodename}" ]; then
  82. distrosupport=unsupported
  83. break
  84. else
  85. distrosupport=supported
  86. fi
  87. done
  88. else
  89. distrosupport=unknown
  90. fi
  91. ## Glibc version
  92. # e.g: 1.17
  93. glibcversion="$(ldd --version | sed -n '1s/.* //p')"
  94. ## tmux version
  95. # e.g: tmux 1.6
  96. if [ ! "$(command -V tmux 2> /dev/null)" ]; then
  97. tmuxv="${red}NOT INSTALLED!${default}"
  98. tmuxvdigit="0"
  99. else
  100. tmuxvdigit="$(tmux -V | sed "s/tmux //" | sed -n '1 p' | tr -cd '[:digit:]')"
  101. if [ "${tmuxvdigit}" -lt "16" ]; then
  102. tmuxv="$(tmux -V) (>= 1.6 required for console log)"
  103. else
  104. tmuxv="$(tmux -V)"
  105. fi
  106. fi
  107. if [ "$(command -V java 2> /dev/null)" ]; then
  108. javaversion="$(java -version 2>&1 | grep "version")"
  109. fi
  110. if [ "$(command -v mono 2> /dev/null)" ]; then
  111. monoversion="$(mono --version 2>&1 | grep -Po '(?<=version )\d')"
  112. fi
  113. ## Uptime
  114. uptime="$(< /proc/uptime)"
  115. uptime=${uptime/[. ]*/}
  116. minutes="$((uptime / 60 % 60))"
  117. hours="$((uptime / 60 / 60 % 24))"
  118. days="$((uptime / 60 / 60 / 24))"
  119. ### Performance information
  120. ## Average server load
  121. load="$(uptime | awk -F 'load average: ' '{ print $2 }')"
  122. ## CPU information
  123. cpumodel="$(awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')"
  124. cpucores="$(awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo)"
  125. cpufreqency="$(awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//')"
  126. # CPU usage of the game server pid
  127. if [ -n "${gameserverpid}" ]; then
  128. cpuused="$(ps --forest -o pcpu -g "${gameserverpid}" | awk '{s+=$1} END {print s}')"
  129. cpuusedmhz="$(echo "${cpufreqency} * ${cpuused} / 100" | bc)"
  130. fi
  131. ## Memory information
  132. # Available RAM and swap.
  133. # Newer distros can use numfmt to give more accurate results.
  134. if [ "$(command -v numfmt 2> /dev/null)" ]; then
  135. # Issue #2005 - Kernel 3.14+ contains MemAvailable which should be used. All others will be calculated.
  136. # get the raw KB values of these fields.
  137. physmemtotalkb="$(grep MemTotal /proc/meminfo | awk '{print $2}')"
  138. physmemfreekb="$(grep ^MemFree /proc/meminfo | awk '{print $2}')"
  139. physmembufferskb="$(grep ^Buffers /proc/meminfo | awk '{print $2}')"
  140. physmemcachedkb="$(grep ^Cached /proc/meminfo | awk '{print $2}')"
  141. physmemreclaimablekb="$(grep ^SReclaimable /proc/meminfo | awk '{print $2}')"
  142. # check if MemAvailable Exists.
  143. if grep -q ^MemAvailable /proc/meminfo; then
  144. physmemactualfreekb="$(grep ^MemAvailable /proc/meminfo | awk '{print $2}')"
  145. else
  146. physmemactualfreekb="$((physmemfreekb + physmembufferskb + physmemcachedkb))"
  147. fi
  148. # Available RAM and swap.
  149. physmemtotalmb="$((physmemtotalkb / 1024))"
  150. physmemtotal="$(numfmt --to=iec --from=iec --suffix=B "${physmemtotalkb}K")"
  151. physmemfree="$(numfmt --to=iec --from=iec --suffix=B "${physmemactualfreekb}K")"
  152. physmemused="$(numfmt --to=iec --from=iec --suffix=B "$((physmemtotalkb - physmemfreekb - physmembufferskb - physmemcachedkb - physmemreclaimablekb))K")"
  153. physmemavailable="$(numfmt --to=iec --from=iec --suffix=B "${physmemactualfreekb}K")"
  154. physmemcached="$(numfmt --to=iec --from=iec --suffix=B "$((physmemcachedkb + physmemreclaimablekb))K")"
  155. swaptotal="$(numfmt --to=iec --from=iec --suffix=B "$(grep ^SwapTotal /proc/meminfo | awk '{print $2}')K")"
  156. swapfree="$(numfmt --to=iec --from=iec --suffix=B "$(grep ^SwapFree /proc/meminfo | awk '{print $2}')K")"
  157. swapused="$(numfmt --to=iec --from=iec --suffix=B "$(($(grep ^SwapTotal /proc/meminfo | awk '{print $2}') - $(grep ^SwapFree /proc/meminfo | awk '{print $2}')))K")"
  158. # RAM usage of the game server pid
  159. # MB
  160. if [ "${gameserverpid}" ]; then
  161. memused="$(ps --forest -o rss -g "${gameserverpid}" | awk '{s+=$1} END {print s}' | awk '{$1/=1024;printf "%.0f",$1}{print $2}')"
  162. # %
  163. pmemused="$(ps --forest -o %mem -g "${gameserverpid}" | awk '{s+=$1} END {print s}')"
  164. fi
  165. else
  166. # Older distros will need to use free.
  167. # Older versions of free do not support -h option.
  168. if [ "$(
  169. free -h > /dev/null 2>&1
  170. echo $?
  171. )" -ne "0" ]; then
  172. humanreadable="-m"
  173. else
  174. humanreadable="-h"
  175. fi
  176. physmemtotalmb="$(free -m | awk '/Mem:/ {print $2}')"
  177. physmemtotal="$(free ${humanreadable} | awk '/Mem:/ {print $2}')"
  178. physmemfree="$(free ${humanreadable} | awk '/Mem:/ {print $4}')"
  179. physmemused="$(free ${humanreadable} | awk '/Mem:/ {print $3}')"
  180. oldfree="$(free ${humanreadable} | awk '/cache:/')"
  181. if [ "${oldfree}" ]; then
  182. physmemavailable="n/a"
  183. physmemcached="n/a"
  184. else
  185. physmemavailable="$(free ${humanreadable} | awk '/Mem:/ {print $7}')"
  186. physmemcached="$(free ${humanreadable} | awk '/Mem:/ {print $6}')"
  187. fi
  188. swaptotal="$(free ${humanreadable} | awk '/Swap:/ {print $2}')"
  189. swapused="$(free ${humanreadable} | awk '/Swap:/ {print $3}')"
  190. swapfree="$(free ${humanreadable} | awk '/Swap:/ {print $4}')"
  191. fi
  192. ### Disk information
  193. ## Available disk space on the partition.
  194. filesystem="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $1}')"
  195. totalspace="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $2}')"
  196. usedspace="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $3}')"
  197. availspace="$(LC_ALL=C df -hP "${rootdir}" | tail -n 1 | awk '{print $4}')"
  198. ## LinuxGSM used space total.
  199. rootdirdu="$(du -sh "${rootdir}" 2> /dev/null | awk '{print $1}')"
  200. if [ -z "${rootdirdu}" ]; then
  201. rootdirdu="0M"
  202. fi
  203. ## LinuxGSM used space in serverfiles dir.
  204. serverfilesdu="$(du -sh "${serverfiles}" 2> /dev/null | awk '{print $1}')"
  205. if [ -z "${serverfilesdu}" ]; then
  206. serverfilesdu="0M"
  207. fi
  208. ## LinuxGSM used space total minus backup dir.
  209. rootdirduexbackup="$(du -sh --exclude="${backupdir}" "${serverfiles}" 2> /dev/null | awk '{print $1}')"
  210. if [ -z "${rootdirduexbackup}" ]; then
  211. rootdirduexbackup="0M"
  212. fi
  213. ## Backup info
  214. if [ -d "${backupdir}" ]; then
  215. # Used space in backups dir.
  216. backupdirdu="$(du -sh "${backupdir}" | awk '{print $1}')"
  217. # If no backup dir, size is 0M.
  218. if [ -z "${backupdirdu}" ]; then
  219. backupdirdu="0M"
  220. fi
  221. # number of backups set to 0 by default.
  222. backupcount=0
  223. # If there are backups in backup dir.
  224. if [ "$(find "${backupdir}" -name "*.tar.gz" | wc -l)" -ne "0" ]; then
  225. # number of backups.
  226. backupcount="$(find "${backupdir}"/*.tar.gz | wc -l)"
  227. # most recent backup.
  228. lastbackup="$(ls -1t "${backupdir}"/*.tar.gz | head -1)"
  229. # date of most recent backup.
  230. lastbackupdate="$(date -r "${lastbackup}")"
  231. # no of days since last backup.
  232. lastbackupdaysago="$((($(date +'%s') - $(date -r "${lastbackup}" +'%s')) / 60 / 60 / 24))"
  233. # size of most recent backup.
  234. lastbackupsize="$(du -h "${lastbackup}" | awk '{print $1}')"
  235. fi
  236. fi
  237. # Network Interface name
  238. netint=$(${ipcommand} -o addr | grep "${ip}" | awk '{print $2}')
  239. netlink=$(${ethtoolcommand} "${netint}" 2> /dev/null | grep Speed | awk '{print $2}')
  240. # Sets the SteamCMD glibc requirement if the game server requirement is less or not required.
  241. if [ "${appid}" ]; then
  242. if [ "${glibc}" = "null" ] || [ -z "${glibc}" ] || [ "$(printf '%s\n'${glibc}'\n' "2.14" | sort -V | head -n 1)" != "2.14" ]; then
  243. glibc="2.14"
  244. fi
  245. fi
  246. # Gather Port Info using ss
  247. ssinfo="$(ss -tuplwn)"