4
0

info_stats.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. # Uses Google analytics.
  8. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. info_distro.sh
  10. # remove uuid that was used in v20.2.0 and below
  11. if [ -f "${datadir}/uuid.txt" ]; then
  12. rm -f "${datadir:?}/uuid.txt"
  13. fi
  14. # generate uuid's
  15. # this consists of a standard uuid and a docker style name
  16. # to allow human readable uuid's.
  17. # e.g angry_proskuriakova_38a9ef76-4ae3-46a6-a895-7af474831eba
  18. if [ ! -f "${datadir}/uuid-${selfname}.txt" ] || [ ! -f "${datadir}/uuid-install.txt" ]; then
  19. # download dictionary words
  20. if [ ! -f "${datadir}/name-left.csv" ]; then
  21. fn_fetch_file_github "lgsm/data" "name-left.csv" "${datadir}" "nochmodx" "norun" "forcedl" "nohash"
  22. fi
  23. if [ ! -f "${datadir}/name-right.csv" ]; then
  24. fn_fetch_file_github "lgsm/data" "name-right.csv" "${datadir}" "nochmodx" "norun" "forcedl" "nohash"
  25. fi
  26. # generate instance uuid
  27. if [ -n "$(command -v uuidgen 2> /dev/null)" ]; then
  28. uuid="$(uuidgen)"
  29. else
  30. uuid="$(cat /proc/sys/kernel/random/uuid)"
  31. fi
  32. nameleft="$(shuf -n 1 "${datadir}/name-left.csv")"
  33. nameright="$(shuf -n 1 "${datadir}/name-right.csv")"
  34. echo "instance_${nameleft}_${nameright}_${uuid}" > "${datadir}/uuid-${selfname}.txt"
  35. # generate install uuid if missing
  36. if [ ! -f "${datadir}/uuid-install.txt" ]; then
  37. echo "${nameleft}_${nameright}_${uuid}" > "${datadir}/uuid-install.txt"
  38. fi
  39. fi
  40. uuidinstance=$(cat "${datadir}/uuid-${selfname}.txt")
  41. uuidinstall=$(cat "${datadir}/uuid-install.txt")
  42. # machine-id is a unique id set on OS install
  43. uuidhardware=$(cat "/etc/machine-id")
  44. # results are rounded up to reduce number of different results in analytics.
  45. # nearest 100Mhz.
  46. cpuusedmhzroundup="$(((cpuusedmhz + 99) / 100 * 100))"
  47. # nearest 100MB
  48. memusedmbroundup="$(((memusedmb + 99) / 100 * 100))"
  49. # Convert any commas to dots.
  50. physmemtotal="${physmemtotal//,/.}"
  51. apisecret="A-OzP02TSMWt4_vHi6ZpUw"
  52. measurementid="G-0CR8V7EMT5"
  53. # Sending stats to Google Analytics GA4
  54. payload="{
  55. \"client_id\": \"${uuidinstance}\",
  56. \"events\": [
  57. {
  58. \"name\": \"LinuxGSM\",
  59. \"params\": {
  60. \"cpuusedmhzroundup\": \"${cpuusedmhzroundup}\",
  61. \"diskused\": \"${serverfilesdu}\",
  62. \"distro\": \"${distroname}\",
  63. \"game\": \"${gamename}\",
  64. \"memusedmbroundup\": \"${memusedmbroundup}\",
  65. \"ramused\": \"${memusedmbroundup}\",
  66. \"servercpu\": \"${cpumodel} ${cpucores} cores\",
  67. \"servercpufreq\": \"${cpufreqency} x${cpucores}\",
  68. \"serverdisk\": \"${totalspace}\",
  69. \"serverfilesdu\": \"${serverfilesdu}\",
  70. \"serverram\": \"${physmemtotal}\",
  71. \"serverramgb\": \"${physmemtotalgb}\",
  72. \"uuidhardware\": \"${uuidhardware}\",
  73. \"uuidinstall\": \"${uuidinstall}\",
  74. \"uuidinstance\": \"${uuidinstance}\",
  75. \"version\": \"${version}\",
  76. \"virtualenvironment\": \"${virtualenvironment}\",
  77. \"tmuxversion\": \"${tmuxversion}\",
  78. \"java\": \"${javaversion}\"
  79. }
  80. }
  81. ]
  82. }"
  83. fn_alert_payload() {
  84. alertpayload="{
  85. \"client_id\": \"${uuidinstance}\",
  86. \"events\": [
  87. {
  88. \"name\": \"LinuxGSM\",
  89. \"params\": {
  90. \"alert\": \"${alerttype}\"
  91. }
  92. }
  93. ]
  94. }"
  95. }
  96. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=A-OzP02TSMWt4_vHi6ZpUw&measurement_id=G-0CR8V7EMT5" -H "Content-Type: application/json" -d "${payload}"
  97. if [ "${discordalert}" == "on" ]; then
  98. alerttype="discord"
  99. fn_alert_payload
  100. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  101. fi
  102. if [ "${emailalert}" == "on" ]; then
  103. alerttype="email"
  104. fn_alert_payload
  105. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  106. fi
  107. if [ "${gotifyalert}" == "on" ]; then
  108. alerttype="gotify"
  109. fn_alert_payload
  110. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  111. fi
  112. if [ "${iftttalert}" == "on" ]; then
  113. alerttype="ifttt"
  114. fn_alert_payload
  115. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  116. fi
  117. if [ "${pushbulletalert}" == "on" ]; then
  118. alerttype="pushbullet"
  119. fn_alert_payload
  120. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  121. fi
  122. if [ "${pushoveralert}" == "on" ]; then
  123. alerttype="pushover"
  124. fn_alert_payload
  125. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  126. fi
  127. if [ "${rocketchatalert}" == "on" ]; then
  128. alerttype="rocketchat"
  129. fn_alert_payload
  130. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  131. fi
  132. if [ "${slackalert}" == "on" ]; then
  133. alerttype="slack"
  134. fn_alert_payload
  135. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  136. fi
  137. if [ "${telegramalert}" == "on" ]; then
  138. alerttype="telegram"
  139. fn_alert_payload
  140. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  141. fi
  142. fn_script_log_info "Send LinuxGSM stats"
  143. fn_script_log_info "* uuid-${selfname}: ${uuidinstance}"
  144. fn_script_log_info "* uuid-install: ${uuidinstall}"
  145. fn_script_log_info "* uuid-hardware: ${uuidhardware}"
  146. fn_script_log_info "* Game Name: ${gamename}"
  147. fn_script_log_info "* Distro Name: ${distroname}"
  148. fn_script_log_info "* Game Server CPU Used: ${cpuusedmhzroundup}MHz"
  149. fn_script_log_info "* Game Server RAM Used: ${memusedmbroundup}MB"
  150. fn_script_log_info "* Game Server Disk Used: ${serverfilesdu}"
  151. fn_script_log_info "* Server CPU Model: ${cpumodel}"
  152. fn_script_log_info "* Server CPU Frequency: ${cpufreqency}"
  153. fn_script_log_info "* Server RAM: ${physmemtotal}"
  154. fn_script_log_info "* Server Disk: ${totalspace}"
  155. fn_script_log_info "* Virtual Environment: ${virtualenvironment}"
  156. fn_script_log_info "* LinuxGSM Version: ${version}"
  157. fn_script_log_info "* Enabled Alerts"