netdata-installer.sh 40 KB

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