info_stats.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash
  2. # LinuxGSM info_stats.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Collect optional Stats sent to LinuxGSM project.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. info_distro.sh
  9. # remove uuid that was used in v20.2.0 and below
  10. if [ -f "${datadir}/uuid.txt" ]; then
  11. rm -f "${datadir:?}/uuid.txt"
  12. fi
  13. # generate uuid's
  14. # this consists of a standard uuid and a docker style name
  15. # to allow human readable uuid's.
  16. # e.g angry_proskuriakova_38a9ef76-4ae3-46a6-a895-7af474831eba
  17. if [ ! -f "${datadir}/uuid-${selfname}.txt" ] || [ ! -f "${datadir}/uuid-install.txt" ]; then
  18. # download dictionary words
  19. if [ ! -f "${datadir}/name-left.csv" ]; then
  20. fn_fetch_file_github "lgsm/data" "name-left.csv" "${datadir}" "nochmodx" "norun" "forcedl" "nohash"
  21. fi
  22. if [ ! -f "${datadir}/name-right.csv" ]; then
  23. fn_fetch_file_github "lgsm/data" "name-right.csv" "${datadir}" "nochmodx" "norun" "forcedl" "nohash"
  24. fi
  25. # generate instance uuid
  26. if [ -n "$(command -v uuidgen 2> /dev/null)" ]; then
  27. uuid="$(uuidgen)"
  28. else
  29. uuid="$(cat /proc/sys/kernel/random/uuid)"
  30. fi
  31. nameleft="$(shuf -n 1 "${datadir}/name-left.csv")"
  32. nameright="$(shuf -n 1 "${datadir}/name-right.csv")"
  33. echo "instance_${nameleft}_${nameright}_${uuid}" > "${datadir}/uuid-${selfname}.txt"
  34. # generate install uuid if missing
  35. if [ ! -f "${datadir}/uuid-install.txt" ]; then
  36. echo "${nameleft}_${nameright}_${uuid}" > "${datadir}/uuid-install.txt"
  37. fi
  38. fi
  39. uuidinstance=$(cat "${datadir}/uuid-${selfname}.txt")
  40. uuidinstall=$(cat "${datadir}/uuid-install.txt")
  41. uuidreport="${uuidinstance##*_}"
  42. # results are rounded up to reduce number of different results.
  43. # nearest 100Mhz.
  44. cpuusedmhzroundup="$(((cpuusedmhz + 99) / 100 * 100))"
  45. # nearest 100MB
  46. memusedmbroundup="$(((memusedmb + 99) / 100 * 100))"
  47. statsdate="$(date -u +%F)"
  48. statsstamp="${datadir}/stats-last-sent-${selfname}"
  49. if [ "$(cat "${statsstamp}" 2> /dev/null)" != "${statsdate}" ] && [ "$(command -v jq 2> /dev/null)" ]; then
  50. echo "${statsdate}" > "${statsstamp}"
  51. statscores="${cpucores}"
  52. if [[ ! "${statscores}" =~ ^[0-9]+$ ]]; then
  53. statscores="${cpulogicalcores}"
  54. fi
  55. statsramgb="$(awk -v mb="${physmemtotalmb}" 'BEGIN{printf "%.1f", mb / 1024}')"
  56. statsdiskgb="$(LC_ALL=C df -BG -P "${rootdir}" 2> /dev/null | awk 'NR==2{gsub("G","",$2); print $2}')"
  57. statsfilesgb="$(awk -v v="${serverfilesdu}" 'BEGIN{n=v+0; u=substr(v,length(v)); if(u=="K")n/=1048576; else if(u=="M")n/=1024; else if(u=="T")n*=1024; printf "%.2f", n}')"
  58. statsalerts=""
  59. for statsalerttype in discord email gotify ifttt matrix ntfy pushbullet pushover rocketchat slack telegram; do
  60. statsalertvar="${statsalerttype}alert"
  61. if [ "${!statsalertvar}" == "on" ]; then
  62. statsalerts+="${statsalerttype} "
  63. fi
  64. done
  65. statspayload="$(jq -cn \
  66. --arg uuid "${uuidreport}" \
  67. --arg game "${gamename}" \
  68. --arg distro "${distroname}" \
  69. --arg version "${version}" \
  70. --arg virt "${virtualenvironment}" \
  71. --arg cores "${statscores}" \
  72. --arg ram "${statsramgb}" \
  73. --arg disk "${statsdiskgb}" \
  74. --arg ramused "${memusedmbroundup}" \
  75. --arg cpuused "${cpuusedmhzroundup}" \
  76. --arg filesgb "${statsfilesgb}" \
  77. --arg alerts "${statsalerts}" \
  78. '{uuid: $uuid, game: $game, distro: $distro, version: $version, virt: $virt, cpu_cores: ($cores | tonumber? // 0), ram_gb: ($ram | tonumber? // 0), disk_gb: ($disk | tonumber? // 0)}
  79. + (if ($ramused | tonumber? // 0) > 0 then {ram_used_mb: ($ramused | tonumber)} else {} end)
  80. + (if ($cpuused | tonumber? // 0) > 0 then {cpu_used_mhz: ($cpuused | tonumber)} else {} end)
  81. + (if ($filesgb | tonumber? // 0) > 0 then {disk_used_gb: ($filesgb | tonumber)} else {} end)
  82. + (if ($alerts | length) > 0 then {alerts: ($alerts | split(" ") | map(select(length > 0)))} else {} end)' 2> /dev/null)"
  83. if [ -n "${statspayload}" ]; then
  84. curl -sS -f --max-time 5 -X POST "https://usage.linuxgsm.com/v1/report" -H "Content-Type: application/json" -d "${statspayload}" > /dev/null 2>&1
  85. fi
  86. fn_script_log_info "Send LinuxGSM stats"
  87. fn_script_log_info "* uuid-${selfname}: ${uuidreport}"
  88. fn_script_log_info "* Game Name: ${gamename}"
  89. fn_script_log_info "* Distro Name: ${distroname}"
  90. fn_script_log_info "* LinuxGSM Version: ${version}"
  91. fn_script_log_info "* Virtual Environment: ${virtualenvironment}"
  92. fn_script_log_info "* Game Server CPU Used: ${cpuusedmhzroundup}MHz"
  93. fn_script_log_info "* Game Server RAM Used: ${memusedmbroundup}MB"
  94. fn_script_log_info "* Game Server Disk Used: ${serverfilesdu}"
  95. fn_script_log_info "* Server CPU Cores: ${statscores}"
  96. fn_script_log_info "* Server RAM: ${statsramgb}GB"
  97. fn_script_log_info "* Server Disk: ${statsdiskgb}GB"
  98. fn_script_log_info "* Alerts: ${statsalerts:-none}"
  99. fi