install-service.sh.in 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. #!/usr/bin/env sh
  2. # SPDX-License-Identifier: GPL-3.0-or-later
  3. # Handle installation of the Netdata agent as a system service.
  4. #
  5. # Exit codes:
  6. # 0 - Successfully installed service.
  7. # 1 - Invalid arguments or other internal error.
  8. # 2 - Unable to detect system service type.
  9. # 3 - Detected system service type, but type not supported.
  10. # 4 - Detected system service type, but could not install due to other issues.
  11. # 5 - Platform not supported.
  12. set -e
  13. SCRIPT_SOURCE="$(
  14. self=${0}
  15. while [ -L "${self}" ]
  16. do
  17. cd "${self%/*}" || exit 1
  18. self=$(readlink "${self}")
  19. done
  20. cd "${self%/*}" || exit 1
  21. echo "$(pwd -P)/${self##*/}"
  22. )"
  23. DUMP_CMDS=0
  24. ENABLE="auto"
  25. EXPORT_CMDS=0
  26. INSTALL=1
  27. LINUX_INIT_TYPES="systemd openrc lsb initd runit dinit"
  28. PLATFORM="$(uname -s)"
  29. SHOW_SVC_TYPE=0
  30. SVC_SOURCE="@libsysdir_POST@"
  31. SVC_TYPE="detect"
  32. WSL_ERROR_MSG="We appear to be running in WSL and were unable to find a usable service manager. We currently support systemd, LSB init scripts, and traditional init.d style init scripts when running under WSL."
  33. # =====================================================================
  34. # Utility functions
  35. cleanup() {
  36. ec="${?}"
  37. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  38. if [ -n "${NETDATA_PROPAGATE_WARNINGS}" ]; then
  39. export NETDATA_WARNINGS="${NETDATA_WARNINGS}${SAVED_WARNINGS}"
  40. fi
  41. fi
  42. trap - EXIT
  43. exit "${ec}"
  44. }
  45. info() {
  46. printf >&2 "%s\n" "${*}"
  47. }
  48. warning() {
  49. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  50. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${*}"
  51. fi
  52. printf >&2 "WARNING: %s\n" "${*}"
  53. }
  54. error() {
  55. if [ -n "${NETDATA_SAVE_WARNINGS}" ]; then
  56. SAVED_WARNINGS="${SAVED_WARNINGS}\n - ${*}"
  57. fi
  58. printf >&2 "ERROR: %s\n" "${*}"
  59. }
  60. get_os_key() {
  61. if [ -f /etc/os-release ]; then
  62. # shellcheck disable=SC1091
  63. . /etc/os-release || return 1
  64. echo "${ID}-${VERSION_ID}"
  65. elif [ -f /etc/redhat-release ]; then
  66. cat /etc/redhat-release
  67. else
  68. echo "unknown"
  69. fi
  70. }
  71. valid_types() {
  72. case "${PLATFORM}" in
  73. Linux)
  74. echo "detect ${LINUX_INIT_TYPES}"
  75. ;;
  76. FreeBSD)
  77. echo "detect freebsd"
  78. ;;
  79. Darwin)
  80. echo "detect launchd"
  81. ;;
  82. *)
  83. echo "detect"
  84. ;;
  85. esac
  86. }
  87. install_generic_service() {
  88. svc_path="${1}"
  89. svc_type_name="${2}"
  90. svc_file="${3}"
  91. svc_enable_hook="${4}"
  92. svc_disable_hook="${5}"
  93. info "Installing ${svc_type_name} service file."
  94. if [ ! -f "${svc_file}" ] && [ "${ENABLE}" = "auto" ]; then
  95. ENABLE="enable"
  96. fi
  97. if ! install -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/${svc_path}/netdata" "${svc_file}"; then
  98. error "Failed to install service file."
  99. exit 4
  100. fi
  101. case "${ENABLE}" in
  102. auto) true ;;
  103. disable)
  104. info "Disabling Netdata service."
  105. ${svc_disable_hook}
  106. ;;
  107. enable)
  108. info "Enabling Netdata service."
  109. ${svc_enable_hook}
  110. ;;
  111. esac
  112. }
  113. dump_cmds() {
  114. [ -n "${NETDATA_START_CMD}" ] && echo "NETDATA_START_CMD='${NETDATA_START_CMD}'"
  115. [ -n "${NETDATA_STOP_CMD}" ] && echo "NETDATA_STOP_CMD='${NETDATA_STOP_CMD}'"
  116. [ -n "${NETDATA_INSTALLER_START_CMD}" ] && echo "NETDATA_INSTALLER_START_CMD='${NETDATA_INSTALLER_START_CMD}'"
  117. return 0
  118. }
  119. export_cmds() {
  120. [ -n "${NETDATA_START_CMD}" ] && export NETDATA_START_CMD="${NETDATA_START_CMD}"
  121. [ -n "${NETDATA_STOP_CMD}" ] && export NETDATA_STOP_CMD="${NETDATA_STOP_CMD}"
  122. [ -n "${NETDATA_INSTALLER_START_CMD}" ] && export NETDATA_INSTALLER_START_CMD="${NETDATA_INSTALLER_START_COMMAND}"
  123. return 0
  124. }
  125. save_cmds() {
  126. dump_cmds > "${SAVE_CMDS_PATH}"
  127. }
  128. # =====================================================================
  129. # Help functions
  130. usage() {
  131. cat << HEREDOC
  132. USAGE: install-service.sh [options]
  133. where options include:
  134. --source Specify where to find the service files to install (default ${SVC_SOURCE}).
  135. --type Specify the type of service file to install. Specify a type of 'help' to get a list of valid types for your platform.
  136. --show-type Display information about what service managers are detected.
  137. --cmds Additionally print a list of commands for starting and stopping the agent with the detected service type.
  138. --export-cmds Export the variables that would be printed by the --cmds option.
  139. --cmds-only Don't install, just handle the --cmds or --export-cmds option.
  140. --enable Explicitly enable the service on install (default is to enable if not already installed).
  141. --disable Explicitly disable the service on install.
  142. --help Print this help information.
  143. HEREDOC
  144. }
  145. help_types() {
  146. cat << HEREDOC
  147. Valid service types for ${PLATFORM} are:
  148. $(valid_types)
  149. HEREDOC
  150. }
  151. # =====================================================================
  152. # systemd support functions
  153. _check_systemd() {
  154. pids=''
  155. p=''
  156. myns=''
  157. ns=''
  158. # if the directory /lib/systemd/system OR /usr/lib/systemd/system (SLES 12.x) does not exit, it is not systemd
  159. if [ ! -d /lib/systemd/system ] && [ ! -d /usr/lib/systemd/system ]; then
  160. echo "NO" && return 0
  161. fi
  162. # if there is no systemctl command, it is not systemd
  163. [ -z "$(command -v systemctl 2>/dev/null || true)" ] && echo "NO" && return 0
  164. # if pid 1 is systemd, it is systemd
  165. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "systemd" ] && echo "YES" && return 0
  166. # it ‘is’ systemd at this point, but systemd might not be running
  167. # if not, return 2 to indicate ‘systemd, but not running’
  168. pids=$(safe_pidof systemd 2> /dev/null)
  169. [ -z "${pids}" ] && echo "OFFLINE" && return 0
  170. # check if the running systemd processes are not in our namespace
  171. myns="$(readlink /proc/self/ns/pid 2> /dev/null)"
  172. for p in ${pids}; do
  173. ns="$(readlink "/proc/${p}/ns/pid" 2> /dev/null)"
  174. # if pid of systemd is in our namespace, it is systemd
  175. [ -n "${myns}" ] && [ "${myns}" = "${ns}" ] && echo "YES" && return 0
  176. done
  177. # else, it is not systemd
  178. echo "NO"
  179. }
  180. check_systemd() {
  181. if [ -z "${IS_SYSTEMD}" ]; then
  182. IS_SYSTEMD="$(_check_systemd)"
  183. fi
  184. echo "${IS_SYSTEMD}"
  185. }
  186. get_systemd_service_dir() {
  187. if [ -w "/lib/systemd/system" ]; then
  188. echo "/lib/systemd/system"
  189. elif [ -w "/usr/lib/systemd/system" ]; then
  190. echo "/usr/lib/systemd/system"
  191. elif [ -w "/etc/systemd/system" ]; then
  192. echo "/etc/systemd/system"
  193. else
  194. error "Unable to detect systemd service directory."
  195. exit 4
  196. fi
  197. }
  198. install_systemd_service() {
  199. SRCFILE="${SVC_SOURCE}/systemd/netdata.service"
  200. PRESET_FILE="${SVC_SOURCE}/systemd/50-netdata.preset"
  201. SVCDIR="$(get_systemd_service_dir)"
  202. if [ "$(systemctl --version | head -n 1 | cut -f 2 -d ' ')" -le 235 ]; then
  203. SRCFILE="${SVC_SOURCE}/systemd/netdata.service.v235"
  204. fi
  205. if [ "${ENABLE}" = "auto" ]; then
  206. if [ "$(check_systemd)" = "YES" ]; then
  207. IS_NETDATA_ENABLED="$(systemctl is-enabled netdata 2> /dev/null || echo "Netdata not there")"
  208. fi
  209. if [ "${IS_NETDATA_ENABLED}" = "disabled" ]; then
  210. ENABLE="disable"
  211. else
  212. ENABLE="enable"
  213. fi
  214. fi
  215. info "Installing systemd service..."
  216. if ! install -p -m 0644 -o 0 -g 0 "${SRCFILE}" "${SVCDIR}/netdata.service"; then
  217. error "Failed to install systemd service file."
  218. exit 4
  219. fi
  220. if [ -f "${PRESET_FILE}" ]; then
  221. if ! install -p -m 0644 -o 0 -g 0 "${PRESET_FILE}" "${SVCDIR}-preset/50-netdata.preset"; then
  222. warning "Failed to install netdata preset file."
  223. fi
  224. fi
  225. if [ "$(check_systemd)" = "YES" ]; then
  226. if ! systemctl daemon-reload; then
  227. warning "Failed to reload systemd unit files."
  228. fi
  229. if ! systemctl "${ENABLE}" netdata; then
  230. warning "Failed to ${ENABLE} Netdata service."
  231. fi
  232. fi
  233. }
  234. systemd_cmds() {
  235. if [ "$(check_systemd)" = "YES" ]; then
  236. NETDATA_START_CMD='systemctl start netdata'
  237. NETDATA_STOP_CMD='systemctl stop netdata'
  238. else # systemd is not running, use external defaults by providing no commands
  239. warning "Detected systemd, but not booted using systemd. Unable to provide commands to start or stop Netdata using the service manager."
  240. fi
  241. }
  242. # =====================================================================
  243. # OpenRC support functions
  244. _check_openrc() {
  245. # if /lib/rc/sh/functions.sh does not exist, it's not OpenRC
  246. [ ! -f /lib/rc/sh/functions.sh ] && echo "NO" && return 0
  247. # if there is no /etc/init.d, it's not OpenRC
  248. [ ! -d /etc/init.d ] && echo "NO" && return 0
  249. # if there is no /etc/conf.d, it's not OpenRC
  250. [ ! -d /etc/conf.d ] && echo "NO" && return 0
  251. # if there is no rc-update command, it's not OpenRC
  252. [ -z "$(command -v rc-update 2>/dev/null || true)" ] && echo "NO" && return 0
  253. # If /run/openrc/softlevel exists, it's OpenRC
  254. [ -f /run/openrc/softlevel ] && echo "YES" && return 0
  255. # if PID 1 is openrc-init, it's OpenRC
  256. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "openrc-init" ] && echo "YES" && return 0
  257. # if there is an openrc command, it's OpenRC, but not booted as such
  258. [ -n "$(command -v openrc 2>/dev/null || true)" ] && echo "OFFLINE" && return 0
  259. # if /etc/init.d/local exists and has `openrc-run` in it's shebang line, it’s OpenRC, but not booted as such
  260. [ -r /etc/init.d/local ] && head -n 1 /etc/init.d/local | grep -q openrc-run && echo "OFFLINE" && return 0
  261. # Otherwise, it’s not OpenRC
  262. echo "NO" && return 0
  263. }
  264. check_openrc() {
  265. if [ -z "${IS_OPENRC}" ]; then
  266. IS_OPENRC="$(_check_openrc)"
  267. fi
  268. echo "${IS_OPENRC}"
  269. }
  270. enable_openrc() {
  271. if [ "$(check_openrc)" = "YES" ]; then
  272. runlevel="$(rc-status -r)"
  273. fi
  274. runlevel="${runlevel:-default}"
  275. if ! rc-update add netdata "${runlevel}"; then
  276. warning "Failed to enable Netdata service in runlevel ${runlevel}."
  277. fi
  278. }
  279. disable_openrc() {
  280. for runlevel in /etc/runlevels/*; do
  281. if [ -e "${runlevel}/netdata" ]; then
  282. runlevel="$(basename "${runlevel}")"
  283. if ! rc-update del netdata "${runlevel}"; then
  284. warning "Failed to disable Netdata service in runlevel ${runlevel}."
  285. fi
  286. fi
  287. done
  288. }
  289. install_openrc_service() {
  290. install_generic_service openrc/init.d OpenRC /etc/init.d/netdata enable_openrc disable_openrc
  291. if [ ! -f /etc/conf.d/netdata ]; then
  292. info "Installing OpenRC configuration file."
  293. if ! install -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/openrc/conf.d/netdata" "/etc/conf.d/netdata"; then
  294. warning "Failed to install configuration file, however the service will still work."
  295. fi
  296. fi
  297. }
  298. openrc_cmds() {
  299. if [ "$(check_openrc)" = "YES" ]; then
  300. NETDATA_START_CMD='rc-service netdata start'
  301. NETDATA_STOP_CMD='rc-service netdata stop'
  302. else # Not booted using OpenRC, use external defaults by not providing commands.
  303. warning "Detected OpenRC, but the system is not booted using OpenRC. Unable to provide commands to start or stop Netdata using the service manager."
  304. fi
  305. }
  306. # =====================================================================
  307. # LSB init script support functions
  308. _check_lsb_ignore_systemd() {
  309. # if there is no /etc/init.d directory, it’s not an LSB system
  310. [ ! -d /etc/init.d ] && echo "NO" && return 0
  311. # If it's an OpenRC system, then it's not an LSB system
  312. [ "$(check_openrc)" != "NO" ] && echo "NO" && return 0
  313. # If /lib/lsb/init-functions exists, it’s an LSB system
  314. [ -f /lib/lsb/init-functions ] && echo "YES" && return 0
  315. echo "NO" && return 0
  316. }
  317. _check_lsb() {
  318. # if there is _any_ systemd, it’s not an LSB system
  319. [ "$(check_systemd)" != "NO" ] && echo "NO" && return 0
  320. _check_lsb_ignore_systemd
  321. }
  322. check_lsb() {
  323. if [ -z "${IS_LSB}" ]; then
  324. IS_LSB="$(_check_lsb)"
  325. fi
  326. echo "${IS_LSB}"
  327. }
  328. enable_lsb() {
  329. if ! update-rc.d netdata defaults; then
  330. warning "Failed to enable Netdata service."
  331. elif ! update-rc.d netdata defaults-disabled; then
  332. warning "Failed to fully enable Netdata service."
  333. fi
  334. }
  335. disable_lsb() {
  336. if ! update-rc.d netdata remove; then
  337. warning "Failed to disable Netdata service."
  338. fi
  339. }
  340. install_lsb_service() {
  341. install_generic_service lsb/init.d LSB /etc/init.d/netdata enable_lsb disable_lsb
  342. }
  343. lsb_cmds() {
  344. NETDATA_START_CMD='/etc/init.d/netdata start'
  345. NETDATA_STOP_CMD='/etc/init.d/netdata stop'
  346. }
  347. # =====================================================================
  348. # init.d init script support functions
  349. _check_initd_ignore_systemd() {
  350. # if there is no /etc/init.d directory, it’s not an init.d system
  351. [ ! -d /etc/init.d ] && echo "NO" && return 1
  352. # if there is no chkconfig command, it's not a (usable) init.d system
  353. [ -z "$(command -v chkconfig 2>/dev/null || true)" ] && echo "NO" && return 0
  354. # if there is _any_ openrc, it’s not init.d
  355. [ "$(check_openrc)" != "NO" ] && echo "NO" && return 0
  356. # if it's not an LSB setup, it’s init.d
  357. [ "$(check_lsb)" != "NO" ] && echo "NO" && return 0
  358. echo "YES" && return 0
  359. }
  360. _check_initd() {
  361. # if there is _any_ systemd, it’s not init.d
  362. [ "$(check_systemd)" != "NO" ] && echo "NO" && return 0
  363. _check_initd_ignore_systemd
  364. }
  365. check_initd() {
  366. if [ -z "${IS_INITD}" ]; then
  367. IS_INITD="$(_check_initd)"
  368. fi
  369. echo "${IS_INITD}"
  370. }
  371. enable_initd() {
  372. if ! chkconfig netdata on; then
  373. warning "Failed to enable Netdata service."
  374. fi
  375. }
  376. disable_initd() {
  377. if ! chkconfig netdata off; then
  378. warning "Failed to disable Netdata service."
  379. fi
  380. }
  381. install_initd_service() {
  382. install_generic_service initd/init.d init.d /etc/init.d/netdata enable_initd disable_initd
  383. }
  384. initd_cmds() {
  385. NETDATA_START_CMD='/etc/init.d/netdata start'
  386. NETDATA_STOP_CMD='/etc/init.d/netdata stop'
  387. }
  388. # =====================================================================
  389. # runit support functions
  390. _check_runit() {
  391. # if there is no runsvdir command, then it's not runit
  392. [ -z "$(command -v runsvdir 2>/dev/null || true)" ] && echo "NO" && return 0
  393. # if there is no runit command, then it's not runit
  394. [ -z "$(command -v runit 2>/dev/null || true)" ] && echo "NO" && return 0
  395. # if /run/runit exists, then it's runit
  396. [ -d /run/runit ] && echo "YES" && return 0
  397. # if /etc/runit/1 exists and is executable, then it's runit
  398. [ -x /etc/runit/1 ] && echo "YES" && return 0
  399. echo "NO" && return 0
  400. }
  401. check_runit() {
  402. if [ -z "${IS_RUNIT}" ]; then
  403. IS_RUNIT="$(_check_runit)"
  404. fi
  405. echo "${IS_RUNIT}"
  406. }
  407. install_runit_service() {
  408. if [ -d /etc/sv ]; then
  409. svc_dir="/etc/sv/netdata"
  410. elif [ -d /etc/runit/sv ]; then
  411. svc_dir="/etc/runit/sv/netdata"
  412. else
  413. error "Failed to locate service directory"
  414. exit 4
  415. fi
  416. if [ -d /service ]; then
  417. live_svc_dir="/service"
  418. elif [ -d /var/service ]; then
  419. live_svc_dir="/var/service"
  420. elif [ -d /run/runit/service ]; then
  421. live_svc_dir="/run/runit/service"
  422. elif [ -d /etc/runit/runsvdir/default ]; then
  423. live_svc_dir="/etc/runit/runsvdir/default"
  424. else
  425. error "Failed to locate live service directory"
  426. exit 4
  427. fi
  428. svc_file="${svc_dir}/run"
  429. info "Installing runit service file."
  430. if [ ! -f "${svc_file}" ] && [ "${ENABLE}" = "auto" ]; then
  431. ENABLE="enable"
  432. fi
  433. if ! install -D -p -m 0755 -o 0 -g 0 "${SVC_SOURCE}/runit/run" "${svc_file}"; then
  434. error "Failed to install service file."
  435. exit 4
  436. fi
  437. case ${ENABLE} in
  438. enable)
  439. if ! ln -s "${svc_dir}" "${live_svc_dir}"; then
  440. warning "Failed to enable the Netdata service."
  441. fi
  442. ;;
  443. disable)
  444. if ! rm "${live_svc_dir}/netdata"; then
  445. warning "Failed to disable the Netdata service."
  446. fi
  447. ;;
  448. esac
  449. }
  450. runit_cmds() {
  451. NETDATA_START_CMD="sv start netdata"
  452. NETDATA_STOP_CMD="sv stop netdata"
  453. }
  454. # =====================================================================
  455. # dinit support functions
  456. _check_dinit() {
  457. # if /etc/dinit.d does not exist, it’s not dinit
  458. [ ! -d /etc/dinit.d ] && echo "NO" && return 0
  459. # if PID 1 is dinit, it’s dinit
  460. [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "dinit" ] && echo "YES" && return 0
  461. # if /run/dinitctl exists and is a socket, it’s dinit
  462. [ -S /run/dinitctl ] && echo "YES" && return 0
  463. # if the dinitctl command exists despite getting to this point, it’s dinit, but not booted as such
  464. [ -n "$(command -v dinitctl 2>/dev/null || true)" ] && echo "OFFLINE" && return 0
  465. echo "NO" && return 0
  466. }
  467. check_dinit() {
  468. if [ -z "${IS_DINIT}" ]; then
  469. IS_DINIT="$(_check_dinit)"
  470. fi
  471. echo "${IS_DINIT}"
  472. }
  473. _run_dinitctl() {
  474. opts=''
  475. if [ "$(check_dinit)" = "OFFLINE" ]; then
  476. opts="-o"
  477. fi
  478. # shellcheck disable=SC2086
  479. dinitctl ${opts} "${@}"
  480. }
  481. enable_dinit() {
  482. _run_dinitctl enable netdata
  483. }
  484. enable_dinit() {
  485. _run_dinitctl disable netdata
  486. }
  487. install_dinit_service() {
  488. install_generic_service dinit/netdata "dinit" /etc/dinit.d enable_dinit disable_dinit
  489. }
  490. dinit_cmds() {
  491. if [ "$(check_dinit)" = "YES" ]; then
  492. NETDATA_START_CMD='dinitctl start netdata'
  493. NETDATA_STOP_CMD='dinitct stop netdata'
  494. else # Not booted using dinit, use external defaults by not providing commands.
  495. warning "Detected dinit, but the system is not booted using dinit. Unable to provide commands to start or stop Netdata using the service manager."
  496. fi
  497. }
  498. # =====================================================================
  499. # WSL support functions
  500. _check_wsl() {
  501. # If uname -r contains the string WSL, then it's WSL.
  502. uname -r | grep -q 'WSL' && echo "YES" && return 0
  503. # If uname -r contains the string Microsoft, then it's WSL.
  504. # This probably throws a false positive on CBL-Mariner, but it's part of what MS officially recommends for
  505. # detecting if you're running under WSL.
  506. uname -r | grep -q "Microsoft" && echo "YES" && return 0
  507. echo "NO" && return 0
  508. }
  509. check_wsl() {
  510. if [ -z "${IS_WSL}" ]; then
  511. IS_WSL="$(_check_wsl)"
  512. fi
  513. echo "${IS_WSL}"
  514. }
  515. install_wsl_service() {
  516. error "${WSL_ERROR_MSG}"
  517. exit 3
  518. }
  519. wsl_cmds() {
  520. error "${WSL_ERROR_MSG}"
  521. exit 3
  522. }
  523. # =====================================================================
  524. # FreeBSD support functions
  525. enable_freebsd() {
  526. if ! sysrc netdata_enable=YES; then
  527. warning "Failed to enable netdata service."
  528. fi
  529. }
  530. disable_freebsd() {
  531. if ! sysrc netdata_enable=NO; then
  532. warning "Failed to disable netdata service."
  533. fi
  534. }
  535. install_freebsd_service() {
  536. install_generic_service freebsd/rc.d "FreeBSD rc.d" /usr/local/etc/rc.d/netdata enable_freebsd disable_freebsd
  537. }
  538. freebsd_cmds() {
  539. NETDATA_START_CMD='service netdata start'
  540. NETDATA_STOP_CMD='service netdata stop'
  541. NETDATA_INSTALLER_START_CMD='service netdata onestart'
  542. }
  543. # =====================================================================
  544. # macOS support functions
  545. install_darwin_service() {
  546. info "Installing macOS plist file for launchd."
  547. if ! install -C -S -p -m 0644 -o 0 -g 0 "${SVC_SOURCE}/launchd/netdata.plist" /Library/LaunchDaemons/com.github.netdata.plist; then
  548. error "Failed to copy plist file."
  549. exit 4
  550. fi
  551. launchctl unload /Library/LaunchDaemons/com.github.netdata.plist 2>/dev/null
  552. if ! launchctl load /Library/LaunchDaemons/com.github.netdata.plist; then
  553. error "Failed to load plist file."
  554. exit 4
  555. fi
  556. }
  557. darwin_cmds() {
  558. NETDATA_START_CMD='launchctl start com.github.netdata'
  559. NETDATA_STOP_CMD='launchctl stop com.github.netdata'
  560. }
  561. # =====================================================================
  562. # Linux support functions
  563. detect_linux_svc_type() {
  564. if [ "${SVC_TYPE}" = "detect" ]; then
  565. found_types=''
  566. for t in wsl ${LINUX_INIT_TYPES}; do
  567. case "$("check_${t}")" in
  568. YES)
  569. SVC_TYPE="${t}"
  570. break
  571. ;;
  572. NO) continue ;;
  573. OFFLINE)
  574. if [ -z "${found_types}" ]; then
  575. found_types="${t}"
  576. else
  577. found_types="${found_types} ${t}"
  578. fi
  579. ;;
  580. esac
  581. done
  582. if [ "${SVC_TYPE}" = "detect" ]; then
  583. if [ -z "${found_types}" ]; then
  584. error "Failed to detect what type of service manager is in use."
  585. else
  586. SVC_TYPE="$(echo "${found_types}" | cut -f 1 -d ' ')"
  587. warning "Failed to detect a running service manager, using detected (but not running) ${SVC_TYPE}."
  588. fi
  589. elif [ "${SVC_TYPE}" = "wsl" ]; then
  590. if [ "$(check_systemd)" = "YES" ]; then
  591. # Support for systemd in WSL.
  592. SVC_TYPE="systemd"
  593. elif [ "$(_check_lsb_ignore_systemd)" = "YES" ]; then
  594. # Support for LSB init.d in WSL.
  595. SVC_TYPE="lsb"
  596. elif [ "$(_check_initd_ignore_systemd)" = "YES" ]; then
  597. # Support for ‘generic’ init.d in WSL.
  598. SVC_TYPE="initd"
  599. fi
  600. fi
  601. fi
  602. echo "${SVC_TYPE}"
  603. }
  604. install_linux_service() {
  605. t="$(detect_linux_svc_type)"
  606. if [ -z "${t}" ] || [ "${t}" = 'detect' ]; then
  607. exit 2
  608. fi
  609. "install_${t}_service"
  610. }
  611. linux_cmds() {
  612. t="$(detect_linux_svc_type)"
  613. if [ -z "${t}" ] || [ "${t}" = 'detect' ]; then
  614. exit 2
  615. fi
  616. "${t}_cmds"
  617. }
  618. # =====================================================================
  619. # Service type display function
  620. show_service_type() {
  621. info "Detected platform: ${PLATFORM}"
  622. case "${PLATFORM}" in
  623. FreeBSD)
  624. info "Detected service managers:"
  625. info " - freebsd: YES"
  626. info "Would use freebsd service management."
  627. ;;
  628. Darwin)
  629. info "Detected service managers:"
  630. info " - launchd: YES"
  631. info "Would use launchd service management."
  632. ;;
  633. Linux)
  634. [ "$(check_wsl)" = "YES" ] && info "Detected WSL environment."
  635. info "Detected service managers:"
  636. for t in ${LINUX_INIT_TYPES}; do
  637. info " - ${t}: $("check_${t}")"
  638. done
  639. info "Would use $(detect_linux_svc_type) service management."
  640. ;;
  641. *)
  642. info "${PLATFORM} is not supported by this script. No service file would be installed."
  643. esac
  644. exit 0
  645. }
  646. # =====================================================================
  647. # Argument handling
  648. parse_args() {
  649. while [ -n "${1}" ]; do
  650. case "${1}" in
  651. "--source" | "-s")
  652. SVC_SOURCE="${2}"
  653. shift 1
  654. ;;
  655. "--type" | "-t")
  656. if [ "${2}" = "help" ]; then
  657. help_types
  658. exit 0
  659. else
  660. SVC_TYPE="${2}"
  661. shift 1
  662. fi
  663. ;;
  664. "--show-type") SHOW_SVC_TYPE=1 ; INSTALL=0 ;;
  665. "--save-cmds")
  666. if [ -z "${2}" ]; then
  667. info "No path specified to save command variables."
  668. exit 1
  669. else
  670. SAVE_CMDS_PATH="${2}"
  671. shift 1
  672. fi
  673. ;;
  674. "--cmds" | "-c") DUMP_CMDS=1 ;;
  675. "--cmds-only") INSTALL=0 ;;
  676. "--export-cmds") EXPORT_CMDS=1 ;;
  677. "--enable" | "-e") ENABLE="enable" ;;
  678. "--disable" | "-d") ENABLE="disable" ;;
  679. "--help" | "-h")
  680. usage
  681. exit 0
  682. ;;
  683. *)
  684. info "Unrecognized option '${1}'."
  685. exit 1
  686. ;;
  687. esac
  688. shift 1
  689. done
  690. if [ "${SVC_SOURCE#@}" = "libsysdir_POST@" ]; then
  691. SVC_SOURCE="$(dirname "${SCRIPT_SOURCE}")/../../lib/netdata/system"
  692. warning "SVC_SOURCE not templated, using ${SVC_SOURCE} as source directory."
  693. fi
  694. if [ ! -d "${SVC_SOURCE}" ] && [ "${INSTALL}" -eq 1 ]; then
  695. error "${SVC_SOURCE} does not appear to be a directory. Please specify a valid source for service files with the --source option."
  696. exit 1
  697. fi
  698. if valid_types | grep -vw "${SVC_TYPE}"; then
  699. error "${SVC_TYPE} is not supported on this platform."
  700. help_types
  701. exit 1
  702. fi
  703. }
  704. # =====================================================================
  705. # Core logic
  706. main() {
  707. trap "cleanup" EXIT
  708. parse_args "${@}"
  709. if [ "${SHOW_SVC_TYPE}" -eq 1 ]; then
  710. show_service_type
  711. else
  712. case "${PLATFORM}" in
  713. FreeBSD)
  714. [ "${INSTALL}" -eq 1 ] && install_freebsd_service
  715. freebsd_cmds
  716. ;;
  717. Darwin)
  718. [ "${INSTALL}" -eq 1 ] && install_darwin_service
  719. darwin_cmds
  720. ;;
  721. Linux)
  722. [ "${INSTALL}" -eq 1 ] && install_linux_service
  723. linux_cmds
  724. ;;
  725. *)
  726. error "${PLATFORM} is not supported by this script."
  727. exit 5
  728. ;;
  729. esac
  730. [ "${DUMP_CMDS}" -eq 1 ] && dump_cmds
  731. [ "${EXPORT_CMDS}" -eq 1 ] && export_cmds
  732. [ -n "${SAVE_CMDS_PATH}" ] && save_cmds
  733. fi
  734. exit 0
  735. }
  736. main "${@}"