v-update-web-domains-traff 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/bash
  2. # info: update domains bandwidth usage
  3. # options: USER
  4. #
  5. # The function recalculates bandwidth usage for all user webdomains.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. user=$1
  11. # Includes
  12. source $VESTA/func/main.sh
  13. source $VESTA/func/domain.sh
  14. source $VESTA/conf/vesta.conf
  15. #----------------------------------------------------------#
  16. # Verifications #
  17. #----------------------------------------------------------#
  18. check_args '1' "$#" 'USER'
  19. is_format_valid 'user'
  20. is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
  21. is_object_valid 'user' 'USER' "$user" "$user"
  22. #----------------------------------------------------------#
  23. # Action #
  24. #----------------------------------------------------------#
  25. for domain in $(search_objects 'web' 'SUSPENDED' "no" 'DOMAIN'); do
  26. # Reset BW counter on the start of the month
  27. if [ "$(date +%d)" = '01' ]; then
  28. update_object_value 'web' 'DOMAIN' "$domain" '$U_BANDWIDTH' '0'
  29. fi
  30. log_file="/var/log/$WEB_SYSTEM/domains/$domain.bytes"
  31. bytes=0
  32. # Parsing log
  33. while read line; do
  34. if [[ "$line" =~ ^[0-9]+$ ]]; then
  35. line=${line#0}
  36. if [ ! -z "$line" ]; then
  37. bytes=$(($bytes + $line))
  38. fi
  39. fi
  40. done < $log_file
  41. # Converting to Mb
  42. mb=$(echo "$bytes / 1024 / 1024"|bc)
  43. # Nulling log
  44. echo > $log_file
  45. get_domain_values 'web'
  46. bandwidth=$((U_BANDWIDTH + mb))
  47. # Updating bandwidth value in config
  48. update_object_value 'web' 'DOMAIN' "$domain" '$U_BANDWIDTH' "$bandwidth"
  49. done
  50. #----------------------------------------------------------#
  51. # Vesta #
  52. #----------------------------------------------------------#
  53. # Recalculating user bandwidth
  54. recalc_user_bandwidth_usage
  55. # No Logging
  56. #log_event "$OK" "$ARGUMENTS"
  57. exit