netdata-installer.sh 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. #!/usr/bin/env bash
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # shellcheck disable=SC1090,SC1091,SC1117,SC2002,SC2034,SC2044,SC2046,SC2086,SC2129,SC2162,SC2166,SC2181
  4. export PATH="${PATH}:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
  5. uniquepath() {
  6. local path=""
  7. while read; do
  8. if [[ ! ${path} =~ (^|:)"${REPLY}"(:|$) ]]; then
  9. [ ! -z "${path}" ] && path="${path}:"
  10. path="${path}${REPLY}"
  11. fi
  12. done < <(echo "${PATH}" | tr ":" "\n")
  13. [ ! -z "${path}" ] && [[ ${PATH} =~ /bin ]] && [[ ${PATH} =~ /sbin ]] && export PATH="${path}"
  14. }
  15. uniquepath
  16. netdata_source_dir="$(pwd)"
  17. installer_dir="$(dirname "${0}")"
  18. if [ "${netdata_source_dir}" != "${installer_dir}" -a "${installer_dir}" != "." ]; then
  19. echo >&2 "Warning: you are currently in '${netdata_source_dir}' but the installer is in '${installer_dir}'."
  20. fi
  21. # -----------------------------------------------------------------------------
  22. # reload the user profile
  23. [ -f /etc/profile ] && . /etc/profile
  24. # make sure /etc/profile does not change our current directory
  25. cd "${netdata_source_dir}" || exit 1
  26. # -----------------------------------------------------------------------------
  27. # load the required functions
  28. if [ -f "${installer_dir}/installer/functions.sh" ]; then
  29. source "${installer_dir}/installer/functions.sh" || exit 1
  30. else
  31. source "${netdata_source_dir}/installer/functions.sh" || exit 1
  32. fi
  33. # make sure we save all commands we run
  34. run_logfile="netdata-installer.log"
  35. # -----------------------------------------------------------------------------
  36. # fix PKG_CHECK_MODULES error
  37. if [ -d /usr/share/aclocal ]; then
  38. ACLOCAL_PATH=${ACLOCAL_PATH-/usr/share/aclocal}
  39. export ACLOCAL_PATH
  40. fi
  41. export LC_ALL=C
  42. umask 002
  43. # Be nice on production environments
  44. renice 19 $$ >/dev/null 2>/dev/null
  45. # you can set CFLAGS before running installer
  46. CFLAGS="${CFLAGS--O2}"
  47. [ "z${CFLAGS}" = "z-O3" ] && CFLAGS="-O2"
  48. # keep a log of this command
  49. printf "\n# " >>netdata-installer.log
  50. date >>netdata-installer.log
  51. printf 'CFLAGS="%s" ' "${CFLAGS}" >>netdata-installer.log
  52. printf "%q " "$0" "${@}" >>netdata-installer.log
  53. printf "\n" >>netdata-installer.log
  54. REINSTALL_PWD="${PWD}"
  55. REINSTALL_COMMAND="$(
  56. printf "%q " "$0" "${@}"
  57. printf "\n"
  58. )"
  59. # remove options that shown not be inherited by netdata-updater.sh
  60. REINSTALL_COMMAND="${REINSTALL_COMMAND// --dont-wait/}"
  61. REINSTALL_COMMAND="${REINSTALL_COMMAND// --dont-start-it/}"
  62. [ "${REINSTALL_COMMAND:0:1}" != "." -a "${REINSTALL_COMMAND:0:1}" != "/" -a -f "./${0}" ] && REINSTALL_COMMAND="./${REINSTALL_COMMAND}"
  63. # shellcheck disable=SC2230
  64. setcap="$(which setcap 2>/dev/null || command -v setcap 2>/dev/null)"
  65. ME="$0"
  66. DONOTSTART=0
  67. DONOTWAIT=0
  68. AUTOUPDATE=0
  69. NETDATA_PREFIX=
  70. LIBS_ARE_HERE=0
  71. NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS-}"
  72. usage() {
  73. netdata_banner "installer command line options"
  74. cat <<USAGE
  75. ${ME} <installer options>
  76. Valid <installer options> are:
  77. --install /PATH/TO/INSTALL
  78. If you give: --install /opt
  79. netdata will be installed in /opt/netdata
  80. --dont-start-it
  81. Do not (re)start netdata.
  82. Just install it.
  83. --dont-wait
  84. Do not wait for the user to press ENTER.
  85. Start immediately building it.
  86. --auto-update | -u
  87. Install netdata-updater to cron,
  88. to update netdata automatically once per day
  89. (can only be done for installations from git)
  90. --enable-plugin-freeipmi
  91. --disable-plugin-freeipmi
  92. Enable/disable the FreeIPMI plugin.
  93. Default: enable it when libipmimonitoring is available.
  94. --enable-plugin-nfacct
  95. --disable-plugin-nfacct
  96. Enable/disable the nfacct plugin.
  97. Default: enable it when libmnl and libnetfilter_acct are available.
  98. --enable-lto
  99. --disable-lto
  100. Enable/disable Link-Time-Optimization
  101. Default: enabled
  102. --disable-x86-sse
  103. Disable SSE instructions
  104. Default: enabled
  105. --zlib-is-really-here
  106. --libs-are-really-here
  107. If you get errors about missing zlib,
  108. or libuuid but you know it is available,
  109. you have a broken pkg-config.
  110. Use this option to allow it continue
  111. without checking pkg-config.
  112. Netdata will by default be compiled with gcc optimization -O2
  113. If you need to pass different CFLAGS, use something like this:
  114. CFLAGS="<gcc options>" ${ME} <installer options>
  115. For the installer to complete successfully, you will need
  116. these packages installed:
  117. gcc make autoconf automake pkg-config zlib1g-dev (or zlib-devel)
  118. uuid-dev (or libuuid-devel)
  119. For the plugins, you will at least need:
  120. curl, bash v4+, python v2 or v3, node.js
  121. USAGE
  122. }
  123. while [ ! -z "${1}" ]; do
  124. if [ "$1" = "--install" ]; then
  125. NETDATA_PREFIX="${2}/netdata"
  126. shift 2
  127. elif [ "$1" = "--zlib-is-really-here" -o "$1" = "--libs-are-really-here" ]; then
  128. LIBS_ARE_HERE=1
  129. shift 1
  130. elif [ "$1" = "--dont-start-it" ]; then
  131. DONOTSTART=1
  132. shift 1
  133. elif [ "$1" = "--dont-wait" ]; then
  134. DONOTWAIT=1
  135. shift 1
  136. elif [ "$1" = "--auto-update" -o "$1" = "-u" ]; then
  137. AUTOUPDATE=1
  138. shift 1
  139. elif [ "$1" = "--enable-plugin-freeipmi" ]; then
  140. NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-plugin-freeipmi/} --enable-plugin-freeipmi"
  141. shift 1
  142. elif [ "$1" = "--disable-plugin-freeipmi" ]; then
  143. NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-plugin-freeipmi/} --disable-plugin-freeipmi"
  144. shift 1
  145. elif [ "$1" = "--enable-plugin-nfacct" ]; then
  146. NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-plugin-nfacct/} --enable-plugin-nfacct"
  147. shift 1
  148. elif [ "$1" = "--disable-plugin-nfacct" ]; then
  149. NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-plugin-nfacct/} --disable-plugin-nfacct"
  150. shift 1
  151. elif [ "$1" = "--enable-lto" ]; then
  152. NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--enable-lto/} --enable-lto"
  153. shift 1
  154. elif [ "$1" = "--disable-lto" ]; then
  155. NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-lto/} --disable-lto"
  156. shift 1
  157. elif [ "$1" = "--disable-x86-sse" ]; then
  158. NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS//--disable-x86-sse/} --disable-x86-sse"
  159. shift 1
  160. elif [ "$1" = "--help" -o "$1" = "-h" ]; then
  161. usage
  162. exit 1
  163. else
  164. echo >&2
  165. echo >&2 "ERROR:"
  166. echo >&2 "I cannot understand option '$1'."
  167. usage
  168. exit 1
  169. fi
  170. done
  171. # replace multiple spaces with a single space
  172. NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS// / }"
  173. netdata_banner "real-time performance monitoring, done right!"
  174. cat <<BANNER1
  175. You are about to build and install netdata to your system.
  176. It will be installed at these locations:
  177. - the daemon at ${TPUT_CYAN}${NETDATA_PREFIX}/usr/sbin/netdata${TPUT_RESET}
  178. - config files in ${TPUT_CYAN}${NETDATA_PREFIX}/etc/netdata${TPUT_RESET}
  179. - web files in ${TPUT_CYAN}${NETDATA_PREFIX}/usr/share/netdata${TPUT_RESET}
  180. - plugins in ${TPUT_CYAN}${NETDATA_PREFIX}/usr/libexec/netdata${TPUT_RESET}
  181. - cache files in ${TPUT_CYAN}${NETDATA_PREFIX}/var/cache/netdata${TPUT_RESET}
  182. - db files in ${TPUT_CYAN}${NETDATA_PREFIX}/var/lib/netdata${TPUT_RESET}
  183. - log files in ${TPUT_CYAN}${NETDATA_PREFIX}/var/log/netdata${TPUT_RESET}
  184. BANNER1
  185. [ "${UID}" -eq 0 ] && cat <<BANNER2
  186. - pid file at ${TPUT_CYAN}${NETDATA_PREFIX}/var/run/netdata.pid${TPUT_RESET}
  187. - logrotate file at ${TPUT_CYAN}/etc/logrotate.d/netdata${TPUT_RESET}
  188. BANNER2
  189. cat <<BANNER3
  190. This installer allows you to change the installation path.
  191. Press Control-C and run the same command with --help for help.
  192. BANNER3
  193. if [ "${UID}" -ne 0 ]; then
  194. if [ -z "${NETDATA_PREFIX}" ]; then
  195. netdata_banner "wrong command line options!"
  196. cat <<NONROOTNOPREFIX
  197. ${TPUT_RED}${TPUT_BOLD}Sorry! This will fail!${TPUT_RESET}
  198. You are attempting to install netdata as non-root, but you plan
  199. to install it in system paths.
  200. Please set an installation prefix, like this:
  201. $0 ${@} --install /tmp
  202. or, run the installer as root:
  203. sudo $0 ${@}
  204. We suggest to install it as root, or certain data collectors will
  205. not be able to work. Netdata drops root privileges when running.
  206. So, if you plan to keep it, install it as root to get the full
  207. functionality.
  208. NONROOTNOPREFIX
  209. exit 1
  210. else
  211. cat <<NONROOT
  212. ${TPUT_RED}${TPUT_BOLD}IMPORTANT${TPUT_RESET}:
  213. You are about to install netdata as a non-root user.
  214. Netdata will work, but a few data collection modules that
  215. require root access will fail.
  216. If you installing netdata permanently on your system, run
  217. the installer like this:
  218. ${TPUT_YELLOW}${TPUT_BOLD}sudo $0 ${@}${TPUT_RESET}
  219. NONROOT
  220. fi
  221. fi
  222. have_autotools=
  223. if [ "$(type autoreconf 2>/dev/null)" ]; then
  224. autoconf_maj_min() {
  225. local maj min IFS=.-
  226. maj=$1
  227. min=$2
  228. set -- $(autoreconf -V | sed -ne '1s/.* \([^ ]*\)$/\1/p')
  229. eval $maj=\$1 $min=\$2
  230. }
  231. autoconf_maj_min AMAJ AMIN
  232. if [ "$AMAJ" -gt 2 ]; then
  233. have_autotools=Y
  234. elif [ "$AMAJ" -eq 2 -a "$AMIN" -ge 60 ]; then
  235. have_autotools=Y
  236. else
  237. echo "Found autotools $AMAJ.$AMIN"
  238. fi
  239. else
  240. echo "No autotools found"
  241. fi
  242. if [ ! "$have_autotools" ]; then
  243. if [ -f configure ]; then
  244. echo "Will skip autoreconf step"
  245. else
  246. netdata_banner "autotools v2.60 required"
  247. cat <<"EOF"
  248. -------------------------------------------------------------------------------
  249. autotools 2.60 or later is required
  250. Sorry, you do not seem to have autotools 2.60 or later, which is
  251. required to build from the git sources of netdata.
  252. EOF
  253. exit 1
  254. fi
  255. fi
  256. if [ ${DONOTWAIT} -eq 0 ]; then
  257. if [ ! -z "${NETDATA_PREFIX}" ]; then
  258. eval "read >&2 -ep \$'\001${TPUT_BOLD}${TPUT_GREEN}\002Press ENTER to build and install netdata to \'\001${TPUT_CYAN}\002${NETDATA_PREFIX}\001${TPUT_YELLOW}\002\'\001${TPUT_RESET}\002 > ' -e -r REPLY"
  259. [ $? -ne 0 ] && exit 1
  260. else
  261. eval "read >&2 -ep \$'\001${TPUT_BOLD}${TPUT_GREEN}\002Press ENTER to build and install netdata to your system\001${TPUT_RESET}\002 > ' -e -r REPLY"
  262. [ $? -ne 0 ] && exit 1
  263. fi
  264. fi
  265. build_error() {
  266. netdata_banner "sorry, it failed to build..."
  267. cat <<EOF
  268. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  269. Sorry! netdata failed to build...
  270. You may need to check these:
  271. 1. The package uuid-dev (or libuuid-devel) has to be installed.
  272. If your system cannot find libuuid, although it is installed
  273. run me with the option: --libs-are-really-here
  274. 2. The package zlib1g-dev (or zlib-devel) has to be installed.
  275. If your system cannot find zlib, although it is installed
  276. run me with the option: --libs-are-really-here
  277. 3. You need basic build tools installed, like:
  278. gcc make autoconf automake pkg-config
  279. Autoconf version 2.60 or higher is required.
  280. If you still cannot get it to build, ask for help at github:
  281. https://github.com/netdata/netdata/issues
  282. EOF
  283. trap - EXIT
  284. exit 1
  285. }
  286. if [ ${LIBS_ARE_HERE} -eq 1 ]; then
  287. shift
  288. echo >&2 "ok, assuming libs are really installed."
  289. export ZLIB_CFLAGS=" "
  290. export ZLIB_LIBS="-lz"
  291. export UUID_CFLAGS=" "
  292. export UUID_LIBS="-luuid"
  293. fi
  294. trap build_error EXIT
  295. # -----------------------------------------------------------------------------
  296. echo >&2
  297. progress "Run autotools to configure the build environment"
  298. if [ "$have_autotools" ]; then
  299. run autoreconf -ivf || exit 1
  300. fi
  301. run ./configure \
  302. --prefix="${NETDATA_PREFIX}/usr" \
  303. --sysconfdir="${NETDATA_PREFIX}/etc" \
  304. --localstatedir="${NETDATA_PREFIX}/var" \
  305. --with-zlib \
  306. --with-math \
  307. --with-user=netdata \
  308. ${NETDATA_CONFIGURE_OPTIONS} \
  309. CFLAGS="${CFLAGS}" || exit 1
  310. # remove the build_error hook
  311. trap - EXIT
  312. # -----------------------------------------------------------------------------
  313. progress "Cleanup compilation directory"
  314. [ -f src/netdata ] && run make clean
  315. # -----------------------------------------------------------------------------
  316. progress "Compile netdata"
  317. run make -j${SYSTEM_CPUS} || exit 1
  318. # -----------------------------------------------------------------------------
  319. progress "Migrate configuration files for node.d.plugin and charts.d.plugin"
  320. # migrate existing configuration files
  321. # for node.d and charts.d
  322. if [ -d "${NETDATA_PREFIX}/etc/netdata" ]; then
  323. # the configuration directory exists
  324. if [ ! -d "${NETDATA_PREFIX}/etc/netdata/charts.d" ]; then
  325. run mkdir "${NETDATA_PREFIX}/etc/netdata/charts.d"
  326. fi
  327. # move the charts.d config files
  328. for x in apache ap cpu_apps cpufreq example exim hddtemp load_average mem_apps mysql nginx nut opensips phpfpm postfix sensors squid tomcat; do
  329. for y in "" ".old" ".orig"; do
  330. if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}" ]; then
  331. run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/charts.d/${x}.conf${y}"
  332. fi
  333. done
  334. done
  335. if [ ! -d "${NETDATA_PREFIX}/etc/netdata/node.d" ]; then
  336. run mkdir "${NETDATA_PREFIX}/etc/netdata/node.d"
  337. fi
  338. # move the node.d config files
  339. for x in named sma_webbox snmp; do
  340. for y in "" ".old" ".orig"; do
  341. if [ -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" -a ! -f "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}" ]; then
  342. run mv -f "${NETDATA_PREFIX}/etc/netdata/${x}.conf${y}" "${NETDATA_PREFIX}/etc/netdata/node.d/${x}.conf${y}"
  343. fi
  344. done
  345. done
  346. fi
  347. # -----------------------------------------------------------------------------
  348. # shellcheck disable=SC2230
  349. md5sum="$(which md5sum 2>/dev/null || command -v md5sum 2>/dev/null || command -v md5 2>/dev/null)"
  350. deleted_stock_configs=0
  351. if [ ! -f "${NETDATA_PREFIX}/etc/netdata/.installer-cleanup-of-stock-configs-done" ]; then
  352. progress "Backup existing netdata configuration before installing it"
  353. if [ "${BASH_VERSINFO[0]}" -ge "4" ]; then
  354. declare -A configs_signatures=()
  355. if [ -f "configs.signatures" ]; then
  356. source "configs.signatures" || echo >&2 "ERROR: Failed to load configs.signatures !"
  357. fi
  358. fi
  359. config_signature_matches() {
  360. local md5="${1}" file="${2}"
  361. if [ "${BASH_VERSINFO[0]}" -ge "4" ]; then
  362. [ "${configs_signatures[${md5}]}" = "${file}" ] && return 0
  363. return 1
  364. fi
  365. if [ -f "configs.signatures" ]; then
  366. grep "\['${md5}'\]='${file}'" "configs.signatures" >/dev/null
  367. return $?
  368. fi
  369. return 1
  370. }
  371. # clean up stock config files from the user configuration directory
  372. for x in $(find -L "${NETDATA_PREFIX}/etc/netdata" -type f); do
  373. if [ -f "${x}" ]; then
  374. # find it relative filename
  375. f="${x/${NETDATA_PREFIX}\/etc\/netdata\//}"
  376. # find the stock filename
  377. t="${f/.conf.installer_backup.*/.conf}"
  378. t="${t/.conf.old/.conf}"
  379. t="${t/.conf.orig/.conf}"
  380. if [ -z "${md5sum}" -o ! -x "${md5sum}" ]; then
  381. # we don't have md5sum - keep it
  382. echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RET}is not known to distribution${TPUT_RESET}. Keeping it."
  383. else
  384. # find its checksum
  385. md5="$(${md5sum} <"${x}" | cut -d ' ' -f 1)"
  386. if config_signature_matches "${md5}" "${t}"; then
  387. # it is a stock version - remove it
  388. echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' is stock version of '${t}'."
  389. run rm -f "${x}"
  390. deleted_stock_configs=$((deleted_stock_configs + 1))
  391. else
  392. # edited by user - keep it
  393. echo >&2 "File '${TPUT_CYAN}${x}${TPUT_RESET}' ${TPUT_RED} does not match stock of '${t}'${TPUT_RESET}. Keeping it."
  394. fi
  395. fi
  396. fi
  397. done
  398. fi
  399. touch "${NETDATA_PREFIX}/etc/netdata/.installer-cleanup-of-stock-configs-done"
  400. # -----------------------------------------------------------------------------
  401. progress "Install netdata"
  402. run make install || exit 1
  403. # -----------------------------------------------------------------------------
  404. progress "Fix generated files permissions"
  405. run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -a \! -name \*.logrotate -exec chmod 755 {} \;
  406. # -----------------------------------------------------------------------------
  407. progress "Add user netdata to required user groups"
  408. homedir="${NETDATA_PREFIX}/var/lib/netdata"
  409. [ ! -z "${NETDATA_PREFIX}" ] && homedir="${NETDATA_PREFIX}"
  410. add_netdata_user_and_group "${homedir}" || run_failed "The installer does not run as root."
  411. # -----------------------------------------------------------------------------
  412. progress "Install logrotate configuration for netdata"
  413. install_netdata_logrotate
  414. # -----------------------------------------------------------------------------
  415. progress "Read installation options from netdata.conf"
  416. # create an empty config if it does not exist
  417. [ ! -f "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ] &&
  418. touch "${NETDATA_PREFIX}/etc/netdata/netdata.conf"
  419. # function to extract values from the config file
  420. config_option() {
  421. local section="${1}" key="${2}" value="${3}"
  422. if [ -s "${NETDATA_PREFIX}/etc/netdata/netdata.conf" ]; then
  423. "${NETDATA_PREFIX}/usr/sbin/netdata" \
  424. -c "${NETDATA_PREFIX}/etc/netdata/netdata.conf" \
  425. -W get "${section}" "${key}" "${value}" ||
  426. echo "${value}"
  427. else
  428. echo "${value}"
  429. fi
  430. }
  431. # the user netdata will run as
  432. if [ "${UID}" = "0" ]; then
  433. NETDATA_USER="$(config_option "global" "run as user" "netdata")"
  434. ROOT_USER="root"
  435. else
  436. NETDATA_USER="${USER}"
  437. ROOT_USER="${NETDATA_USER}"
  438. fi
  439. NETDATA_GROUP="$(id -g -n ${NETDATA_USER})"
  440. [ -z "${NETDATA_GROUP}" ] && NETDATA_GROUP="${NETDATA_USER}"
  441. # the owners of the web files
  442. NETDATA_WEB_USER="$(config_option "web" "web files owner" "${NETDATA_USER}")"
  443. NETDATA_WEB_GROUP="${NETDATA_GROUP}"
  444. if [ "${UID}" = "0" -a "${NETDATA_USER}" != "${NETDATA_WEB_USER}" ]; then
  445. NETDATA_WEB_GROUP="$(id -g -n ${NETDATA_WEB_USER})"
  446. [ -z "${NETDATA_WEB_GROUP}" ] && NETDATA_WEB_GROUP="${NETDATA_WEB_USER}"
  447. fi
  448. NETDATA_WEB_GROUP="$(config_option "web" "web files group" "${NETDATA_WEB_GROUP}")"
  449. # port
  450. defport=19999
  451. NETDATA_PORT="$(config_option "web" "default port" ${defport})"
  452. # directories
  453. NETDATA_LIB_DIR="$(config_option "global" "lib directory" "${NETDATA_PREFIX}/var/lib/netdata")"
  454. NETDATA_CACHE_DIR="$(config_option "global" "cache directory" "${NETDATA_PREFIX}/var/cache/netdata")"
  455. NETDATA_WEB_DIR="$(config_option "global" "web files directory" "${NETDATA_PREFIX}/usr/share/netdata/web")"
  456. NETDATA_LOG_DIR="$(config_option "global" "log directory" "${NETDATA_PREFIX}/var/log/netdata")"
  457. NETDATA_USER_CONFIG_DIR="$(config_option "global" "config directory" "${NETDATA_PREFIX}/etc/netdata")"
  458. NETDATA_STOCK_CONFIG_DIR="$(config_option "global" "stock config directory" "${NETDATA_PREFIX}/usr/lib/netdata/conf.d")"
  459. NETDATA_RUN_DIR="${NETDATA_PREFIX}/var/run"
  460. cat <<OPTIONSEOF
  461. Permissions
  462. - netdata user : ${NETDATA_USER}
  463. - netdata group : ${NETDATA_GROUP}
  464. - web files user : ${NETDATA_WEB_USER}
  465. - web files group : ${NETDATA_WEB_GROUP}
  466. - root user : ${ROOT_USER}
  467. Directories
  468. - netdata user config dir : ${NETDATA_USER_CONFIG_DIR}
  469. - netdata stock config dir : ${NETDATA_STOCK_CONFIG_DIR}
  470. - netdata log dir : ${NETDATA_LOG_DIR}
  471. - netdata run dir : ${NETDATA_RUN_DIR}
  472. - netdata lib dir : ${NETDATA_LIB_DIR}
  473. - netdata web dir : ${NETDATA_WEB_DIR}
  474. - netdata cache dir : ${NETDATA_CACHE_DIR}
  475. Other
  476. - netdata port : ${NETDATA_PORT}
  477. OPTIONSEOF
  478. # -----------------------------------------------------------------------------
  479. progress "Fix permissions of netdata directories (using user '${NETDATA_USER}')"
  480. if [ ! -d "${NETDATA_RUN_DIR}" ]; then
  481. # this is needed if NETDATA_PREFIX is not empty
  482. run mkdir -p "${NETDATA_RUN_DIR}" || exit 1
  483. fi
  484. # --- conf dir ----
  485. for x in "python.d" "charts.d" "node.d" "health.d" "statsd.d"; do
  486. if [ ! -d "${NETDATA_USER_CONFIG_DIR}/${x}" ]; then
  487. echo >&2 "Creating directory '${NETDATA_USER_CONFIG_DIR}/${x}'"
  488. run mkdir -p "${NETDATA_USER_CONFIG_DIR}/${x}" || exit 1
  489. fi
  490. done
  491. run chown -R "${ROOT_USER}:${NETDATA_GROUP}" "${NETDATA_USER_CONFIG_DIR}"
  492. run find "${NETDATA_USER_CONFIG_DIR}" -type f -exec chmod 0640 {} \;
  493. run find "${NETDATA_USER_CONFIG_DIR}" -type d -exec chmod 0755 {} \;
  494. run chmod 755 "${NETDATA_USER_CONFIG_DIR}/edit-config"
  495. # --- stock conf dir ----
  496. [ ! -d "${NETDATA_STOCK_CONFIG_DIR}" ] && mkdir -p "${NETDATA_STOCK_CONFIG_DIR}"
  497. helplink="000.-.USE.THE.orig.LINK.TO.COPY.AND.EDIT.STOCK.CONFIG.FILES"
  498. [ ${deleted_stock_configs} -eq 0 ] && helplink=""
  499. for link in "orig" "${helplink}"; do
  500. if [ ! -z "${link}" ]; then
  501. [ -L "${NETDATA_USER_CONFIG_DIR}/${link}" ] && run rm -f "${NETDATA_USER_CONFIG_DIR}/${link}"
  502. run ln -s "${NETDATA_STOCK_CONFIG_DIR}" "${NETDATA_USER_CONFIG_DIR}/${link}"
  503. fi
  504. done
  505. run chown -R "${ROOT_USER}:${NETDATA_GROUP}" "${NETDATA_STOCK_CONFIG_DIR}"
  506. run find "${NETDATA_STOCK_CONFIG_DIR}" -type f -exec chmod 0640 {} \;
  507. run find "${NETDATA_STOCK_CONFIG_DIR}" -type d -exec chmod 0755 {} \;
  508. # --- web dir ----
  509. if [ ! -d "${NETDATA_WEB_DIR}" ]; then
  510. echo >&2 "Creating directory '${NETDATA_WEB_DIR}'"
  511. run mkdir -p "${NETDATA_WEB_DIR}" || exit 1
  512. fi
  513. run chown -R "${NETDATA_WEB_USER}:${NETDATA_WEB_GROUP}" "${NETDATA_WEB_DIR}"
  514. run find "${NETDATA_WEB_DIR}" -type f -exec chmod 0664 {} \;
  515. run find "${NETDATA_WEB_DIR}" -type d -exec chmod 0775 {} \;
  516. # --- data dirs ----
  517. for x in "${NETDATA_LIB_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"; do
  518. if [ ! -d "${x}" ]; then
  519. echo >&2 "Creating directory '${x}'"
  520. run mkdir -p "${x}" || exit 1
  521. fi
  522. run chown -R "${NETDATA_USER}:${NETDATA_GROUP}" "${x}"
  523. #run find "${x}" -type f -exec chmod 0660 {} \;
  524. #run find "${x}" -type d -exec chmod 0770 {} \;
  525. done
  526. run chmod 755 "${NETDATA_LOG_DIR}"
  527. # --- plugins ----
  528. if [ ${UID} -eq 0 ]; then
  529. # find the admin group
  530. admin_group=
  531. test -z "${admin_group}" && getent group root >/dev/null 2>&1 && admin_group="root"
  532. test -z "${admin_group}" && getent group daemon >/dev/null 2>&1 && admin_group="daemon"
  533. test -z "${admin_group}" && admin_group="${NETDATA_GROUP}"
  534. run chown "${NETDATA_USER}:${admin_group}" "${NETDATA_LOG_DIR}"
  535. run chown -R root "${NETDATA_PREFIX}/usr/libexec/netdata"
  536. run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
  537. run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0644 {} \;
  538. run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.plugin -exec chmod 0755 {} \;
  539. run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -a -name \*.sh -exec chmod 0755 {} \;
  540. if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" ]; then
  541. setcap_ret=1
  542. if ! iscontainer; then
  543. if [ ! -z "${setcap}" ]; then
  544. run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
  545. run chmod 0750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
  546. run setcap cap_dac_read_search,cap_sys_ptrace+ep "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
  547. setcap_ret=$?
  548. fi
  549. if [ ${setcap_ret} -eq 0 ]; then
  550. # if we managed to setcap
  551. # but we fail to execute apps.plugin
  552. # trigger setuid to root
  553. "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" -t >/dev/null 2>&1
  554. setcap_ret=$?
  555. fi
  556. fi
  557. if [ ${setcap_ret} -ne 0 ]; then
  558. # fix apps.plugin to be setuid to root
  559. run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
  560. run chmod 4750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin"
  561. fi
  562. fi
  563. if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin" ]; then
  564. run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin"
  565. run chmod 4750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/freeipmi.plugin"
  566. fi
  567. if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network" ]; then
  568. run chown root:${NETDATA_GROUP} "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network"
  569. run chmod 4750 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network"
  570. fi
  571. if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network-helper.sh" ]; then
  572. run chown root "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network-helper.sh"
  573. run chmod 0550 "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/cgroup-network-helper.sh"
  574. fi
  575. else
  576. # non-privileged user installation
  577. run chown "${NETDATA_USER}:${NETDATA_GROUP}" "${NETDATA_LOG_DIR}"
  578. run chown -R "${NETDATA_USER}:${NETDATA_GROUP}" "${NETDATA_PREFIX}/usr/libexec/netdata"
  579. run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type f -exec chmod 0755 {} \;
  580. run find "${NETDATA_PREFIX}/usr/libexec/netdata" -type d -exec chmod 0755 {} \;
  581. fi
  582. # --- fix #1292 bug ---
  583. [ -d "${NETDATA_PREFIX}/usr/libexec" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/libexec"
  584. [ -d "${NETDATA_PREFIX}/usr/share/netdata" ] && run chmod a+rX "${NETDATA_PREFIX}/usr/share/netdata"
  585. # -----------------------------------------------------------------------------
  586. progress "Install netdata at system init"
  587. NETDATA_START_CMD="${NETDATA_PREFIX}/usr/sbin/netdata"
  588. install_netdata_service || run_failed "Cannot install netdata init service."
  589. # -----------------------------------------------------------------------------
  590. # check if we can re-start netdata
  591. started=0
  592. if [ ${DONOTSTART} -eq 1 ]; then
  593. generate_netdata_conf "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" "http://localhost:${NETDATA_PORT}/netdata.conf"
  594. else
  595. restart_netdata ${NETDATA_PREFIX}/usr/sbin/netdata "${@}"
  596. if [ $? -ne 0 ]; then
  597. echo >&2
  598. echo >&2 "SORRY! FAILED TO START NETDATA!"
  599. echo >&2
  600. exit 1
  601. fi
  602. started=1
  603. echo >&2 "OK. NetData Started!"
  604. echo >&2
  605. # -----------------------------------------------------------------------------
  606. # save a config file, if it is not already there
  607. download_netdata_conf "${NETDATA_USER}" "${NETDATA_PREFIX}/etc/netdata/netdata.conf" "http://localhost:${NETDATA_PORT}/netdata.conf"
  608. fi
  609. if [ "$(uname)" = "Linux" ]; then
  610. # -------------------------------------------------------------------------
  611. progress "Check KSM (kernel memory deduper)"
  612. ksm_is_available_but_disabled() {
  613. cat <<KSM1
  614. ${TPUT_BOLD}Memory de-duplication instructions${TPUT_RESET}
  615. You have kernel memory de-duper (called Kernel Same-page Merging,
  616. or KSM) available, but it is not currently enabled.
  617. To enable it run:
  618. ${TPUT_YELLOW}${TPUT_BOLD}echo 1 >/sys/kernel/mm/ksm/run${TPUT_RESET}
  619. ${TPUT_YELLOW}${TPUT_BOLD}echo 1000 >/sys/kernel/mm/ksm/sleep_millisecs${TPUT_RESET}
  620. If you enable it, you will save 40-60% of netdata memory.
  621. KSM1
  622. }
  623. ksm_is_not_available() {
  624. cat <<KSM2
  625. ${TPUT_BOLD}Memory de-duplication not present in your kernel${TPUT_RESET}
  626. It seems you do not have kernel memory de-duper (called Kernel Same-page
  627. Merging, or KSM) available.
  628. To enable it, you need a kernel built with CONFIG_KSM=y
  629. If you can have it, you will save 40-60% of netdata memory.
  630. KSM2
  631. }
  632. if [ -f "/sys/kernel/mm/ksm/run" ]; then
  633. if [ $(cat "/sys/kernel/mm/ksm/run") != "1" ]; then
  634. ksm_is_available_but_disabled
  635. fi
  636. else
  637. ksm_is_not_available
  638. fi
  639. fi
  640. # -----------------------------------------------------------------------------
  641. progress "Check version.txt"
  642. if [ ! -s webserver/gui/version.txt ]; then
  643. cat <<VERMSG
  644. ${TPUT_BOLD}Version update check warning${TPUT_RESET}
  645. The way you downloaded netdata, we cannot find its version. This means the
  646. Update check on the dashboard, will not work.
  647. If you want to have version update check, please re-install it
  648. following the procedure in:
  649. https://github.com/netdata/netdata/tree/master/installer#installation
  650. VERMSG
  651. fi
  652. if [ -f "${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin" ]; then
  653. # -----------------------------------------------------------------------------
  654. progress "Check apps.plugin"
  655. if [ "${UID}" -ne 0 ]; then
  656. cat <<SETUID_WARNING
  657. ${TPUT_BOLD}apps.plugin needs privileges${TPUT_RESET}
  658. Since you have installed netdata as a normal user, to have apps.plugin collect
  659. all the needed data, you have to give it the access rights it needs, by running
  660. either of the following sets of commands:
  661. To run apps.plugin with escalated capabilities:
  662. ${TPUT_YELLOW}${TPUT_BOLD}sudo chown root:${NETDATA_GROUP} \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
  663. ${TPUT_YELLOW}${TPUT_BOLD}sudo chmod 0750 \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
  664. ${TPUT_YELLOW}${TPUT_BOLD}sudo setcap cap_dac_read_search,cap_sys_ptrace+ep \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
  665. or, to run apps.plugin as root:
  666. ${TPUT_YELLOW}${TPUT_BOLD}sudo chown root:${NETDATA_GROUP} \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
  667. ${TPUT_YELLOW}${TPUT_BOLD}sudo chmod 4750 \"${NETDATA_PREFIX}/usr/libexec/netdata/plugins.d/apps.plugin\"${TPUT_RESET}
  668. apps.plugin is performing a hard-coded function of data collection for all
  669. running processes. It cannot be instructed from the netdata daemon to perform
  670. any task, so it is pretty safe to do this.
  671. SETUID_WARNING
  672. fi
  673. fi
  674. # -----------------------------------------------------------------------------
  675. progress "Create netdata-uninstaller.sh"
  676. cp ./installer/netdata-uninstaller.sh netdata-uninstaller.sh
  677. chmod 750 netdata-uninstaller.sh
  678. # -----------------------------------------------------------------------------
  679. progress "Basic netdata instructions"
  680. cat <<END
  681. netdata by default listens on all IPs on port ${NETDATA_PORT},
  682. so you can access it with:
  683. ${TPUT_CYAN}${TPUT_BOLD}http://this.machine.ip:${NETDATA_PORT}/${TPUT_RESET}
  684. To stop netdata run:
  685. ${TPUT_YELLOW}${TPUT_BOLD}${NETDATA_STOP_CMD}${TPUT_RESET}
  686. To start netdata run:
  687. ${TPUT_YELLOW}${TPUT_BOLD}${NETDATA_START_CMD}${TPUT_RESET}
  688. END
  689. echo >&2 "Uninstall script is located at: ${TPUT_RED}${TPUT_BOLD}./netdata-uninstaller.sh${TPUT_RESET}"
  690. if [ -d .git ]; then
  691. cp ./installer/netdata-updater.sh netdata-updater.sh
  692. sed -i "s|THIS_SHOULD_BE_REPLACED_BY_INSTALLER_SCRIPT|${REINSTALL_PWD}|" netdata-updater.sh
  693. chmod 755 netdata-updater.sh
  694. echo >&2 "Update script is located at: ${TPUT_GREEN}${TPUT_BOLD}./netdata-updater.sh${TPUT_RESET}"
  695. echo >&2
  696. echo >&2 "${TPUT_DIM}${TPUT_BOLD}netdata-updater.sh${TPUT_RESET}${TPUT_DIM} can work from cron. It will trigger an email from cron"
  697. echo >&2 "only if it fails (it does not print anything when it can update netdata).${TPUT_RESET}"
  698. if [ "${UID}" -eq "0" ]; then
  699. crondir=
  700. [ -d "/etc/periodic/daily" ] && crondir="/etc/periodic/daily"
  701. [ -d "/etc/cron.daily" ] && crondir="/etc/cron.daily"
  702. if [ ! -z "${crondir}" ]; then
  703. if [ -f "${crondir}/netdata-updater.sh" -a ! -f "${crondir}/netdata-updater" ]; then
  704. # remove .sh from the filename under cron
  705. progress "Fixing netdata-updater filename at cron"
  706. mv -f "${crondir}/netdata-updater.sh" "${crondir}/netdata-updater"
  707. fi
  708. if [ ! -f "${crondir}/netdata-updater" ]; then
  709. if [ "${AUTOUPDATE}" = "1" ]; then
  710. progress "Installing netdata-updater at cron"
  711. run ln -fs "${PWD}/netdata-updater.sh" "${crondir}/netdata-updater"
  712. else
  713. echo >&2 "${TPUT_DIM}Run this to automatically check and install netdata updates once per day:${TPUT_RESET}"
  714. echo >&2
  715. echo >&2 "${TPUT_YELLOW}${TPUT_BOLD}sudo ln -fs ${PWD}/netdata-updater.sh ${crondir}/netdata-updater${TPUT_RESET}"
  716. fi
  717. else
  718. progress "Refreshing netdata-updater at cron"
  719. run rm "${crondir}/netdata-updater"
  720. run ln -fs "${PWD}/netdata-updater.sh" "${crondir}/netdata-updater"
  721. fi
  722. else
  723. [ "${AUTOUPDATE}" = "1" ] && echo >&2 "Cannot figure out the cron directory to install netdata-updater."
  724. fi
  725. else
  726. [ "${AUTOUPDATE}" = "1" ] && echo >&2 "You need to run the installer as root for auto-updating via cron."
  727. fi
  728. else
  729. [ -f "netdata-updater.sh" ] && rm "netdata-updater.sh"
  730. [ "${AUTOUPDATE}" = "1" ] && echo >&2 "Your installation method does not support daily auto-updating via cron."
  731. fi
  732. # Save environment variables
  733. cat <<EOF > installer/.environment.sh
  734. PATH="${PATH}"
  735. CFLAGS="${CFLAGS}"
  736. NETDATA_PREFIX="${NETDATA_PREFIX}"
  737. NETDATA_CONFIGURE_OPTIONS="${NETDATA_CONFIGURE_OPTIONS}"
  738. NETDATA_ADDED_TO_GROUPS="${NETDATA_ADDED_TO_GROUPS}"
  739. INSTALL_UID="${UID}"
  740. REINSTALL_PWD="${REINSTALL_PWD}"
  741. REINSTALL_COMMAND="${REINSTALL_COMMAND}"
  742. EOF
  743. # -----------------------------------------------------------------------------
  744. echo >&2
  745. progress "We are done!"
  746. if [ ${started} -eq 1 ]; then
  747. netdata_banner "is installed and running now!"
  748. else
  749. netdata_banner "is installed now!"
  750. fi
  751. echo >&2 " enjoy real-time performance and health monitoring..."
  752. echo >&2
  753. exit 0