Add option to updater to report status of auto-updates on the system. (#19248)
* Add option to updater to report status of auto-updates on the system. * Improve systemd detection. The _official_ way to check if a system is running systemd is to call `systemctl is-system-running` and check the output. This adds that checking to places where we are otherwise looking for systemd. * Remove pointless subshell.
Austin S. Hemmelgarn committed
Jan 8, 2025 at 06:37 UTC
64b9f5e280a7a2ac9f781b5571d7b3348dcb593a
4 files changed
+157
packaging/installer/functions.sh
+12
@@ -608,6 +608,18 @@ issystemd() {
608
return 1
609
fi
610
611
+ # Check the output of systemctl is-system-running.
612
+ # If this reports 'offline', it’s not systemd. If it reports 'unknown'
613
+ # or nothing at all (which indicates the command is not supported), it
614
+ # may or may not be systemd, so continue to other checks. If it reports
615
+ # anything else, it is systemd.
616
+ case "$(systemctl is-system-running)" in
617
+ offline) return 1 ;;
618
+ unknown) : ;;
619
+ "") : ;;
620
+ *) return 0 ;;
621
+ esac
622
+
623
# if pid 1 is systemd, it is systemd
624
[ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
625
packaging/installer/netdata-uninstaller.sh
+12
@@ -474,6 +474,18 @@ issystemd() {
474
return 1
475
fi
476
477
+ # Check the output of systemctl is-system-running.
478
+ # If this reports 'offline', it’s not systemd. If it reports 'unknown'
479
+ # or nothing at all (which indicates the command is not supported), it
480
+ # may or may not be systemd, so continue to other checks. If it reports
481
+ # anything else, it is systemd.
482
+ case "$(systemctl is-system-running)" in
483
+ offline) return 1 ;;
484
+ unknown) : ;;
485
+ "") : ;;
486
+ *) return 0 ;;
487
+ esac
488
+
489
# if pid 1 is systemd, it is systemd
490
[ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
491
packaging/installer/netdata-updater.sh
+120
@@ -111,6 +111,20 @@ is_integer () {
111
esac
112
}
113
114
+safe_pidof() {
115
+ pidof_cmd="$(command -v pidof 2> /dev/null)"
116
+ if [ -n "${pidof_cmd}" ]; then
117
+ ${pidof_cmd} "${@}"
118
+ return $?
119
+ else
120
+ ps -acxo pid,comm |
121
+ sed "s/^ *//g" |
122
+ grep netdata |
123
+ cut -d ' ' -f 1
124
+ return $?
125
+ fi
126
+}
127
+
128
issystemd() {
129
# if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
130
if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
@@ -123,6 +137,23 @@ issystemd() {
137
return 1
138
fi
139
140
+ # Check the output of systemctl is-system-running.
141
+ # If this reports 'offline', it’s not systemd. If it reports 'unknown'
142
+ # or nothing at all (which indicates the command is not supported), it
143
+ # may or may not be systemd, so continue to other checks. If it reports
144
+ # anything else, it is systemd.
145
+ #
146
+ # This may return a non-zero exit status in cases when it actually
147
+ # succeeded for our purposes, so we need to toggle set -e off here.
148
+ set +e
149
+ case "$(systemctl is-system-running)" in
150
+ offline) return 1 ;;
151
+ unknown) : ;;
152
+ "") : ;;
153
+ *) return 0 ;;
154
+ esac
155
+ set -e
156
+
157
# if pid 1 is systemd, it is systemd
158
[ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && return 0
159
@@ -346,6 +377,91 @@ disable_netdata_updater() {
377
return 0
378
}
379
380
+auto_update_status() {
381
+ case "$(_get_scheduler_type)" in
382
+ systemd) info "The default auto-update scheduling method for this system is: systemd timer units" ;;
383
+ crontab) info "The default auto-update scheduling method for this system is: drop-in crontab" ;;
384
+ interval) info "The default auto-update scheduling method for this system is: drop-in periodic script" ;;
385
+ *) info "No recognized auto-update scheduling method found" ; return ;;
386
+ esac
387
+
388
+ duplicate=""
389
+ enabled=""
390
+
391
+ if issystemd; then
392
+ if systemctl list-units --full -all | grep -Fq "netdata-updater.timer"; then
393
+ if systemctl is-enabled netdata-updater.timer; then
394
+ info "Auto-updates using a systemd timer unit are ENABLED"
395
+ enabled="systemd"
396
+ else
397
+ info "Auto-updates using a systemd timer unit are DISABLED"
398
+ fi
399
+ else
400
+ info "Auto-updates using a systemd timer unit are NOT SUPPORTED due to: Required unit files not installed"
401
+ fi
402
+ else
403
+ info "Auto-updates using a systemd timer unit are NOT SUPPORTED due to: Systemd not present"
404
+ fi
405
+
406
+ interval_found=""
407
+
408
+ if [ -d /etc/cron.daily ]; then
409
+ interval_found="1"
410
+
411
+ if [ -x /etc/cron.daily/netdata-updater.sh ] || [ -x /etc/cron.daily/netdata-updater ]; then
412
+ info "Auto-updates using a drop-in periodic script in /etc/cron.daily are ENABLED"
413
+
414
+ if [ -n "${enabled}" ]; then
415
+ duplicate="1"
416
+ else
417
+ enabled="cron.daily"
418
+ fi
419
+ else
420
+ info "Auto-updates using a drop-in periodic script in /etc/cron.daily are DISABLED"
421
+ fi
422
+ else
423
+ info "Auto-updates using a drop-in periodic script in /etc/cron.daily are NOT SUPPORTED: due to: Directory does not exist"
424
+ fi
425
+
426
+ if [ -d /etc/periodic/daily ]; then
427
+ if [ -x /etc/periodic/daily/netdata-updater.sh ] || [ -x /etc/periodic/daily/netdata-updater ]; then
428
+ info "Auto-updates using a drop-in periodic script in /etc/periodic/daily are ENABLED"
429
+
430
+ if [ -n "${enabled}" ]; then
431
+ duplicate="1"
432
+ else
433
+ enabled="periodic/daily"
434
+ fi
435
+ else
436
+ if [ -z "${interval_found}" ]; then
437
+ info "Auto-updates using a drop-in periodic script in /etc/periodic/daily are DISABLED"
438
+ fi
439
+ fi
440
+ elif [ -z "${interval_found}" ]; then
441
+ info "Auto-updates using a drop-in periodic script in /etc/periodic/daily are NOT SUPPORTED due to: Directory does not exist"
442
+ fi
443
+
444
+ if [ -d /etc/cron.d ]; then
445
+ if [ -f /etc/cron.d/netdata-updater ] || [ -f /etc/cron.d/netdata-updater-daily ]; then
446
+ info "Auto-updates using a drop-in crontab are ENABLED"
447
+
448
+ if [ -n "${enabled}" ]; then
449
+ duplicate="1"
450
+ else
451
+ enabled="cron.d"
452
+ fi
453
+ else
454
+ info "Auto-updates using a drop-in crontab are DISABLED"
455
+ fi
456
+ else
457
+ info "Auto-updates using a drop-in crontab are NOT SUPPORTED due to: Directory does not exist"
458
+ fi
459
+
460
+ if [ -n "${duplicate}" ]; then
461
+ warning "More than one method of auto-updates is enabled! Please disable and re-enable auto-updates to correct this."
462
+ fi
463
+}
464
+
465
str_in_list() {
466
printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
467
return $?
@@ -1123,6 +1239,10 @@ while [ -n "${1}" ]; do
1239
disable_netdata_updater
1240
exit $?
1241
;;
1242
+ --auto-update-status)
1243
+ auto_update_status
1244
+ exit 0
1245
+ ;;
1246
*) fatal "Unrecognized option ${1}" U001A ;;
1247
esac
1248
system/install-service.sh.in
+13
@@ -193,6 +193,19 @@ _check_systemd() {
193
# if there is no systemctl command, it is not systemd
194
[ -z "$(command -v systemctl 2>/dev/null || true)" ] && echo "NO" && return 0
195
196
+ # Check the output of systemctl is-system-running.
197
+ #
198
+ # This may return a non-zero exit status in cases when it actually
199
+ # succeeded for our purposes, so we need to toggle set -e off here.
200
+ set +e
201
+ case "$(systemctl is-system-running)" in
202
+ offline) echo "OFFLINE" && return 0 ;;
203
+ unknown) : ;;
204
+ "") : ;;
205
+ *) echo "YES" && return 0 ;;
206
+ esac
207
+ set -e
208
+
209
# if pid 1 is systemd, it is systemd
210
[ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && echo "YES" && return 0
211