forked from masdzub/tools-for-work
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reseller_usage.sh
50 lines (38 loc) · 1.66 KB
/
reseller_usage.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
# Function to display help information
show_help() {
echo "Usage: $0 [OPTION]"
echo "Calculate and display disk usage for each reseller (excluding 'root')."
echo
echo "Options:"
echo " -h, --help Show this help message and exit"
}
# Check if the user has passed the help option
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
show_help
exit 0
fi
# Create or empty the temporary file
> .resellers.tmp
# Loop through each unique reseller (excluding 'root')
for reseller in $(cut -d' ' -f2 /etc/trueuserowners | grep -v "^root$" | sort | uniq); do
# Display the current reseller being checked
echo -en "Checking reseller ${reseller}\033[0K\r"
# Calculate total disk used by the reseller in MB
total_disk_mb=$(whmapi1 --output=jsonpretty listaccts search=${reseller} searchtype=owner searchmethod=exact want=diskused \
| grep diskused | cut -d'"' -f4 | cut -d'M' -f1 | paste -sd+ - | bc)
# Convert MB to GB
total_disk_gb=$(bc <<< "scale=2; ${total_disk_mb} / 1024")
# Get the list of accounts for the reseller
accounts=$(whmapi1 listaccts search=${reseller} searchtype=owner --output=json | jq -r '.data.acct[] | .user')
# Count the number of accounts
account_count=$(echo "$accounts" | wc -l)
# Append reseller and their disk usage to the temporary file
echo "${total_disk_gb} ${reseller} - ${total_disk_gb} GB - ${account_count} Accounts" >> .resellers.tmp
done
# Indicate completion
echo -e "\nDone"
# Sort the temporary file by the disk usage in ascending order and display the results
sort -n .resellers.tmp | cut -d' ' -f2-
# Remove the temporary file
rm -f .resellers.tmp