summaryrefslogtreecommitdiffstats
path: root/system/netdata-openrc.in
blob: ce76f7e64a93301296cfb58b8c3802222e6fcea4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/sbin/openrc-run

# The user netdata is configured to run as.
# If you edit its configuration file to set a different
# user, set it here too, to have its files switch ownership
: ${NETDATA_OWNER:=netdata:netdata}

# The URL to download netdata config.
: ${NETDATA_CONFIG_URL:=http://localhost:19999/netdata.conf}

# The timeout in seconds to wait for netdata
# to save its database on disk and exit.
: ${NETDATA_WAIT_EXIT_TIMEOUT:=60}

# When set to 1, if netdata does not exit in
# NETDATA_WAIT_EXIT_TIMEOUT, we will force it
# to exit.
: ${NETDATA_FORCE_EXIT:=0}

# Netdata will use these services, only if they
# are enabled to start.
: ${NETDATA_START_AFTER_SERVICES:=apache2 squid nginx mysql named opensips upsd hostapd postfix lm_sensors}

extra_started_commands="getconf"
pidfile="/run/netdata.pid"
command="@sbindir_POST@/netdata"
command_background="yes"
command_args="-P ${pidfile} ${NETDATA_EXTRA_ARGS}"
# start_stop_daemon_args="-u ${NETDATA_OWNER}"
start_stop_daemon_args=""

depend() {
	use logger
	need net
	after ${NETDATA_START_AFTER_SERVICES}

	checkpath -o ${NETDATA_OWNER} -d @localstatedir_POST@/cache/netdata /run/netdata
}

start_post() {
	if [ ! -f @sysconfdir_POST@/netdata/netdata.conf ]; then
		ebegin "Downloading default configuration to @sysconfdir_POST@/netdata/netdata.conf"
		sleep 2
		curl -s -o @sysconfdir_POST@/netdata/netdata.conf.new "${NETDATA_CONFIG_URL}"
		ret=$?
		if [ $ret -eq 0 && -s @sysconfdir_POST@/netdata/netdata.conf.new ]; then
			mv @sysconfdir_POST@/netdata/netdata.conf.new @sysconfdir_POST@/netdata/netdata.conf
		else
			ret=1
			rm @sysconfdir_POST@/netdata/netdata.conf.new 2>/dev/null
		fi
		eend $ret
	fi
}

stop_post() {
	local result ret=0 count=0 sigkill=0

	ebegin "Waiting for netdata to save its database"
	while [ -f "${pidfile}" ]; do
		if [ $count -gt ${NETDATA_WAIT_EXIT_TIMEOUT} ]; then
			sigkill=1
			break
		fi

		count=$((count + 1))
		kill -0 $(cat ${pidfile}) 2>/dev/null
		ret=$?
		test $ret -eq 0 && sleep 1
	done
	eend $sigkill

	if [ $sigkill -eq 1 -a -f "${pidfile}" ]; then
		ebegin "Netdata is taking too long to exit, forcing it to quit"
		kill -SIGKILL $(cat ${pidfile}) 2>/dev/null
		eend $?
	fi
}

getconf() {
	ebegin "Downloading configuration from netdata to /tmp/netdata.conf"
	curl -o /tmp/netdata.conf "${NETDATA_CONFIG_URL}"
	eend $?
}