init-metrics.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env bash
  2. set -e
  3. here=$(dirname "$0")
  4. # shellcheck source=net/common.sh
  5. source "$here"/common.sh
  6. usage() {
  7. exitcode=0
  8. if [[ -n "$1" ]]; then
  9. exitcode=1
  10. echo "Error: $*"
  11. fi
  12. cat <<EOF
  13. usage: $0 [-e] [-d] [-c database_name] [username]
  14. Creates a testnet dev metrics database
  15. username InfluxDB user with access to create a new database
  16. -c Manually specify a database to create, rather than read from config file
  17. -d Delete the database instead of creating it
  18. -e Assume database already exists and SOLANA_METRICS_CONFIG is
  19. defined in the environment already
  20. EOF
  21. exit $exitcode
  22. }
  23. useEnv=false
  24. delete=false
  25. createWithoutConfig=false
  26. host="https://internal-metrics.solana.com:8086"
  27. while getopts ":hdec:" opt; do
  28. case $opt in
  29. h)
  30. usage
  31. exit 0
  32. ;;
  33. c)
  34. createWithoutConfig=true
  35. netBasename=$OPTARG
  36. ;;
  37. d)
  38. delete=true
  39. ;;
  40. e)
  41. useEnv=true
  42. ;;
  43. *)
  44. usage "unhandled option: $OPTARG"
  45. ;;
  46. esac
  47. done
  48. shift $((OPTIND - 1))
  49. if $useEnv; then
  50. [[ -n $SOLANA_METRICS_CONFIG ]] ||
  51. usage "SOLANA_METRICS_CONFIG is not defined in the environment"
  52. else
  53. username=$1
  54. [[ -n "$username" ]] || usage "username not specified"
  55. read -rs -p "InfluxDB password for $username: " password
  56. [[ -n $password ]] || { echo "Password not specified"; exit 1; }
  57. echo
  58. password="$(urlencode "$password")"
  59. if ! $createWithoutConfig; then
  60. loadConfigFile
  61. fi
  62. query() {
  63. echo "$*"
  64. set -x
  65. curl -XPOST \
  66. "$host/query?u=${username}&p=${password}" \
  67. --data-urlencode "q=$*"
  68. }
  69. query "DROP DATABASE \"$netBasename\""
  70. ! $delete || exit 0
  71. query "CREATE DATABASE \"$netBasename\""
  72. query "ALTER RETENTION POLICY autogen ON \"$netBasename\" DURATION 7d"
  73. query "GRANT READ ON \"$netBasename\" TO \"ro\""
  74. query "GRANT WRITE ON \"$netBasename\" TO \"${username}\""
  75. SOLANA_METRICS_CONFIG="host=$host,db=$netBasename,u=${username},p=${password}"
  76. set +x
  77. fi
  78. set -x
  79. echo "export SOLANA_METRICS_CONFIG=\"$SOLANA_METRICS_CONFIG\"" >> "$configFile"
  80. exit 0