This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscale.sh
executable file
·248 lines (206 loc) · 6.7 KB
/
autoscale.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
# Use this Script to scale Servers hosted on hosttech.cloud automatically.
# This script requieres high privileges to make Changes on the System ressources. Run it as root.
if ! [ $(id -u) = 0 ]; then
echo "This script should be used as root."
exit 1
fi
if ! command -v jq >/dev/null; then
read -p "This script requires jq to be installed to work. Would you like to install jq now? (Y/N)" answer
if [[ "$answer" == "y" || "$answer" == "Y" ]]; then
apt update && apt install -y jq
else
echo "jq is reqiered. Exit now."
exit 1
fi
fi
conf=hosttechAutoscale.conf
if [[ -f $conf ]]; then
source $conf
fi
#Initialize the Script and install a Cronjob, if desired.
function setavload() {
case $avloadabc in
a)
avload=1
avloadminutes=1
;;
b)
avload=2
avloadminutes=5
;;
c)
avload=3
avloadminutes=15
;;
esac
}
function initialize() {
if [[ ! -f $conf ]]; then
echo "This script scales your Server on the hosttech.cloud."
read -p "Please enter your USER_UUID as found on https://hosttech.cloud/Easy/APIs/: " USER_UUID
read -p "Please enter your API-Token as found on https://hosttech.cloud/Easy/APIs/: " API_TOKEN
clear
# Get the List of Servernames and SERVER_UUID
serversjson=$(curl -s -H "Content-Type: application/json" \
-H "X-Auth-UserId: $USER_UUID" \
-H "X-Auth-Token: $API_TOKEN" \
-X GET \
https://api.hosttech.cloud/objects/servers)
echo "These Servers are available:"
echo $serversjson | jq '.[] | .[] | "\(.name) \(.object_uuid)" ' | tr -d "\"" | column -t -s' '
read -p "Please enter the SERVER_UUID of this Server: " SERVER_UUID
clear
read -p "Enter max CPU (default 32): " cpumax
if [[ -z "$cpumax" ]]; then
cpumax=32
else
while [[ ! $cpumax =~ ^[0-9]+$ ]]; do
read -p "Enter a valid number (default 32): " cpumax
if [[ -z "$cpumax" ]]; then
cpumax=32
fi
done
fi
[[ $cpumax =~ ^[0-9]+$ ]] && echo "Your host will scale up to $cpumax CPU cores"
sleep 2
clear
echo -e "Depending on the Apps, Databases etc. it takes some time for the host to \"calm down\" afer reboot. \nBy default there is a delay of 10 Minutes until the script starts scaling. You can increase this value as you wish."
read -p "Enter number of Minutes for delay: " delay
if [[ -z "$delay" ]]; then
delay=10
else
while [[ ! $delay =~ ^[0-9]+$ ]]; do
read -p "Enter a valid number for the delay: " delay
if [[ -z "$delay" ]]; then
delay=10
fi
done
fi
[[ $delay =~ ^[0-9]+$ ]] && echo "You have set $delay minutes"
sleep 2
clear
echo -e "Load is refered to as a Value counting all processes waiting to be executed on the next cycle. This script uses Load Average during one minute to decide if more ressources are needed. \nYou can choose either 1 Minute (a), 5 Minutes (b) or 15 Minutes (c)"
echo "Enter a for 1 minute Average Load (default)"
echo "Enter b for 5 minute Average Load"
echo "Enter c for 15 minute Average Load"
read avloadabc
if [[ -z "$avloadabc" ]]; then
avloadabc=a
setavload
else
setavload
while [[ ! $avloadabc =~ ^[a-c]+$ ]]; do
read -p "Enter a valid value (a-c): " avloadabc
setavload
if [[ -z "$avloadabc" ]]; then
avloadabc=a
setavload
fi
done
fi
[[ $avloadabc =~ ^[a-c]+$ ]] && echo "You have set $avloadminutes minutes as Averave Load"
sleep 2
clear
echo -e "Threshold is a value relative to all available cores. If you enter 50, the script already scales by one core when the CPU's capacity is half utilized."
read -p "Enter Threshold (Number between 50-99, default: 90): " threshold
if [[ -z "$threshold" ]]; then
threshold=90
else
while [[ ! $threshold =~ ^[5-9][0-9]?$ ]]; do
read -p "This Number is not valid. Enter Threshold (Number between 50-99, default: 90): " threshold
if [[ -z "$threshold" ]]; then
threshold=90
fi
done
fi
[[ $threshold =~ ^[5-9][0-9]?$ ]] && echo "You have set Threshold to $threshold% CPU utilization"
touch hosttechAutoscale.conf
cat <<EOT >>hosttechAutoscale.conf
USER_UUID="$USER_UUID"
API_TOKEN="$API_TOKEN"
SERVER_UUID="$SERVER_UUID"
cpumax="$cpumax"
delay="$delay"
avload="$avload"
threshold="$threshold"
EOT
sleep 2
clear
echo "It's recommended to install a Cronjob for this script. Would you like to install a Cronjpb now?"
read -p "Install Cronjob? (Y/N): " cronjob && [[ $cronjob == [yY] || $cronjob == [yY][eE][sS] ]] || exit 1
if [[ $cronjob == [yY] || $cronjob == [yY][eE][sS] ]]; then
echo cronjob
crontab -l >mycron
echo "* * * * * cd $PWD && /bin/bash $PWD/autoscale.sh >/dev/null 2>&1" >>mycron
crontab mycron
rm mycron
fi
fi
}
# Allocate new resources
function ressource_update() {
# Based on script by William Lam - http://engineering.ucsb.edu/~duonglt/vmware/
# Bring CPUs online
for CPU_DIR in /sys/devices/system/cpu/cpu[0-9]*; do
CPU=${CPU_DIR##*/}
echo "Found cpu: '${CPU_DIR}' ..."
CPU_STATE_FILE="${CPU_DIR}/online"
if [ -f "${CPU_STATE_FILE}" ]; then
if grep -qx 1 "${CPU_STATE_FILE}"; then
echo -e "\t${CPU} already online"
else
echo -e "\t${CPU} is new cpu, onlining cpu ..."
echo 1 >"${CPU_STATE_FILE}"
fi
else
echo -e "\t${CPU} already configured prior to hot-add"
fi
done
# Bring all new Memory online
for RAM in $(grep line /sys/devices/system/memory/*/state); do
echo "Found ram: ${RAM} ..."
if [[ "${RAM}" == *":offline" ]]; then
echo "Bringing online"
echo $RAM | sed "s/:offline$//" | sed "s/^/echo online > /" | source /dev/stdin
else
echo "Already online"
fi
done
}
# Function to add more CPU and allocate them on the machine
function more_cpu() {
cpu=${1:-1}
cpuvar='{"cores": '$cpu' }'
curl -H "Content-Type: application/json" \
-H "X-Auth-UserId: $USER_UUID" \
-H "X-Auth-Token: $API_TOKEN" \
-d "$cpuvar" \
-X PATCH \
https://api.hosttech.cloud/objects/servers/$SERVER_UUID
sleep 10
ressource_update
}
initialize
# Variables for the Script. Finetuning should be made here.
cpu=$(grep -c ^processor /proc/cpuinfo)
cputotal=$((cpu))00
cpulimit=$(($cputotal / 100 * $threshold))
cpuuse=$(cat /proc/loadavg | awk '{print $'$avload'}' | tr -d "." | sed 's/^0*//')
uptime=$(uptime | awk '{print $3}' | cut -f 1 -d "," | tr -d ":")
echo "CPU Cores active: $cpu"
echo "Usage / Limit: $cpuuse/$cpulimit"
# This Script scales by 1 CPU if conditions are met (CPU-Threshold reached and Uptime longer than $delay Minutes).
if [[ $((cpuuse)) -ge $cpulimit && $((uptime)) -gt $delay ]]; then
cpunew=$((cpu + 1))
# Exit if maxcpu reached
if [[ $cpunew > $cpumax ]]; then
echo "Max CPU reached."
exit
fi
echo "New CPU $cpunew"
more_cpu $cpunew
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: The number of CPU on this Server was updated. $cpunew Cores are activ now." >>update.log
else
exit
fi