]> arthur.barton.de Git - netdata.git/blob - system/netdata-openrc
70d4da1579e18311ebc65aaaf46ec84b12a174fc
[netdata.git] / system / netdata-openrc
1 #!/sbin/runscript
2
3 # The path netdata has been installed.
4 # Leave it empty if netdata is installed in /
5 NETDATA_INSTALL_PATH=${NETDATA_INSTALL_PATH-}
6
7 # The user netdata is configured to run as.
8 # If you edit its configuration file to set a different
9 # user, set it here too, to have its files switch ownership
10 NETDATA_OWNER=${NETDATA_OWNER-netdata:netdata}
11
12 # The URL to download netdata config.
13 NETDATA_CONFIG_URL=${NETDATA_CONFIG_URL-http://localhost:19999/netdata.conf}
14
15 # The timeout in seconds to wait for netdata
16 # to save its database on disk and exit.
17 NETDATA_WAIT_EXIT_TIMEOUT=${NETDATA_WAIT_EXIT_TIMEOUT-15}
18
19 # When set to 1, if netdata does not exit in
20 # NETDATA_WAIT_EXIT_TIMEOUT, we will force it
21 # to exit.
22 NETDATA_FORCE_EXIT=${NETDATA_FORCE_EXIT-0}
23
24
25 extra_started_commands="getconf"
26 pidfile="/var/run/netdata/netdata.pid"
27 command="${NETDATA_INSTALL_PATH}/usr/sbin/netdata"
28 command_background="yes"
29
30 depend() {
31         use logger
32         need net
33
34         # FIXME
35         # We should depend on these only if they are enabled:
36         # apache, squid, nginx, mysql, named, opensips, nut, hostapd, postfix, lm-sensors
37 }
38
39 start() {
40         local ret
41
42         if [ ! -d ${NETDATA_INSTALL_PATH}/var/cache/netdata ]
43                 then
44                 ebegin "Creating ${NETDATA_INSTALL_PATH}/var/cache/netdata"
45                 mkdir -p ${NETDATA_INSTALL_PATH}/var/cache/netdata
46                 chown ${NETDATA_OWNER} ${NETDATA_INSTALL_PATH}/var/cache/netdata
47                 echo "${NETDATA_OWNER}" >${NETDATA_INSTALL_PATH}/var/cache/netdata/.last_owner
48                 eend $?
49         fi
50
51         if [ "$(cat ${NETDATA_INSTALL_PATH}/var/cache/netdata/.last_owner 2>/dev/null)" != "${NETDATA_OWNER}" ]
52                 then
53                 ebegin "Switching ownership of ${NETDATA_INSTALL_PATH}/var/cache/netdata"
54                 chown -R ${NETDATA_OWNER} ${NETDATA_INSTALL_PATH}/var/cache/netdata
55                 echo "${NETDATA_OWNER}" >${NETDATA_INSTALL_PATH}/var/cache/netdata/.last_owner
56                 eend 0
57         fi
58
59         ebegin "Starting netdata"
60         start-stop-daemon --start --quiet --pidfile ${pidfile} --exec ${command} -- ${NETDATA_EXTRA_ARGS}
61         ret=$?
62         eend $ret
63
64         if [ $ret -eq 0 -a ! -f ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf ]
65                 then
66                 ebegin "Downloading default configuration to ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf"
67                 sleep 2
68                 curl -s -o ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf.new "${NETDATA_CONFIG_URL}"
69                 ret=$?
70                 if [ $ret -eq 0 -a -s ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf.new ]
71                         then
72                         mv ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf.new ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf
73                 else
74                         ret=1
75                         rm ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf.new 2>/dev/null
76                 fi
77                 eend $ret
78         fi
79 }
80
81 stop() {
82         local result ret=0 count=0 sigkill=0
83
84         ebegin "Stopping netdata"
85         start-stop-daemon --stop --quiet --pidfile ${pidfile}
86         result=$?
87         eend $result
88
89         ebegin "Waiting for netdata to save its database"
90         while [ -f "${pidfile}" ]
91         do
92                 if [ $count -gt ${NETDATA_WAIT_EXIT_TIMEOUT} ]
93                         then
94                         sigkill=1
95                         break
96                 fi
97
98                 count=$[count + 1]
99                 kill -0 $(cat ${pidfile}) 2>/dev/null
100                 ret=$?
101                 test $ret -eq 0 && sleep 1
102         done
103         eend $sigkill
104
105         if [ $sigkill -eq 1 -a -f "${pidfile}" ]
106                 then
107                 ebegin "Netdata is taking too long to exit, forcing it to quit"
108                 kill -SIGKILL $(cat ${pidfile}) 2>/dev/null
109                 eend $?
110         fi
111 }
112
113 getconf() {
114         ebegin "Downloading configuration from netdata to /tmp/netdata.conf"
115         curl -o /tmp/netdata.conf "${NETDATA_CONFIG_URL}"
116         eend $?
117 }