capsul-flask/capsulflask/shell_scripts/capacity-avaliable.sh

34 lines
1.1 KiB
Bash
Raw Normal View History

2020-05-13 18:56:43 +00:00
#!/bin/sh
2020-05-20 21:37:45 +00:00
#
# check available RAM and IPv4s
2020-05-13 18:56:43 +00:00
ram_bytes_to_allocate="$1"
ram_bytes_available=$(grep -E "^(size|memory_available_bytes)" /proc/spl/kstat/zfs/arcstats | awk '{sum+=$3} END {printf "%.0f", sum}')
ram_bytes_remainder="$((ram_bytes_available - ram_bytes_to_allocate))"
2020-05-13 18:56:43 +00:00
if echo "$ram_bytes_to_allocate" | grep -vqE "^[0-9]+$"; then
echo "ram_bytes_to_allocate \"$ram_bytes_to_allocate\" must be an integer"
2020-05-13 18:56:43 +00:00
exit 1
fi
2021-03-09 01:24:47 +00:00
# 20GB
if [ "$ram_bytes_remainder" -le $((20 * 1024 * 1024 * 1024)) ]; then
2020-05-20 21:37:45 +00:00
echo "VM is requesting more RAM than $(hostname -f) has available."
echo "Bytes requested: $ram_bytes_to_allocate"
echo "Bytes available: $ram_bytes_available"
2020-05-20 21:37:45 +00:00
exit 1
fi
2020-05-13 18:56:43 +00:00
ipv4_limit=28
2021-05-15 14:49:36 +00:00
used_ips=$(grep ip-add "/var/lib/libvirt/dnsmasq/virbr2.status" | cut -d '"' -f 4)
reserved_ips=$(cat "/var/lib/libvirt/dnsmasq/public2.hostsfile" | cut -d ',' -f 2)
total_addresses_used=$(printf "$used_ips\n$reserved_ips" | sort | uniq | wc -l)
ipv4_count=$(printf "$total_addresses_used")
2020-05-13 18:56:43 +00:00
if [ "$ipv4_count" -ge "$ipv4_limit" ]; then
2020-05-13 18:56:43 +00:00
echo "IPv4 address limit reached"
exit 1
fi
2021-03-09 01:24:47 +00:00
2020-05-20 21:37:45 +00:00
echo "yes"