]> arthur.barton.de Git - netdata.git/blob - netdata.start
c12608980a7dda5e58225927331d8c224ce68cc1
[netdata.git] / netdata.start
1 #!/bin/bash
2
3 netdata=$(which netdata)
4
5 if [ -z "${netdata}" -o ! -f src/netdata ]
6 then
7         echo >&2 "Running ./autogen.sh ..."
8         ./autogen.sh || exit 1
9
10         echo >&2 "Running ./configure ..."
11         ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-zlib --with-math --with-user=netdata CFLAGS="-march=native -O3 -Wall -Wextra" || exit 1
12
13         echo >&2 "Compiling netdata ..."
14         make || exit 1
15 fi
16
17 if [ -z "${netdata}" -o src/netdata -nt "${netdata}" ]
18 then
19         echo >&2 "Installing netdata ..."
20         make install
21
22         echo >&2 "Adding netdata group ..."
23         getent group netdata > /dev/null || groupadd -r netdata
24
25         echo >&2 "Adding netdata user ..."
26         getent passwd netdata > /dev/null || useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata
27 fi
28
29 netdata=$(which netdata)
30
31 base="`dirname "$0"`"
32
33 if [ ! -d "${base}" ]
34 then
35         echo >&2 "Cannot find my home directory '${base}'."
36         exit 1
37 fi
38 cd "${base}" || exit 1
39
40
41 # -----------------------------------------------------------------------------
42 # load options from the configuration file
43
44 # create an empty config if it does not exist
45 [ ! -f conf.d/netdata.conf ] && touch conf.d/netdata.conf
46
47 # function to extract values from the config file
48 config_option() {
49         local key="${1}" value="${2}" line=
50
51         if [ -s "/etc/netdata/netdata.conf" ]
52                 then
53                 line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "conf.d/netdata.conf" | head -n 1 )"
54                 [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )"
55         fi
56
57         echo "${value}"
58 }
59
60 # user
61 defuser="netdata"
62 [ ! "${UID}" = "0" ] && defuser="${USER}"
63 NETDATA_USER="$( config_option "run as user" "${defuser}" )"
64
65 # debug flags
66 defdebug=0
67 NETDATA_DEBUG="$( config_option "debug flags" ${defdebug} )"
68
69 # port
70 defport=19999
71 NETDATA_PORT="$( config_option "port" ${defport} )"
72
73 # directories
74 NETDATA_CACHE_DIR="$( config_option "database directory" "/var/cache/netdata" )"
75 NETDATA_WEB_DIR="$( config_option "web files directory" "/usr/share/netdata/web" )"
76 NETDATA_LOG_DIR="log"
77 NETDATA_CONF_DIR="conf.d"
78
79
80 # -----------------------------------------------------------------------------
81 # prepare the directories
82
83 echo >&2 "Fixing directory permissions for user ${NETDATA_USER}..."
84 for x in "${NETDATA_WEB_DIR}" "${NETDATA_CONF_DIR}" "${NETDATA_CACHE_DIR}" "${NETDATA_LOG_DIR}"
85 do
86         if [ ! -d "${x}" ]
87                 then
88                 mkdir "${x}" || exit 1
89         fi
90         chown -R "${NETDATA_USER}" "${x}" || echo >&2 "WARNING: Cannot change the ownership of the files in directory ${x} to ${NETDATA_USER}..."
91         chmod 0775 "${x}" "${x}" || echo >&2 "WARNING: Cannot change the permissions of the directory ${x} to 0755..."
92 done
93
94
95 # -----------------------------------------------------------------------------
96 # stop a running netdata
97
98 printf >&2 "Stopping a (possibly) running netdata..."
99 ret=0
100 count=0
101 while [ $ret -eq 0 ]
102 do
103         if [ $count -gt 15 ]
104                 then
105                 echo >&2 "Cannot stop the running netdata."
106                 exit 1
107         fi
108
109         count=$((count + 1))
110         killall netdata 2>/dev/null
111         ret=$?
112         test $ret -eq 0 && printf >&2 "." && sleep 2
113 done
114 echo >&2
115
116
117 # -----------------------------------------------------------------------------
118 # run netdata
119
120 # avoid extended stat(/etc/localtime)
121 # http://stackoverflow.com/questions/4554271/how-to-avoid-excessive-stat-etc-localtime-calls-in-strftime-on-linux
122 export TZ=":/etc/localtime"
123
124 echo >&2 "Starting netdata..."
125 netdata "${@}"
126
127 if [ $? -ne 0 ]
128         then
129         echo >&2
130         echo >&2 "SORRY! FAILED TO START NETDATA!"
131         exit 1
132 else
133         echo >&2 "OK. NetData Started!"
134 fi
135
136
137 # -----------------------------------------------------------------------------
138 # save a config file, if it is not already there
139
140 if [ ! -s /etc/netdata/netdata.conf ]
141         then
142         echo >&2 "Downloading default configuration from netdata..."
143         sleep 5
144
145         # remove a possibly obsolete download
146         [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
147
148         # try wget
149         wget 2>/dev/null -O /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
150         ret=$?
151
152         # try curl
153         if [ $ret -ne 0 -o ! -s /etc/netdata/netdata.conf.net ]
154                 then
155                 curl -s -o /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf"
156                 ret=$?
157         fi
158
159         if [ $ret -eq 0 -a -s /etc/netdata/netdata.conf.new ]
160                 then
161                 mv /etc/netdata/netdata.conf.new /etc/netdata/netdata.conf
162                 echo >&2 "New configuration saved for you to edit at /etc/netdata/netdata.conf"
163
164                 chown "${NETDATA_USER}" /etc/netdata/netdata.conf
165                 chmod 0664 /etc/netdata/netdata.conf
166         else
167                 echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'"
168                 [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new
169         fi
170 fi
171
172 echo >&2
173 echo >&2 "Hit http://localhost:${NETDATA_PORT}/ from your browser."
174 echo >&2
175 echo >&2 "Enjoy..."
176 echo >&2