v_list_db_host 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/bash
  2. # info: listing data base servers
  3. #----------------------------------------------------------#
  4. # Variable&Function #
  5. #----------------------------------------------------------#
  6. # Argument defenition
  7. type=$1
  8. host=$2
  9. format=${3-shell}
  10. # Importing variables
  11. source $VESTA/conf/vars.conf
  12. source $V_FUNC/shared_func.sh
  13. source $V_FUNC/db_func.sh
  14. # Json function
  15. json_list_dbhost() {
  16. # Definigng variables
  17. i=1
  18. # Define words number
  19. fields_count=$(echo "$fields" | wc -w)
  20. # Reading file line by line
  21. line=$(grep "HOST='$host'" $conf)
  22. # Print top bracket
  23. echo '{'
  24. # Assign key=value
  25. for key in $line; do
  26. eval ${key%%=*}=${key#*=}
  27. done
  28. # Starting output loop
  29. for field in $fields; do
  30. # Parsing key=value
  31. eval value=$field
  32. # Checking first field
  33. if [ "$i" -eq 1 ]; then
  34. echo -e "\t\"$value\": {"
  35. else
  36. if [ "$fields_count" -eq "$i" ]; then
  37. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
  38. else
  39. echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
  40. fi
  41. fi
  42. (( ++i))
  43. done
  44. # If there was any output
  45. if [ -n "$value" ]; then
  46. echo -e "\t}"
  47. fi
  48. # Printing bottom json bracket
  49. echo -e "}"
  50. }
  51. # Shell function
  52. shell_list_dbhost() {
  53. # Reading file line by line
  54. line=$(grep "HOST='$host'" $conf)
  55. # Parsing key=value
  56. for key in $line; do
  57. eval ${key%%=*}=${key#*=}
  58. done
  59. # Print result line
  60. for field in $fields; do
  61. eval key="$field"
  62. echo "${field//$/}: $key"
  63. done
  64. }
  65. #----------------------------------------------------------#
  66. # Verifications #
  67. #----------------------------------------------------------#
  68. # Checking args
  69. check_args '2' "$#" 'type host [format]'
  70. # Checking argument format
  71. format_validation 'host'
  72. # Checking db type
  73. is_type_valid 'db' "$type"
  74. # Checking db host
  75. is_db_host_valid
  76. #----------------------------------------------------------#
  77. # Action #
  78. #----------------------------------------------------------#
  79. # Defining config type
  80. conf=$V_DB/$type.conf
  81. # Defining fileds to select
  82. fields='$HOST $PORT $MAX_USERS $MAX_DB $U_SYS_USERS $U_DB_BASES $ACTIVE $DATE'
  83. # Listing database
  84. case $format in
  85. json) json_list_dbhost ;;
  86. plain) nohead=1; shell_list_dbhost ;;
  87. shell) shell_list_dbhost | column -t;;
  88. *) check_args '2' '0' 'type host [format]'
  89. esac
  90. #----------------------------------------------------------#
  91. # Vesta #
  92. #----------------------------------------------------------#
  93. # Logging
  94. log_event 'system' "$V_EVENT"
  95. exit