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