]> arthur.barton.de Git - netdata.git/blob - system/netdata-openrc
756a521bd9e0a7391ee1b6e346b7c4b100d7f93c
[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 # Netdata will use these services, only if they
25 # are enabled to start.
26 NETDATA_START_AFTER_SERVICES="${NETDATA_START_AFTER_SERVICES-apache2 squid nginx mysql named opensips upsd hostapd postfix lm_sensors}"
27
28 extra_started_commands="getconf"
29 pidfile="/var/run/netdata.pid"
30 command="${NETDATA_INSTALL_PATH}/usr/sbin/netdata"
31 command_background="yes"
32
33 depend() {
34         use logger
35         need net
36
37         local x
38         for x in ${NETDATA_START_AFTER_SERVICES}
39         do
40                 if [ -f /etc/runlevels/default/${x} ]
41                         then
42                         use ${x}
43                 fi
44         done
45 }
46
47 start() {
48         local ret
49
50         if [ ! -d ${NETDATA_INSTALL_PATH}/var/cache/netdata ]
51                 then
52                 ebegin "Creating ${NETDATA_INSTALL_PATH}/var/cache/netdata"
53                 mkdir -p ${NETDATA_INSTALL_PATH}/var/cache/netdata
54                 chown ${NETDATA_OWNER} ${NETDATA_INSTALL_PATH}/var/cache/netdata
55                 echo "${NETDATA_OWNER}" >${NETDATA_INSTALL_PATH}/var/cache/netdata/.last_owner
56                 eend $?
57         fi
58
59         if [ "$(cat ${NETDATA_INSTALL_PATH}/var/cache/netdata/.last_owner 2>/dev/null)" != "${NETDATA_OWNER}" ]
60                 then
61                 ebegin "Switching ownership of ${NETDATA_INSTALL_PATH}/var/cache/netdata"
62                 chown -R ${NETDATA_OWNER} ${NETDATA_INSTALL_PATH}/var/cache/netdata
63                 echo "${NETDATA_OWNER}" >${NETDATA_INSTALL_PATH}/var/cache/netdata/.last_owner
64                 eend 0
65         fi
66
67         ebegin "Starting netdata"
68         start-stop-daemon --start --quiet --pidfile ${pidfile} --exec ${command} -- ${NETDATA_EXTRA_ARGS}
69         ret=$?
70         eend $ret
71
72         if [ $ret -eq 0 -a ! -f ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf ]
73                 then
74                 ebegin "Downloading default configuration to ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf"
75                 sleep 2
76                 curl -s -o ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf.new "${NETDATA_CONFIG_URL}"
77                 ret=$?
78                 if [ $ret -eq 0 -a -s ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf.new ]
79                         then
80                         mv ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf.new ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf
81                 else
82                         ret=1
83                         rm ${NETDATA_INSTALL_PATH}/etc/netdata/netdata.conf.new 2>/dev/null
84                 fi
85                 eend $ret
86         fi
87 }
88
89 stop() {
90         local result ret=0 count=0 sigkill=0
91
92         ebegin "Stopping netdata"
93         start-stop-daemon --stop --quiet --pidfile ${pidfile}
94         result=$?
95         eend $result
96
97         ebegin "Waiting for netdata to save its database"
98         while [ -f "${pidfile}" ]
99         do
100                 if [ $count -gt ${NETDATA_WAIT_EXIT_TIMEOUT} ]
101                         then
102                         sigkill=1
103                         break
104                 fi
105
106                 count=$[count + 1]
107                 kill -0 $(cat ${pidfile}) 2>/dev/null
108                 ret=$?
109                 test $ret -eq 0 && sleep 1
110         done
111         eend $sigkill
112
113         if [ $sigkill -eq 1 -a -f "${pidfile}" ]
114                 then
115                 ebegin "Netdata is taking too long to exit, forcing it to quit"
116                 kill -SIGKILL $(cat ${pidfile}) 2>/dev/null
117                 eend $?
118         fi
119 }
120
121 getconf() {
122         ebegin "Downloading configuration from netdata to /tmp/netdata.conf"
123         curl -o /tmp/netdata.conf "${NETDATA_CONFIG_URL}"
124         eend $?
125 }