当前位置:   article > 正文

netdata软件安装脚本_the package uuid-dev (or libuuid-devel) has to be

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/658693
推荐阅读
相关标签
  

闽ICP备14008679号