4
0

info_stats.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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${cpulogicalcores}\",
  68. \"servercpuphysicalcores\": \"${cpuphysicalcoresdisplay}\",
  69. \"servercpulogicalcpus\": \"${cpulogicalcores}\",
  70. \"servercpufreqrange\": \"${cpufrequencydisplay}\",
  71. \"serverdisk\": \"${totalspace}\",
  72. \"serverfilesdu\": \"${serverfilesdu}\",
  73. \"serverram\": \"${physmemtotal}\",
  74. \"serverramgb\": \"${physmemtotalgb}\",
  75. \"uuidhardware\": \"${uuidhardware}\",
  76. \"uuidinstall\": \"${uuidinstall}\",
  77. \"uuidinstance\": \"${uuidinstance}\",
  78. \"version\": \"${version}\",
  79. \"virtualenvironment\": \"${virtualenvironment}\",
  80. \"tmuxversion\": \"${tmuxversion}\",
  81. \"java\": \"${javaversion}\"
  82. }
  83. }
  84. ]
  85. }"
  86. fn_alert_payload() {
  87. alertpayload="{
  88. \"client_id\": \"${uuidinstance}\",
  89. \"events\": [
  90. {
  91. \"name\": \"LinuxGSM\",
  92. \"params\": {
  93. \"alert\": \"${alerttype}\"
  94. }
  95. }
  96. ]
  97. }"
  98. }
  99. 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}"
  100. if [ "${discordalert}" == "on" ]; then
  101. alerttype="discord"
  102. fn_alert_payload
  103. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  104. fi
  105. if [ "${emailalert}" == "on" ]; then
  106. alerttype="email"
  107. fn_alert_payload
  108. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  109. fi
  110. if [ "${gotifyalert}" == "on" ]; then
  111. alerttype="gotify"
  112. fn_alert_payload
  113. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  114. fi
  115. if [ "${iftttalert}" == "on" ]; then
  116. alerttype="ifttt"
  117. fn_alert_payload
  118. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  119. fi
  120. if [ "${pushbulletalert}" == "on" ]; then
  121. alerttype="pushbullet"
  122. fn_alert_payload
  123. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  124. fi
  125. if [ "${pushoveralert}" == "on" ]; then
  126. alerttype="pushover"
  127. fn_alert_payload
  128. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  129. fi
  130. if [ "${rocketchatalert}" == "on" ]; then
  131. alerttype="rocketchat"
  132. fn_alert_payload
  133. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  134. fi
  135. if [ "${slackalert}" == "on" ]; then
  136. alerttype="slack"
  137. fn_alert_payload
  138. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  139. fi
  140. if [ "${telegramalert}" == "on" ]; then
  141. alerttype="telegram"
  142. fn_alert_payload
  143. curl -X POST "https://www.google-analytics.com/mp/collect?api_secret=${apisecret}&measurement_id=${measurementid}" -H "Content-Type: application/json" -d "${alertpayload}"
  144. fi
  145. fn_script_log_info "Send LinuxGSM stats"
  146. fn_script_log_info "* uuid-${selfname}: ${uuidinstance}"
  147. fn_script_log_info "* uuid-install: ${uuidinstall}"
  148. fn_script_log_info "* uuid-hardware: ${uuidhardware}"
  149. fn_script_log_info "* Game Name: ${gamename}"
  150. fn_script_log_info "* Distro Name: ${distroname}"
  151. fn_script_log_info "* Game Server CPU Used: ${cpuusedmhzroundup}MHz"
  152. fn_script_log_info "* Game Server RAM Used: ${memusedmbroundup}MB"
  153. fn_script_log_info "* Game Server Disk Used: ${serverfilesdu}"
  154. fn_script_log_info "* Server CPU Model: ${cpumodel}"
  155. fn_script_log_info "* Server CPU Average Frequency: ${cpufreqency}"
  156. fn_script_log_info "* Server RAM: ${physmemtotal}"
  157. fn_script_log_info "* Server Disk: ${totalspace}"
  158. fn_script_log_info "* Virtual Environment: ${virtualenvironment}"
  159. fn_script_log_info "* LinuxGSM Version: ${version}"
  160. fn_script_log_info "* Enabled Alerts"