netdata-installer.sh 40 KB

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