-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkickstart.sh
2506 lines (2179 loc) · 90 KB
/
kickstart.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Next unused error code: F051A
# ======================================================================
# Constants
DEFAULT_RELEASE_CHANNEL="nightly"
KICKSTART_OPTIONS="${*}"
KICKSTART_SOURCE="$(
self=${0}
while [ -L "${self}" ]
do
cd "${self%/*}" || exit 1
self=$(readlink "${self}")
done
cd "${self%/*}" || exit 1
echo "$(pwd -P)/${self##*/}"
)"
DEFAULT_PLUGIN_PACKAGES=""
PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
REPOCONFIG_DEB_VERSION="3-2"
REPOCONFIG_RPM_VERSION="3-2"
START_TIME="$(date +%s)"
STATIC_INSTALL_ARCHES="x86_64 armv7l armv6l aarch64 ppc64le"
# ======================================================================
# URLs used throughout the script
AGENT_BUG_REPORT_URL="https://github.com/netdata/netdata/issues/new/choose"
CLOUD_BUG_REPORT_URL="https://github.com/netdata/netdata-cloud/issues/new/choose"
DISCORD_INVITE="https://discord.gg/5ygS846fR6"
DISCUSSIONS_URL="https://github.com/netdata/netdata/discussions"
DOCS_URL="https://learn.netdata.cloud/docs/"
FORUM_URL="https://community.netdata.cloud/"
INSTALL_DOC_URL="https://learn.netdata.cloud/docs/install-the-netdata-agent/one-line-installer-for-all-linux-systems"
PACKAGES_SCRIPT="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/install-required-packages.sh"
PUBLIC_CLOUD_URL="https://app.netdata.cloud"
RELEASE_INFO_URL="https://repo.netdata.cloud/releases"
REPOCONFIG_DEB_URL_PREFIX="https://repo.netdata.cloud/repos/repoconfig"
REPOCONFIG_RPM_URL_PREFIX="https://repo.netdata.cloud/repos/repoconfig"
TELEMETRY_URL="https://us-east1-netdata-analytics-bi.cloudfunctions.net/ingest_agent_events"
# ======================================================================
# Defaults for environment variables
DRY_RUN=0
SELECTED_INSTALL_METHOD="none"
INSTALL_TYPE="unknown"
INSTALL_PREFIX=""
NETDATA_AUTO_UPDATES="default"
NETDATA_CLAIM_URL="https://app.netdata.cloud"
NETDATA_COMMAND="default"
NETDATA_DISABLE_CLOUD=0
NETDATA_INSTALLER_OPTIONS=""
NETDATA_FORCE_METHOD=""
NETDATA_OFFLINE_INSTALL_SOURCE=""
NETDATA_REQUIRE_CLOUD=1
NETDATA_WARNINGS=""
RELEASE_CHANNEL="default"
if [ -n "$DISABLE_TELEMETRY" ]; then
NETDATA_DISABLE_TELEMETRY="${DISABLE_TELEMETRY}"
elif [ -n "$DO_NOT_TRACK" ]; then
NETDATA_DISABLE_TELEMETRY="${DO_NOT_TRACK}"
else
NETDATA_DISABLE_TELEMETRY=0
fi
NETDATA_TARBALL_BASEURL="${NETDATA_TARBALL_BASEURL:-https://github.com/netdata/netdata-nightlies/releases}"
if echo "${0}" | grep -q 'kickstart-static64'; then
NETDATA_FORCE_METHOD='static'
fi
if [ ! -t 1 ]; then
INTERACTIVE=0
else
INTERACTIVE=1
fi
CURL="$(PATH="${PATH}:/opt/netdata/bin" command -v curl 2>/dev/null && true)"
# ======================================================================
# Shared messages used in multiple places throughout the script.
BADCACHE_MSG="Usually this is a result of an older copy of the file being cached somewhere upstream and can be resolved by retrying in an hour"
BADNET_MSG="This is usually a result of a networking issue"
ERROR_F0003="Could not find a usable HTTP client. Either curl or wget is required to proceed with installation."
BADOPT_MSG="If you are following a third-party guide online, please see ${INSTALL_DOC_URL} for current instructions for using this script. If you are using a local copy of this script instead of fetching it from our servers, consider updating it. If you intended to pass this option to the installer code, please use either --local-build-options or --static-install-options to specify it instead."
# ======================================================================
# Core program logic
main() {
case "${ACTION}" in
uninstall)
uninstall
printf >&2 "Finished uninstalling the Netdata Agent."
deferred_warnings
cleanup
trap - EXIT
exit 0
;;
reinstall-clean)
NEW_INSTALL_PREFIX="${INSTALL_PREFIX}"
uninstall
cleanup
ACTION=''
INSTALL_PREFIX="${NEW_INSTALL_PREFIX}"
# shellcheck disable=SC2086
main
trap - EXIT
exit 0
;;
prepare-offline)
prepare_offline_install_source "${OFFLINE_TARGET}"
deferred_warnings
trap - EXIT
exit 0
;;
esac
handle_existing_install
set_tmpdir
if [ -n "${INSTALL_VERSION}" ]; then
if echo "${INSTALL_VERSION}" | grep -E -o "^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+$" > /dev/null 2>&1; then
NEW_SELECTED_RELEASE_CHANNEL="stable"
else
NEW_SELECTED_RELEASE_CHANNEL="nightly"
fi
if ! [ "${NEW_SELECTED_RELEASE_CHANNEL}" = "${SELECTED_RELEASE_CHANNEL}" ]; then
warning "Selected release channel does not match this version and it will be changed automatically."
SELECTED_RELEASE_CHANNEL="${NEW_SELECTED_RELEASE_CHANNEL}"
fi
fi
case "${SYSTYPE}" in
Linux) install_on_linux ;;
Darwin) install_on_macos ;;
FreeBSD) install_on_freebsd ;;
esac
if [ -n "${NETDATA_CLAIM_TOKEN}" ]; then
claim
elif [ "${NETDATA_DISABLE_CLOUD}" -eq 1 ]; then
soft_disable_cloud
fi
set_auto_updates
printf >&2 "%s\n\n" "Successfully installed the Netdata Agent."
deferred_warnings
success_banner
telemetry_event INSTALL_SUCCESS "" ""
cleanup
trap - EXIT
}
# ======================================================================
# Usage info
usage() {
cat << HEREDOC
USAGE: kickstart.sh [options]
where options include:
--non-interactive Do not prompt for user input. (default: prompt if there is a controlling terminal)
--interactive Prompt for user input even if there is no controlling terminal.
--dont-start-it Do not start the agent by default (only for static installs or local builds)
--dry-run Report what we would do with the given options on this system, but don’t actually do anything.
--release-channel Specify the release channel to use for the install (default: ${DEFAULT_RELEASE_CHANNEL})
--stable-channel Equivalent to "--release-channel stable"
--nightly-channel Equivalent to "--release-channel nightly"
--no-updates Do not enable automatic updates (default: enable automatic updates using the best supported scheduling method)
--auto-update Enable automatic updates.
--auto-update-type Specify a particular scheduling type for auto-updates (valid types: systemd, interval, crontab)
--disable-telemetry Opt-out of anonymous statistics.
--native-only Only install if native binary packages are available.
--static-only Only install if a static build is available.
--build-only Only install using a local build.
--disable-cloud Disable support for Netdata Cloud (default: detect)
--require-cloud Only install if Netdata Cloud can be enabled. Overrides --disable-cloud.
--install-prefix <path> Specify an installation prefix for local builds (default: autodetect based on system type).
--old-install-prefix <path> Specify an old local builds installation prefix for uninstall/reinstall (if it's not default).
--install-version <version> Specify the version of Netdata to install.
--claim-token Use a specified token for claiming to Netdata Cloud.
--claim-rooms When claiming, add the node to the specified rooms.
--claim-* Specify other options for the claiming script.
--no-cleanup Don't do any cleanup steps. This is intended to help with debugging the installer.
--local-build-options Specify additional options to pass to the installer code when building locally. Only valid if --build-only is also specified.
--static-install-options Specify additional options to pass to the static installer code. Only valid if --static-only is also specified.
--offline-architecture Limit an offline install source being prepared with --prepare-offline-install-source to only include the specified static build architecture.
The following options are mutually exclusive and specifiy special operations other than trying to install Netdata normally or update an existing install:
--reinstall If there is an existing install, reinstall it instead of trying to update it. If there is no existing install, install netdata normally.
--reinstall-even-if-unsafe If there is an existing install, reinstall it instead of trying to update it, even if doing so is known to potentially break things. If there is no existing install, install Netdata normally.
--reinstall-clean If there is an existing install, uninstall it before trying to install Netdata. Fails if there is no existing install.
--uninstall Uninstall an existing installation of Netdata. Fails if there is no existing install.
--claim-only If there is an existing install, only try to claim it without attempting to update it. If there is no existing install, install and claim Netdata normally.
--repositories-only Only install repository configuration packages instead of doing a full install of Netdata. Automatically sets --native-only.
--prepare-offline-install-source Instead of installing the agent, prepare a directory that can be used to install on another system without needing to download anything.
Additionally, this script may use the following environment variables:
TMPDIR: Used to specify where to put temporary files. On most systems, the default we select
automatically should be fine. The user running the script needs to both be able to
write files to the temporary directory, and run files from that location.
ROOTCMD: Used to specify a command to use to run another command with root privileges if needed. By
default we try to use sudo, doas, or pkexec (in that order of preference), but if
you need special options for one of those to work, or have a different tool to do
the same thing on your system, you can specify it here.
DISABLE_TELEMETRY If set to a value other than 0, behave as if \`--disable-telemetry\` was specified.
HEREDOC
}
# ======================================================================
# Telemetry functions
telemetry_event() {
if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ] || [ "${DRY_RUN}" -eq 1 ]; then
return 0
fi
now="$(date +%s)"
total_duration="$((now - START_TIME))"
if [ -e "/etc/os-release" ]; then
eval "$(grep -E "^(NAME|ID|ID_LIKE|VERSION|VERSION_ID)=" < /etc/os-release | sed 's/^/HOST_/')"
fi
if [ -z "${HOST_NAME}" ] || [ -z "${HOST_VERSION}" ] || [ -z "${HOST_ID}" ]; then
if [ -f "/etc/lsb-release" ]; then
DISTRIB_ID="unknown"
DISTRIB_RELEASE="unknown"
DISTRIB_CODENAME="unknown"
eval "$(grep -E "^(DISTRIB_ID|DISTRIB_RELEASE|DISTRIB_CODENAME)=" < /etc/lsb-release)"
if [ -z "${HOST_NAME}" ]; then HOST_NAME="${DISTRIB_ID}"; fi
if [ -z "${HOST_VERSION}" ]; then HOST_VERSION="${DISTRIB_RELEASE}"; fi
if [ -z "${HOST_ID}" ]; then HOST_ID="${DISTRIB_CODENAME}"; fi
fi
fi
KERNEL_NAME="$(uname -s)"
if [ "${KERNEL_NAME}" = FreeBSD ]; then
TOTAL_RAM="$(sysctl -n hw.physmem)"
elif [ "${KERNEL_NAME}" = Darwin ]; then
TOTAL_RAM="$(sysctl -n hw.memsize)"
elif [ -r /proc/meminfo ]; then
TOTAL_RAM="$(grep -F MemTotal /proc/meminfo | cut -f 2 -d ':' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | cut -f 1 -d ' ')"
TOTAL_RAM="$((TOTAL_RAM * 1024))"
fi
MD5_PATH="$(exec <&- 2>&-; which md5sum || command -v md5sum || type md5sum)"
if [ "${KERNEL_NAME}" = Darwin ] && command -v ioreg >/dev/null 2>&1; then
DISTINCT_ID="macos-$(ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }')"
elif [ -f /etc/machine-id ] && [ -n "$MD5_PATH" ]; then
DISTINCT_ID="machine-$($MD5_PATH < /etc/machine-id | cut -f1 -d" ")"
elif [ -f /var/db/dbus/machine-id ] && [ -n "$MD5_PATH" ]; then
DISTINCT_ID="dbus-$($MD5_PATH < /var/db/dbus/machine-id | cut -f1 -d" ")"
elif [ -f /var/lib/dbus/machine-id ] && [ -n "$MD5_PATH" ]; then
DISTINCT_ID="dbus-$($MD5_PATH < /var/lib/dbus/machine-id | cut -f1 -d" ")"
elif command -v uuidgen > /dev/null 2>&1; then
DISTINCT_ID="uuid-$(uuidgen | tr '[:upper:]' '[:lower:]')"
else
DISTINCT_ID="null"
fi
REQ_BODY="$(cat << EOF
{
"event": "${1}",
"properties": {
"distinct_id": "${DISTINCT_ID}",
"event_source": "agent installer",
"\$current_url": "agent installer",
"\$pathname": "netdata-installer",
"\$host": "installer.netdata.io",
"\$ip": "127.0.0.1",
"script_variant": "kickstart-ng",
"error_code": "${3}",
"error_message": "${2}",
"install_options": "${KICKSTART_OPTIONS}",
"install_interactivity": "${INTERACTIVE}",
"install_auto_updates": "${NETDATA_AUTO_UPDATES}",
"install_command": "${NETDATA_COMMAND}",
"total_runtime": "${total_duration}",
"selected_install_method": "${SELECTED_INSTALL_METHOD}",
"netdata_release_channel": "${RELEASE_CHANNEL:-null}",
"netdata_install_type": "${INSTALL_TYPE}",
"host_os_name": "${HOST_NAME:-unknown}",
"host_os_id": "${HOST_ID:-unknown}",
"host_os_id_like": "${HOST_ID_LIKE:-unknown}",
"host_os_version": "${HOST_VERSION:-unknown}",
"host_os_version_id": "${HOST_VERSION_ID:-unknown}",
"system_kernel_name": "${KERNEL_NAME}",
"system_kernel_version": "$(uname -r)",
"system_architecture": "$(uname -m)",
"system_total_ram": "${TOTAL_RAM:-unknown}",
"system_distinct_id": "${DISTINCT_ID}"
}
}
EOF
)"
succeeded=0
if [ -n "${CURL}" ]; then
if "${CURL}" --silent -o /dev/null -X POST --max-time 2 --header "Content-Type: application/json" -d "${REQ_BODY}" "${TELEMETRY_URL}" > /dev/null; then
succeeded=1
fi
fi
if [ "${succeeded}" -eq 0 ]; then
if command -v wget > /dev/null 2>&1; then
if wget --help 2>&1 | grep BusyBox > /dev/null 2>&1; then
# BusyBox-compatible version of wget, there is no --no-check-certificate option
wget -q -O - \
-T 1 \
--header 'Content-Type: application/json' \
--post-data "${REQ_BODY}" \
"${TELEMETRY_URL}" > /dev/null
else
wget -q -O - --no-check-certificate \
--method POST \
--timeout=1 \
--header 'Content-Type: application/json' \
--body-data "${REQ_BODY}" \
"${TELEMETRY_URL}" > /dev/null
fi
fi
fi
}
trap_handler() {
code="${1}"
lineno="${2}"
deferred_warnings
printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ERROR ${TPUT_RESET} Installer exited unexpectedly (${code}-${lineno})"
case "${code}" in
0) printf >&2 "%s\n" "This is almost certainly the result of a bug. If you have time, please report it at ${AGENT_BUG_REPORT_URL}." ;;
*)
printf >&2 "%s\n" "This is probably a result of a transient issue on your system. Things should work correctly if you try again."
printf >&2 "%s\n" "If you continue to experience this issue, you can reacn out to us for support on:"
support_list
;;
esac
telemetry_event INSTALL_CRASH "Installer exited unexpectedly (${code}-${lineno})" "E${code}-${lineno}"
trap - EXIT
cleanup
exit 1
}
trap 'trap_handler 0 ${LINENO}' EXIT
trap 'trap_handler 1 0' HUP
trap 'trap_handler 2 0' INT
trap 'trap_handler 3 0' QUIT
trap 'trap_handler 13 0' PIPE
trap 'trap_handler 15 0' TERM
# ======================================================================
# Utility functions
canonical_path() {
cd "$(dirname "${1}")" || exit 1
case "$(basename "${1}")" in
..) dirname "$(pwd -P)" ;;
.) pwd -P ;;
*) echo "$(pwd -P)/$(basename "${1}")" ;;
esac
}
setup_terminal() {
TPUT_RESET=""
TPUT_WHITE=""
TPUT_BGRED=""
TPUT_BGGREEN=""
TPUT_BOLD=""
TPUT_DIM=""
# Is stderr on the terminal? If not, then fail
test -t 2 || return 1
if command -v tput > /dev/null 2>&1; then
if num_colors=$(tput colors 2> /dev/null) && [ "${num_colors:-0}" -ge 8 ]; then
# Enable colors
TPUT_RESET="$(tput sgr 0)"
TPUT_WHITE="$(tput setaf 7)"
TPUT_BGRED="$(tput setab 1)"
TPUT_BGGREEN="$(tput setab 2)"
TPUT_BOLD="$(tput bold)"
TPUT_DIM="$(tput dim)"
fi
fi
echo "${TPUT_RESET}"
return 0
}
support_list() {
printf >&2 "%s\n" " - GitHub: ${DISCUSSIONS_URL}"
printf >&2 "%s\n" " - Discord: ${DISCORD_INVITE}"
printf >&2 "%s\n" " - Our community forums: ${FORUM_URL}"
}
success_banner() {
printf >&2 "%s\n" "To view your system's real-time performance metrics, open your web browser and enter http://NODE:19999."
printf >&2 "%s\n\n" "Replace NODE with the IP address or hostname of your Netdata server to access the dashboard."
printf >&2 "%s\n\n" "Official documentation can be found online at ${DOCS_URL}."
if [ -z "${CLAIM_TOKEN}" ]; then
printf >&2 "%s\n\n" "Looking to monitor all of your infrastructure with Netdata? Check out Netdata Cloud at ${PUBLIC_CLOUD_URL}."
fi
printf >&2 "%s\n" "Join our community and connect with us on:"
support_list
}
cleanup() {
if [ -z "${NO_CLEANUP}" ] && [ -n "${tmpdir}" ]; then
cd || true
DRY_RUN=0
run_as_root rm -rf "${tmpdir}"
fi
}
deferred_warnings() {
if [ -n "${NETDATA_WARNINGS}" ]; then
printf >&2 "%s\n" "The following non-fatal warnings or errors were encountered:"
# shellcheck disable=SC2059
printf >&2 "${NETDATA_WARNINGS}"
printf >&2 "\n\n"
fi
}
fatal() {
deferred_warnings
printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} ABORTED ${TPUT_RESET} ${1}"
printf >&2 "%s\n" "For community support, you can connect with us on:"
support_list
telemetry_event "INSTALL_FAILED" "${1}" "${2}"
cleanup
trap - EXIT
exit 1
}
ESCAPED_PRINT_METHOD=
# shellcheck disable=SC3050
if printf "%s " test > /dev/null 2>&1; then
ESCAPED_PRINT_METHOD="printfq"
fi
escaped_print() {
if [ "${ESCAPED_PRINT_METHOD}" = "printfq" ]; then
# shellcheck disable=SC3050
printf "%s " "${@}"
else
printf "%s" "${*}"
fi
return 0
}
progress() {
echo >&2 " --- ${TPUT_BOLD}${*}${TPUT_RESET} --- "
}
run_logfile="/dev/null"
run() {
user="${USER--}"
dir="${PWD}"
if [ "$(id -u)" = "0" ]; then
info="[root ${dir}]# "
info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]# "
else
info="[${user} ${dir}]$ "
info_console="[${TPUT_DIM}${dir}${TPUT_RESET}]$ "
fi
if [ "${DRY_RUN}" -eq 1 ]; then
printf >&2 "%s" "Would run command:\n"
fi
{
printf "%s" "${info}"
escaped_print "${@}"
printf " ... "
} >> "${run_logfile}"
printf >&2 "%s" "${info_console}${TPUT_BOLD}"
escaped_print >&2 "${@}"
printf >&2 "%s\n" "${TPUT_RESET}"
if [ "${DRY_RUN}" -ne 1 ]; then
"${@}"
ret=$?
else
ret=0
fi
if [ ${ret} -ne 0 ]; then
printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} FAILED ${TPUT_RESET}"
printf "%s\n" "FAILED with exit code ${ret}" >> "${run_logfile}"
# shellcheck disable=SC2089
NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - Command \"${*}\" failed with exit code ${ret}."
else
printf >&2 "%s\n\n" "${TPUT_BGGREEN}${TPUT_WHITE}${TPUT_BOLD} OK ${TPUT_RESET}"
printf "OK\n" >> "${run_logfile}"
fi
return ${ret}
}
run_as_root() {
confirm_root_support
if [ "$(id -u)" -ne "0" ]; then
printf >&2 "Root privileges required to run %s\n" "${*}"
fi
run ${ROOTCMD} "${@}"
}
run_script() {
set_tmpdir
export NETDATA_SCRIPT_STATUS_PATH="${tmpdir}/.script-status"
export NETDATA_SAVE_WARNINGS=1
export NETDATA_PROPAGATE_WARNINGS=1
# shellcheck disable=SC2090
export NETDATA_WARNINGS="${NETDATA_WARNINGS}"
# shellcheck disable=SC2086
run ${ROOTCMD} "${@}"
ret="$?"
if [ -r "${NETDATA_SCRIPT_STATUS_PATH}" ]; then
# shellcheck disable=SC1090
. "${NETDATA_SCRIPT_STATUS_PATH}"
rm -f "${NETDATA_SCRIPT_STATUS_PATH}"
fi
return "${ret}"
}
warning() {
printf >&2 "%s\n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD} WARNING ${TPUT_RESET} ${*}"
NETDATA_WARNINGS="${NETDATA_WARNINGS}\n - ${*}"
}
_cannot_use_tmpdir() {
testfile="$(TMPDIR="${1}" mktemp -q -t netdata-test.XXXXXXXXXX)"
ret=0
if [ -z "${testfile}" ]; then
return "${ret}"
fi
if printf '#!/bin/sh\necho SUCCESS\n' > "${testfile}"; then
if chmod +x "${testfile}"; then
if [ "$("${testfile}")" = "SUCCESS" ]; then
ret=1
fi
fi
fi
rm -f "${testfile}"
return "${ret}"
}
create_tmp_directory() {
if [ -z "${TMPDIR}" ] || _cannot_use_tmpdir "${TMPDIR}"; then
if _cannot_use_tmpdir /tmp; then
if _cannot_use_tmpdir "${PWD}"; then
fatal "Unable to find a usable temporary directory. Please set \$TMPDIR to a path that is both writable and allows execution of files and try again." F0400
else
TMPDIR="${PWD}"
fi
else
TMPDIR="/tmp"
fi
fi
mktemp -d -t netdata-kickstart-XXXXXXXXXX
}
set_tmpdir() {
if [ -z "${tmpdir}" ] || [ ! -d "${tmpdir}" ]; then
tmpdir="$(create_tmp_directory)"
progress "Using ${tmpdir} as a temporary directory."
cd "${tmpdir}" || fatal "Failed to change current working directory to ${tmpdir}." F000A
fi
}
check_for_remote_file() {
url="${1}"
succeeded=0
checked=0
if echo "${url}" | grep -Eq "^file:///"; then
[ -e "${url#file://}" ] || return 1
return 0
elif [ -n "${NETDATA_ASSUME_REMOTE_FILES_ARE_PRESENT}" ]; then
return 0
fi
if [ -n "${CURL}" ]; then
checked=1
if "${CURL}" --output /dev/null --silent --head --fail "${url}"; then
succeeded=1
fi
fi
if [ "${succeeded}" -eq 0 ]; then
if command -v wget > /dev/null 2>&1; then
checked=1
if wget -S --spider "${url}" 2>&1 | grep -q 'HTTP/1.1 200 OK'; then
succeeded=1
fi
fi
fi
if [ "${succeeded}" -eq 1 ]; then
return 0
elif [ "${checked}" -eq 1 ]; then
return 1
else
fatal "${ERROR_F0003}" F0003
fi
}
download() {
url="${1}"
dest="${2}"
succeeded=0
checked=0
if echo "${url}" | grep -Eq "^file:///"; then
run cp "${url#file://}" "${dest}" || return 1
return 0
fi
if [ -n "${CURL}" ]; then
checked=1
if run "${CURL}" --fail -q -sSL --connect-timeout 10 --retry 3 --output "${dest}" "${url}"; then
succeeded=1
else
rm -f "${dest}"
fi
fi
if [ "${succeeded}" -eq 0 ]; then
if command -v wget > /dev/null 2>&1; then
checked=1
if run wget -T 15 -O "${dest}" "${url}"; then
succeeded=1
fi
fi
fi
if [ "${succeeded}" -eq 1 ]; then
return 0
elif [ "${checked}" -eq 1 ]; then
return 1
else
fatal "${ERROR_F0003}" F0003
fi
}
get_actual_version() {
major="${1}"
channel="${2}"
url="${RELEASE_INFO_URL}/${channel}/${major}"
if check_for_remote_file "${RELEASE_INFO_URL}"; then
if check_for_remote_file "${url}"; then
download "${url}" -
else
echo "NONE"
fi
else
echo ""
fi
}
get_redirect() {
url="${1}"
succeeded=0
checked=0
if [ -n "${CURL}" ]; then
checked=1
if run sh -c "${CURL} ${url} -s -L -I -o /dev/null -w '%{url_effective}' | grep -Eo '[^/]+$'"; then
succeeded=1
fi
fi
if [ "${succeeded}" -eq 0 ]; then
if command -v wget > /dev/null 2>&1; then
checked=1
if run sh -c "wget -S -O /dev/null ${url} 2>&1 | grep -m 1 Location | grep -Eo '[^/]+$'"; then
succeeded=1
fi
fi
fi
if [ "${succeeded}" -eq 1 ]; then
return 0
elif [ "${checked}" -eq 1 ]; then
return 1
else
fatal "${ERROR_F0003}" F0003
fi
}
safe_sha256sum() {
# Within the context of the installer, we only use -c option that is common between the two commands
# We will have to reconsider if we start using non-common options
if command -v shasum > /dev/null 2>&1; then
shasum -a 256 "$@"
elif command -v sha256sum > /dev/null 2>&1; then
sha256sum "$@"
else
fatal "Could not find a usable checksum tool. Either sha256sum, or a version of shasum supporting SHA256 checksums is required to proceed with installation." F0004
fi
}
get_system_info() {
SYSARCH="$(uname -m)"
case "$(uname -s)" in
Linux)
SYSTYPE="Linux"
if [ -z "${SKIP_DISTRO_DETECTION}" ]; then
os_release_file=
if [ -s "/etc/os-release" ] && [ -r "/etc/os-release" ]; then
os_release_file="/etc/os-release"
elif [ -s "/usr/lib/os-release" ] && [ -r "/usr/lib/os-release" ]; then
os_release_file="/usr/lib/os-release"
else
warning "Cannot find usable OS release information. Native packages will not be available for this install."
fi
if [ -n "${os_release_file}" ]; then
# shellcheck disable=SC1090
. "${os_release_file}"
DISTRO="${ID}"
SYSVERSION="${VERSION_ID}"
SYSCODENAME="${VERSION_CODENAME}"
else
DISTRO="unknown"
DISTRO_COMPAT_NAME="unknown"
SYSVERSION="unknown"
SYSCODENAME="unknown"
fi
else
warning "Distribution auto-detection overridden by user. This is not guaranteed to work, and is not officially supported."
fi
supported_compat_names="debian ubuntu centos fedora opensuse ol amzn arch"
if str_in_list "${DISTRO}" "${supported_compat_names}"; then
DISTRO_COMPAT_NAME="${DISTRO}"
else
case "${DISTRO}" in
opensuse-leap)
DISTRO_COMPAT_NAME="opensuse"
;;
opensuse-tumbleweed)
DISTRO_COMPAT_NAME="opensuse"
SYSVERSION="tumbleweed"
;;
cloudlinux|almalinux|centos-stream|rocky|rhel)
DISTRO_COMPAT_NAME="centos"
;;
artix|manjaro|obarun)
DISTRO_COMPAT_NAME="arch"
;;
*)
DISTRO_COMPAT_NAME="unknown"
;;
esac
fi
case "${DISTRO_COMPAT_NAME}" in
centos|ol) SYSVERSION=$(echo "$SYSVERSION" | cut -d'.' -f1) ;;
esac
;;
Darwin)
SYSTYPE="Darwin"
SYSVERSION="$(sw_vers -buildVersion)"
;;
FreeBSD)
SYSTYPE="FreeBSD"
SYSVERSION="$(uname -K)"
;;
*) fatal "Unsupported system type detected. Netdata cannot be installed on this system using this script." F0200 ;;
esac
}
str_in_list() {
printf "%s\n" "${2}" | tr ' ' "\n" | grep -qE "^${1}\$"
return $?
}
confirm_root_support() {
if [ "$(id -u)" -ne "0" ]; then
if [ -z "${ROOTCMD}" ] && command -v sudo > /dev/null; then
if [ "${INTERACTIVE}" -eq 0 ]; then
ROOTCMD="sudo -n"
else
ROOTCMD="sudo"
fi
fi
if [ -z "${ROOTCMD}" ] && command -v doas > /dev/null; then
if [ "${INTERACTIVE}" -eq 0 ]; then
ROOTCMD="doas -n"
else
ROOTCMD="doas"
fi
fi
if [ -z "${ROOTCMD}" ] && command -v pkexec > /dev/null; then
ROOTCMD="pkexec"
fi
if [ -z "${ROOTCMD}" ]; then
fatal "This script needs root privileges to install Netdata, but cannot find a way to gain them (we support sudo, doas, and pkexec). Either re-run this script as root, or set \$ROOTCMD to a command that can be used to gain root privileges." F0201
fi
fi
}
confirm() {
prompt="${1} [y/n]"
while true; do
echo "${prompt}"
read -r yn
case "$yn" in
[Yy]*) return 0;;
[Nn]*) return 1;;
*) echo "Please answer yes or no.";;
esac
done
}
# ======================================================================
# Existing install handling code
update() {
updater="${ndprefix}/usr/libexec/netdata/netdata-updater.sh"
if run_as_root test -x "${updater}"; then
if [ "${DRY_RUN}" -eq 1 ]; then
progress "Would attempt to update existing installation by running the updater script located at: ${updater}"
return 0
fi
if [ "${INTERACTIVE}" -eq 0 ]; then
opts="--non-interactive"
else
opts="--interactive"
fi
if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
export NETDATA_OFFLINE_INSTALL_SOURCE="${NETDATA_OFFLINE_INSTALL_SOURCE}"
fi
if run_script "${updater}" ${opts} --not-running-from-cron; then
progress "Updated existing install at ${ndprefix}"
return 0
else
if [ -n "${EXIT_REASON}" ]; then
fatal "Failed to update existing Netdata install at ${ndprefix}: ${EXIT_REASON}" "${EXIT_CODE}"
else
fatal "Failed to update existing Netdata install at ${ndprefix}: Encountered an unhandled error in the updater. Further information about this error may be displayed above." U0000
fi
fi
else
warning "Could not find a usable copy of the updater script. We are unable to update this system in place."
return 1
fi
}
uninstall() {
set_tmpdir
get_system_info
detect_existing_install
if [ -n "${OLD_INSTALL_PREFIX}" ]; then
INSTALL_PREFIX="$(echo "${OLD_INSTALL_PREFIX}/" | sed 's/$/netdata/g')"
else
INSTALL_PREFIX="${ndprefix}"
fi
uninstaller="${INSTALL_PREFIX}/usr/libexec/netdata/netdata-uninstaller.sh"
uninstaller_url="https://raw.githubusercontent.com/netdata/netdata/master/packaging/installer/netdata-uninstaller.sh"
if [ $INTERACTIVE = 0 ]; then
FLAGS="--yes --force"
else
FLAGS="--yes"
fi
if run_as_root test -x "${uninstaller}"; then
if [ "${DRY_RUN}" -eq 1 ]; then
progress "Would attempt to uninstall existing install with uninstaller script found at: ${uninstaller}"
return 0
else
progress "Found existing netdata-uninstaller. Running it.."
# shellcheck disable=SC2086
if ! run_script "${uninstaller}" ${FLAGS}; then
warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
fi
fi
else
if [ "${DRY_RUN}" -eq 1 ]; then
progress "Would download installer script from: ${uninstaller_url}"
progress "Would attempt to uninstall existing install with downloaded uninstaller script."
return 0
else
progress "Downloading netdata-uninstaller ..."
download "${uninstaller_url}" "${tmpdir}/netdata-uninstaller.sh"
chmod +x "${tmpdir}/netdata-uninstaller.sh"
# shellcheck disable=SC2086
if ! run_script "${tmpdir}/netdata-uninstaller.sh" ${FLAGS}; then
warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
fi
fi
fi
}
detect_existing_install() {
set_tmpdir
progress "Checking for existing installations of Netdata..."
EXISTING_INSTALL_IS_NATIVE="0"
if [ -n "${INSTALL_PREFIX}" ]; then
searchpath="/opt/netdata/bin:${INSTALL_PREFIX}/bin:${INSTALL_PREFIX}/sbin:${INSTALL_PREFIX}/usr/bin:${INSTALL_PREFIX}/usr/sbin:${PATH}"
searchpath="${INSTALL_PREFIX}/netdata/bin:${INSTALL_PREFIX}/netdata/sbin:${INSTALL_PREFIX}/netdata/usr/bin:${INSTALL_PREFIX}/netdata/usr/sbin:${searchpath}"
else
searchpath="/opt/netdata/bin:${PATH}"
fi
while [ -n "${searchpath}" ]; do
_ndpath="$(PATH="${searchpath}" command -v netdata 2>/dev/null)"
if [ -n "${_ndpath}" ]; then
_ndpath="$(canonical_path "${_ndpath}")"
fi
if [ -z "${ndpath}" ] && [ -n "${_ndpath}" ]; then
ndpath="${_ndpath}"
elif [ -n "${_ndpath}" ] && [ "${ndpath}" != "${_ndpath}" ]; then
fatal "Multiple installs of Netdata agent detected (located at '${ndpath}' and '${_ndpath}'). Such a setup is not generally supported. If you are certain you want to operate on one of them despite this, use the '--install-prefix' option to specifiy the install you want to operate on." F0517
fi
if [ -n "${INSTALL_PREFIX}" ] && [ -n "${ndpath}" ]; then
break
elif [ -z "${_ndpath}" ]; then
break
elif echo "${searchpath}" | grep -v ':'; then
searchpath=""
else
searchpath="$(echo "${searchpath}" | cut -f 2- -d ':')"
fi
done