-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
check-updates.sh
378 lines (357 loc) · 13 KB
/
check-updates.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/bash
#################
# Check Updates #
#################
# shellcheck disable=SC2034
VERSION="1.5"
#Variable / Function
LOCAL_FILES="/etc/ultimate-updater"
CONFIG_FILE="$LOCAL_FILES/update.conf"
# Colors
BL="\e[36m"
OR="\e[1;33m"
RD="\e[1;91m"
GN="\e[1;92m"
CL="\e[0m"
ARGUMENTS () {
while test $# -gt -0; do
ARGUMENT="$1"
case "$ARGUMENT" in
-c)
RICM=true
;;
-u)
RDU=true
;;
chost)
COMMAND=true
OUTPUT_TO_FILE
CHECK_HOST_ITSELF
;;
ccontainer)
COMMAND=true
OUTPUT_TO_FILE
CHECK_CONTAINER
;;
cvm)
COMMAND=true
OUTPUT_TO_FILE
CHECK_VM
;;
host)
COMMAND=true
OUTPUT_TO_FILE
if [[ "$WITH_HOST" == true ]]; then CHECK_HOST_ITSELF; fi
if [[ "$WITH_LXC" == true ]]; then CONTAINER_CHECK_START; fi
if [[ "$WITH_VM" == true ]]; then VM_CHECK_START; fi
;;
cluster)
COMMAND=true
OUTPUT_TO_FILE
HOST_CHECK_START
;;
*)
echo -e "\n${RD} Error: Got an unexpected argument \"$ARGUMENT\"${CL}";
USAGE;
exit 2;
;;
esac
shift
done
}
# Usage
USAGE () {
echo -e "\nUsage: $0 {COMMAND}\n"
echo -e "{COMMAND}:"
echo -e "========="
echo -e " host Host-Mode"
echo -e " cluster Cluster-Mode\n"
echo -e "Report issues at: <https://github.com/BassT23/Proxmox/issues>\n"
}
READ_WRITE_CONFIG () {
WITH_HOST=$(awk -F'"' '/^CHECK_WITH_HOST=/ {print $2}' $CONFIG_FILE)
WITH_LXC=$(awk -F'"' '/^CHECK_WITH_LXC=/ {print $2}' $CONFIG_FILE)
WITH_VM=$(awk -F'"' '/^CHECK_WITH_VM=/ {print $2}' $CONFIG_FILE)
RUNNING=$(awk -F'"' '/^CHECK_RUNNING_CONTAINER=/ {print $2}' $CONFIG_FILE)
STOPPED=$(awk -F'"' '/^CHECK_STOPPED_CONTAINER=/ {print $2}' $CONFIG_FILE)
EXCLUDED=$(awk -F'"' '/^EXCLUDE_UPDATE_CHECK=/ {print $2}' $CONFIG_FILE)
ONLY=$(awk -F'"' '/^ONLY_UPDATE_CHECK=/ {print $2}' $CONFIG_FILE)
}
## HOST ##
# Host Check Start
HOST_CHECK_START () {
for HOST in $HOSTS; do
CHECK_HOST "$HOST"
done
}
# Host Check
CHECK_HOST () {
HOST=$1
ssh "$HOST" mkdir -p $LOCAL_FILES
scp $LOCAL_FILES/update.conf "$HOST":$LOCAL_FILES/update.conf >/dev/null 2>&1
ssh "$HOST" 'bash -s' < "$0" -- "-c host"
}
CHECK_HOST_ITSELF () {
apt-get update >/dev/null 2>&1
SECURITY_APT_UPDATES=$(apt-get -s upgrade | grep -ci "^inst.*security" | tr -d '\n')
NORMAL_APT_UPDATES=$(apt-get -s upgrade | grep -ci "^inst." | tr -d '\n')
if [[ -f /var/run/reboot-required.pkgs ]]; then REBOOT_REQUIRED=true; fi
if [[ $SECURITY_APT_UPDATES != 0 || $NORMAL_APT_UPDATES != 0 || $REBOOT_REQUIRED == true ]]; then
echo -e "${GN}Host${CL} : ${GN}$HOSTNAME${CL}"
fi
if [[ $REBOOT_REQUIRED == true ]]; then echo -e "${OR} Reboot required${CL}"; fi
if [[ $SECURITY_APT_UPDATES != 0 && $NORMAL_APT_UPDATES != 0 ]]; then
echo -e "S: $SECURITY_APT_UPDATES / N: $NORMAL_APT_UPDATES"
elif [[ $SECURITY_APT_UPDATES != 0 ]]; then
echo -e "S: $SECURITY_APT_UPDATES / "
elif [[ $NORMAL_APT_UPDATES != 0 ]]; then
echo -e "N: $NORMAL_APT_UPDATES"
fi
}
## Container ##
# Container Check Start
CONTAINER_CHECK_START () {
# Get the list of containers
CONTAINERS=$(pct list | tail -n +2 | cut -f1 -d' ')
# Loop through the containers
if ! [[ -d $LOCAL_FILES/temp/ ]]; then mkdir $LOCAL_FILES/temp/; fi
for CONTAINER in $CONTAINERS; do
if [[ "$ONLY" == "" ]] && [[ "$EXCLUDED" =~ $CONTAINER ]]; then
continue
elif [[ "$ONLY" != "" ]] && ! [[ "$ONLY" =~ $CONTAINER ]]; then
continue
else
STATUS=$(pct status "$CONTAINER")
if [[ "$STATUS" == "status: stopped" && "$STOPPED" == true ]]; then
# Start the container
pct start "$CONTAINER"
sleep 5
CHECK_CONTAINER "$CONTAINER"
# Stop the container
pct shutdown "$CONTAINER"
elif [[ "$STATUS" == "status: running" && "$RUNNING" == true ]]; then
CHECK_CONTAINER "$CONTAINER"
fi
fi
done
rm -rf $LOCAL_FILES/temp/temp
}
# Container Check
CHECK_CONTAINER () {
if [[ "$RDU" != true ]]; then
CONTAINER=$1
else
CONTAINER=$(awk -F'"' '/^CONTAINER=/ {print $2}' $LOCAL_FILES/temp/var)
fi
pct config "$CONTAINER" > $LOCAL_FILES/temp/temp
OS=$(awk '/^ostype/' $LOCAL_FILES/temp/temp | cut -d' ' -f2)
NAME=$(pct exec "$CONTAINER" hostname)
if [[ "$OS" =~ ubuntu ]] || [[ "$OS" =~ debian ]] || [[ "$OS" =~ devuan ]]; then
pct exec "$CONTAINER" -- bash -c "apt-get update" >/dev/null 2>&1
SECURITY_APT_UPDATES=$(pct exec "$CONTAINER" -- bash -c "apt-get -s upgrade | grep -ci ^inst.*security | tr -d '\n'")
NORMAL_APT_UPDATES=$(pct exec "$CONTAINER" -- bash -c "apt-get -s upgrade | grep -ci ^inst. | tr -d '\n'")
# NOT_INSTALLED=$(apt-get -s upgrade | grep -ci "^inst.*not")
if [[ "$SECURITY_APT_UPDATES" -gt 0 || "$NORMAL_APT_UPDATES" != 0 ]]; then
echo -e "${GN}LXC ${BL}$CONTAINER${CL} : ${GN}$NAME${CL}"
fi
if [[ "$SECURITY_APT_UPDATES" -gt 0 && "$NORMAL_APT_UPDATES" != 0 ]]; then
echo -e "S: $SECURITY_APT_UPDATES / N: $NORMAL_APT_UPDATES"
elif [[ "$SECURITY_APT_UPDATES" -gt 0 ]]; then
echo -e "S: $SECURITY_APT_UPDATES / "
elif [[ "$NORMAL_APT_UPDATES" -gt 0 ]]; then
echo -e "N: $NORMAL_APT_UPDATES"
fi
elif [[ "$OS" =~ fedora ]]; then
pct exec "$CONTAINER" -- bash -c "dnf update" >/dev/null 2>&1
UPDATES=$(pct exec "$CONTAINER" -- bash -c "dnf check-update| grep -Ec ' updates$'")
if [[ "$UPDATES" -gt 0 ]]; then
echo -e "${GN}LXC ${BL}$CONTAINER${CL} : ${GN}$NAME${CL}"
echo -e "$UPDATES"
fi
elif [[ "$OS" =~ archlinux ]]; then
pct exec "$CONTAINER" -- bash -c "pacman -Syu" >/dev/null 2>&1
UPDATES=$(pct exec "$CONTAINER" -- bash -c "pacman -Qu | wc -l")
if [[ "$UPDATES" -gt 0 ]]; then
echo -e "${GN}LXC ${BL}$CONTAINER${CL} : ${GN}$NAME${CL}"
echo -e "$UPDATES"
fi
elif [[ "$OS" =~ alpine ]]; then
pct exec "$CONTAINER" -- ash -c "apk update" >/dev/null 2>&1
else
pct exec "$CONTAINER" -- bash -c "yum update" >/dev/null 2>&1
UPDATES=$(pct exec "$CONTAINER" -- bash -c "yum -q check-update | wc -l")
if [[ "$UPDATES" -gt 0 ]]; then
echo -e "${GN}LXC ${BL}$CONTAINER${CL} : ${GN}$NAME${CL}"
echo -e "$UPDATES"
fi
fi
}
## VM ##
# VM Check Start
VM_CHECK_START () {
# Get the list of VMs
VMS=$(qm list | tail -n +2 | cut -c -10)
# Loop through the VMs
for VM in $VMS; do
PRE_OS=$(qm config "$VM" | grep 'ostype:' | sed 's/ostype:\s*//')
if [[ "$ONLY" == "" && "$EXCLUDED" =~ $VM ]]; then
continue
elif [[ "$ONLY" != "" ]] && ! [[ "$ONLY" =~ $VM ]]; then
continue
elif [[ "$PRE_OS" =~ w ]]; then
continue
else
STATUS=$(qm status "$VM")
if [[ "$STATUS" == "status: stopped" && "$STOPPED" == true ]]; then
# Check if connection is available
if [[ $(qm config "$VM" | grep 'agent:' | sed 's/agent:\s*//') == 1 ]] || [[ -f $LOCAL_FILES/VMs/"$VM" ]]; then
# Start the VM
qm start "$VM" >/dev/null 2>&1
sleep 45
CHECK_VM "$VM"
# Stop the VM
qm stop "$VM"
fi
elif [[ "$STATUS" == "status: running" && "$RUNNING" == true ]]; then
CHECK_VM "$VM"
fi
fi
done
}
# VM Check
CHECK_VM () {
if [[ "$RDU" != true ]]; then
VM=$1
else
VM=$(awk -F'"' '/^VM=/ {print $2}' $LOCAL_FILES/temp/var)
fi
NAME=$(qm config "$VM" | grep 'name:' | sed 's/name:\s*//')
if [[ -f $LOCAL_FILES/VMs/"$VM" ]]; then
IP=$(awk -F'"' '/^IP=/ {print $2}' $LOCAL_FILES/VMs/"$VM")
if ! (ssh "$IP" exit) >/dev/null 2>&1; then
CHECK_VM_QEMU
else
OS_BASE=$(qm config "$VM" | grep ostype)
if [[ "$OS_BASE" =~ l2 ]]; then
OS=$(ssh "$IP" hostnamectl | grep System)
if [[ "$OS" =~ Ubuntu ]] || [[ "$OS" =~ Debian ]] || [[ "$OS" =~ Devuan ]]; then
ssh "$IP" "apt-get update" >/dev/null 2>&1
SECURITY_APT_UPDATES=$(ssh "$IP" "apt-get -s upgrade | grep -ci ^inst.*security")
NORMAL_APT_UPDATES=$(ssh "$IP" "apt-get -s upgrade | grep -ci ^inst.")
if ssh "$IP" stat /var/run/reboot-required.pkgs \> /dev/null 2\>\&1; then REBOOT_REQUIRED=true; fi
if [[ "$SECURITY_APT_UPDATES" -gt 0 || "$NORMAL_APT_UPDATES" -gt 0 || "$REBOOT_REQUIRED" == true ]]; then
echo -e "${GN}VM ${BL}$VM${CL} : ${GN}$NAME${CL}"
fi
if [[ "$REBOOT_REQUIRED" == true ]]; then echo -e "${OR} Reboot required${CL}"; fi
if [[ "$SECURITY_APT_UPDATES" -gt 0 && "$NORMAL_APT_UPDATES" -gt 0 ]]; then
echo -e "S: $SECURITY_APT_UPDATES / N: $NORMAL_APT_UPDATES"
elif [[ "$SECURITY_APT_UPDATES" -gt 0 ]]; then
echo -e "S: $SECURITY_APT_UPDATES / "
elif [[ "$NORMAL_APT_UPDATES" -gt 0 ]]; then
echo -e "N: $NORMAL_APT_UPDATES"
fi
elif [[ "$OS" =~ Fedora ]]; then
ssh "$IP" "dnf -y update" >/dev/null 2>&1
UPDATES=$(ssh "$IP" "dnf check-update| grep -Ec ' updates$'")
if [[ "$UPDATES" -gt 0 ]]; then
echo -e "${GN}VM ${BL}$VM${CL} : ${GN}$NAME${CL}"
echo -e "$UPDATES"
fi
elif [[ "$OS" =~ Arch ]]; then
UPDATES=$(ssh "$IP" "pacman -Qu | wc -l")
if [[ "$UPDATES" -gt 0 ]]; then
echo -e "${GN}VM ${BL}$VM${CL} : ${GN}$NAME${CL}"
echo -e "$UPDATES"
fi
elif [[ "$OS" =~ Alpine ]]; then
return
elif [[ "$OS" =~ CentOS ]]; then
UPDATES=$(ssh "$IP" "yum -q check-update | wc -l")
if [[ "$UPDATES" -gt 0 ]]; then
echo -e "${GN}VM ${BL}$VM${CL} : ${GN}$NAME${CL}"
echo -e "$UPDATES"
fi
fi
fi
fi
else
CHECK_VM_QEMU
fi
}
CHECK_VM_QEMU () {
if qm guest exec "$VM" test >/dev/null 2>&1; then
OS=$(qm guest cmd "$VM" get-osinfo | grep name)
if [[ "$OS" =~ Ubuntu ]] || [[ "$OS" =~ Debian ]] || [[ "$OS" =~ Devuan ]]; then
qm guest exec "$VM" -- bash -c "apt-get update" >/dev/null 2>&1
SECURITY_APT_UPDATES=$(qm guest exec "$VM" -- bash -c "apt-get -s upgrade | grep -ci ^inst.*security | tr -d '\n'" | tail -n +4 | head -n -1 | cut -c 18- | rev | cut -c 2- | rev)
NORMAL_APT_UPDATES=$(qm guest exec "$VM" -- bash -c "apt-get -s upgrade | grep -ci ^inst. | tr -d '\n'" | tail -n +4 | head -n -1 | cut -c 18- | rev | cut -c 2- | rev)
if [[ $(qm guest exec "$VM" -- bash -c "[ -f /var/run/reboot-required.pkgs ]" | grep exitcode) =~ 0 ]]; then REBOOT_REQUIRED=true; fi
if [[ "$SECURITY_APT_UPDATES" -gt 0 || "$NORMAL_APT_UPDATES" -gt 0 || "$REBOOT_REQUIRED" == true ]]; then
echo -e "${GN}VM ${BL}$VM${CL} : ${GN}$NAME${CL}"
fi
if [[ "$REBOOT_REQUIRED" == true ]]; then echo -e "${OR} Reboot required${CL}"; fi
if [[ "$SECURITY_APT_UPDATES" -gt 0 && "$NORMAL_APT_UPDATES" -gt 0 ]]; then
echo -e "S: $SECURITY_APT_UPDATES / N: $NORMAL_APT_UPDATES"
elif [[ "$SECURITY_APT_UPDATES" -gt 0 ]]; then
echo -e "S: $SECURITY_APT_UPDATES / "
elif [[ "$NORMAL_APT_UPDATES" -gt 0 ]]; then
echo -e "N: $NORMAL_APT_UPDATES"
fi
elif [[ "$OS" =~ Fedora ]]; then
qm guest exec "$VM" -- bash -c "dnf -y update" >/dev/null 2>&1
UPDATES=$(qm guest exec "$VM" -- bash -c "dnf check-update | grep -Ec ' updates$'" | tail -n +4 | head -n -1 | cut -c 18- | rev | cut -c 2- | rev)
if [[ "$UPDATES" -gt 0 ]]; then
echo -e "${GN}VM ${BL}$VM${CL} : ${GN}$NAME${CL}"
echo -e "$UPDATES"
fi
elif [[ "$OS" =~ Arch ]]; then
UPDATES=$(qm guest exec "$VM" -- bash -c "pacman -Qu | wc -l" | tail -n +4 | head -n -1 | cut -c 18- | rev | cut -c 2- | rev)
if [[ "$UPDATES" -gt 0 ]]; then
echo -e "${GN}VM ${BL}$VM${CL} : ${GN}$NAME${CL}"
echo -e "$UPDATES"
fi
elif [[ "$OS" =~ Alpine ]]; then
return
elif [[ "$OS" =~ CentOS ]]; then
UPDATES=$(qm guest exec "$VM" -- bash -c "yum -q check-update | wc -l" | tail -n +4 | head -n -1 | cut -c 18- | rev | cut -c 2- | rev)
if [[ "$UPDATES" -gt 0 ]]; then
echo -e "${GN}VM ${BL}$VM${CL} : ${GN}$NAME${CL}"
echo -e "$UPDATES"
fi
fi
fi
}
# Output to file
OUTPUT_TO_FILE () {
if [[ "$RDU" != true && "$RICM" != true ]]; then
touch $LOCAL_FILES/check-output
exec > >(tee $LOCAL_FILES/check-output)
fi
}
# Check Cluster Mode
if [[ -f /etc/corosync/corosync.conf ]]; then
HOSTS=$(awk '/ring0_addr/{print $2}' "/etc/corosync/corosync.conf")
MODE="Cluster"
else
MODE="Host"
fi
# Run
if [[ "$(wget -q --spider http://google.com)" -eq 0 ]]; then
READ_WRITE_CONFIG
ARGUMENTS "$@"
else
echo -e "${OR} You are offline${CL}"
exit 2
fi
# Run without commands (Automatic Mode)
if [[ "$COMMAND" != true && "$RDU" == true ]]; then
OUTPUT_TO_FILE
CHECK_RUNNUNG_MACHINE
elif [[ "$COMMAND" != true ]]; then
OUTPUT_TO_FILE
if [[ "$MODE" =~ Cluster ]]; then HOST_CHECK_START; else
if [[ "$WITH_HOST" == true ]]; then CHECK_HOST_ITSELF; fi
if [[ "$WITH_LXC" == true ]]; then CONTAINER_CHECK_START; fi
if [[ "$WITH_VM" == true ]]; then VM_CHECK_START; fi
fi
fi