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